/*
	@author: Patricia Adamsson, patricia.adamsson@cloudnine.se
	@created: 2009-03-01
*/

function easterEgg() {
	
		$("#easterOverLaid").css("height", ($(document).height()) * 1.05);
		$("#easterOverLaid").css("opacity", 0).fadeTo("medium", 0.9);

		var setPosV = ($(window).height() / 2) - ($("#leEgg").height() / 2) // Finds the vertical center
		var setPosH = ($(window).width() / 2) - ($("#leEgg").width() / 2) // Finds the horizontal center

		$("#leEgg").css({ top: setPosV, left: setPosH });

		$("#easterOverLaid, #leEgg").click(function() {
			$("#easterOverLaid, #leEgg").remove();
		});
	
}
function buildOverlay() {
	$("body").prepend("<div id='pPopOlay'></div>");
	$("#pPopOlay").css("height", $(document).height());
	$("#pPopOlay").css("opacity", 0).fadeTo("medium", 0.6);
}

function setCenterPos() {
	var winTop = $(window).scrollTop();
	var setLeft = ($(window).width() / 2) - ($("#pPop").width() / 2)
	var setTop = ($("#pPop").height() > ($(window).height() - 20)) ? (10 + winTop) + "px" : (($(window).height() / 2) - ($("#pPop").height() / 2) + winTop) + "px";

	$("#pPop").css({ top: setTop, left: setLeft });
}

function openOverlay() {
	$(".productImages .productLeash a").each(function() {
		$(this).click(function(e) {
			e.preventDefault();

			$("body").prepend("<div id='pPopPlaceHolder'></div>");

			buildOverlay();

			var imgHref = $(this).attr("href").toString(); // Gets the img path of the clicked <a>

			$("#pPopPlaceHolder").load("/UI/pages/lightbox.html", function() {

				$("#pPopBox").append('<img id="bigPic" src="' + imgHref + '" alt="" />'); // Puts the img tag with the right src in the popup

				$("#bigPic").load(function() {
					setCenterPos();
				});

				$("#pPopClose, #pPopOlay").click(function() {
					$("#pPopPlaceHolder, #pPopOlay").remove();
				});
			});
		});
	});
}

// Hints for Login fields e.g "Username"
function loginHints() {
	$("#login .userName").val($("#login .usernameLabel").text());
	$("#login .password").val($("#login .passwordLabel").text());

	$("#login .userName, #login .password").focus(function() {
		$(this).val("");
		$(this).css({ color: "#000" })
	});
}

function calander() {
	if ($("#topSpace div input").hasClass("calanderFrom")) {
		var input1 = $("#topSpace .calanderFrom").attr("id");
		var input2 = $("#topSpace .calanderTo").attr("id");

		$('#' + input1).datepicker({ showOn: 'button', buttonImage: '/UI/images/icon.calendar.gif', buttonImageOnly: true });
		$('#' + input2).datepicker({ showOn: 'button', buttonImage: '/UI/images/icon.calendar.gif', buttonImageOnly: true });
	}
}

// extWindows() makes links open in a different window. use rel="external" instead of target="_blank" and rel="popup" or rel="popup|[width]|[height]" for popups
function extWindows(){
	$("a[rel=external]").attr("target","_blank");
	$("a[rel^=popup]").click(function(){
		theHref = $(this).attr("href");
		$(this).attr("href","javascript:void(0)");
		var popupWidth, popupHeight
		var relSplit = $(this).attr("rel").split("|");
		if(relSplit[1]){
			popupWidth = relSplit[1];
			popupHeight = relSplit[2];
		} else {
			popupWidth = 435;
			popupHeight = 350;
		}
		popMeUp(theHref,popupWidth,popupHeight)
	});
}
function popMeUp(strURL, strWidth, strHeight) {
    theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
    theWin.focus();
}

$(document).ready(function() {
	$(".back2Products").mouseover(function() {
		$("img", this).attr("src", "/UI/images/icon.back2.products.gif")
		$("img", this).attr("src", "/UI/images/icon.back2.products2.gif")
	});
	$(".back2Products").mouseout(function() {
		$("img", this).attr("src", "/UI/images/icon.back2.products.gif")
	});
	$(".loginError .xBtn").click(function() {
		$(".loginError").remove();
	});
	openOverlay();
	calander();
	loginHints();
	extWindows();
	easterEgg();
});