var globalPosition = 0;

$(window).load(function(){
	resizeMe();
	resizeMe();
	$(window).resize(function() {
		resizeMe();
		if($("#photoCollection").length > 0){
			resizeCollection();
		}
	});

	if($("#photoCollection").length > 0){
		resizeCollection();
	}
	
	$("#arrow").click(function(){
		rotateImgHome();
	});

	window.setInterval("rotateImgHome()", 4000);
});

$("#buyNow").hide();
$("#shopNow").hover(function(){
	$("#buyNow").show();
}, function(){
	$("#buyNow").hide();
});

function rotateImgHome(){
	var total = $("#longlong").data("total");
	if (total > 1){
		var next = globalPosition + 1;
		var bigImg = $(".bigImg");
		if (next < total){
			bigImg.fadeOut("slow");
			$(bigImg[next]).fadeIn("slow");
			globalPosition = next
		}else{
			bigImg.fadeOut("slow");
			$(bigImg[0]).fadeIn("slow");
			globalPosition = 0
		}
	}
	return false;
}

function resizeMe(){
	var globalWidth = parseInt($(window).width());
	$("#container").css("width", globalWidth);
	$("#vimeoVideo").css("width", globalWidth);
	$("#iframeContainer").css("width", globalWidth);
	// la larghezza è uguale alla larghezza totale meno la nav meno il margin left (cioè 205px)
	var myWidth = globalWidth - 265;
	if (globalWidth < 890){
		$("#container").css("width", 890);
		myWidth = 642;
	}
	var globalHeight = parseInt($(window).height());
	$("#container").css("height", globalHeight);
	$("#vimeoVideo").css("height", globalHeight);
	$("#iframeContainer").css("height", globalHeight);
	var myHeight = globalHeight - 160;
	if (globalHeight < 785){
		$("#container").css("height", 785);
		myHeight = 625;
	}

	var myHeightHome = globalHeight - 80;
	if (globalHeight < 785){
		myHeightHome = 705;
	}
	
	var globalLength = $(".bigImg").length * myWidth;
	$("#longlong").css("width", globalLength);
	
	$("#mask .bigImg").css("width", myWidth);
	$("#mask .bigImg").css("height", myHeight);
	$("#mask").css("width", myWidth);
	$("#mask").css("height", myHeight);
	$("#homeMask").css("width", myWidth);
	$("#homeMask").css("height", myHeightHome);
	$("#homeMask .bigImg").css("width", myWidth);
	$("#homeMask .bigImg").css("height", myHeightHome);
	$("#testWidth").html(myWidth);
	$("#testHeight").html(myHeight);
	$("#testHeightGlobal").html(globalHeight);
	
	var image = null;
	$('.bigImg').each(function(index) {
		image = $("img", this);
			resizeImage(image, myWidth, myHeight);
	});
	if ($("#center").length > 0)
		centerDiv(myHeight);
}

function resizeImage(image, myWidth, myHeight){
	var imgHeight = parseInt(image.height());
	var imgWidth = parseInt(image.width());
	var imgHeightDest = 0;
	var imgWidthDest = 0;
	var useHeight = false;

	imgWidthDest = parseInt(myWidth);
	imgHeightDest = parseInt((imgWidthDest * imgHeight) / imgWidth);
	
	if (imgHeightDest != 0 && imgHeightDest < myHeight){
		imgHeightDest = myHeight;
		useHeight = true
	}
	
	if (useHeight){
		image.css("height", imgHeightDest);
		image.css("width", "auto");
	}else{
		image.css("width", myWidth);
		image.css("height", "auto");
	}
	image.fadeIn("slow");
}

function centerDiv(heightMask){
	// padding = (heightMask - 414) / 2;
	// $("#center").css("paddingTop", padding);
}

// news
$(".rightArrow").click(function(){
	var newsContainer = $(this).parent();
	var imgMask = $(".imgMask", newsContainer);
	var nextElement = $(":visible", imgMask).next();
	$("img", imgMask).hide();
	if (nextElement.length > 0){
		nextElement.fadeIn();
	}else{
		$("img", imgMask).first().fadeIn();
	}
	return false;
})

