var FinalPrice = 0;
var defprice = 0;
var iml = 0;
var icl = 0;
var isz = 0;
var model_change=false;

/**
 * Return flash object by ID "TShirtModel"
 */
function getTShirtModel() {
	return document.getElementById("TShirtModel");
}

/**
 * Call flash methods for change color in target layers
 */
function changeColorInFlash(layer, color, id, select_id) {
	var oListbox = document.getElementById(id);
	if ('- как на макете -' == oListbox.options[0].text)
		oListbox.remove(0);

	var movie = getTShirtModel();
	if (swf_flag) {
		if (hexarr[color] != '0x') {
			movie.changeColorInFlashJS(layer, hexarr[color]);
		} else {
			movie.changeColorInFlashJS(layer, '0x' + deflayercolor[id]);
		}
	}

	layersarr[id] = prarr[color];
	imgid = 'img' + id;
	document.getElementById(imgid).src = picpath + 'colorpic/' + picarr[color];
	// CalcPrice();
	changeDesc(iml, icl, isz);
}


/*
function CalcPrice() {
	a = 0;
	b = defprice;
	for (x in layersarr) {
		if (a < layersarr[x]) {
			a = layersarr[x];
		}
	}

	FinalPrice = (b - 0) + (a - 0);
	changeDesc(iml, icl, isz);
	document.getElementById("priceDIV").innerHTML = FinalPrice;
	document.addtocartform.pricee.value = FinalPrice;
}*/
        function CalcPrice() 
        { return false;
            a = 0;
            b = parseInt(top.model[$('#s  option:selected').val()][2]);
            c = parseInt(top.model[$('#s').val()][5][$('#s2 option:selected').val()][4][$('#s3 option:selected').val()][2]);
            if(!c || c==undefined || isNaN(c))
                c=0;
            for (x in layersarr)
                { 
                    if(layersarr[x]>a)
                        a = layersarr[x];
                }
                //alert('a='+a+' b='+b+' c='+c);
                FinalPrice = (b-0)+(a-0)+(c-0);
	document.getElementById("priceDIV").innerHTML = FinalPrice;
	document.addtocartform.pricee.value = FinalPrice;
        } 

function ClickCheked() {
	if (document.getElementById("tshirtoff").checked) {
		document.getElementById('container').style.visibility = 'hidden';
		document.getElementById('tshirtpic').style.display = 'block';
		document.getElementById('modelpics').style.display = 'block';
		document.getElementById('tshirtpic').style.marginTop = '-15px';
		document.getElementById('modelpics').style.marginTop = '-434px';
	} else {
		document.getElementById('container').style.visibility = 'visible';

		document.getElementById('tshirtpic').style.display = 'none';
		document.getElementById('modelpics').style.display = 'none';
		document.getElementById('modelpics').style.marginTop = '0px';
	}
}

// Выбор моделей
function displayModel() 
    {
            selectModel="<select id=\"s\" name=\"s\" onchange=\"displayColor(this.options[this.selectedIndex].value);\"  style='width: 237px; padding-left: 0px;'>";
            t=0;
           
            for (i=0; i<top.model.length; i++) 
        {
            //if(!sel && def_color>
                
                if (def_model==top.model[i][1]) {
                    sele=" selected=\"selected\"";
                    t=i;
                } 
            else 
            {
                    sele="";
                }
                selectModel+="<option"+sele+" value="+i+">"+top.model[i][0]+"</option>";
            }
             
            selectModel+="</select>";
            document.getElementById("modelDIV").innerHTML=selectModel;
            // document.getElementById("infoPage").innerHTML=infoPage;
          
            displayColor(t);
            
        }

        // Выбор цвета
        function displayColor(ml)
        { 
            iml = ml;
            selectColor="<select id=\"s2\" name=\"s2\" onchange=\"displaySize("+ml+",this.options[this.selectedIndex].value);\" style='width: 60%; padding-left: 0px;'>";
            //selectColor += "<option value=-1>- не выбрано -</option>";
			//Проверка на соответсвие цвета и макета
			var color_index = false;
            for (i=0; i<top.model[ml][5].length; i++)
            {    
                if (def_color==top.model[ml][5][i][1]) color_index =true;
            }			
			if(!color_index) top.def_color=top.model[ml][5][0][1];    
            for (i=0; i<top.model[ml][5].length; i++)
            {    
                if (def_color==top.model[ml][5][i][1]) {
                    sele=" selected=\"selected\"";
                    t=i;
                } 
                else 
                {
                    sele="";
                }
                selectColor+="<option"+sele+" value="+i+">"+top.model[ml][5][i][0]+"</option>";
                //alert (top.model[ml][3][i][3]);
            }
            selectColor+="</select>&nbsp;";

            document.getElementById("colorDIV").innerHTML=selectColor;
            displaySize(ml,t);
        }

        // Выбор размера
        function displaySize(ml,cl)
        {
              icl = cl;
            selectSize="<select  style='padding-left: 0px;' name=s3 id=s3 onchange=\"changeDesc("+ml+","+cl+",this.options[this.selectedIndex].value);\">";
            for (i=0; i<top.model[ml][5][cl][4].length; i++)
            { 
            if(top.model[ml][5][cl][4][i]) 
                selectSize+="<option value="+i+">"+top.model[ml][5][cl][4][i][0]+"</option>";
            }
            selectSize+="</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            top.def_color=top.model[ml][5][cl][1];            
            document.getElementById("sizeDIV").innerHTML=selectSize;
            changeDesc(ml,cl,0);
            
            CalcPrice();
            changeDesc(iml,icl,isz);
        }

