// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var openSlide = false;
var openSlideMemoryCard = false;

jQuery(document).ready(function($) {

	//var menu_option = $("#j_menu_option").attr("option");
	//$("#"+menu_option).slideDown();

	$(".j_click_me_close").click(function (e) {

		if(openSlide)
		{
			$("#"+menu_option).slideDown();
			$(".j_click_me_close").css({
				 "background-image": "url(/ui/zoombits/hotspot_left_nav.gif)",
				 "margin-top":"8px"
				 });
			openSlide = false;

		}
		else
		{
			$("#"+menu_option).slideUp();
			$(".j_click_me_close").css({
				 "background-image": "url(/ui/zoombits/left_nav_arrow.gif)",
				 "margin-top":"10px"
				 });
			openSlide = true;
		}
	})

	/*
	$(".j_click_me_close_memory_card").click(function (e) {
		if(openSlideMemoryCard){
			$("#"+menu_option).slideDown();openSlideMemoryCard = false;}
		else{
			$("#"+menu_option).slideUp();openSlideMemoryCard = true;}
	})*/



	jQuery("a.j_confirm").click(function (e) {

		var answer = confirm("are you sure?")
		if (answer == false){
			e.preventDefault();
		}

	});


	jQuery("a.j_buy_now").click(function (e) {
		// stop normal link click
	    e.preventDefault();
		window.scrollTo(0,0);


		//$("#popupBasketLoading").html('<div><p align="center"><img src="/ui/zoombits/lightbox/lightbox-ico-loading.gif" alt="Shopping Basket Loading" /></p></div>');
		//centering with css
		//centerPopupLoading();
		//load popup
		//loadPopupLoading();

		var products_id = $(this).attr("products_id");
		var category_id = $(this).attr("category_id");
		var snapshot;
		//alert('This item will now be added to your shopping cart');

		if(category_id == 2)
		{
			var url_action = 'add_hunt_product';
		}
		else
		{
			var url_action = 'buy_now';
		}

		$.ajax({
    		url: "/ajax_shopping_cart.php",
    		type: "POST",
			data: {action : url_action, products_id: products_id }, // whatever the data is
			success: function(msg){
				//disablePopupLoading();
				//alert(msg);
				$("#j_basket_summary").html(msg);
				$("#popupAddToBasket").html('<div><p align="center"><img src="http://www.memorybits.co.uk/images/bespoke/cartimage.gif" alt="Shopping Basket" /></p><p align="center"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/shopcartinfo.gif"></p><p align="center"><a href="/promo"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/gotocheckoutbtn.gif"></a> &nbsp;&nbsp; <a onclick="changeClass()" style="cursor:pointer;"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/shopmorebtn.gif"></a></p></div>');
				//centering with css
				centerPopup();
				//load popup
				loadPopup();
    		},
			error: function(msg,text,error){
						alert("Item now added to your shopping cart");
                        //alert(msg);
						window.location.reload();
			}

		});

	});

	jQuery("a.j_buy2_now").click(function (e) {
		// stop normal link click
	    e.preventDefault();
		window.scrollTo(0,0);


		//$("#popupBasketLoading").html('<div><p align="center"><img src="/ui/zoombits/lightbox/lightbox-ico-loading.gif" alt="Shopping Basket Loading" /></p></div>');
		//centering with css
		//centerPopupLoading();
		//load popup
		//loadPopupLoading();

		var products_id = $(this).attr("products_id");
		var snapshot;
		//alert('This item will now be added to your shopping cart');

		$.ajax({
    		url: "/ajax_shopping_cart.php",
    		type: "POST",
    		data: {action : 'buy2_now', products_id: products_id }, // whatever the data is
    		success: function(msg){
				//disablePopupLoading();
				//alert(msg);
				$("#j_basket_summary").html(msg);
				$("#popupAddToBasket").html('<div><p align="center"><img src="http://www.memorybits.co.uk/images/bespoke/cartimage.gif" alt="Shopping Basket" /></p><p align="center"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/shopcartinfo.gif"></p><p align="center"><a href="/promo"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/gotocheckoutbtn.gif"></a> &nbsp;&nbsp; <a onclick="changeClass()" style="cursor:pointer;"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/shopmorebtn.gif"></a></p></div>');
				//centering with css
				centerPopup();
				//load popup
				loadPopup();
    		},
			error: function(msg,text,error){
						alert("Item now added to your shopping cart");
                        //alert(msg);
						window.location.reload();
			}

		});

	});

    jQuery("#referrer_email_address").click(function (e) {
        var loc = window.location;
        var loc_hostname = loc.href.substring(0, loc.href.lastIndexOf('/'));

        $("#popupReferrerEmailAddress").html('<div style="margin-bottom: 2px; width: 100%; text-align: right;"><a href="#" onclick="disablePopup();">Close</a></div><iframe src="'+loc_hostname+'/viral.php?modal_mode=1" width="100%" height="98%" scrolling="yes"  horizontalscrolling="no" verticalscrolling="yes" style="overflow-x: hidden; overflow-y: scroll;" frameborder=0></iframe>');
        centerPopupViralReferrer();
        loadPopupViralReferrer();

        //centerPopup();
        //loadPopup();
    });

	jQuery("a.j_buy_now_extra").click(function (e) {
	    e.preventDefault();
		window.scrollTo(0,0);
		var products_id = $(this).attr("products_id");
		var snapshot;
		$.ajax({
    		url: "/ajax_shopping_cart.php",
    		type: "POST",
    		data: {action : 'buy_now', products_id: products_id },
    		success: function(msg){
						window.location.href = "/shopping_cart";
    		},
			error: function(msg,text,error){
						window.location.href = "/shopping_cart";
			}

		});

	});


	//$(document).everyTime(10000,function(){
	//		changeClass();
	//	},0,true);

	//CLOSING POPUP
	//Click out event!
	//$("#backgroundPopup").click(function(){
	//	disablePopup();
	//});

	$("#popupLuckyDay").click(function(){
		disablePopup();
	});


	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

	$("#luckyDayButton").click(function(){
		$("#popupLuckyDay").html('<div><p align="center"><img src="http://www.memorybits.co.uk/images/bespoke/further_discount.gif" alt="More money off!" /></p></div>');
		//centering with css
		centerPopupLucky();
		loadPopupLucky();
	});

	$("#promoConfirmButton").click(function(){
		$("#popupAddToBasket").html('<div><p align="center"><img src="http://www.memorybits.co.uk/images/bespoke/cartimage.gif" alt="Shopping Basket" /></p><p align="center"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/shopcartinfo.gif"></p><p align="center"><a href="/promo"><img border="0" src="http://www.memorybits.co.uk/images/bespoke/gotocheckoutbtn.gif"></a></p></div>');
		//centering with css
		centerPopup();
		loadPopup();

	});

});




