//On Hover Over
function megaHoverOver(){

	$(this).find(".sub").css({'visibility' : 'visible'});
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}

//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}


$(function () {
	
	// Modal boxes - to all links with rel="facebox"
//	$('a[rel*=facebox]').facebox();
	$("#menu ul li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#menu ul li .sub").hide();

	//var path = "http://www.higer.com.au/client/assets/";
	var path = 'http://' + location.host + "/client/assets/";
	//path = 'http://www.ngageit.net/higer/public_html/client/assets/';
    $('a.lightbox').lightBox({
		imageLoading: path + 'images/lightbox-ico-loading.gif',
		imageBtnClose: path + 'images/lightbox-btn-close.gif',
		imageBtnPrev: path + 'images/lightbox-btn-prev.gif',
		imageBtnNext: path + 'images/lightbox-btn-next.gif'
   });

	
	$("#slider").easySlider();		
	$("#slider2").easySlider();		
	$("#internal-slider").easySlider();	

	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 50, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 200, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$("#menu ul li").hoverIntent(config); //Trigger Hover intent with custom configurations

});

Number.prototype.formatMoney = function(c, d, t){ 
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; 
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); 
 }; 

function memberLogin(){
	var divLogin = document.getElementById('divLogin');
	var divRegister = document.getElementById('divRegister');
	divRegister.style.display="none";
	divLogin.style.display="block";
}

function memberRegister(){
	var divLogin = document.getElementById('divLogin');
	var divRegister = document.getElementById('divRegister');
	divLogin.style.display="none";
	divRegister.style.display="block";
}

function memberShipping(){
	if(!document.getElementById('cbPostal').checked){
	 	document.getElementById('txtDeliveryAddress1').value = "";
	 	document.getElementById('txtDeliveryAddress2').value = "";
	 	document.getElementById('txtDeliveryCity').value = "";
	 	document.getElementById('txtDeliveryPostcode').value = "";
	 	document.getElementById('ddlDeliveryCountry').value = "";
	 	document.getElementById('ddlDeliveryState').value = "";
	}else{
	 	document.getElementById('txtDeliveryAddress1').value = document.getElementById('txtBillingAddress1').value;
	 	document.getElementById('txtDeliveryAddress2').value = document.getElementById('txtBillingAddress2').value;
	 	document.getElementById('txtDeliveryCity').value = document.getElementById('txtBillingCity').value;
	 	document.getElementById('txtDeliveryPostcode').value = document.getElementById('txtBillingPostcode').value;
	 	document.getElementById('ddlDeliveryCountry').value = document.getElementById('ddlBillingCountry').value;
	 	document.getElementById('ddlDeliveryState').value = document.getElementById('ddlBillingState').value;
	}
}

function giftMessage(){
	var wrap = document.getElementById('wrap');	
	var hdnWrap = document.getElementById('hdnWrap');	
	var cbWrap = document.getElementById('cbWrap');	
	var divGiftMessage = document.getElementById('divGiftMessage');	
	divGiftMessage.style.display = (cbWrap.checked) ? "block" : "none";	
	wrap.innerHTML =(cbWrap.checked) ? '$' + hdnWrap.value + "" : '$0.00';
	getTotal();
}

function registeredPost(){
	var reg = document.getElementById('reg');	
	var hdnReg = document.getElementById('hdnReg');	
	var cbRegistered = document.getElementById('cbRegistered');	
	(cbRegistered.checked) ? reg.innerHTML = '$' + hdnReg.value + "" : reg.innerHTML = '$0.00';
	getTotal();
}

function getTotal() {
	var hdnSubTotal = document.getElementById('hdnSubTotal');	
	var hdnPostage = document.getElementById('hdnPostage');	
	var hdnWrap = document.getElementById('hdnWrap');	
	var hdnReg = document.getElementById('hdnReg');	
	var cbWrap = document.getElementById('cbWrap');	
	var cbRegistered = document.getElementById('cbRegistered');	
	var total = document.getElementById('total');	
	var t = (parseFloat(hdnSubTotal.value) + parseFloat(hdnPostage.value));
	t += (cbWrap.checked) ? parseFloat(hdnWrap.value) : 0;
	t += (cbRegistered.checked) ? parseFloat(hdnReg.value) : 0;
	total.innerHTML =  '$' + t.formatMoney(2,'.',',') + "";
}

function pageSize(link){
	document.location.href = link + document.getElementById('tbl-page').value;
}

