var loopObject = {start:0, end:10, current:0, interval:null};

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function ColorCell(ColorType,id_el,BaseColor, Side, NumberOfColor, R, G, B, w, h) {
	color='#'+Dec2Hex((R << 16) + (G << 8) + B);
	temp='<td bgcolor="'+color+'" ';
	t_id_el='"'+id_el+'"';
    if (ColorType==1 || ColorType==3){
    	temp+='onmouseover="document.getElementById(\''+id_el+'\').style.background=(\''+color+'\')"  ';
        temp+='onmouseout="document.getElementById(\''+id_el+'\').style.background=(\''+BaseColor+'\')"  ';
    } else{
     	temp+='onmouseover="document.getElementById(\''+id_el+'\').style.color=(\''+color+'\')"  ';
	    temp+='onmouseout="document.getElementById(\''+id_el+'\').style.color=(\''+BaseColor+'\')"  ';
    }
	if (ColorType==1){
		temp+='onclick="f_change_color('+Side+','+NumberOfColor+',\''+color+'\');redraw_prop('+Side+');" ';
		}else{
			if (ColorType==2){
			temp+='onclick="change_color('+NumberOfColor+',\''+color+'\')" ';
			} else{
			 temp+='onclick="select_colorb('+NumberOfColor+',\''+color+'\')" ';
			}
		}
	temp+='width='+w+' height='+h+'></td>';
	return temp;
}

function Colors(ColorType, id_el,BaseColor,Side, NumberOfColor) {
	var s = '<table cellspacing=0 border=0>';
	var c = 0,n = 1;
	var r,g,b;
	for (j = 0; j < 15; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i++){
				if(j<12){
				s += ColorCell(ColorType,id_el,BaseColor, Side, NumberOfColor, 255-(Math.floor(j / 2) * 51), 255-(k * 51 + (j % 2) * 51 * 3),255-(i * 51), 5, 8);
				}else{
					if(n<=14){
						r = 255-(n * 17);
						g=b=0;
					}else if(n>14 && n<=28){
						g = 255-((n-14) * 17);
						r=b=0;
					}else if(n>28 && n<=42){
						b = 255-((n-28) * 17);
						r=g=0;
					}else{
						r=g=b=255-((n-42) * 17);
					}
					s += ColorCell(ColorType,id_el, BaseColor, Side, NumberOfColor, r, g,b, 5, 8);
					n++;
				}
			}
		s += "</tr>";
	}
s += "</table>";
return s;
}

function Dec2Hex(v) {
	v = v.toString(16);
	for(; v.length < 6; v = '0' + v);
	return v;
}

function cover(level,template_type,template){
  document.form2.level.value=level;
  document.form2.template_type.value=template_type;
  document.form2.template.value=template;
  document.form2.submit();
}

function Selection(level,template_type){
  document.form2.level.value=level;
  document.form2.template_type.value=template_type;
  document.form2.BackToMyCover.value=1;
  document.form2.submit();
}

function f_change_color($side,$number,$color){
 temp=document.getElementById("template"+$side);
 ParamValue="url(template"+$side+".php?x="+Math.round(1000*Math.random())+"&number="+$number+"&color=";
 ParamValue=ParamValue+escape($color)+"&change_side_color=1&side="+$side+")";
 temp.style.backgroundImage=ParamValue;
}

function f_change_gradient($side,$number){
 redraw_prop($side);
 temp=document.getElementById("template"+$side);
 ParamValue="url(template"+$side+".php?x="+Math.round(1000*Math.random())+"&number="+$number;
 ParamValue=ParamValue+"&side="+$side+"&change_gradient=1)";
 temp.style.backgroundImage=ParamValue;
}

