var $windowWidth = 0;
var $windowHeight = 0;

var $intoWishPopup = 0;
var $intoBagPopup = 0;


// **************************************************************************************************************************************************** 
// ********    jQuery    *********************************************************************************************************************************** 
// **************************************************************************************************************************************************** 
$(document).ready(function() {
	
	// CHANGE WINDOW ON RESIZE 
	$.fn.changeOnLoadOrResize = function() {
		$windowWidth = $(window).width();
		$windowHeight = $(window).height();
		//$("#ajaxLoader").css("top", Math.round($windowHeight - ($windowHeight / 2) - 25) );
		//$("#ajaxLoader").css("left", Math.round($windowWidth - ($windowWidth / 2) - 25) );
		//$("#ajaxLoader").css("top", ($axisY - 25) ); // relative to mouse Y mouse position 
		//$("#ajaxLoader").css("left", ($axisX - 25) ); // relative to mouse X mouse position 
	};
	
	$(window).load(function() { // On load 
		$(this).changeOnLoadOrResize();
		//alert($windowWidth + " * " + $windowHeight + " * " + $(window).width() + " * " + $(window).height()); // TEST 
	});
	$(window).resize(function() {
		$(this).changeOnLoadOrResize();
		//alert($windowWidth + " * " + $windowHeight + " * " + $(window).width() + " * " + $(window).height()); // TEST 
	});
	
	// ************  KEYBOARD NAVIGATION  ***********************   
	$(document).keydown(function(event) {
		//alert(event.which + "\n=========\n" + event); // TEST 
		if($intoWishPopup && event.keyCode == 27) { // Escape = close wish popup 
			$(this).closeWishPopup();
		}
		if($intoBagPopup && event.keyCode == 27) { // Escape = close bag popup 
			$(this).closeBagPopup();
		}
		/*
		if ($fullImagePreview) {
			if(event.keyCode == 27 || event.keyCode == 88) { // Escape | x = Close Full Image 
				$(this).closeFullImageWindow();
			} else if(event.keyCode == 37 || event.keyCode == 38) { // Left Arrow | Up Arrow = Previous Image 
				$(this).displayPreviousImage();
			} else if(event.keyCode == 39 || event.keyCode == 40 || event.keyCode == '13') { // Right Arrow | Down Arrow | Enter = Next Image 
				$(this).displayNextImage();
			}
		}
		*/
	});
	
	// INSERT INTO WISH 
	$(".addToWishListIntoGroups, .addToWishList")
		.bind("click", function() {
			$intoWishPopup = 1;
			// show background area 
			$("#ajaxBackground").show(100);
			// show into wishlist popup 
			$("#intoWishPopup").css({"top" : 250, "left" : Math.round($windowWidth / 2) - Math.round($("#intoWishPopup").width() / 2)});
			$("#intoWishPopupProductname").html($(this).find("input[name='productName']").attr("value"));
			$("#intoWishPopup").fadeIn(300);
		})
		;
	// CLOSE WISH POPUP 
	$.fn.closeWishPopup = function() {
		$('#ajaxBackground').hide(300);
		$("#intoWishPopup").hide(300);
		$intoWishPopup = 0;
	}
	$("#intoWishPopup button").bind("click", function() { $(this).closeWishPopup(); });
	
	// INSERT INTO BAG 
	$(".addToShoppingBagIntoGroups, .addToShoppingBag, .productIntoShoppingbag")
		.bind("click", function() {
			$intoBagPopup = 1;
			// show background area 
			$("#ajaxBackground").show(100);
			// show into bag popup 
			$("#intoBagPopup").css({"top" : 250, "left" : Math.round($windowWidth / 2) - Math.round($("#intoBagPopup").width() / 2)});
			$("#intoBagPopupProductname").html($(this).find("input[name='productName']").attr("value"));
			$("#intoBagPopup").fadeIn(300);
		})
		;
	// CLOSE BAG POPUP 
	$.fn.closeBagPopup = function() {
		$('#ajaxBackground').hide(300);
		$("#intoBagPopup").hide(300);
		$intoBagPopup = 0;
	}
	$("#intoBagPopup button").bind("click", function() { $(this).closeBagPopup(); });
	
	
});

