
$(document).ready(function(){
	new selectGridMode();
	new selectList($("#selectSortList")[0]);

	gridLoader();
	//headerFooterAnimate();
});

function headerFooterAnimate(){
	var head = $("#header"),
		foot = $("#footer");
	
	head.css("top",head.height() * -1);
	foot.css("bottom",foot.height() * -1);
	
	head.animate({"top":0}, 1200, "easeInOutQuart", gridLoader);
	foot.animate({"bottom":0}, 1200, "easeInOutQuart");
	
}

function initQuickView(){
	/*$("#gridWrapper div[id*='gridbox']").each(function(i,el){
		new productQuickView(el);
	});*/
	
	new productQuickView($(this).parents("div[id*='gridbox']")[0]);
}
	
	function selectGridMode(){
		this.gridModeList = $('.gridControl .mode');
		this.gridModeList.find('a').click(function(o){
			return function(){
				o.reset();
				$(this).parent().addClass("selected");
				return false;
			}
		}(this));
	}
	selectGridMode.prototype = {
		reset: function(){
			this.gridModeList.find("li").removeClass("selected");
		}
	};
	


var SelectGrid = {
		
	selectOtherGrid: function() {
		
	$("div#gridWrapper").removeClass("standard").addClass("other");
	
	$("img.getScene7").each(function(){
		 var thisImg = $(this);
		 try {
			 setGridSizes();
		 }catch(e){}
		 
		formScene7URL(thisImg);
		
	});
	
	},
	
	
	selectStandardGrid: function() {
		
		$("img.getScene7").each(function(){
			 var thisImg = $(this);
			 try {
				thisImg.attr("iSize","grids");
			 }catch(e){}
			 
			formScene7URL(thisImg);
			
		});
		
		$("div#gridWrapper").removeClass("color").addClass("standard");
	}
}



var GridBox =  {
		
		selectPrevious: function(gridbox) {
			
			if (gridbox != null && gridbox > 1) {
				
				var previousGridbox = "#gridbox"+(gridbox-1);
			
				$(previousGridbox + " .quickView").click();
			
			}
		},

		selectNext: function(gridbox) {
				if (gridbox != null) {
				
				var nextGridbox = "#gridbox"+(gridbox+1);
				$(nextGridbox + " .quickView").click();
			
			}
		}
}







//$(document).ready(function(){
//	new selectList($(".selectList")[0]);
//});

function selectList(list){
	this.selectList = $(list);
	this.displayField = this.selectList.find("span").first();
	this.options = this.selectList.find(".option").first();
	this.dataSrc = this.selectList.parent().find("select").first();
	
	this.init();
}

selectList.prototype = {
	init: function(){
		
		var self = this;
		
//		$.each(this.dataSrc.find("option"),function(i,val){
//			self.options.append("<a href='' name=" + i + ">" + $(val).html() + "</a>");
//		})
	
		this.selectList.find("a").first().bind({
			click: function(o){
				return function(){
					o.show();
					
					console.log(this);
					
					$(this).focus();
					return false;
				}
			}(this),
			blur: function(o){
				return function(){
					o.hide();
					return false;
				}
			}(this)
		});
		
		this.selectList.find(".option a").bind("click", function(o){
			return function(){
				o.select(this);
				o.hide();
				return false;
			}
		}(this));
	},
	show: function(){
		this.options.show();
	},
	hide: function(){
		this.options.hide();
	},
	select: function(el){
		this.displayField.html($(el).html());
		this.dataSrc[0].selectedIndex = $(el).attr("name");
	}
};

function productQuickView(el){

	$(el).bind({
		mouseenter: this.show,
		mouseleave: this.hide
	});
	
	
	$(el).find(".quickView").click(this.click);
}

