var caching = true;
/*******************************************
	Slide all the menus up and down
*******************************************/

function initMenuHover(){
	$('.hasMenu').hover(
		function (){
			slideElement = $(this).children("li").children("ul");
			if(slideElement.is(':animated')){
				slideElement.stop().hide().css("height", "auto").slideDown(500);
			}
			else{
				slideElement.slideDown(500);
			}
		}
		,
		function (){
			slideElement = $(this).children("li").children("ul");
			if(slideElement.is(':animated')){
				slideElement.stop().slideUp(500, function(){$(this).css("height", "auto");});
			}
			else{
				slideElement.slideUp(500);
			}
		}
	);
}




/*******************************************
	Change the opacity of elements
*******************************************/


function initMenuItemsHover(){
	$('.menuBody > li').css('opacity', 0.3);
	$('.menuBody > li').hover(
		function (){
			$(this).stop().animate({ opacity: 0.6 }, 500);
			var collectionID = $(this).children('a').attr("title");
			var content = $('#collectionContent'+collectionID).html();
			$('#collectionDescription').stop().html(content).show();
		}
		,
		function (){
			$(this).stop().animate({ opacity: 0.3 }, 500);
			$('#collectionDescription').stop().hide();
		}
	);
}



function initMenuItemsClick(){
	
	// Variables for the main menu
	var dimensions = Array(
		Array('900px', '73px', '50px', 'ajaxContentHolder', 'ajaxContentClose','povesteaNoastra'),			//Povestea noastra
		Array('450px', '320px', '80px', 'ajaxContentHolder', 'ajaxContentClose','contact'),							//Contact
		Array('900px', '70px', '100px', 'ajaxContentHolder', 'ajaxContentClose', 'noutati'),						//Noutati
		Array('900px', '728px', '50px', 'ajaxContentHolder', 'ajaxContentClose', 'revistaPresei')				//Revista presei
	);
	
	$('.mainMenuElement').click(
		function (){
			var menuElem = $(this);
			var title = $(menuElem).attr("title");
			
			switch(title){
				case 'Povestea noastra':
					menuDim = 0;
					break;
				case 'Contact':
					menuDim = 1;
					break;
				case 'Lumea noastra':
					menuDim = 2;
					break;
				case 'Revista presei':
					menuDim = 3;
					break;
				case 'Colectii':
					return false;
				default:
					return false;
			}
			
			var targetElement = dimensions[menuDim][3];
			var closeElement = dimensions[menuDim][4];
			var module = dimensions[menuDim][5];
			
			var html = $.ajax({
				url: "ajax.php",
				type: "GET",
				cache: caching,
				data: "module="+module,
				beforeSend: function(XMLHttpRequest){
					$("#"+targetElement).html('');
					$("#"+targetElement).css("width","100px").css("height", "50px").css("background", "url('img/loading.gif') no-repeat center top").show();
				},
				success: function(html){
					fadeOutStageButtons();
					$(".backgroundBlured").fadeIn(500);
					$("#"+targetElement).css("height", "auto").css("background", "none").hide()
					$("#"+targetElement).html(html);
					$("#"+targetElement).css("width", dimensions[menuDim][0]).slideDown(500);
					$("#"+closeElement).css("right", dimensions[menuDim][1]).css("top",  dimensions[menuDim][2]).fadeIn(500);
				}
			});
			return false;
		}
	);
	
	$("#ajaxContentClose").click(
		function (){
			$(this).fadeOut(500);
			$(".backgroundBlured").fadeOut(500, function (){fadeInStageButtons();});
			$("#ajaxContentHolder").slideUp(500);
		}
	);
	$('.submenuElement').click(enableColectionsMenu);
	$('.outletHomeMenuBtn').click(openOutlet);
	$('.limitedEditionHomeMenuBtn').click(openLimitedEdition);
	$('.previewCollectionHomeMenuBtn').click(openPreviewCollection);
	$("#ajaxContentFullScreenL1Close").click(
		function (){
			$(this).fadeOut(500);
			$("#ajaxContentHolderFullScreenL1").fadeOut(500);
		}
	);
	
	$("#ajaxContentFullScreenL2Close").click(
		function (){
			$(this).fadeOut(500);
			$("#ajaxContentHolderFullScreenL2").fadeOut(500);
		}
	);
	$("#ajaxContentFullScreenL3Close").click(
		function (){
			$(this).fadeOut(500);
			$("#ajaxContentHolderFullScreenL3").fadeOut(500);
		}
	);


}

