/*******************************************************
* Salmat
* Version: v1 24/04/07
*
* Copyright Hothouse Interactive :: www.hothouse.com.au ::
* Unauthorised modification / use is a criminal offence, and will be prosecuted the fullest extent permitted by law.
* All Rights Reserved 
* These Scripts require the jQuery library to run http://jquery.com
*******************************************************/

$(function() {

	/* catalogue widget menu */

	$(".lasoo-catalogue-toolbar ul.lasoo-catalogue-sub-nav").parent().parent().hover(
		function() {
			var panelName = $(this).parent().attr("class");
			var objFirsPanel = $(this).parent().parent().children("div:first");
			var objLastPanel = $(this).parent().parent().children("div:last");
			var objToolbarOuter = $(this).parent().parent().parent();
			var objToolbar = $(this).parent().parent().parent().parent();
			var toolbarId = $(objToolbar).attr("id");
			var toolbarIdNumber = toolbarId.charAt(toolbarId.length-1);
			 
			if (objFirsPanel.attr("class") == panelName) {
				$(objToolbar).addClass("lasoo-catalogue-toolbar-outer-hover");
				$(objToolbar).addClass("lasoo-catalogue-toolbar-outer-hover-" + toolbarIdNumber);
			} else if (objLastPanel.attr("class") == panelName) {
				$(objToolbar).addClass("lasoo-catalogue-toolbar-hover");
				$(objToolbar).addClass("lasoo-catalogue-toolbar-hover-" + toolbarIdNumber);
			}
			
			$(this).children().children("ul.lasoo-catalogue-sub-nav").stop().slideDown("fast", function() {
				$(this).css("height", "auto");
			});
		},
		function() {
			var panelName = $(this).parent().attr("class");
			var objFirsPanel = $(this).parent().parent().children("div:first");
			var objLastPanel = $(this).parent().parent().children("div:last");
			var objToolbarOuter = $(this).parent().parent().parent();
			var objToolbar = $(this).parent().parent().parent().parent();
			var toolbarId = $(objToolbar).attr("id");
			var toolbarIdNumber = toolbarId.charAt(toolbarId.length-1);
			
			$(this).children().children("ul.lasoo-catalogue-sub-nav").stop().slideUp("fast", function() {
				$(this).css("height", "auto");
				if (objFirsPanel.attr("class") == panelName) {
					$(objToolbar).removeClass("lasoo-catalogue-toolbar-outer-hover");
					$(objToolbar).removeClass("lasoo-catalogue-toolbar-outer-hover-" + toolbarIdNumber);
				} else if (objLastPanel.attr("class") == panelName) {
					$(objToolbar).removeClass("lasoo-catalogue-toolbar-hover");
					$(objToolbar).removeClass("lasoo-catalogue-toolbar-hover-" + toolbarIdNumber);
				}
			});
		}
	);

/*********************************************************
Product List Sort By Hover State
*********************************************************/

	/*$("ul.sort_list li").not(".active").children("a").parent().hover(function(){
	  $(this).addClass("active hover");
	},function(){
	  $(this).removeClass("active hover");
	});*/
	
/*********************************************************
Browse by Catalogue Tabs Hover State
*********************************************************/

	$(".tabs_sub li").hover(function(){
	  $(this).addClass("hover");
	},function(){
	  $(this).removeClass("hover");
	});
	
/*********************************************************
Left hand search
*********************************************************/
	
	$("#container-popup").tabs();
	$("#nearest-store-list ul.orange_list a").click( function() { 
		$("#nearest-store-list").hide();
		$($(this).attr("href")).show();
		return false;
	} );
	$(".show-nearest-store-list").click( function() { 
		$(this).parent().parent().hide();
		$("#nearest-store-list").show(); 
		return false;
	} );
	$("#nearest-store-details-tab").click( function() { 
		$(".nearest-store-details").hide();
		$("#nearest-store-list").show();
	} );
		
	
	// Super Light Blue headers - B004b
	$(".roundbox_lightblue_box").each(function(){
		$(this).parents().eq(2).prev(0).children().eq(0).prepend($(this).find("div.head_section:eq(0)"));
	});
	
	// Replace the form buttons with links
	$("input[@class*=btn]").each( function () {
		var span = $('<span></span>').attr('class',$(this).attr("class"));
		$('<a>'+ $(this).attr("value") +'</a>').attr('href','#').attr('title',$(this).attr("value")).appendTo(span);
		$(this).before(span);
		$(this).remove();
	});
	
	//Execute form functions off href button click  

      $("a[@title=refine]").click(function(){
            $(".catalogue-form").submit();
      });
      
      $("a[@title=Show Pages]").click(function(){
            $("#catalogueCategoryRefine").submit();
      });
      
	
	// Top and Left position functions
	$.fn.offsetLeft = function() {
		var e = this.get(0);
		if(!e.offsetParent) return e.offsetLeft;
		return e.offsetLeft + $(e.offsetParent).offsetLeft(); 
	}
	
	$.fn.offsetTop = function() {
		var e = this.get(0);
		if(!e.offsetParent) return e.offsetTop;
		return e.offsetTop + $(e.offsetParent).offsetTop(); 
	} 
	
	// Show / Hide login box
	/*if ($(".loginbutton").html() != null ) {	
		var loginLeftOffset = $(".loginbutton").offsetLeft() - 80;
		var loginTopOffset = $(".loginbutton").offsetTop() + 20;
		$(".loginbutton").click(function(e){
			$(".logincontainer").css({top:loginTopOffset+"px",left:loginLeftOffset+"px"}).show();
			$('.popupContainer').hide();
			$('.popup_two').hide();
		});
	}*/
	
	// Show / Hide Show Nearest Store box
	
	if ($(".changebutton").html() != null ) {	
		//var changeLeftOffset = $(".changebutton").offsetLeft() - 230;
		//var changeTopOffset = $(".changebutton").offsetTop() + 10;
		$(".changebutton").click(function(e){
		//	$(".popup_two").css({top:changeTopOffset+"px",left:changeLeftOffset+"px"}).show();
		//	$('.popupContainer').hide();
			googleMapOnPageLoad();
		});
	}
	
	// Hide the all categories section on mouseout
	/*var onfocus = 0;
	
	function login_hide() {
		if (onfocus == 0) {
			$(".logincontainer").hide();
		} 
	}
	
	$(".logincontainer").hover(function(){
	  //$(this).addClass("hover");
	},function(){
		if (onfocus == 0) {
			$(this).hide();
		} 
	  //$(this).hide();
	});*/
	
	// The login container stay if it's click but hide once click on other section
	/*$(".logincontainer").click( function() {
		onfocus = 1;
		login_hide();
	});
	
	$("#main_content").click( function() {
		onfocus = 0;
		login_hide();
	});*/
	
	$("#pnav").click( function() {
		onfocus = 0;
		login_hide();
	});
	
	$("#right_column").click( function() {
		onfocus = 0;
		login_hide();
	});
	
	// initialise global varaible for popups
	globalPopupShow = 0;
	
	/*
	// when you click anywhere on the document - hide popups & the auto_box for setting location via ajax
	$("#dc_site_wrap, .ps_tabs a").click( function() {
		if(globalPopupShow == 0) {
			$(".popupContainer").hide();
			$(".logincontainer").hide();
			$("#auto_box").hide();
		}
		else {
			globalPopupShow = 0;
		}
	});
	// when you click on a popup allow the popup to stay
	$(".popupContainer, .logincontainer").click( function() {
		globalPopupShow = 1;
	});
	
	*/
	
	//Hide AJAX BOX HIDE SHOW
	$("#auto_box_container").hover(function() {
	  //do nothing							  
	},function(){
		$("#auto_box_container").hide();
	});
		
});