productQuickView.prototype = {
	show: function(){
		/*productQuickViewStyle(this);*/
		$(this).addClass("hover");
		
		var target = $(this).find(".productThumbnail img").first(),
			zoom_width = $(this).find(".productThumbnail").width(),
			zoom_pos = zoom_width / -2;
		
		target.stop().animate({
			"width": zoom_width,
			"height": zoom_width,
			"margin-top": zoom_pos,
			"margin-left": zoom_pos
		},"fast");

	},
	hide: function(){
		/*productQuickViewRemoveStyle(this);*/
		$(this).removeClass("hover");
		
		var target = $(this).find(".productThumbnail img").first(),
			zoom_width = $(this).width(),
			zoom_pos = zoom_width / -2;
		
		target.stop().animate({
			"width": zoom_width,
			"height": zoom_width,
			"margin-top": zoom_pos,
			"margin-left": zoom_pos
		},"fast");
		
	},
	click: function(){
		$("#dialog").cc_dialog( {qv_popup: true} );
		//profileAddressJs.refreshNewAddress();
		
		//var newAddressDialog = globalDialogObject;
		
		
		var pid = $(this).attr("pid");
		var sid = $(this).attr("sid");
		var gridbox = $(this).attr("gridbox");
		var pgName = $(this).attr("pgName");
		var notLastGridbox = $(this).attr("notLastGridbox");
		globalDialogObject.dialogTitle = '';
		globalDialogObject.dialogUrl = "/cc/dialog/quickView.jsp?pId="+pid+"&sId="+sid+"&gridbox="+gridbox+"&notLastGridbox="+notLastGridbox+"&pgName="+pgName;
		globalDialogObject.dialogClassName = "qv-popup";
		globalDialogObject.dialogWidth = 600;
		globalDialogObject.popupGlobalDialog(function(){
			triggerDocumentReady_PDP();
			triggerDocumentReady_QV();
		});		
		return false;
		//alert('show quick view: ' + $(this).parent().attr("sku"));
	}
};

function productQuickViewStyle(el){
	var productThumbnailSize = $(el).css("width"),
		requiredClassName = "";
		
	//productThumbnailWrapper.css({"width":productThumbnailSize, "height":productThumbnailSize});
	
	switch(productThumbnailSize){
		case "76px":
			requiredClassName = "size76";
		break;
		case "154px":
			requiredClassName = "size154";
		break;
		case "232px":
			requiredClassName = "size232";
		break;
		case "310px":
			requiredClassName = "size310";
		break;
	}
	
	$(el).find(".productThumbnail").addClass(requiredClassName)
}

function productQuickViewRemoveStyle(el){
	$(el).find(".productThumbnail")
	.removeClass("size76")
	.removeClass("size154")
	.removeClass("size232")
	.removeClass("size310");
}



function gridLoader(){

	var imgList = [],
		targetGrid = $("#gridWrapper div[id*='gridbox'] .productThumbnail img");

	if(($.browser.msie && parseInt($.browser.version) < 8) || ($.browser.mozilla && parseFloat($.browser.version) < 2)){ // && $.browser.version == "7.0"
		targetGrid.each(function(i,el){
			
			var func = initQuickView;
			
			$(el).css({
				"visibility":"visible",
				"display":"none"
			});
			setTimeout(function(o,f){
				return function(){
					//$(o).fadeIn();
					$(o).show("scale", {percent: 100, easing: "easeOutQuart", origin: ['middle','center']}, 400, f);
				}
			}(el,func),450 * i);
		});
	}else{

		targetGrid.each(function(i,el){
			
			var imgSrc = $(el).attr("src");
			
			if(imgSrc == ""){
				
				setTimeout(gridLoader,500);
				return false;
			}else{
				imgList.push(imgSrc);
			}
		});
		
		if(targetGrid.length == imgList.length){
		
			// Preload Level - 20%
			imgList = imgList.slice(0, Math.ceil(imgList.length*0.2));
		
			ImagePreloader(imgList, function(){
				
				targetGrid.each(function(i,el){
					
					var delay = ($.browser.msie && parseInt($.browser.version) < 9)?450:150, func = initQuickView;
					
					$(el).css({
						"visibility":"visible",
						"display":"none"
					});
					
					setTimeout(function(o,f){
						return function(){
							//$(o).fadeIn();
							$(o).show("scale", {percent: 100, easing: "easeOutQuart", origin: ['middle','center']}, 400, f);
						}
					}(el,func),delay * i);
				});
			});
		
		}

	}
}
