// For Additional Product Images Scroll Bar 
var speed = 5;
var grap  = 240;

$(function() {
$('#affiliatesBoxSideContent ul').append('<li><a href="program-details-cms-11.html">Program Details</a></li><li><a href="affiliate-agreement-cms-12.html">Affiliate Agreement</a></li>');
$("#recentBlogPosts").before("<a href='index.php?main_page=account'><img border=0 src='includes/templates/custom/images/loyaltypoints.jpg'/></a>");


});

/*

$(function() {

$(".level2 span").click(function() {

if($(this).parent().hasClass("submenu")) { 



         if($(this).parent().find("ul").is(":visible")) {
           
            //createCookie("menuPos",n,1);
            return true;

         } else {

           $(".level3").slideUp();

         }

	$(this).parent().find("ul").slideToggle();


	
	var n = $(".level2 span").index(this);
	//console.log(n);	
	createCookie("menuPos",n,1);	

	return false;

}

});

var menuVal = getCookie("menuPos");

if(menuVal!=null) {
$(".level2 span").eq(menuVal).parent().find("ul").show();
}

});


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 getCookie(cookieName){var cookieJar=document.cookie.split("; ");for(var x=0;x<cookieJar.length;x++){var oneCookie=cookieJar[x].split("=");if(oneCookie[0]==escape(cookieName)){return unescape(oneCookie[1])}}return null}

*/


sfHover = function() {
	var sfEls = document.getElementById("nav-cat").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);






$(function() {
/* Predictive Search Code
------------------------------------------------------------------------------*/

	$("#predictiveSearch").html("<div id='predictive-top'></div><div id='predictive-content'></div><div id='predictive-bot'></div>");

	$(".search_box").attr("autocomplete","off");

	searchStore = $(".search_box").val();

	$(".search_box").click(function(e) {
		ajaxSearch($(this));
	});

	$(".search_box").keyup(function(e) {

		            var $this = $(this);
		            if (e.keyCode == '13') {  // on enter key go to highlighted result
		            	if($("#predictiveSearch .highlight").length>0) {
		            		var getURL = $(".products_wrap").eq(position).attr("href");
		            		window.location = getURL;
		            	}
		                e.preventDefault();
		            }

		            if(e.keyCode == '40') {  // key down
		            	if(position>$("#predictive-content tr").size()-1) { return false; }
		            	$(".ajax-wrap").removeClass("highlight");
		           		position++;
		           		$(".ajax-wrap").eq(position).addClass("highlight");
		           		return false;
		            }

		            if(e.keyCode == '38') {  // key up
		            	if(position<1) { return false; }
		            	$(".ajax-wrap").removeClass("highlight");
		           		position--;
		           		$(".ajax-wrap").eq(position).addClass("highlight");
		           		return false;
		            }

		            position = -1;

		            ajaxSearch($(this));

		});

		function ajaxSearch(searchString) {

			//$("#shadow").css("height",$(document).height()+"px");

			$this = searchString;

			if($this.val()==searchStore) { // Saves repeat searches
				$("#predictiveSearch").show();
				//$("#shadow").stop(true,true).fadeTo("fast",0.5);
				return false;
			} else {
				searchStore = $this.val();
			}

		            if ($this.val().length <= 3) {
		               $("#predictive-content").html("<p style='text-align:center;margin:0;padding:10px;'>More than 3 characters required.</p>");
		               return;
		            }  else {    // Passed now we're going to try the search
		            	$("#predictive-content").html("");
		            	$("#search-loading").show();
		            }

			 $.ajax({
			        type: "POST",
			        url:  "ajax.php",
			        data: "type=product&action=predictiveSearch&l=6&s=small&d=70&iurl=true&searchfor="+$this.val(),
							dataType: "json",
							beforeSend: function(){
									//$("#shadow").stop(true,true).fadeTo("fast",0.5);
							},
							success: function(options){

								$("#search-loading").hide();

								$("#predictive-content").html("");

								var string = "<table id='ajax-search-table' cellpadding=0 width='100%' cellspacing=0>";

								$.each(options,function(a,b) {
									$("#predictive-content").append("<a class='products_wrap' href='"+ b['url'] +"'><div class='products_image'><img src='" + b['image'] + "' /></div><div class='products_name'>" + b['name'] + "</div>" + "<div class='clear'></div></a><div class='clear'></div>");
									string+= "<tr class='ajax-wrap'>";
									string+= "<td class='product-image'><a href='"+b['url']+"'><img src='" + b['image'] + "' /></a></td>"; // Image
									string+= "<td><p class='product-title'><a href='"+b['url']+"'>" + b['name'] + "</a></p><p class='predictive-description'>"+b['description']+"</p></td>"; // Text
									string+= "</tr>";
								});

								string+= "</table>";

								$("#predictive-content").html(string);

								$("#predictive-content").find("td").click(function() {
									window.location = $(this).find("a").attr("href");
								});

								if(options.length>0) $("#predictiveSearch .products_wrap:last").css({ 'border-bottom' : '0' });
								if(options.length<1) $("#predictive-content").html("<p style='text-align:center;margin:0;padding:10px;'><strong>0</strong> products found.</p>");

								if($("#predictiveSearch").is(":visible")) {
									$("#predictiveSearch").animate({"opacity" : "show", "height" : "auto" },300);
								} else {
									$("#predictiveSearch").animate({"opacity" : "toggle", "height" : "toggle" },300);
								}

			                }
			       	});

		}


	$(".search_box").blur(function() {
		$("#predictiveSearch").animate({"opacity" : "hide", "height" : "toggle" },300);
		//$("#shadow").fadeOut("fast");
	});

});



jQuery.cookie=function(key,value,options){if(arguments.length>1&&String(value)!=="[object Object]"){options=jQuery.extend({},options);if(value===null||value===undefined){options.expires=-1}if(typeof options.expires==='number'){var days=options.expires,t=options.expires=new Date();t.setDate(t.getDate()+days)}value=String(value);return(document.cookie=[encodeURIComponent(key),'=',options.raw?value:encodeURIComponent(value),options.expires?'; expires='+options.expires.toUTCString():'',options.path?'; path='+options.path:'',options.domain?'; domain='+options.domain:'',options.secure?'; secure':''].join(''))}options=value||{};var result,decode=options.raw?function(s){return s}:decodeURIComponent;return(result=new RegExp('(?:^|; )'+encodeURIComponent(key)+'=([^;]*)').exec(document.cookie))?decode(result[1]):null};

$(function() {

	$(".level2 .submenu > span > a").click(function(e){

		e.preventDefault();

		$this = $(this);
		$ul = $this.parent().parent();
		$level3 = $ul.find(".level3");

		if($level3.is(":visible")) {
			$level3.hide();
		} else {
			$level3.show();
		}

		$(".level3").not($level3).hide();	

	});

$(".level3 a").click(function() {
var n = $("#categoriescss a").index(this);
$.cookie("menu",n);
});

if($.cookie("menu")!=null) {
$("#categoriescss a").eq($.cookie("menu")).parent().parent().parent().show();
$("#categoriescss a").eq($.cookie("menu")).attr("style","color: #80131c !important");
}

});


