/**
 * @author Zach
 */
var MW = function() {
    function getBigImagePath(path) {
        path1 = path.substring(0,path.indexOf('.jpg'));
        path2 = path.substring(path.indexOf('.jpg'),path.length);
        return path1+"_full"+path2;
    }
    
    function getSmallImagePath(path) {
        path1 = path.substring(0,path.indexOf('.jpg'));
        path2 = path.substring(path.indexOf('.jpg'),path.length);
        return path1+"_small"+path2;
    }
	
	function getDetailImagePath(path) {
        path1 = path.substring(0,path.indexOf('.jpg'));
        path2 = path.substring(path.indexOf('.jpg'),path.length);
        return path1+"_detail"+path2;
    }
   
	return {
		MainMenu: "",
       
		SubMenu: "",
		
		collectionover: false,
		
		aboutover: false,
		
		portfolioover: false,
       
		menuover: function(obj,menu) {
			if (menu !== MW.MainMenu) {
				obj.className = "menuitemover";
			}
		},
       
		menuout: function(obj,menu) {
			if (menu !== MW.MainMenu) {
				obj.className = "menuitem";
			}
		},
		
		submenuover: function(obj,menu) {
			if (menu !== MW.SubMenu) {
				obj.className = "submenuitemover";
			}
			switch(menu) {
				case "Collections":
					this.showthirdmenu(menu+"sub");
					this.collectionsover = true;
					break;
				case "About":
					this.showthirdmenu(menu+"sub")
					this.aboutover = true;
					break;
				case "Portfolio":
					this.showthirdmenu(menu+"sub")
					this.portfolioover = true;
					break;
					break;
			}
		},
		
		submenuout: function(obj,menu) {
			if (menu !== MW.SubMenu) {
				obj.className = "submenuitem";
			}
			switch(menu) {
				case "Collections":
					this.collectionsover = false;
					break;
				case "About":
					this.aboutover = false;
					break;
				case "Portfolio":
					this.portfolioover = false;
					break;
			}
			var self = this;
			setTimeout(function(){
				switch (menu) {
					case "Collections":
						if (!self.collectionsover) {
							self.hidethirdmenu(menu+"sub")
						}
						break;
					case "About":
						if (!self.aboutover) {
							self.hidethirdmenu(menu+"sub")
						}
						break;
					case "Portfolio":
						if (!self.portfolioover) {
							self.hidethirdmenu(menu+"sub")
						}
						break;
				}
			},100);
		},

		showthirdmenu: function(id) {
			var p = id.substring(0,id.length-3).toLowerCase();
			var c = document.getElementById("submenu");
			var top = c.offsetTop+20;
			var left = c.offsetLeft;
			var t2 = c.clientTop;
			var l2 = c.clientLeft;
			var isIE7 = navigator.userAgent.indexOf("MSIE 7") !== -1;
			var isIE6 = navigator.userAgent.indexOf("MSIE 6") !== -1;
			
			if (isIE6 || isIE7) {
				switch (p) {
					case "collections":
						//left = left + 184;
						left = left + 78;
						break;
					case "about":
						//left = left + 442;
						left = left + 336;
						break;
					case "portfolio":
						//left = left + 184;
						left = left + 78
						break;
				}
			} else {
				if (p === "about") {
					left = left + 258;
				}
			}
			
			var obj = document.getElementById(id)
			obj.style.top = top+"px";
			obj.style.left = left+"px";
			obj.style.display = "block";
		},
		
		hidethirdmenu: function(id) {
			document.getElementById(id).style.display = "none";
		},
		
		thirdmenucontainerover: function(obj) {
			switch (obj.id) {
				case "Collectionssub":
					this.collectionsover = true;
					break;
				case "Aboutsub":
					this.aboutover = true;
				case "Portfoliosub":
					this.portfolioover = true;
					break;
			}
		},
		
		thirdmenucontainerout: function(obj) {
			switch (obj.id) {
				case "Collectionssub":
					this.collectionsover = false;
					break;
				case "Aboutsub":
					this.aboutover = false;
					break;
				case "Portfoliosub":
					this.portfolioover = false;
					break;
			}
			var self = this;
			setTimeout(function(){
				switch (obj.id) {
					case "Collectionssub":
						if (!self.collectionsover) {
							self.hidethirdmenu(obj.id);
						}
						break;
					case "Aboutsub":
						if (!self.aboutover) {
							self.hidethirdmenu(obj.id);
						}
						break;
					case "Portfoliosub":
						if (!self.portfolioover) {
							self.hidethirdmenu(obj.id);
						}
						break;
				}
			},1000);
		},
		
		thirdmenuover: function(obj,menu) {
			obj.className = "thirdmenuitemover";
		},
		
		thirdmenuout: function(obj,menu) {
			obj.className = "thirdmenuitem";
		},
		
		buildgrid: function(arr,text,click) {
			var arrRightGrid = [];
			var arrSmallGrid = [];
			for (var a = 0; a < arr.length; a++) {
				var funct1;
				var funct2;
				if (click) {
					funct1 = "onclick='MW.gridclick(this,"+a+");'";
					funct2 = (text === true) ? "onmouseover='MW.gridover(this,"+a+","+a+",true);'" : "onclick='MW.gridclick(this,"+a+");'";
				} else {
					funct1="";
					funct2 = (text === true) ? "onmouseover='MW.gridover(this,"+a+","+a+",false);'" : "";
				}
				arrRightGrid.push("<div class='griditem'>"+
						"<img src='"+arr[a].src+"' class='gridimage' style='left:0px;' "+funct2+" />"+
					"</div>");
				arrSmallGrid.push("<div class='smallgriditem'>"+
						"<img src='"+getSmallImagePath(arr[a].src)+"' class='smallgridimage' style='left:0px;' "+funct1+"  />"+
					"</div>");
			}
			arrRightGrid.push("<div style='clear:both;'></div>")
			arrSmallGrid.push("<div style='clear:both;'></div>")
			document.getElementById("rightgrid").innerHTML = arrRightGrid.join("");
			document.getElementById("rightgridsmall").innerHTML = arrSmallGrid.join("");
		},
		
		gridover: function(obj,index,plus,enableclick) {
			if (enableclick == "undefined") {enableclick = true;}
			if (!MW.gridshadow) {
				MW.gridshadow = document.createElement("div");
				MW.gridshadow.className = "gridshadow";
				MW.gridshadow.onmouseout = function(){
					MW.gridout();
				}
				document.body.appendChild(MW.gridshadow);
			}
			if (enableclick) {
				MW.gridshadow.onclick = function(){
					MW.gridclick(obj,index)
				}
				MW.gridshadow.style.cursor = "pointer";
			} else {
				MW.gridshadow.style.cursor = "default";
			}
			MW.gridshadow.style.top = obj.parentNode.offsetTop+"px";
			MW.gridshadow.style.left = obj.parentNode.offsetLeft+"px";
			var str = "1234567890",
				print = MW.imagearray[index].print,
				last = print.charAt(print.length-1),
				print = str.indexOf(last) != -1 ? print.substring(0,print.length-1) : print;
			MW.gridshadow.innerHTML = "<div class='gridshadowtext'>"+print;
			MW.gridshadow.style.display = "block";
		},
		
		gridout: function() {
			MW.gridshadow.style.display = "none";
		},
		gridclick: function(obj,index) {
			imgobj = MW.imagearray[index];
            if (!imgobj) {
                location.href = location.href.split("?")[0];
            }
			var additionalcolors="<p><a href='javascript:void(0);' onclick='MW.showcolors();'>additional colors</a></p>";
			if (MW.SubMenu !== "Photo Gallery") {
                if (MW.gridshadow) {
                    MW.gridshadow.style.display = "none";    
                }
				
				document.getElementById("detailimage").src = getDetailImagePath(imgobj.src);
			} else {
				additionalcolors = "";
				document.getElementById("detailimage").style.display = "none";
			}
			var print = imgobj.print, file = print.toLowerCase().replace(/ /g,"_");
			document.getElementById("leftmaintext").style.display = "none";
			if (print !== "") {
				var str = "1234567890", last = print.charAt(print.length-1);
				print = str.indexOf(last) != -1 ? print.substring(0,print.length-1) : print;
				document.getElementById("leftgridtext").innerHTML = "<p style='margin:1px 0px;padding:0px;'>"+print+"</p>"+
					"<p style='margin:1px 0px;padding:0px;'>Shown in "+MW.imagearray[index].color+"</p>"+
					"<p style='margin:2px 0px;padding:0px;'><a href='/images/rugs/collections/"+imgobj.collection+"/pdf/"+file+".pdf' target='_blank'>print "+print+"</a></p>"+
					additionalcolors;
			}
			document.getElementById("leftgridtext").style.display = "block";
			document.getElementById("rightgrid").style.display = "none";
			document.getElementById("rightgridimage").src = getBigImagePath(imgobj.src);
			document.getElementById("rightgriddetail").style.display = "block";
		},
		
		print: function(index) {
		},
		
		showcolors: function() {
			var isIE7 = navigator.userAgent.indexOf("MSIE 7") !== -1;
			var isIE6 = navigator.userAgent.indexOf("MSIE 6") !== -1;
			var colorpanel = document.getElementById("colors");
			if (isIE7 || isIE6) {
				var menu = document.getElementById("submenu");
				colorpanel.style.left = (menu.offsetLeft-65)+"px"; 
			}
			colorpanel.style.display = "block";
		}
	};
}();
