// For Additional Product Images Scroll Bar
var speed = 5;
var grap  = 175;

$(function() { // I'm Ready!

/* Global Functions
------------------------------------------------------------------------------*/



/* Predictive Search Code
------------------------------------------------------------------------------*/

	$("#predictiveSearch").html("<div id='predictive-content'></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);
                                   // $("#shadow").css("width",$(document).width()+"px");
                                    //$("#shadow").css("height",$(document).height()+"px");
							},
							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
------------------------------------------------------------------------------*/

    $(".column_display .productPriceDiscount").each(function() {

        $removeSave = $(this);
        var html = $removeSave.html();
        html = html.replace('Save:&nbsp;','');
        html = html.split(/\s/);
        $removeSave.html("<span>"+html[0]+"</span><span>"+html[1]+"</span>");  // wrap in span

    });

	$(".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));
					};
				});

	};

    $('#left .productPriceDiscount, #right .productPriceDiscount').each(function(){
        $removeSave = $(this);
        //$removeSave.html( $removeSave.html().replace('Save:&nbsp;','') );  // this removes 'save:' followed by non-breaking-space.
        var html = $removeSave.html();
        html = html.replace('Save:&nbsp;','');
        html = html.split(/\s/);
        $removeSave.html("<span>"+html[0]+"</span><span>"+html[1]+"</span>")  // wrap in span
    });    

	$(".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);

}); // I'm all out of ready...

