$(document).ready(function(){
	$(".popup").css('display', 'none');
	$(".shopping-cart a").click(
		function (event) {
			var ID = $(this).attr('rel');
			centerPopup(ID);
			loadPopup(ID);
			event.preventDefault();
		}
	);

	$(".popup-close").click(function(){
		disablePopup();
	});

	$("#popup-background").click(function(){
		disablePopup();
	});

	$(document).keypress(
		function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		}
	);
	
	$("form").submit(function(event){
		var index = $("form").index(this);
		var parameter = $(".select-frame-color:eq("+index+")").val() + " - " + $(".select-lens-color:eq("+index+")").val().replace("ReActive ", "");
		$("input[name='os0']:eq("+index+")").val(parameter);
	})
	
	$(".popup-close").hover(
		function(){
			$(".popup-close img").attr("src", "assets/images/global/close-button-hover.png");
		},
		function(){
			$(".popup-close img").attr("src", "assets/images/global/close-button.png");
		}
	).click(function(event){event.preventDefault()});
});

var popupStatus = 0;

function loadPopup(ID){
	if(popupStatus==0){
		$("#popup-background").css({
			"opacity": "0.7"
		});
		$("#popup-background").fadeIn("slow");
		$("#" + ID).fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#popup-background").fadeOut("slow");
		$(".popup").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(ID) {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#" + ID).height();
	var popupWidth = $("#" + ID).width();
	
	$("#" + ID).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#popup-background").css({
		"height": windowHeight
	});
}
