﻿var tmpColor
var picOffBorderColor = "#FFFFFF" 
var picOnBorderColor = "#FFFFFF";//"#FFAF24"
var loadingImagePath = '../images/css/loading.gif'
var loadingImg = new Image();
loadingImg.src = loadingImagePath; //
var g_bHaveSelectColor=false;
var g_strCurrentSelectImageColorID;

function formatSmallPic(PicDivID, BigPicDivID, BigPicBasePath,strItemID,bMockup){
    var bigImg = document.getElementById(BigPicDivID).getElementsByTagName("img")[0];
	var PicDivObj = document.getElementById(PicDivID);
	if(PicDivObj==null)
	{
	    return;
	}
	var ImgObjs = PicDivObj.getElementsByTagName("IMG")		
	//set smallPic event fucntion
	for (var i = 0; i < ImgObjs.length; i++){		
		if (i == 0){
			//bigImg.src = ImgObjs[i].getAttribute("bigPic");//modify
			//ImgObjs[i].style.borderColor = picOnBorderColor;
		}
		ImgObjs[i].onmouseover = function(){
			tmpColor = this.style.borderColor;
			this.style.borderColor = picOnBorderColor;
		}
		ImgObjs[i].onmouseout = function(){
			this.style.borderColor = tmpColor;
		}
		ImgObjs[i].onclick = function(){
			setCurPic(ImgObjs, this);			
			ChgBigPic(BigPicDivID, BigPicBasePath + this.getAttribute("bigPic"),strItemID,this.title,bMockup);
		}
		ImgObjs[i].onerror = function() {
			this.parentNode.parentNode.style.display = "none";
			//setEfectBigPic(bigImg, PicDivObj, BigPicBasePath);//modify
		}
	}	
}
function setEfectBigPic(bigImg, PicDivObj, BigPicBasePath){
	var ImgObjs = PicDivObj.getElementsByTagName("IMG")	
	for(var i = 0; i < ImgObjs.length; i++){
		if(ImgObjs[i].parentNode.parentNode.style.display != "none"){
			setCurPic(ImgObjs, ImgObjs[i]);	
			bigImg.src = BigPicBasePath + ImgObjs[i].getAttribute("bigPic");
			break;
		}
	}
}
function setCurPic(ImgObjs, ImgObj){
	for(var i = 0; i < ImgObjs.length; i++){
		ImgObjs[i].style.borderColor = picOffBorderColor;		
	}
	tmpColor = picOnBorderColor;
	ImgObj.style.borderColor = picOnBorderColor;
}
function ChgBigPic(BigPicDivID, BigPicPath,strItemID,strTitle,bMockup){
    var img = new Image();	
	var DivObj = document.getElementById(BigPicDivID);
	DivObj.innerHTML = '';
	DivObj.style.height = '300px';	
	DivObj.style.background = 'url(images/css/loading.gif) no-repeat center center'	

	var strFrontPic = BigPicPath.toLowerCase().replace(".jpg",".png");
	var arrayData=strFrontPic.split("/");
	g_strCurrentSelectImageColorID=arrayData[arrayData.length-1].split("_")[1].split(".")[0];
	
    var strBackPic = BigPicPath.toLowerCase().replace(".jpg","_b.png");	    
    var strDetailPic = BigPicPath.toLowerCase().replace(".jpg","_d.png");
		    
    SetAsaiCookie("onclickpicid",BigPicPath);
    SetAsaiCookie("onclickitemid",strItemID);
    SetAsaiCookie("onclickpiccolor",strTitle);
    
	var objMainViewColourName = document.getElementById("MainViewColourName");
	if(objMainViewColourName!=null)
	{
	    objMainViewColourName.innerHTML=strTitle;
	}
	if(document.getElementById("SelectOtherSideButton")!=null)
	{
	    var strSelectImgSrc = BigPicPath.toLowerCase().replace("colourfore","ColourSmall");
	    document.getElementById("SelectOtherSideButton").src=strSelectImgSrc;
	    var arrayFileName=strSelectImgSrc.split(".");
	    document.getElementById("SelectOtherSideButton_B").src=arrayFileName[0]+"_b."+arrayFileName[1];
    	
	    document.getElementById("OtherSideThumb").style.display="";	
	}
	
	
    g_bHaveSelectColor=true;
    if(bMockup=="1")
    {
        $.fancybox.close();
        document.getElementById("MockupStockBt").click();    
    }
    if(document.getElementById("MockupStockBt_fk")!=null)
    {
        document.getElementById("MockupStockBt_fk").style.display="none";
        document.getElementById("MockupStockBt").style.display="";
    }
    /*
    if(document.getElementById("MockupStockBt")!=null)
        document.getElementById("MockupStockBt").href= "http://www.newwaves.com.au/mu/SMockUp.aspx?duid=72C76845-9EB6-30D4-0C50-E35E0EBE0F29$"+document.getElementById("MockupStockBt").name+"$http://www.newwaves.com.au/"+ strFrontPic +"$http://www.newwaves.com.au/"+strBackPic+"$"; //Mocking Up
	*/	    
	img.onload = function(){
		DivObj.style.background = '';
		DivObj.style.height='';
		//DivObj.innerHTML = '<img src="' + this.src + '" />'	; //original
		
		////---modify begin-----		
		DivObj.innerHTML = "<img id=\"productPic\" onerror=\"this.src='images/space.gif'\"  src=\"" + this.src + "\" />"
		if(!!document.getElementById("linkPicBack")){
		    document.getElementById("linkDownProductPic").href=BigPicPath;
		    var backImgSrc = BigPicPath.toLowerCase().replace("colourfore","colourback");
		    document.getElementById("linkPicBack").href="javascript:swapPic('productPic','"+BigPicPath+"','"+backImgSrc+"')";		    
		    
		    cachBackImg(backImgSrc)//function at detail Page
		}
		fixMinHeight('righter','leftNav', 450)//function in main.js
		////---modify end----
	}
	img.onerror = function(){
		DivObj.style.background = '';
		DivObj.innerHTML = 'sorry! Load error.'
	}
	img.src = BigPicPath;
}
function ChangeSideTumb(BigPicPath,strType){
    var img = new Image();	
	var DivObj = document.getElementById("divProductPic");
	DivObj.innerHTML = '';
	DivObj.style.height = '300px';	
	DivObj.style.background = 'url(images/css/loading.gif) no-repeat center center';
	
	BigPicPath+="_"+g_strCurrentSelectImageColorID+strType+".jpg";
	
	img.onload = function(){
		DivObj.style.background = '';
		DivObj.style.height='';
		//DivObj.innerHTML = '<img src="' + this.src + '" />'	; //original
		
		////---modify begin-----		
		DivObj.innerHTML = "<img id=\"productPic\" onerror=\"this.src='images/space.gif'\"  src=\"" + this.src + "\" />"
		fixMinHeight('righter','leftNav', 450)//function in main.js
		////---modify end----
	}
	img.onerror = function(){
		DivObj.style.background = '';
		DivObj.innerHTML = 'sorry! Load error.'
	}
	img.src = BigPicPath;
}
function SetAsaiCookie(name,value)
{
var Days = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+"; path=/";
}