var QLJS = new Object();
/*
 * This function checks to see if a variable is null or empty.
 * It's helpful for making code easier to read.
 * We leave this as a global function because it makes code that uses it much neater
 */
function isEmpty(value) {
	if (value == null || value == "") {
		return true;
	} else {
		return false;
	}
}

/*
 * This function allows you to store information in a cookie using JavaScript
 */
QLJS.setCookie = function(c_name, value, expireDays) {
	var exdate = new Date();
	if (expireDays == null) {
		expireDays = 999;
	}
	exdate.setDate(exdate.getDate() + expireDays);
	var cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toGMTString();
	document.cookie = cookie;
}

/*
 * This function allows you to retrieve information from a cookie using JavaScript
 */
QLJS.getCookie = function(c_name) {
	if (document.cookie.length > 0){
		var c_start = document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start = c_start + c_name.length + 1; 
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

/*
 * This method is used to initialise the visibility of each of the 
 * facets on the category tree according to data in a cookie.
 * 
 * It also sets up the onclick events for hiding/showing facets.
 */
QLJS.setupFacetHiding = function() {
	var facetVisibilities = new Object();
	
	//When the page loads fetch the facet visibilities from the cookie
	var visibilityJSON = QLJS.getCookie('facetVisibilities');
	if (!isEmpty(visibilityJSON)) {
		// The cookie contained the information
		facetVisibilities = JSON.parse(visibilityJSON);
	} else {
		// The cookie was empty or didn't exist so we initialise 
		// the facet visibilities all to false (hidden)
		$("span.categoryTree_bold").each(function() {
			var id = $(this).attr("id");
			// Maillife want the category facet expanded by default. (QLMAI-1314)
			if (id == "category_facetValues") {
				facetVisibilities[id] = true;
			} else {
				facetVisibilities[id] = false;
			}
		});
	}
	
	/* 
	 * After the facet visibilities have been loaded (or initialised)
	 * we iterate over the facets and hide them if their visibility 
	 * is set to false (hidden).
	 * We don't need to do anything for the visible ones as they are 
	 * visible by default (to allow for non JS browsers).
	 */
	for (var id in facetVisibilities) {
		var visible = facetVisibilities[id];
		$("#" + id).each(function() {
			//alert(visible + ": " + (visible === 'undefined'));
			var currentFacetValues = $(this).next("ul");
			if (visible) {
				// The facet was stored as being visible in the cookie
				// We don't need to do anything.
			} else {
				// The facet was stored as being collapsed in the cookie
				$(currentFacetValues).css("display", "none");
				$(currentFacetValues).removeClass("expanded");
				$(this).removeClass("active");
			}
		});
	}

	//Register onClick event on titles (purple)
	$("span.categoryTree_bold").click(function() {
		// Declare a helper function for recording facet visibilities.
		this.updateFacetVisibilities = function() {
			var visibilityJSON = JSON.stringify(facetVisibilities);
			QLJS.setCookie('facetVisibilities', visibilityJSON, 1);
		}
		
		var currentFacetValues = $(this).next("ul");
		var id = $(this).attr("id");
		
		if ($(currentFacetValues).hasClass("expanded")) {
			// The user clicked on the currently expanded facet so we collapse it.
			$(currentFacetValues).slideUp();
			$(currentFacetValues).removeClass("expanded");
			$(this).removeClass("active");

			// Mark this facet as hidden
			facetVisibilities[id] = false;
			this.updateFacetVisibilities();
		} else {
			// The user clicked on a currently closed facet
			$("ul li.categoryTree_facetTree ul.expanded").each(function() {
				//Collapses all expanded facets values.
				$(this).removeClass("expanded");
				$(this).slideUp();
				$(this).prev("span").removeClass("active");
				facetVisibilities[$(this).prev("span").attr("id")] = false;
			});
			//Expand requested facet values.
			$(currentFacetValues).slideDown();
			$(currentFacetValues).addClass("expanded");
			$(this).addClass("active");

			// Mark this facet as shown
			facetVisibilities[id] = true;
			this.updateFacetVisibilities();
		}
	});
}

/*
 * This function is used on the payment page to control the hiding/showing 
 * of the add address form.
 */
QLJS.setUpAddAddressHiding = function() {
	var visibility = QLJS.getCookie("addAddressVisibility");
	if (isEmpty(visibility)) {
		visibility = "hidden";
	}
	
	if (visibility == "hidden") {
		// Hide the form if the visibility is set to false
		$(".addressBookAdd_content_addressBookAddLong").hide();
	}
	
	/*
	 * Register the onclick event for hiding/showing the add address form.
	 */
	$("#showHideAddAddressButton").click(function () {
		$(".addressBookAdd_content_addressBookAddLong").each(function() {
			if ($(this).css("display") == "none") {
				// If the form is hidden, show it
				$(this).slideDown();
				QLJS.setCookie("addAddressVisibility", "visible", 1);
			} else {
				// If the form is shown, hide it.
				$(this).slideUp();
				QLJS.setCookie("addAddressVisibility", "hidden", 1);
			}
		});
		return false;
	});
}

/*
 * This is required by the mini basket to set up the mouse over etc
 * 
 */
QLJS.setUpMiniBasket = function(itemsInBasket) {
    var timeouts = new Array;

    $(".myBasket").hover(
        function(){
            var parposition = $("#mb").offset();
            parposition.top = parposition.top + $("#mb").height() - 1;
            $(".myBasket_contents").css(parposition);
            $(".myBasket_contents").slideDown();
        },
        function(){
            timeouts.push(setTimeout( function() {
                $(".myBasket_contents").stop(true, true).slideUp();
            	}, 50));
        }
    );

    $(".myBasket_contents").mouseover(
       	function(){ 
           	while (timeouts.length > 0)
           		clearTimeout(timeouts.pop());
        }
    );
    
    $(".myBasket").mouseover(
    	function(){ 
        	while (timeouts.length > 0)
        		clearTimeout(timeouts.pop());
    }
    );
    
    $(".myBasket_contents").mouseout(
        function(){
        	timeouts.push(setTimeout( function() {
                $(".myBasket_contents").stop(true, true).slideUp();
            	}, 500));
        }
    );
}
