$(document).ready(function () {
    //loginShowHide();
    //subNavAnimation();
    //productImageSwap();
    //focusOnSearch();
    //panelRollOver();

    if ($(".overlabel").length > 0) {
        try {
            $(function () {
                $(".overlabel").overlabel();
            });
        } catch (ex) { };

    };

    if ($("ul.sf-menu").length > 0) {
        try {
            $(function () {
                $('ul.sf-menu').superfish();
            });
        } catch (ex) { };

    };

});



function loginShowHide() {

		var login = $('#login');

		// create links
		var loginToggle = $("<a href='#' class='loginToggle'>Login</a>");
		var closeToggle = $("<a href='#' class='loginToggle'>Close</a>");


		loginToggle.click(function() {
			login.show();
			$(this).hide();
			login.find('input[@type=text]:first').focus();
		});
		closeToggle.click(function() {
			login.hide();
			loginToggle.show();
		});

		// inject links
		$('#login form').append(closeToggle);
		$('#utilities').prepend(loginToggle);
		login.hide();
	};


	function setCategoryMenu() {

		//Expand the selected branch
		$('#subnav .selected').parents('ul').css('display', 'block');

	}


	function subNavAnimation() {

		$('#subnav .open').click(function () {
			$("#subnav > li > ul:visible").slideUp();
			$(this).siblings('ul:not(:visible)').slideDown();
			return false;
		});

		//Expand the selected branch
		$('#subnav .selected').parents('ul').css('display', 'block');


	}


	function subNavAnimation_Basic() {

		var subnav = $('#subnav');
		var childLinks = $('#subnav .parent > a');


		$('#subnav li ul').each(function() {
			// set the height first otherwise you get a jump in the animation because jQuery can't work out height with margins and padding - doh!
			$(this).css('height', $(this).height())

			// find all lists that don't have a child with the 'selected' class and hide them
			$(this).filter(function(index) {
				return $(this).find('.selected').length < 1;
			}).hide();
		});

		// onclick show the ul but not if this one is already open
		childLinks.click(function() {
			$('#subnav ul:visible').not($(this).siblings('ul')).slideUp();
			$(this).siblings('ul').slideDown();
			return false;
		});


	};

	function productImageSwap() {
		$('.product-view-thumbs a').click(function() {

			// swap img src to link href
			var imageSrc = $(this).attr('href');
			$('.product-view img')
            .not('.product-view-thumbs img')
            .attr('src', imageSrc);

			// change product caption to link title
			if ($(this).attr('title') != '') {
				$('#product-image p').text($(this).attr('title')).show();
			} else {
				$('#product-image p').hide();
			}
			return false;
		});
	};

	function focusOnSearch() {
		$('#search input:first').focus();
	};

	function panelRollOver() {
		var infoHeight = $('.panel a em').outerHeight();

		$('.panel a em').css({
			'top': infoHeight * -1,
			'display': 'block'
		});

		$('.panel a')
        .mouseenter(function() {
        	var info = $(this).find('em');
        	info.stop().animate({
        		'top': 2
        	}, 300);
        })
        .mouseleave(function() {
        	var info = $(this).find('em');
        	info.stop().animate({
        		'top': infoHeight * -1
        	});

        });
	};



			function createCookie(name,value,days) {
				if (days) {
					var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
				}
				else var expires = "";
				document.cookie = name+"="+value+expires+"; path=/";
			}
			
			function readCookie(name) {
				var nameEQ = name + "=";
				var ca = document.cookie.split(';');
				for(var i=0;i < ca.length;i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1,c.length);
					if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
				}
				return null;
			}
			
			function eraseCookie(name) {
				createCookie(name,"",-1);
			}
			
			function storeCookie(sCookieName, sCookieValue) {
				//For backward compat
				createCookie(sCookieName,sCookieValue,180);
			}

			function getCookieItem(sCookieName) {
				//For backward compat			
				return readCookie(sCookieName);			    

			}
			
			function switchMenu(obj){
				var el = document.getElementById(obj);
				el.style.block = (el.style.display != 'block') ? 'block' : 'none';
			}
			function storeCookieWithFutureExpiry(sCookieName, sCookieValue, iCookieExpiryIncrement) {
        var expireDate = new Date();
		    expireDate.setTime(expireDate.getTime()+ iCookieExpiryIncrement);
        document.cookie = sCookieName + '=' + escape(sCookieValue) + ';expires=' + expireDate.toGMTString();
			}
			function selectMenu(lID){
			//alert(lID);
			//return;
			
				var oEle = document.getElementById(lID);
				if (lID){
					//Get it the sub menus
					var oCol=document.getElementsByTagName("div");
					var lThisCatID = 0;
					var lThisParentID = 0;
					var sID = '';
					for(var i=0;i<oCol.length;i++){
						if (oCol[i].id.indexOf("|") != -1){
							sID = oCol[i].id;
							lThisCatID = sID.split("|")[0];
							lThisParentID = sID.split("|")[1];
							if (lID==lThisCatID){
								oCol[i].style.display = (oCol[i].style.display != 'block') ? 'block' : 'none';
							}
						}
						
					}
				}
				
			}


			function hideModalBG() {
				$('#modal-bg').fadeOut();
			}

			function showModal(sDivID) {


				try {


					//Get the screen height and width  
					var maskHeight = $(document).height();

					//Set height and width to mask to fill up the whole screen  
					$('#modal-bg').css({ 'height': maskHeight });

					//transition effect       
					$('#modal-bg').fadeTo("fast", 0.25);

					var id = '#modal-message-container';
					var sContent = document.getElementById(sDivID).innerHTML;
					document.getElementById("model-message-content").innerHTML = sContent;

					var startY = parseInt($(id).css('top'), 10);

					if (startY == 0) {
						startY = 120;
					}
					var scrollY = $(this).scrollTop();

					//Set the popup window to center  
					$(id).css('top', startY + scrollY);

					//transition effect  
					$(id).fadeIn(250);

					//return false;



				}
				catch (err) {
					// Ignore and carry on posting the form.
				}

				return true;


			}



$(document).ready(function () {
    $('#mainnav li').hover(
			function () { $('ul', this).slideDown();},
			function () { $('ul', this).css('display', 'none'); });

});