//http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//Generic Popup For cross site ajax
function generic_popup2(targ,name,adjustmentY,adjustmentX) {
			/*
			globalPopupShow = 1;
			//move all popups to top left location
			$(".popupContainer").css({display:"block",top: "0px",left: "0px"});
			$(".logincontainer").css({display:"block",top: "0px",left: "0px"});
			$(".popup_two").css({display:"block",top: "0px",left: "0px"});
			
			//find position of object that was clicked
			var objPosition = findPos(targ);
			
			//find position of popup on page (should always be the top left - set above)
			defaultPopupPosition = findPos(document.getElementById(name));
			
			//calculate position popUp should be placed in with adjustments
			popupPositionX = objPosition[0] - defaultPopupPosition[0] + adjustmentX;
			popupPositionY = objPosition[1] - defaultPopupPosition[1] + adjustmentY;
			
			$('.popup').hide();
			$('.popupContainer').hide();
			$('.logincontainer').hide();
			$('.popup_two').hide();
			
			$(".popup").show();
			$("#"+name).css({display:"block",top: popupPositionY+"px",left: popupPositionX+"px"});
			*/
			$("#"+name).modal(Global.modalBoxOptions);
}

function showModalPopup(popupId) {
	$("#"+popupId).modal(Global.modalBoxOptions);
}

function closeModalPopup() {
	$.modal.impl.close();
}

//Generic Popup For Reviews and Terms Condition
function generic_popup(e,name,adjustmentY,adjustmentX) {
			/*
			if (!e) var e = window.event;
			
			//find target object
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) // defeat Safari bug
				targ = targ.parentNode;
			*/
			// generic_popup2(targ,name,adjustmentY,adjustmentX);
			$("#"+name).modal(Global.modalBoxOptions);
}				
			

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
// As seen in Lightbox JS: Fullsize Image Overlays 
// by Lokesh Dhakar - http://www.huddletogether.com
//

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// As seen in Lightbox JS: Fullsize Image Overlays 
// by Lokesh Dhakar - http://www.huddletogether.com
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}	

var modalPostCloseEvent=null;
//Simple Modal options
var Global = {};

Global._newCaptchaUrl= '';

Global.closeModal = function (dialog) {
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
		$.modal.close(); // must call this to have SimpleModal
		// re-insert the data correctly and
		// clean up the dialog elements
		if (modalPostCloseEvent) {
			modalPostCloseEvent();
			modalPostCloseEvent=null;
		}
	} else {
		dialog.data.fadeOut('fast', function () {
			dialog.container.slideUp('fast', function () {
				dialog.overlay.fadeOut('fast', function () {
					$.modal.close(); // must call this to have SimpleModal
					// re-insert the data correctly and
					// clean up the dialog elements
					if (modalPostCloseEvent) {
						modalPostCloseEvent();
						modalPostCloseEvent=null;
					}
				});
			});
		});
	}
};

Global.modalBoxOptions = {
	persist: true,
	closeClass: 'modalOverlay',
	onClose: Global.closeModal
};

function showLoginPopup(hint) {
	$.modal.close();
	if (hint) {
		$("#loginpurpose").html(hint)
	} else {
		$("#loginpurpose").empty();
	}
	$('div.logincontainer').modal(Global.modalBoxOptions);
}

$(document).ready(function () {
	// login function
	if ($("#header .loginbutton").size() > 0) {
		$("#header .loginbutton").click(function () {			
			showLoginPopup();
		});
	}

});