function removenewtext(){
   var temp=document.getElementById("newtextWrap");
   if (temp) {
    temp.parentNode.removeChild(temp);
  }
}
//удаляет все тексты и всплывающие к ним редактирование свойств
function removeall() {
 for(i=0; i<=99; i++){
   var temp=document.getElementById("t"+i+"Wrap");
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("submenu"+i);
   if (temp) {
    temp.parentNode.removeChild(temp);
    }

   var temp=document.getElementById("n"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("tt"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("e"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("c"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }

 }
 removenewtext();
}

//удаляет конкретный текст и сопутствущие элементы

function removeitem(i) {
   var temp=document.getElementById("textare"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
//        alert("textare"+i+"deleted");
        }
   var temp=document.getElementById("t"+i+"Wrap");
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("submenu"+i);
   if (temp) {
    temp.parentNode.removeChild(temp);
    }
   var temp=document.getElementById("n"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("tt"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("e"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("c"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("button"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("face"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("size"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
   var temp=document.getElementById("t"+i);
   if (temp) {
        temp.parentNode.removeChild(temp);
        }
}

//рисует все тексты
function  draw_divs(CoverSide){
   JsHttpRequest.query(  'draw_divs.php', // backend
	                    {'CoverSide': CoverSide, 'x': Math.round(1000*Math.random())},
                    	// Function is called when an answer arrives.
                    	function(result, errors){
							eval(result);
						}
	);
}

//удаляет текст

// удаляет все тексты
//перерисовывает картинку
//рисует новые тексты
function delete_text(CoverSide,item) {
   removeall();
   temp=document.getElementById("template"+CoverSide);
   ParamValue="url(template"+CoverSide+".php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&delete_text=1&CoverSide="+CoverSide+"&text_id="+item+")";
   temp.style.backgroundImage=ParamValue;
   setTimeout('draw_divs('+CoverSide+')',2000);
   setTimeout('redraw_prop('+CoverSide+')',2000);
}

function add_new_text(CoverSide){
   removeall();

   temp=document.getElementById("template"+CoverSide);
   ParamValue="url(template"+CoverSide+".php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&addnew="+CoverSide+")";
   temp.style.backgroundImage=ParamValue;
   setTimeout('draw_divs('+CoverSide+')',2000);
   setTimeout('redraw_prop('+CoverSide+')'  ,2000);
}

function position(CoverSide,position,text){

  removeall();

   temp=document.getElementById("template"+CoverSide);
   ParamValue="url(template"+CoverSide+".php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&CoverSide="+CoverSide+"&change_position=1&position="+position+"&text="+text+")";
   temp.style.backgroundImage=ParamValue;

  draw_divs(CoverSide);
}

function change_face(el,id){
   temp1=document.getElementById(el).value;
   temp=document.getElementById(el);
   JsHttpRequest.query('change_face.php', // backend
                    {'Face': temp1,'NumText': id,
                     'x': Math.round(1000*Math.random())},
                	function(result, errors){
                      removeitem(id);
                      eval(result);
                    }
    );
   temp.style.display="none";
}

function change_size(el,id){
   temp1=document.getElementById(el).value;
   temp=document.getElementById(el);
   JsHttpRequest.query('change_size.php', // backend
                    {'Size': temp1,'NumText': id,
                     'x': Math.round(1000*Math.random())},
                	function(result, errors){
                        removeitem(id);
                        eval(result);
                    }
    );
   temp.style.display="none";
}

function change_faceb(num){
   temp1=document.getElementById("faceb"+num).value;
   temp2=document.getElementById("faceb"+num);
   temp=document.getElementById("template3");
   ParamValue="url(template3.php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&change_faceb"+num+"=1&face="+temp1+")";
   temp.style.backgroundImage=ParamValue;
   temp2.style.display="none";
   redrawb(num);
}

function change_color(id,color){

   temp=document.getElementById('c'+id);
   JsHttpRequest.query('change_color.php', // backend
                    {'Color': color,'NumText': id,
                     'x': Math.round(1000*Math.random())},
                	function(result, errors){
                      removeitem(id);
                      eval(result);
                    }
    );
   temp.style.display="none";
}

function select_colorb(num,color) {
   temp=document.getElementById("template3");
   ParamValue="url(template3.php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&change_colorb"+num+"=1&color="+escape(color)+")";
   temp.style.backgroundImage=ParamValue;
   redrawb(num);
}
function change_text(id) {
   temp=document.getElementById('textare'+id);
   temp2=document.getElementById('button'+id);
   JsHttpRequest.query('change_text.php', // backend
                    {'Text': temp.value,'NumText': id,
                     'x': Math.round(1000*Math.random())},
                	function(result, errors){
                      removeitem(id);
                      eval(result);
                    }
    );
   temp.style.display="none";
   temp2.style.display="none";
}



// меняет боковой текст
function change_textb(num){
   temp1=document.getElementById("textarb"+num).value;
   temp=document.getElementById("template3");
   ParamValue="url(template3.php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&change_textb"+num+"=1&text="+escape(temp1)+")";
   temp.style.backgroundImage=ParamValue;
   setTimeout('redrawb(1);',800);
   setTimeout('redrawb(2);',800);
}

// меняет боковые тексты
function redrawb(num){
    var temp=document.getElementById("btable"+num);
   if (temp) { temp.parentNode.removeChild(temp);}
    var temp=document.getElementById("btt"+num);
   if (temp) { temp.parentNode.removeChild(temp);}
    var temp=document.getElementById("textarb"+num);
   if (temp) { temp.parentNode.removeChild(temp);}
    var temp=document.getElementById("be"+num);
   if (temp) { temp.parentNode.removeChild(temp);}
    var temp=document.getElementById("bc"+num);
   if (temp) { temp.parentNode.removeChild(temp);}
    var temp=document.getElementById("faceb"+num);
   if (temp) { temp.parentNode.removeChild(temp);}
   JsHttpRequest.query(  'draw_divs.php', // backend
	                    {'b': num, 'x': Math.round(1000*Math.random())},
                    	function(result, errors){
							eval(result);
						}
	);
}

// удаляет боковой текст
function delete_textb(num){
   temp1="        ";
   var temp=document.getElementById("textarb"+num);
   temp.value="          ";
   temp=document.getElementById("template3");
   ParamValue="url(template3.php?x="+Math.round(1000*Math.random());
   ParamValue=ParamValue+"&change_textb"+num+"=1&text="+escape(temp1)+")";
   temp.style.backgroundImage=ParamValue;
}

// меняет свойства картинки
function redraw_prop(num){
   var temp2=document.getElementById("sub"+num);
   if (temp2) temp2.parentNode.removeChild(temp2);
   var temp2=document.getElementById("png"+num);
   if (temp2) temp2.parentNode.removeChild(temp2);
   JsHttpRequest.query(  'draw_propetries.php', // backend
	                    {'CoverSide': num, 'x': Math.round(1000*Math.random())},
                    	function(result, errors){
							eval(result);
						}
	);
}


function centerOnWindow(elemID) {
    // 'obj' is the positionable object
    var Zobj = ZgetRawObject(elemID);
    // window scroll factors
    var scrollX = 0, scrollY = 0;
    if (document.body && typeof document.body.scrollTop != "undefined") {
        scrollX += document.body.scrollLeft;
        scrollY += document.body.scrollTop;
        if (document.body.parentNode &&
            typeof document.body.parentNode.scrollTop != "undefined") {
            scrollX += document.body.parentNode.scrollLeft;
            scrollY += document.body.parentNode.scrollTop
        }
    } else if (typeof window.pageXOffset != "undefined") {
        scrollX += window.pageXOffset;
        scrollY += window.pageYOffset;
    }
    var x = Math.round((ZgetInsideWindowWidth()/2) - (ZgetObjectWidth(Zobj)/2)) + scrollX;
    var y = Math.round((ZgetInsideWindowHeight()/2) -  (ZgetObjectHeight(Zobj)/2)) + scrollY;
    ZshiftTo(Zobj, x, y);
    Zshow(Zobj);
}

function initProgressBar() {
	// create quirks object whose default (CSS-compatible) values
    // are zero; pertinent values for quirks mode filled in later
	if (navigator.appName == "Microsoft Internet Explorer" &&
        navigator.userAgent.indexOf("Win") != -1 &&
        (typeof document.compatMode == "undefined" ||
        document.compatMode == "BackCompat")) {
        document.getElementById("progressBar").style.height = "81px";
        document.getElementById("progressBar").style.width = "444px";
        document.getElementById("sliderWrapper").style.fontSize = "xx-small";
        document.getElementById("slider").style.fontSize = "xx-small";
        document.getElementById("slider").style.height = "13px";
        document.getElementById("slider").style.width = "415px";
    }
}

function showProgressBar() {
    centerOnWindow("progressBar");
}

function calcProgress(current, total) {
    if (current <= total) {
        var factor = current/total;
        var pct = Math.ceil(factor * 100);
        document.getElementById("sliderWrapper").firstChild.nodeValue = pct + "%";
        document.getElementById("slider").firstChild.nodeValue = pct + "%";
        document.getElementById("slider").style.clip = "rect(0px " + parseInt(factor * 417) + "px 16px 0px)";
    }
}

function hideProgressBar() {
    Zhide("progressBar");
    calcProgress(0, 0);
}
function phase1(){
    Draw_Divs();
    calcProgress(2, 7);
    setTimeout('phase2();',0);
}

function phase2(){
    Draw_Divb1();
    calcProgress(3, 7);
    setTimeout('phase3();',0);
}

function phase3(){
    Draw_Divb2();
    calcProgress(4, 7);
    setTimeout('phase4();',0);
}

function phase4(){
    EditCover1();
    calcProgress(5, 7);
    setTimeout('phase5();',0);
}

function phase5(){
    EditCover2();
    calcProgress(6, 7);
    setTimeout('phase6();',0);
}

function phase6(){
    EditCover3();
    calcProgress(7, 7);
    setTimeout('phase7();',0);
}

function phase7(){
  hideProgressBar();
}

function runit() {
    calcProgress(1,7);
    setTimeout('phase1();',0);
 }

function Ztest() {
    showProgressBar();
    runit();
}

function makeclone(num){
   JsHttpRequest.query(  'makeclone.php', // backend
	                    {'num': num, 'x': Math.round(1000*Math.random())},
                    	function(result, errors){
							eval(result);
						}
	);
}
function BackToMyCover(){
  document.form2.level.value=1;
  document.form2.BackToMyCover.value=1;
  document.form2.submit();
}

function reload(){
  document.location.reload(true);
}

function myDate(){
var d=new Date();
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
  return month[d.getMonth()]+" "+d.getDay()+", "+d.getFullYear();
}

function ShowMainTemplates(){
  document.form2.show_main_templates.value=1;
  document.form2.level.value=0;
  document.form2.submit();
}

function feedback(){
window.open("feedback.php?url="+window.location.href, "feedback" , "height=450, width=350", "location=No,directories=No,status=No,menubar=No,scrollbars=No,toolbar=No")
}
function thank(){
 alert("Thank you for sending us your feedback.");
 window.close();
}