function displayColorpicImg(cp) {
	document.addtocartform.colorpic.value = top.colorpic[cp][0];
	cpprice = top.colorpic[cp][3];
	changeColor(top.colorpic[cp][4]);
	if (top.colorpic[cp][2] != "no") {
		if (document.getElementById("colorpicimgDIV"))
			document.getElementById("colorpicimgDIV").innerHTML = "<img border=\"1\" width=\"30\" height=\"18\" src=\""
					+ picpath
					+ "colorpic/"
					+ top.colorpic[cp][2]
					+ "\" style='border: 1px solid #fff'>";
	} else {
		if (document.getElementById("colorpicimgDIV"))
			document.getElementById("colorpicimgDIV").innerHTML = "";
	}
}

function displayColorpic() {
	selectColorpic = "<select id=\"s4\" name=\"s4\" onchange=\"displayColorpicImg(this.options[this.selectedIndex].value);\" style='width: 250px'>";
	tt = 0;
	for (i = 0; i < top.colorpic.length; i++) {
		// alert(top.colorpic[i][4]);
		if (sel == top.colorpic[i][1]) {
			sele1 = " selected=\"selected\"";
			tt = i;
		} else {
			sele1 = "";
		}

		if (tt == 0) {
			if (top.colorpic[i][4] == '0x') {
				sele1 = " selected=\"selected\"";
				tt = i;
			}
		}

		if (top.colorpic[i][3] != 0) {
			upprice = " (+ " + top.colorpic[i][3] + " руб.)";
		} else {
			upprice = "";
		}
		selectColorpic += "<option" + sele1 + " value=" + i + ">"
				+ top.colorpic[i][1] + upprice + "</option>";
	}
	selectColorpic += "</select>";

	if (document.getElementById("colorpicDIV"))
		document.getElementById("colorpicDIV").innerHTML = selectColorpic;

	displayColorpicImg(tt);
}

function getMovie() 
    {
        if($.browser.opera){
            if (window.document["maika1"]) 
              {
                  return window.document["maika1"];
              }
              else if (document.embeds && document.embeds["maika1"]){
                  return document.embeds["maika1"]; 
              }
              else if (document.getElementById("maika1"))
              {
                return document.getElementById("maika1");
              }
        }
        else if($.browser.msie)
        {
            
              if (window.document["maika1"]) 
              {
                  return window.document["maika1"];
              }
              if (navigator.appName.indexOf("Microsoft Internet")==-1)
              {
                if (document.embeds && document.embeds["maika1"])
                  return document.embeds["maika1"]; 
              }
              else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
              {
                return document.getElementById("maika1");
              }
            //return document.embeds.maika1;
        }
        else if($.browser.mozilla)
        {
             M$ =  navigator.appName.indexOf("Microsoft")!=-1;
            return (M$ ? window : document)["maika1"];
        }
        else if($.browser.safari)
        {
            if (window.document["maika1"]) 
              {
                  return window.document["maika1"];
              }
              else if (document.embeds && document.embeds["maika1"]){
                  return document.embeds["maika1"]; 
              }
              else if (document.getElementById("maika1"))
              {
                return document.getElementById("maika1");
              }
        }
        else
        {
        
           if (window.document["maika1"]) 
              {
                  return window.document["maika1"];
              }
              else if (document.embeds && document.embeds["maika1"]){
                  return document.embeds["maika1"]; 
              }
              else if (document.getElementById("maika1"))
              {
                return document.getElementById("maika1");
              }
        }
            //return document.["maika1"]
            //return $(\'#maika1\').get(0);
        }

function changeColor(value) {
 
 
 

}

