salmon.namespace.addNamespace("boots.product.listgridview");
boots.product.listgridview = {
	v:{
		btn_addToBag : "/wcsstore/cmsassets/Boots/Library/Icon/PD_AddToBasketSearchResults/btn_addtobasket02.gif",
		btn_sizeSelect : "/wcsstore/cmsassets/Boots/Library/Icon/ProductListIcons/btn_selectSize/btn_selectSize.gif",
		color_large : "/wcsstore/cmsassets/Boots/Library/Icon/ProductListIcons/btn_variantSelect/btn_variantSelect.gif",
		color_small : "/wcsstore/cmsassets/Boots/Library/Icon/ProductListIcons/btn_variantSelect_sml/btn_variantSelect_sml.gif",
		size_image_count : 0,
		color_image_count : 0
	},
	checkListRenderedOk:function(){
		var cookieValue = this.getListCookie();
		if(cookieValue == "gridView"){
			$("#listViewType #view_grid").click();
		} else {
			$("#listViewType #view_list").click();
		}
	},
	setEvents:function(){
		$("#listViewType #view_list").bind('click', function() {
			var bpl = boots.product.listgridview;
			$("#listViewType #view_list").removeClass('show').addClass('hide');
			$("#listViewType #view_grid").removeClass('hide').addClass('show');
			$("#ProductViewListGrid").removeClass('gridView').addClass('listView');
			bpl.v.color_image_count = $(".pl_colour").length;
			if(bpl.v.color_image_count > 0){
				$(".pl_colour").attr("src",bpl.v.color_large);
			}
			bpl.changeCurrentViewLinksTo("listView");
			
			$(".btn_requestEmail").each(function(){
				$(this).parent().find(".out_of_stock_help").removeClass('hide').addClass("show");
			});

			bpl.createPageViewTag("grid to list");
			
			return false;
		});
		$("#listViewType #view_grid").bind('click', function() {
			var bpl = boots.product.listgridview;
			$("#listViewType #view_grid").removeClass('show').addClass('hide');
			$("#listViewType #view_list").removeClass('hide').addClass('show');
			$("#ProductViewListGrid").removeClass('listView').addClass('gridView');
			bpl.v.color_image_count = $(".pl_colour").length;
			if(bpl.v.color_image_count > 0){
				$(".pl_colour").attr("src",bpl.v.color_small);
			}
			bpl.v.size_image_count = $(".pl_sizeVariant").length;
			if(bpl.v.size_image_count > 0){
				//$(".pl_colour").attr("src",bpl.v.btn_sizeSelect);
			}
			bpl.changeCurrentViewLinksTo("gridView");
	
			$(".btn_requestEmail").each(function(){
				$(this).parent().find(".out_of_stock_help").removeClass('show').addClass("hide");
			});
		
			bpl.createPageViewTag("list to grid");		
			
			return false;
		});
	},
	changeCurrentViewLinksTo:function(list_type){
		$(".pagination a").each(function(){
		    var urlAttributes = {}
		    var thisLink = $(this).attr("href");
			var queryString = thisLink.substring(thisLink.indexOf("?")+1);
			var urlOnly =  thisLink.substring(0,thisLink.indexOf("?"));
			var params = queryString.split('&');
			var hasViewType = false;
			for (var i=0; i<params.length; i++) {
				var position = params[i].indexOf('=');
				if (position > 0) {
					var key = params[i].substring(0,position);
					var val = params[i].substring(position+1);
					if(key == "viewType"){
						params[i] = "viewType=" + list_type;
						hasViewType = true;
					}
				}
			}
			if(!hasViewType){
				params.push("viewType=" + list_type);
			}
			for (var i=0; i<params.length; i++) {
				urlOnly += (i == 0) ? "?" : "&";
				urlOnly += params[i];
			}
			$(this).attr("href",urlOnly);
		});
		$("input#viewType").attr("value",list_type);
		this.setListCookie(list_type);
	},
	createPageViewTag:function(list_type){
		var titleOut = document.getElementById("titleOut");
			var categoryOut = document.getElementById("categoryOut");
			var searchTermOut = document.getElementById("searchTermOut");
			var resultCountOut = document.getElementById("resultCountOut");
			var storeIdOut = document.getElementById("storeIdOut");
			var exploreAttributes = document.getElementById("exploreAttributes");
			
			var exploreAttValue = exploreAttributes.value.replace(/-_-/g, "~");
			
			var exploreAtts = exploreAttValue.split("~");
			
			var newExploreString = "";
			
			for (var i = 0; i < exploreAtts.length; i++){
				if (i > 0){
					newExploreString += "-_-";
				}
				
				if (i == 6){
					newExploreString += list_type;
				}
				else{
					newExploreString += exploreAtts[i];
				}									
			}
			
			cmCreatePageviewTag(titleOut.value, categoryOut.value, searchTermOut.value, resultCountOut.value, storeIdOut.value, newExploreString);
			
			//update the explore attribute cookie with the new value
			newExploreString = newExploreString.replace("list to grid", "Grid");
			newExploreString = newExploreString.replace("grid to list", "List");

			boots.common.cookie.setCookie("_bdccookie_cm_explore", newExploreString);
	},
	setListCookie:function(list_type){
		var cookieString = "boots_pd_listType=" + escape(list_type);
		document.cookie = cookieString;
	}, 
	getListCookie:function(){
		var allcookies = document.cookie;
		var cookieName = "boots_pd_listType=";
		var pos = allcookies.indexOf(cookieName);
		if (pos != -1) {
			var start = pos + cookieName.length;
			var end = allcookies.indexOf(";", start);
			if (end == -1) end = allcookies.length;
			var value = allcookies.substring(start, end);
			value = decodeURIComponent(value);
			return value;
		}
		return null;
	},
	setupAllActionsForALoadedQuickView:function(){
		boots.colourPalette.init();
		this.lineUpViewAllButtonOnQuickView();
	},
	lineUpViewAllButtonOnQuickView:function(){
		var qvheight = $("#pdqv_inner2").innerHeight();
		var standardheight = 226;
		if(qvheight > standardheight){
			var newMargin = qvheight - standardheight;
			$(".viewItemDetails").css("margin-top",newMargin + "px");
		} else {
			$(".viewItemDetails").css("margin-top","10px");
		}
	},
	init:function(){
		this.setEvents();
		this.checkListRenderedOk();
	}
}

