// For Additional Product Images Scroll Bar
var speed = 5;
var grap  = 175;

$(function() { // I'm Ready!

/* Global Functions
------------------------------------------------------------------------------*/

$("#head-message").cycle({
    timeout:        5000,
    speed:          1500,
    cleartypeNoBg:  true
});
$('#home-cycle').after('<div id="cycle_nav">').cycle({
		fx:'scrollLeft',
		speed:300,
		timeout:5000,
		pager:'#cycle_nav'

});
$("#head-banner").cycle({
    /*
    fx: 'custom',
    cssBefore: {
        top:  0,
        left: 0,
        width: 0,
        height: 0,
        zIndex: 1
    },
    animIn:  {
        width: 336,
        height: 200,
        opacity: 1
    },
    animOut: {
        //top:  200,
        //left: 350,
        //width: 0,
       // height: 0
        opacity: 0
    },
    cssAfter: {
        zIndex: 0
    },
    speedOut: 600,
    speedIn: 1000,
    delay: -1000,*/
    cleartypeNoBg: true
});

$('#home-banner').after('<div id="cycle_nav">').cycle({
	fx:         'scrollLeft',
	speed:      500,
	timeout:    8000,
	pager:     '#cycle_nav'
});


var manuHTML = $("#manufacturers select").html();
$("#manu-top-select").html(manuHTML);
$("#manu-top span").html($("#manufacturers option:selected").html());

$("#manu-top-select").change(function() {
    var text = $(this).find(":selected").html();
    var val = $(this).find(":selected").val();
    $(this).parent().find("span").html(text);
    $("#manufacturers option").removeAttr("selected");
    $("#manufacturers option[value='"+val+"']").attr("selected","selected");
    $("#manufacturers form").submit();
});

/* 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");
	});



/* Cosmetic
------------------------------------------------------------------------------*/



	$(".item").click(function() {
		window.location = $(this).find("a").eq(0).attr("href");
	});

    var basketHTML = $("#basket-container a").html();
        basketHTML = basketHTML.replace(")","");
        basketHTML = basketHTML.replace("(","");
    $("#basket-container a").html(basketHTML);


	/* Equalizer
		Makes sure that per row, column_display for example are equal heights
	------------------------------------------------------------------------------*/
	$.fn.equalizeCols = function(){
			var height = 0,
				reset = $.browser.msie && $.browser.version < 7 ? "1%" : "auto";

			return this
				.css("height", reset)
				.each(function() {
					height = Math.max(height, this.offsetHeight);
				})
				.css("height", height)
				.each(function() {
					var h = this.offsetHeight;
					if (h > height) {
						$(this).css("height", height - (h - height));
					};
				});

	};

	$(".item_product_image").equalizeCols();
	$(".item_price").equalizeCols();
	$(".item_name").equalizeCols();
	$(".item").equalizeCols();
	$(".address_block").equalizeCols();



/* IE PNG fix multiple filters */
(function ($) {
    if (!$) return;
    $.fn.extend({
        fixPNG: function(sizingMethod, forceBG) {
                if (!($.browser.msie)) return this;
                var emptyimg = "includes/templates/custom/images/blank.gif"; //Path to empty 1x1px GIF goes here
                sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
                this.each(function() {
                        var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                                imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                                src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                        this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                        if (isImg) this.src = emptyimg;
                        else this.style.backgroundImage = "url(" + emptyimg + ")";
                });
                return this;
        }
    });
})(jQuery);

/*
$(".submenu").hoverIntent(function() {

  //$(this).find(".level2").stop(true,true).slideDown("fast");

	$elem = $(this).find(".level2");
	if($elem.is(":hidden")) {
 		$elem.stop(true,true).animate				({ 'height' : 'toggle', 'opacity' : 'toggle' },200);
	} else { // javascript kicked in too late
		$elem.show();
	}



}, function() {
	if($elem.is(":visible")) {
 		//$elem.stop(true,true).animate				({ 'height' : 'toggle', 'opacity' : 'toggle' },200);
$elem.fadeOut("fast");
	} else { // javascript kicked in too late
		$elem.hide();
	}

}); */

$(".submenu").hoverIntent(function() {

  $(this).find(".level2").stop(true,true).show();

}, function() {
$(this).find(".level2").stop(true,true).hide();


});

$(".submenu ul li a:last").css("border","0");

var maxheight=150;

$("#manu-scroll img").each(function() {
    var height = $(this).height();
    $(this).css({ "margin-top" : (maxheight-height)/2+"px" });
});

$("#nav-cat li:last").hide();


}); // I'm all out of ready...



/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