/// POP UPS FOR ADDING TO BASKET


//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;



function changeClass()
{
	$('#j_basket_summary').removeClass("basket_summary_empty").addClass("basket_summary");
	disablePopup();
}



//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupAddToBasket").fadeIn("slow");
		popupStatus = 1;
	}
}

//loading popup with jQuery magic!
function loadPopupLoading(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupBasketLoading").fadeIn("slow");
	}
}

//centering popup
function centerPopupLoading(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupBasketLoading").height();
	var popupWidth = $("#popupBasketLoading").width();

	//centering
	$("#popupBasketLoading").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"opacity": "1",
		//"top": '100px',
		//"left": '50%'
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight
	});

}

//disabling popup with jQuery magic!
function disablePopupLoading(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#popupBasketLoading").fadeOut("slow");
		popupStatus = 0;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupAddToBasket").fadeOut("slow");
		$("#popupLuckyDay").fadeOut("slow");
		$("#popupXmas").fadeOut("slow");
        $("#popupReferrerEmailAddress").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupAddToBasket").height();
	var popupWidth = $("#popupAddToBasket").width();

	//centering
	$("#popupAddToBasket").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"opacity": "1",
		//"top": '100px',
		//"left": '50%'
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight
	});

}

//loading popup with jQuery magic!
function loadPopupLucky(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupLuckyDay").fadeIn("slow");
		popupStatus = 1;
	}
}

//centering popup
function centerPopupLucky(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupLuckyDay").height();
	var popupWidth = $("#popupLuckyDay").width();

	//centering
	$("#popupLuckyDay").css({
		"position": "absolute",
		//"top": windowHeight/2-popupHeight/2,
		"top": 150,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight
	});

}

function loadPopupViralReferrer() {
	//loads popup only if it is disabled
	if(popupStatus == 0) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});

		$("#backgroundPopup").fadeIn("slow");
		$("#popupReferrerEmailAddress").fadeIn("slow");
		popupStatus = 1;
	}
}

function centerPopupViralReferrer() {
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupReferrerEmailAddress").height();
	var popupWidth = $("#popupReferrerEmailAddress").width();

	//centering
	$("#popupReferrerEmailAddress").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"opacity": "1",
		//"top": '100px',
		//"left": '50%'
		"left": windowWidth/2-popupWidth/2
	});

	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}
