// JavaScript Document

// POSTBACK
function pageLoad() {
	// Prod Listing
	//$(".listing #panel_2 ul li:not(.brands #panel_2 ul.alphabet li)").css("background","url(/images/bg/li_divide.png) top right no-repeat");
	//$(".listing #panel_2 ul li:last-child:not(.brands #panel_2 ul.alphabet li)").css("background","none");
	//$(".listing #panel_2 ul:not(.brands #panel_2 ul.alphabet li)").css("background","url(/images/bg/ul_divide.png) bottom center no-repeat");
	//$(".listing #panel_2 ul:last-child:not(.brands #panel_2 ul.alphabet li)").css("background","none");
	
	// New sticker
	if ($("img").hasClass("newsticker")) {
		$("img.newsticker").parent("a").after("<div class='newsticker' />");
	};
	
	// Bestsellers sticker
	if ($("img").hasClass("beststicker")) {
		$("img.beststicker").parent("a").after("<div class='beststicker' />");
	};
	
	// Offers sticker
	if ($("img").hasClass("offersticker")) {
		$("img.offersticker").parent("a").after("<div class='offersticker' />");
	};	
	
};

// DOM READY
$(function() {  
		   	   
	// preload wrapper background images
	var preload = new Image();
	var images = [ 
	   "images/bg/bg/bg_body_tile.jpg", 
	   "images/bg/bg_body.jpg",
	   "images/bg/bg_panel_top.png", 
	   "images/bg/bg_panel.gif",
	   "images/bg/bg_panel_btm.png",
	   "images/bg/bg_product_top.jpg",
	   "images/bg/bg_product_btm.png",
	   "images/bg/bg_product_middle.jpg", 
	   "images/bg/bg_listing_sidebar.jpg",
	   "images/bg/bg_listing_sidebar_middle.jpg" ]
	
	for (i=0; i>images.length; i++) {
		preload.src=images[i];	
	}
	
	// Delivery panel
	$("#deliverydetails").hide();
	$("input#delbox").click(function() {
		$("#deliverydetails").toggle("slow");
		sIFR.replace(vaground, {
			selector: '.vaground',
			css: '.sIFR-root { color: #5c8200; }'
		});
	});
		
	// Keyword Search
	var searchbox = $("#ctl00_txtQuickSearch");
	searchbox.focus(function() {
		if (searchbox.val() == "Keyword Search") {
			searchbox.val("");	
		}
	});
	
	searchbox.blur(function() {
		if (searchbox.val() == "" ) {
			searchbox.val("Keyword Search");	
		}
	});
	
	// Accordion
    $("#accordion").accordion({				  
		header: 'a.primary',
		autoHeight: false,
		navigation: false
    });
	
	// original accordion
    /*$(".accordion-nested").accordion({	
		header: 'a.secondary',
		autoHeight: false,
		navigation: true
    });*/
	
	// new accordion
	$(".accordion-nested").find("a.secondary").each(function(){
		if (window.location.href == $(this).attr("href")) {
			$(this).addClass("ui-state-active");
			$(this).next().slideDown("slow");	
		}
	});
	$(".accordion-nested").find("ul>li>a").each(function(){
		if (window.location.href == $(this).parent().parent().prev().attr("href")) {
			$(this).parent().parent().show();
		}
	});
											   
	// Basket modal
	$modal = $("#mdlPopup");
	modalH = $modal.outerHeight();
	modalW = $modal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	$modal.before("<div id=\"overlay\"><!--[if lte IE 6]><iframe></iframe><![endif]--></div>");
	$overlay = $("#overlay").css({ height: $(document).height(), opacity: "0.6" });
	
	// Info modal
	$infomodal = $("#infomdlPopup");
	infomodalH = $infomodal.outerHeight();
	infomodalW = $infomodal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	$infomodal.before("<div id=\"overlay\"><!--[if lte IE 6]><iframe></iframe><![endif]--></div>");

});

// Basket modal
basketModal = function(text) {
	modalH = $modal.outerHeight();
	modalW = $modal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	if(text) $modal.find("p").html(text);
	var left = (windowW/2)-(modalW/2);
	var top = ((windowH/2)+$(window).scrollTop())-(modalH/2) - 22;
	$overlay.fadeIn(500);
	$("#overlay iframe").css({ top: top, left: left, width: $modal.outerWidth(), height: $modal.outerHeight() })
	
	$modal
	.css({ top: top, left: left })
	.fadeIn(500)
	.find("img[class='modal-close'], img[class='continue']")
	.click(function(e) {
		e.preventDefault();
		$modal.hide();
		$overlay.fadeOut(750);
	});
}

$(window).resize(function() {
	modalH = $modal.outerHeight();
	modalW = $modal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	var left = (windowW / 2) - (modalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (modalH / 2) - 22;
	if ($modal.is(":visible")) {
		$modal.animate({ top: top, left: left }, 500);
	}
});

// Info modal
infoModal = function(text) {
	infomodalH = $infomodal.outerHeight();
	infomodalW = $infomodal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	if (text) $infomodal.find("p").html(text);
	var left = (windowW / 2) - (modalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (infomodalH / 2) - 22;
	$overlay.fadeIn(500);
	$("#overlay iframe").css({ top: top, left: left, width: $infomodal.outerWidth(), height: $infomodal.outerHeight() })

	$infomodal
	.css({ top: top, left: left })
	.fadeIn(500)
	.find("img[class='modal-close'], img[class='continue']")
	.click(function(e) {
		e.preventDefault();
		$infomodal.hide();
		$overlay.fadeOut(750);
	});
}
$(window).resize(function() {
	infomodalH = $infomodal.outerHeight();
	infomodalW = $infomodal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	var left = (windowW / 2) - (infomodalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (infomodalH / 2) - 22;
	if ($infomodal.is(":visible")) {
		$infomodal.animate({ top: top, left: left }, 500);
	}
});
