

jQuery(document).ready(function(){

	var status = false;	
	var delay;

	// set the margin for the top-Menu
	var topMenu	= jQuery('#t3Navigation-Top');
	if(topMenu){
		var topMenuFields	= topMenu.find('.level1');
		var middleField		= topMenuFields[1];
		middleField		= jQuery(middleField);
		if(middleField){
			middleField.css("margin","0px 10px");
		}
	}

	var elLength	= jQuery(".colorbox").length;
	if( elLength ){
	jQuery(".rzcolorbox").mouseenter(function(){

		if(jQuery(this).hasClass("gallery")){
			return true;
		}

		 var element				= jQuery(this);
		 var imageOverlayElement	= element.find('.imageOverlay');
		 element.prepend('<div class="imageOverlay"><div class="imageOverlayIcon"></div></div>');

		  var imageWidth	= element.find('img').attr("width");
		  var imageHeight	= element.find('img').attr("height");
		  element.find('.imageOverlay').css("width", imageWidth);
		  element.find('.imageOverlay').css("height", imageHeight);
	});

		jQuery(".rzcolorbox").mouseleave(function(){

			if(jQuery(this).hasClass("gallery")){
				return true;
			}


			var element	= jQuery(this);
			var imageOverlayElement	= element.find('.imageOverlay');
			imageOverlayElement.remove();
	});
	}


	var imageWidth		= 0;
	var bigImageWidth	= 0;
	var imageHeight		= 0;
	var bigImageHeight	= 0;

	jQuery("div.thumb div.galleryItemWrapper").each(function(){

	// calculate size foreach image
		var imgEl	= jQuery(this).find('img');

		if(imgEl != null){
		var height	= imgEl.attr('height');
		var width	= imgEl.attr('width');
		var widthStatus	= false;

		while(imgEl.attr('width') > 174 || imgEl.attr('height') > 174 ){
			if(imgEl.attr('width') > 174){
				var diffWidth	= parseInt(width)/174;
				imgEl.attr('width',174);
				imgEl.attr('height',height/diffWidth);
			}
			else if(imgEl.attr('height') > 174){
				var diffHeight	= parseInt(height)/174;
				imgEl.attr('height',174);
				imgEl.attr('width',imgEl.attr('width')/diffHeight);
			}
			
			else if(imgEl.attr('width') > 174){
				var diffWidth	= parseInt(width)/174;
				imgEl.attr('width',174);
				imgEl.attr('height',imgEl.attr('height')/diffWidth);
			}
			
		}

		jQuery(this).data('width',jQuery(this).find('img').attr('width'));
		jQuery(this).data('height',jQuery(this).find('img').attr('height'));

		var imageInformationBox	= jQuery(this).find('.galleryImageContent');

		var offset	= 174 - jQuery(this).find('img').attr('height');
		jQuery(this).data('offset',offset);
		imageInformationBox.css('bottom',offset);

		var linkEl		=  jQuery(this).find('a.rzcolorbox');
		if(linkEl != null){
			var imageLink	= linkEl.attr('href');
			if(imageLink){
				imageLink	= imageLink.split('/');
				var arraySize	= imageLink.length;
				imageInformationBox.html('<span class="imageTitel">'+imageLink[arraySize-1]+'</span><span class="imageDownload">download</span>');
			}
		}

	}


	});

	jQuery.fn.wait = function(time, type) {

		time = 50

        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                jQuery(self).dequeue();
            }, time);
        });
	};




	jQuery("div.galleryItemWrapper").hover(function() {
	
	   var element = jQuery(this);
			
	   delay	= setTimeout(function() {
		if(status) {
		 return false;
		}
	
		status = true;
		var imgEl	= element.find('img');
		bigImageWidth	= element.data('width')*1.5;
		bigImageHeight	= element.data('height')*1.5;
		element.css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/
		
		var itemContainer			= element.find('div.galleryItem');
			itemContainer.addClass("hover");
		var itemContainerWrapper	= element.find('div.galleryImageWrapper');

		itemContainerWrapper.animate({
			backgroundColor: 'white'
		}, 200);


		itemContainer.animate({
			top: '-40px',
			left: '-40px'
		}, 200);

		imgEl.animate({
			width: bigImageWidth,
			height: bigImageHeight
		  }, 200, function() {
			// Animation complete.
		});
	   } , 500 ); // delays 1.5 sec
	} , function() {
		clearTimeout(delay);
		var imgEl		= jQuery(this).find('img');
	
		var itemContainer	= jQuery(this).find('div.galleryItem');
			itemContainer.removeClass("hover");
		var itemContainerWrapper	= jQuery(this).find('div.galleryImageWrapper');

		itemContainerWrapper.wait().animate({
			backgroundColor: 'transparent'
		}, 400);

		itemContainer.wait().animate({
			top: '0',
			left: '0',
			backgroundImage: 'none'
		}, 400);

		imgEl.wait().animate({
			width: jQuery(this).data('width'),
			height: jQuery(this).data('height')
		  }, 400, function() {
			// Animation complete.
		});

		jQuery(this).css({'z-index' : '0'}); /* Set z-index back to 0 */

	});

	jQuery("div.galleryItemWrapper").mouseleave(function() {
		status = false;
	});


	// set the margin for the top-Menu
	var topMenu	= jQuery('#t3Navigation-Top');
	if(topMenu){
		var topMenuFields	= topMenu.find('.level1');
		var middleField		= topMenuFields[1];
		middleField			= jQuery(middleField);
		if(middleField){
			middleField.css("margin","0px 10px");
		}
	}




imagePagingActive	= false;
numberPerPage		= 15;

var galleryItem	= jQuery("div#galleryContainer");
var imageLength = galleryItem.find(".galleryItemWrapper").length;

if(imageLength > numberPerPage ){
	imagePagingActive	= true;
}


if(imagePagingActive){

	elementOffset = 0
	elementCounter	= 0;

	showCurrentImages(elementOffset);
	createPaging();
	changeActiveImageList(0);
}

function createPaging(){

	var allElements		= jQuery("div.thumb div.galleryItemWrapper").length;
	var boxCount		= Math.ceil(allElements / numberPerPage);

	var pagingContainer	= jQuery("#imagePageContainer");

	if(pagingContainer){
		pagingContainer.html('<ul class="imageSelectionList"></ul><br class="styleClear"/>');
	}

	var pagingContainerList	= jQuery("#imagePageContainer ul");


	var boxCounter = 0;
	while(boxCount){
		showValue	= boxCounter +1; 
		pagingContainerList.append('<li id="galleryButton'+boxCounter+'" class="showImageListButton" onclick="changeActiveImageList('+boxCounter+')">'+showValue+'</li>');
		boxCounter++;
		boxCount	= boxCount -1;
	}



}

});

function changeActiveImageList(offset){
	showCurrentImages(offset);
	if(!offset) { 
		offset = 0 
	}
	console.log(offset);
	jQuery(".showImageListButton").removeClass('active');
	jQuery("#galleryButton"+offset).addClass('active');

}


function showCurrentImages(elementOffset){

	jQuery("div.thumb div.galleryItemWrapper").hide();
	var firstElement	= elementOffset * numberPerPage;
	var lastElement		= firstElement + numberPerPage;
	var showElements	= jQuery("div.thumb div.galleryItemWrapper").slice(firstElement,lastElement);

	showElements.each(function(){
		jQuery(this).show();
	});

	return showElements;

}