function enableColectionsMenu(){
	var menuElem = $(this);
	var collectionID = $(this).attr('title');
	
	$.ajax({
		url: "ajax.php",
		type: "GET",
		cache: caching,
		data: "module=getCollection&collectionID="+collectionID,
		beforeSend: function(XMLHttpRequest){
			$("#ajaxContentHolderFullScreenL1").html('');
			$("#ajaxContentHolderFullScreenL1").css("background", "#fff url('/img/loading.gif') no-repeat 50% 50%").show();
		},
		success: function(html){
			$("#ajaxContentHolderFullScreenL1").html(html);
			$("#ajaxContentFullScreenL1Close").css("right", "10px").css("top",  "10px").fadeIn(500);
		}
	});
	return false;
}

function openOutlet(){
	$.ajax({
		url: "ajax.php",
		type: "GET",
		cache: caching,
		data: "module=outlet",
		beforeSend: function(XMLHttpRequest){
			$("#ajaxContentHolderFullScreenL1").html('').css("background", "#fff url('/img/loading.gif') no-repeat 50% 50%").show();
		},
		success: function(html){
			$("#ajaxContentHolderFullScreenL1").html(html);
			$("#ajaxContentHolderFullScreenL1").fadeIn(500);
			$("#ajaxContentFullScreenL1Close").css("right", "10px").css("top",  "10px").fadeIn(500);
		}
	});
	return false;
}

function openLimitedEdition(){
	$.ajax({
		url: "ajax.php",
		type: "GET",
		cache: caching,
		data: "module=limitedEdition",
		beforeSend: function(XMLHttpRequest){
			$("#ajaxContentHolderFullScreenL1").html('');
			$("#ajaxContentHolderFullScreenL1").css("background", "url('/img/loading.gif') no-repeat 50% 50%").show();
		},
		success: function(html){
			$("#ajaxContentHolderFullScreenL1").html(html);
			$("#ajaxContentHolderFullScreenL1").fadeIn(500);
			$("#ajaxContentFullScreenL1Close").css("right", "10px").css("top",  "10px").fadeIn(500);
			
			if(images.length == 0){
				$("#ajaxContentHolderFullScreenL1").css("background", "#fff url('/img/loading.gif') no-repeat 50% 50%");
				loadImage(myImageArray[cnt]);
			}
			else{
				cnt = 0;
				$("#ajaxContentHolderFullScreenL1").css("background", "#cfc5b9");
				nextSlideShowImg();
			}
		}
	});
	return false;
}

/*
function openPreviewCollection(){
	$.ajax({
		url: "ajax.php",
		type: "GET",
		cache: caching,
		data: "module=previewCollection",
		beforeSend: function(XMLHttpRequest){
			$("#ajaxContentHolderFullScreenL1").html('');
			$("#ajaxContentHolderFullScreenL1").css("background", "url('/img/loading.gif') no-repeat 50% 50%").show();
		},
		success: function(html){
			$("#ajaxContentHolderFullScreenL1").html(html);
			$("#ajaxContentHolderFullScreenL1").fadeIn(500);
			$("#ajaxContentFullScreenL1Close").css("right", "10px").css("top",  "10px").fadeIn(500);

			if(imagesPreview.length == 0){
				$("#ajaxContentHolderFullScreenL1").css("background", "#fff url('/img/loading.gif') no-repeat 50% 50%");
				loadPreviewImage(myImageArrayPreview[cntPreview]);
			}
			else{
				cntPreview = 0;
				$("#ajaxContentHolderFullScreenL1").css("background", "#cfc5b9");
				nextSlideShowImg();
			}
		}
	});
	return false;
}
*/


function openPreviewCollection(){
	var collectionID = 7;
	
	$.ajax({
		url: "ajax.php",
		type: "GET",
		cache: caching,
		data: "module=getCollection&collectionID="+collectionID,
		beforeSend: function(XMLHttpRequest){
			$("#ajaxContentHolderFullScreenL1").html('');
			$("#ajaxContentHolderFullScreenL1").css("background", "#fff url('/img/loading.gif') no-repeat 50% 50%").show();
		},
		success: function(html){
			$("#ajaxContentHolderFullScreenL1").html(html);
			$("#ajaxContentFullScreenL1Close").css("right", "10px").css("top",  "10px").fadeIn(500);
		}
	});
	return false;
}

