$(document).ready(function() {
						   
	initMenu();
        initShipping();
       
       $("#ShippingOptions").onChanged(function(e) {  initShipping(); });
       $("#shippingCountry").onChanged(function(e) {  initShipping(); });
      						   
	var isLoggedIn = $("#loggedIn").val();	
	//alert('isLoggedIn = ' + isLoggedIn);
	if(isLoggedIn == 1) {		
		$('.memberLink').css("display","inline"); // show member links
		$('.loginLink').css("display","none"); // hide login links
	} else {
		$('.loginLink').css("display","inline"); // hide login links
		$('.memberLink').css("display","none"); // show member links
	}

	/* Popup functionality */
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  (winH/2-$(id).height()/2)+400);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
});

// Need to fix shipping options to override state tax selection
function initShipping() {
  // Set VAT as default option for state selection and hide the div
  $("#shippingState").val("8575");
  $('.shippingStateDiv').css('display','none');

  // Clear contents and readonly attribute on state field 
  $("#ShippingState").removeAttr("readonly");
  $("#ShippingState").val("");	
}

/* Populates the billing address from the delivery address */
function populateBillingAddress(theCheckBox) {
	if(theCheckBox.checked) {
		populateBillingAddress();			
	} else {
		document.getElementById("BillingAddress").value = "";
		document.getElementById("BillingCity").value = "";
		document.getElementById("BillingState").value = "";
		document.getElementById("BillingZip").value = "";
	}
}

function populateBillingAddress() {
       document.getElementById("BillingAddress").value = document.getElementById("ShippingAddress").value;
       document.getElementById("BillingCity").value = document.getElementById("ShippingCity").value;				
       document.getElementById("BillingState").value = document.getElementById("ShippingState").value;
       document.getElementById("BillingZip").value = document.getElementById("ShippingZip").value;	
}

/* Accordion menu
------------------------------------------*/
function initMenu() {
	$('#nav_516780 ul').hide();
	loadMenuState();
	//$('#nav_516780 ul:first').show();
	//$('#nav_516780 ul:eq(0)').show();
	$('#nav_516780 li a').click(
	function() {
		var checkElement = $(this).next();
		if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('#nav_516780 ul:visible').slideUp('normal');
			checkElement.slideDown('norm	al');           
			// Reset the active element
			$(".active").removeClass("active");
			checkElement.addClass("active");
			saveMenuState();
			return false;
		}
	});
}
/* Load accordion menu state from cookies */
function loadMenuState() {
    var regex = new RegExp("scorpionmenu_" + encodeURIComponent("516780") + "=([01]+)");
    //alert('regex = ' + regex);
    var match = regex.exec(document.cookie);
    //alert('match = ' + match);
    if (match) {
        var states = match[1].split("");
        $('#nav_516780 ul').each(function(i) {
            this.className = (states[i] == 0 ? "active" : "");
            //alert(alert('states[i] = ' + states[i] + ', className = ' + this.className);
        });
		$('#nav_516780 ul.active').show();
	}
}
/* Save accordion menu state to cookies */
function saveMenuState() {
    var states = new Array();
    $('#nav_516780 ul').each(function(i) {
        //alert(this.className);
        states.push(this.className == "active" ? 0 : 1);
    });
    var d = new Date();
    d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
	//"; expires=" + d.toGMTString() + 
    document.cookie = "scorpionmenu_" + encodeURIComponent("516780") + "=" + states.join("") + "; path=/";
}


function ConvertCurrency() {
var find = "&euro;";
var repl = "&pound;";
var page = document.body.innerHTML;
  while (page.indexOf(find) >= 0) {
    var i = page.indexOf(find);
    var j = find.length;
    page = page.substr(0,i) + repl + page.substr(i+j);
    document.body.innerHTML = page;
  }
}

