var decals = new Array();

function ch_changeTorso(newImage) { parent.document.torso.src = newImage; }
function ch_changeHead(newImage) { parent.document.head.src = newImage; }
function ch_changeLegs(newImage) { parent.document.legs.src = newImage; }
function ch_changeArms(newImage) { parent.document.arms.src = newImage; }
function ch_changeEpaulette(newImage) { parent.document.epaulette.src = newImage;}

function ch_changeTorso2(newImage,sX,sY) { 
	parent.document.torso.src = "./image-part.php?img="+newImage+"&x=0&y="+sX+"&w=0&h="+sY+"&tw=89&th=76"; 
	parent.document.torso.style.width =  (89) + "px";
	parent.document.torso.style.height = (74) + "px"; 
}
function ch_changeTorsoB(newImage) {
	parent.document.torsoback.src = newImage;
	a= newImage.replace("torso","Ttorso");
	a= a.replace(".jpg",".gif");
	parent.document.arms.src = a;
	}
	
function changeTorso(newImage) {
document.torso.src = newImage;
}

function changeTorso2(newImage,sX,sY) {
document.torso.src = newImage;
//resetDecalMargins();
decalBigger2(sx,sy);
}

function changeHead(newImage) {
document.head.src = newImage;
}
function changeLegs(newImage) {
document.legs.src = newImage;
}
function changeTorsoB111(newImage) {
document.torsoback.src = newImage;
}
function changeTorsoB(newImage) {
parent.document.torsoback.src = newImage;
a= newImage.replace("torso","Ttorso");
a= a.replace(".jpg",".gif");
parent.document.arms.src = a;
}

function changeEpaulette(newImage) {
document.epaulette.src = newImage;
}

var windowNumber=1;
function loadTorso() {
var torso = document.torso.src;
var wn = window.open('','_blank', "width=300,height=300");
wn.document.open();
wn.document.writeln('<html><head></head><body>'); 
wn.document.writeln('<center><img src="' + torso + '" /></center>');
wn.document.writeln("</body></html>");
wn.document.close();
}

function createMinifig() {
var torso = document.torso.src;
var head = document.head.src;
var legs = document.legs.src;
var torsoback = document.torsoback.src;
var epaulette  = document.epaulette.src;
var arms  = document.arms.src;
var torsoML = parseInt(document.torso.style.marginLeft); 
var torsoB = parseInt(document.torso.style.marginBottom); 
var torsoW = parseInt(document.torso.style.width);
var torsoH = parseInt(document.torso.style.height);

if(isNaN(torsoB)) torsoB = 0;

var wn = window.open('','_blank',"width=300,height=450,menubar=yes");
wn.document.open();
wn.document.writeln('<html><head><meta content="application/binary"></head><body>'); 
wn.document.writeln('<h1>Minifig picture</h1>');
wn.document.writeln('<small>2007-12-27<br>The new vesion generates a complete png picture of the designed minifig. Currently the feature has beta version character. For downloading it, simply save the picture below <nd resize it to your wishes (maybe to create an avatar ....</small><br><br>');
wn.document.writeln('<img src="./minifig-create.php?head='+encodeURIComponent(head)+'&torso='+arms+'&torso2='+encodeURIComponent(torso)+'&legs='+legs+'&epa='+epaulette+'&tdx='+torsoML+'&tdy='+torsoB+'&tdw='+torsoW+'&tdh='+torsoH+'">');

wn.document.writeln("</body></html>");
wn.document.close();

}

function loadPrivateLeft() {
parent.iframeL.document.open();
parent.iframeL.document.close();
parent.iframeL.document.location.href = "" + document.form123.userin.value;
}

function loadPrivateRight() {
parent.iframeR.document.open();
parent.iframeR.document.close();
parent.iframeR.document.location.href = "" + document.form123.userin.value;
}

function resetLeftFrame() {
parent.iframeL.document.open();
parent.iframeL.document.close();
parent.iframeL.document.location.href = "main.html";
}

function includeJS(filename) {
includeJSRaw(filename); 
setTimeout("showDecalJS()",500);
}

function includeJSRaw(filename)
{
var body = document.getElementsByTagName('body').item(0);
script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';

body.appendChild(script);
}

var ds = 1.0;
function scaleFactor() {

    if(ds == 1.0) { ds=2.0; document.scale.style.width = "20px"; }
    else if(ds == 2.0) { ds=0.5; document.scale.style.width = "12px";}
    else { ds = 1.0; document.scale.style.width = "16px";}
}

function moveDecal(dx,dy) {
 
var lm = parseInt(document.torso.style.marginLeft);
var tm = parseInt(document.torso.style.marginBottom);

document.torso.style.marginLeft =   (lm + (dx*ds)) + "px";
document.torso.style.marginBottom =   (tm + (dy*ds)) + "px";

} 