/* This function is used only for parsing links. When you click on a shoe you have the event in modules/getCollection.js*/
function openShoe(customLink){
	if(customLink == ""){
		var customLink = $(this).attr("href");
	}
	var shoeID = customLink.match(/#showShoe=([0-9]+)/);
	shoeID = shoeID[1];
	$.ajax({
		url: "ajax.php",
		type: "GET",
		cache: false,
		data: "module=getShoe&shoeID="+shoeID,
		beforeSend: function(XMLHttpRequest){
			$("#ajaxContentHolderFullScreenL2").html('');
			$("#ajaxContentHolderFullScreenL2").css("background", "#fff url('img/loading.gif') no-repeat left top");
		},
		success: function(html){
			$("#ajaxContentHolderFullScreenL2").html(html);
			$("#ajaxContentHolderFullScreenL2").fadeIn(500);
			$("#ajaxContentFullScreenL2Close").css("right", "10px").css("top",  "10px").fadeIn(500);
		}
	});
}

/**********************************************
	Limited Edition
**********************************************/


var images = new Array();
var cnt = 0;
var intervalID;

myImageArray = new Array();
myImageArray[0]="/img/limitedEdition/colaj/0.jpg";
myImageArray[1]="/img/limitedEdition/colaj/1.jpg";
myImageArray[2]="/img/limitedEdition/colaj/2.jpg";
myImageArray[3]="/img/limitedEdition/colaj/3.jpg";
myImageArray[4]="/img/limitedEdition/colaj/4.jpg";
myImageArray[5]="/img/limitedEdition/colaj/5.jpg";
myImageArray[6]="/img/limitedEdition/colaj/6.jpg";
myImageArray[7]="/img/limitedEdition/colaj/7.jpg";
myImageArray[8]="/img/limitedEdition/colaj/8.jpg";
myImageArray[9]="/img/limitedEdition/colaj/9.jpg";
myImageArray[10]="/img/limitedEdition/colaj/10.jpg";

function loadImage(imgPath){
	var img = new Image();
	$(img)
		.attr('src', imgPath)
		.load(
			function(){
				if(cnt == 10){
					$("#ajaxContentHolderFullScreenL1").css("background", "#cfc5b9");
					cnt = 0;
					nextSlideShowImg();
				}
				else{
					images[cnt] = this;
					cnt++;
					loadImage(myImageArray[cnt]);
				}
			}
		)
		;
}


var imagesPreview = new Array();
var cntPreview = 0;
var previewIntervalID;

myImageArrayPreview = new Array();
myImageArrayPreview[0]="/img/previewCollection/colaj/0.jpg";
myImageArrayPreview[1]="/img/previewCollection/colaj/1.jpg";
myImageArrayPreview[2]="/img/previewCollection/colaj/2.jpg";
myImageArrayPreview[3]="/img/previewCollection/colaj/3.jpg";
myImageArrayPreview[4]="/img/previewCollection/colaj/4.jpg";
myImageArrayPreview[5]="/img/previewCollection/colaj/5.jpg";
myImageArrayPreview[6]="/img/previewCollection/colaj/6.jpg";
myImageArrayPreview[7]="/img/previewCollection/colaj/7.jpg";
myImageArrayPreview[8]="/img/previewCollection/colaj/8.jpg";
myImageArrayPreview[9]="/img/previewCollection/colaj/9.jpg";
myImageArrayPreview[10]="/img/previewCollection/colaj/10.jpg";
myImageArrayPreview[11]="/img/previewCollection/colaj/11.jpg";
myImageArrayPreview[12]="/img/previewCollection/colaj/12.jpg";


function loadPreviewImage(imgPath){
	var img = new Image();
	$(img)
		.attr('src', imgPath)
		.load(
			function(){
				if(cntPreview == 12){
					imagesPreview[cntPreview] = this;
					$("#ajaxContentHolderFullScreenL1").css("background", "#cfc5b9");
					cntPreview = 0;
					nextSlideShowImg();
				}
				else{
					imagesPreview[cntPreview] = this;
					cntPreview++;
					loadPreviewImage(myImageArrayPreview[cntPreview]);
				}
			}
		)
		;
}