$("#men").click(function(){
	$(".menChild").slideToggle(350);
});

$("#women").click(function(){
	$(".womenChild").slideToggle(350);
});

function resizeCollection(){
	var widthContainer = parseInt($("#mask").width());
	var heightContainer = parseInt($("#mask").height());
	var widthDestination = widthContainer - 290;
	var heightDestination = heightContainer - 50;
	var heightPhoto = $("#photoCollection img:first").height();
	var widthPhoto = $("#photoCollection img:first").width();
	var heightPhotoDestination = 0;
	var widthPhotoDestination = 0;
	if (heightPhoto > widthPhoto){
		heightPhotoDestination = heightDestination;
		widthPhotoDestination = (heightPhotoDestination * widthPhoto) / heightPhoto;
		if (widthPhotoDestination > widthDestination){
			widthPhotoDestination = widthDestination;
			heightPhotoDestination = (widthPhotoDestination * heightPhoto) / widthPhoto;
		}
	}else{
		widthPhotoDestination = widthDestination;
		heightPhotoDestination = (widthPhotoDestination * heightPhoto) / widthPhoto;
		if (heightPhotoDestination > heightDestination){
			heightPhotoDestination = heightDestination;
			widthPhotoDestination = (heightPhotoDestination * widthPhoto) / heightPhoto;
		}
	}
	$("#photoCollection img:first").css("width", widthPhotoDestination);
	$("#photoCollection img:first").css("height", heightPhotoDestination);
	$("#photoCollection img:first").css("marginTop", heightPhotoDestination/-2);
	$(".arrowProduct").css("marginTop", (heightDestination/2 - 20));
	$("#photoCollection").css("width", widthPhotoDestination);
	$("#photoCollection").css("height", heightDestination);
	$("#descriptionProduct").css("marginTop", (heightDestination-145));
	$("#hideThis").fadeOut();
}

$(".toLinkify").linkify();

$("#image0").click(function(){
	$("footer").hide();
	$("#iframeContainer").fadeIn();
	var dataIframe = $("#iframeContainer").attr("data-iframe");
	// if (dataIframe == "none"){
	// 	var numRand = Math.floor(Math.random()*101);
	var numRand = 51
		if (numRand > 50){
			$('<iframe />').attr("id", "vimeoVideo").attr('src', 'http://player.vimeo.com/video/36263662?autoplay=1').attr('frameborder', 0).css("display", "none").appendTo('#iframeContainer'); 
			$("#iframeContainer").attr("data-iframe", "36264247");
		}else{
			$('<iframe />').attr("id", "vimeoVideo").attr('src', 'http://player.vimeo.com/video/36264247?autoplay=1').attr('frameborder', 0).css("display", "none").appendTo('#iframeContainer'); 
			$("#iframeContainer").attr("data-iframe", "36263662");
		}
	// }else{
	// 	$('<iframe />').attr("id", "vimeoVideo").attr('src', 'http://player.vimeo.com/video/'+dataIframe+'?autoplay=1').attr('frameborder', 0).css("display", "none").appendTo('#iframeContainer'); 
	// 	if (dataIframe == "36263662")
	// 		$("#iframeContainer").attr("data-iframe", "36264247");
	// 	else
	// 		$("#iframeContainer").attr("data-iframe", "36263662");
	// }
	$("#vimeoVideo").css("width", $("#iframeContainer").width());
	$("#vimeoVideo").css("height", $("#iframeContainer").height());
	$("#vimeoVideo").show();
})

$(document).keyup(function(e) {
  if (e.keyCode == 27) { 
	$("#iframeContainer").hide();  
	$("#vimeoVideo").remove();
  } 
});

$("#closeVideo").click(function(){
	$("#iframeContainer").hide();  
	$("#vimeoVideo").remove();
	$("footer").show();
})