boots.quickview = { 
	busy:false,
	Add2ShopCartViaQuickView:function(){ 
		if ($(this).hasClass("freeGift" || "freeGiftColour")) {
			return false;
		}
		if (!boots.quickview.busy) {
			boots.quickview.busy = true;
			$("#OrderItemAddForm").attr({action:window.location.protocol + "//" + window.location.host + "/webapp/wcs/stores/servlet/OrderItemAdd"});
			$("#OrderItemAddForm")[0].URL.value = document.location;
			$("#OrderItemAddForm").submit();
		}
	}
};

$(document).ready(function(){
	boots.product.listgridview.init();
});


function RequestEmailWhenBackInStock(productId) {
	var url = "";
   	if (!busy) {
		currUrl="${prodDisplayURL}"
		strId="${WCParam.storeId}";
		busy = true;
		url = window.location.protocol + "//" + window.location.host + "/webapp/wcs/stores/servlet/SignUpForEMailWhenBackInStock?storeId=" + strId + "&productId="+productId+ "&itemId="+productId+"&errorViewName=ProductDisplayErrorView"+"&URL=" + currUrl;
		document.location=url;
	}
}
function RequestEmailWhenBackInStock(productId,strId,productDisplayURL) {
	var url = "";
   	if (!busy) {
		busy = true;
		url = window.location.protocol + "//" + window.location.host + "/webapp/wcs/stores/servlet/SignUpForEMailWhenBackInStock?storeId=" + strId + "&productId="+productId+ "&itemId="+productId+"&errorViewName=ProductDisplayErrorView"+"&URL=" + productDisplayURL;
		document.location=url;
	}
}
function RequestEmailWhenBackInStockQuickView(productId,strId,productDisplayURL) {
	var url = "";
   	if (!busy) {
		busy = true;
		url = window.location.protocol + "//" + window.location.host + "/webapp/wcs/stores/servlet/SignUpForEMailWhenBackInStock?storeId=" + strId + "&productId="+productId+ "&itemId="+productId+"&errorViewName=ProductDisplayErrorView"+"&URL=" + productDisplayURL;
		document.location=url;
	}
}