function resetDecalMargins() {
document.torso.style.marginLeft = "0px";
document.torso.style.marginBottom  = "0px";
document.torso.style.width = "89px";
document.torso.style.height = "74px";
}

function decalBiggerX() { decalBigger2(1.05,1); }
function decalBiggerY() { decalBigger2(1,1.05); }
function decalSmallerX() { decalBigger2(0.95,1); }
function decalSmallerY() { decalBigger2(1,0.95); }

function decalBigger2(sx,sy) {
w = document.torso.width;
w = w*sx;
document.torso.style.width = w + "px";
h = document.torso.height * sy;
document.torso.style.height = h + "px";
}

var small=0;
function decalSmaller() {
small -=10;
document.torso.style.margin = small + "px";
}

function loadDecalJS() {
//includeJS(document.form123.userin.value);
collectImages();
}

function showDecalJS() {
collectImages2(document);
}


function showDecalLeft() {
collectImages2(parent.iframeL.document);
}


function resetLeftFrameX() {
collectImages2();
}

function loadExternalJS() {
includeJSRaw(document.form123.userin.value);
setTimeout("showDecalLeft()",500);
}

function loadImageT(img) {
document.torso.src = img;
}
function loadImage() {
document.torso.src = "./save-image.php?img=" + encodeURIComponent(document.form123.userin.value);
setTimeout("loadImageT(\""+ document.form123.userin.value + "\")", 300);
//document.torso.src = "" + document.form123.userin.value;
}
function loadImageHead() {
document.head.src = "./head-tool.php?head=" + encodeURIComponent(document.head.src) + "&face=" + encodeURIComponent(document.form123.userin.value) + "&dx=-0.5&sx=1";
}

function insertImageL() {
document.torso.src = parent.iframeL.document.location.href;
}

function insertImageR() {
document.torso.src = parent.iframeR.document.location.href;
}

function loadImagesFromUrl(a) {
parent.iframeL.document.location.href = "./extract-img.php?url="  + encodeURIComponent(a);
//document.iframeL.document.location.href = "./extract-img.php?url=" +  document.form123.userin.value;
}

function paste() {
    //document.form123.text.focus();
    document.form123.userin.value = document.getSelection();
}

function clearAddress() {
document.form123.userin.value = "" ;
}

function collectImages2(element){
    var iText = "None";
    var scaleX = 0.0;
    var scaleY = 0.0;
    var width  = 96;
    var part   = "torso";
    if(parent.iframeL.document.images) {

        iText = '<script>function changeTorso(newImage) { parent.document.torso.src = newImage; }';
        iText += 'function changeHead(newImage) { parent.document.head.src = newImage; }';
        iText += 'function changeLegs(newImage) { parent.document.legs.src = newImage; }';
        iText += 'function changeArms(newImage) { a= newImage.replace("torso","Ttorso"); a= a.replace(".jpg",".gif"); parent.document.arms.src = a; }'

		iText += 'function changeEpaulette(newImage) { parent.document.epaulette.src = newImage;}';

iText += 'function changeTorso2(newImage,sX,sY) { \
parent.document.torso.src = "./image-part.php?img="+newImage+"&x=0&y="+sX+"&w=0&h="+sY+"&tw=89&th=76"; \
parent.document.torso.style.width =  (89) + "px"; \
parent.document.torso.style.height = (74) + "px"; } \
</script>';

        iText += '<body style =" background-image:url(./images/temp/contentbackground.jpg); ">';
        for (z=0, z2=0;z<decals.length;z++,z2++){
            if(decals[z] == "html") { iText += decals[z+1]; z++; z2=1; }
            else if (decals[z] == "scale") { scaleX = decals[z+1] + 0; scaleY = decals[z+2] + 0; z+=2; z2--; }
            else if (decals[z] == "width") { width = decals[z+1] + 0;  z+=1; z2--; }
            else if (decals[z] == "part") { part = decals[z+1];  z+=1; z2--; }
            else {

              if(part == "torso")  iText += ('<a href=\'javascript:changeTorso2("' + decals[z] + '", ' + scaleX + ',' + scaleY + ')\'>');
              else if(part == "head") iText += ('<a href=\'javascript:changeHead("' + decals[z] + '")\'>');
              else if(part == "arms") iText += ('<a href=\'javascript:changeArms("' + decals[z] + '")\'>');
			  else if(part == "epaulette") iText += ('<a href=\'javascript:changeEpaulette("' + decals[z] + '")\'>');
              else iText += ('<a href=\'javascript:changeLegs("' + decals[z] + '")\'>');

            iText += ('<img src="' + decals[z] + '" width ="' + width + '"></a>');
            //if((z2+1)%2==0) iText +="<br>";
            //else 
			iText += "&nbsp;";
            }
        }
        iText += "</body>";
    }    

    element.open();
    element.writeln(iText);
    element.close();
}