function changeModel(value) {
	if (value !== '') {
 $('#cords').css('backgroundImage', 'url('+value+')');   	
	    /*
		//movie = getMovie();
		// alert(movie);
		if (typeof (movie) == 'object') {
			movie.SetVariable("wmodel", value);
		}
		if (typeof (movie) == 'function') {
			movie.SetVariable("wmodel", value);
		}
		*/
	}
}

 

// Смена описания
function changeDesc(ml, cl, sz) { // alert(ml+","+cl+","+sz);
	isz = sz;
	infoPages = 'Модель:';
	i = 0;
	str = '';
	for (x in layersarr) {
		text = document.getElementById('SelectforLayer' + i).options[document
				.getElementById('SelectforLayer' + i).selectedIndex].text;
		if (text.indexOf("+") != -1) {
			text = text.substring(0, text.indexOf("+"));
		}
		str += ' ' + l_arr[i] + ': ' + text + ';';
		i++;
	}
	document.getElementById("descDIV").innerHTML = model[ml][0] + ", цвет "
			+ model[ml][5][cl][0] + ", размер " + model[ml][5][cl][4][sz][0]
			+ ";" + str;
	moprice = model[ml][2];
	cpprice = model[ml][5][cl][4][sz][2];
	changePrice(moprice, cpprice);
	a = 0;
	b = defprice;
	for (x in layersarr) {
		if (a < layersarr[x])
			a = layersarr[x];
	}

	FinalPrice = (b - 0) + (a - 0);
	document.getElementById("priceDIV").innerHTML = FinalPrice;
	document.addtocartform.pricee.value = FinalPrice;
	if (top.model[ml][5][cl][2] != "") {
		document.getElementById("colormodelimgDIV").innerHTML = "<img border=\"1\" width=\"30\" height=\"18\" src=\""
				+ picpath + "color/" + top.model[ml][5][cl][2] + "\">";
	} else {
		document.getElementById("colormodelimgDIV").innerHTML = "";
	}

	document.getElementById("modelpics").innerHTML = "<a href=\"javascript:open_windowf(\'"
			+ picpath
			+ "model/"
			+ top.modelpic[ml][1][0]
			+ "\',"
			+ ((top.modelpic[ml][1][2]) - 0 + 40)
			+ ","
			+ ((top.modelpic[ml][1][3]) - 0 + 40)
			+ ");\"><img style=\"margin: 0 0 5px\" border=\"0\" "
			+ top.modelpic[ml][1][1]
			+ " src=\""
			+ picpath
			+ "model/th_"
			+ top.modelpic[ml][1][0]
			+ "\"></a><br><a href=\"javascript:open_windowf(\'"
			+ picpath
			+ "model/"
			+ top.modelpic[ml][1][0]
			+ "\',"
			+ ((top.modelpic[ml][1][2]) - 0 + 40)
			+ ","
			+ ((top.modelpic[ml][1][3]) - 0 + 40)
			+ ");\">Увеличить</a><br><br>";
	document.addtocartform.model.value = top.model[ml][1];
	document.addtocartform.color.value = top.model[ml][5][cl][1];
	document.addtocartform.size.value = top.model[ml][5][cl][4][sz][1];
	if (top.model[ml][4] != '') {
		infoPages = "<b>Модель:</b>&nbsp;&nbsp;";
	}
    /*
	if (top.model[ml][4] != '') {
		infoPages = "<b>Модель:</b>&nbsp;&nbsp;<a href=\"javascript:open_windowf('"
				+ top.model[ml][4] + "',419,347);\">Фотографии фасона</a>";
	}	
	*/
	document.getElementById("infoPage").innerHTML = infoPages;
	if (top.model[ml][3] != '') {
		if (top.model[ml][5][cl][3] != '') {
			displayColorpic();
			mod_pic_wcolor = picpath+'/model/' + top.model[ml][3] + '/'
					+ top.model[ml][5][cl][3] + '_big.png';// gif
			// if (navigator.appName !=='Opera') {
			changeModel(mod_pic_wcolor);
		}
	}
}

function changePrice(mll, cpp) {
	totalpr = (mll - 0) + (cpp - 0);
	document.getElementById("priceDIV").innerHTML = "<b>Цена: " + totalpr
			+ " руб.</b>";
	document.addtocartform.pricee.value = totalpr;
	defprice = totalpr;
}

function open_windowf(link,w,h) //opens new window
{
    var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
    newWin = window.open("http://"+location.host+PRE_PATH+"/popup.php?target="+link,'newWin',win);
    newWin.focus();
}
// Редирект   на нужную модель с нужным цветом
function Model_show(model,color,url)
{
location.replace(url+'?model='+model+'&color='+color);
}
