// Author: Marcel Wouterse
// Copyright to Author
// Not allowed to be used without written permission of author!

function gotoPage(pPageName){
	location.href = gRootPath+'?'+pPageName;
}

function showWizard(){
	location.href = gRootPath+'?fa=wizard&wizard_id=index';
}

function showCoverWizard(){
	location.href = gRootPath+'?fa=wizard&wizard_id=index_cover';
}

function showAnimationWizard(){
	location.href = gRootPath+'?fa=wizard&wizard_id=index_animation';
}

function showHelp(){
	var lForm=document.ActionForm;
	var lValue = lForm.fa.value;
	location.href = gRootPath+'?fa=help&help_id='+lValue;
}

//##################################
//#        Common Functions        #
//##################################

var selectedElement;
var DeskTop;
var State = "drag";
var DeskOffsetX;
var DeskOffsetY;
var LayerArray = new Array();
var ImageArray = new Array();
var PropertyArray = new Array();
var TextImage = new Image();


//##################################
//#           Set Status           #
//##################################

function doAction(pWhat,pOptions){
var lForm = document.ActionForm;
lForm.fa.value=pWhat;
// First check the actions for multiple images or actions who do not need pics.
if(pWhat=="selected_picture")
	return sendSelectedImage(lForm,pOptions);
else if (pWhat=='clear_images')
	return sendActionForm(true);
else if(pWhat=="upload")
	return showUpload();
else if(pWhat=="next" || pWhat=="previous")
	return sendActionForm(true);
else if(pWhat=="add_transparency")
	return showAddTransparency();
else if(pWhat=="swirl_animation" || pWhat=="wave_animation" || pWhat=="rotate_animation"){
	if(!ensureSmallDimensions())
		return;}
else if (pWhat=="do_overlay_image")
	return 	showToolbarOverlayInfo();
else if(pWhat=="create_canvas")
	return showToolbarForCreateCanvas();

if(ImageArray.length > 2){
	alert("Select an image to work on");
	return false;
}
// There is only one image to perform following actions on
cancelCropOrResize();
if (pWhat=="crop_frame")
	return setToolbarForSelectBox();
else if(pWhat=="rotate")
	return showToolbarForRotation();
else if(pWhat=="mirror")
	return showToolbarForMirror();
else if (pWhat=="set_border")
	return setToolbarForBorder();
else if (pWhat=="refresh")
	return refreshImages();
else if (pWhat=="resize")
	return setToolbarForResizeBox();
else if (pWhat=="create_text")
	return setToolbarForText();
else if (pWhat=="overlay_image")
	return showToolbarForOverlay();
else if(pWhat=="save")
	return showSaveLayer();
else if(pWhat=="convert")
	lForm.data.value="format="+pOptions;
sendActionForm();
}

function showToolbarForCreateCanvas(){
	hideParametersBar();
	var l_infoBar = document.getElementById('apply_toolbar');
	l_infoBar.style.visibility='visible';
	showParametersDimensions(l_infoBar,true);
	setDefaultsCanvas();
	showParametersColors(l_infoBar,true);
	showParametersButtons(l_infoBar,true);
}

function showParametersColors(p_infoBar,pVisible){
	l_element = document.getElementById('infoFillType');
	p_infoBar.appendChild(l_element);
	l_element.style.visibility='visible';
	l_element = document.getElementById('infoColor');
	p_infoBar.appendChild(l_element);
	l_element.style.visibility='visible';
	l_element = document.getElementById('infoColor2');
	p_infoBar.appendChild(l_element);
	l_element.style.visibility='visible';
}

function setDefaultsCanvas(){
	var lForm = document.ActionForm
	lForm.width.value="150";
	lForm.height.value="100";
}

function cancelUploadOrSave(){
	hideSaveLayer();
	hideUpload();
	setDeskTopHeight();
}

function showSaveLayer(){
	cancelUploadOrSave();
	setStyle(document.getElementById('SaveToLayer'),'visibility','visible');
	setStyle(document.getElementById('SaveToLayer'),'zIndex',1000);
	var lDeskTopStatus=document.getElementById("DesktopStatus");
		lDeskTopStatus.style.visibility='hidden';
}

function hideSaveLayer(){
	setStyle(document.getElementById('SaveToLayer'),'visibility','hidden');
	setStyle(document.getElementById('SaveToLayer'),'zIndex',-1);
	var lDeskTopStatus=document.getElementById("DesktopStatus");
	lDeskTopStatus.style.visibility='visible';
}


function showToolbarForOverlay(){
	hideParametersBar();
	var l_infoBar = document.getElementById('apply_toolbar');
	l_infoBar.style.visibility='visible';
	var lForm = document.ActionForm;
	showUploadFromDisk(l_infoBar,true);
}

function showUploadFromDisk(pContainer,pVisible){
	l_element = document.getElementById('infoOverlay');
	pContainer.appendChild(l_element);
	l_element.style.visibility='visible';
}

function showToolbarForRotation(){
	hideParametersBar();
	var l_infoBar = document.getElementById('apply_toolbar');
	l_infoBar.style.visibility='visible';

	showParametersRotation(l_infoBar,true);
	showParametersButtons(l_infoBar,true);
}

function showParametersRotation(pContainer,pVisible){
	l_element = document.getElementById('infoOptRotation');
	pContainer.appendChild(l_element);
	l_element.style.visibility='visible';
}

function showToolbarForMirror(){
	hideParametersBar();
	var l_infoBar = document.getElementById('apply_toolbar');
	l_infoBar.style.visibility='visible';

	showParametersMirror(l_infoBar,true);
	showParametersButtons(l_infoBar,true);
}

function showToolbarInformation(){
	if(gOverlayPath=="")
		return;
	doAction('do_overlay_image',null);
}

function showToolbarOverlayInfo(){
	hideParametersBar();
	var l_infoBar = document.getElementById('apply_toolbar');
	l_infoBar.style.visibility='visible';
	l_element = document.getElementById('infoTextOverlay');
	l_infoBar.appendChild(l_element);
	l_element.style.visibility='visible';
	showParametersButtons(l_infoBar,true);
	showOverlayImage();
}

function showOverlayImage(){
	if (gOverlayPath !=""){
		var lImage = document.getElementById("OverlayImage");
		lImage.src = gOverlayPath;
		lImage.style.visibility='visible';
  		var lImgLayer = document.getElementById('OverlayImg');
        lImgLayer.style.zIndex=999;
        setReadyToDrag();}
}

function showAddTransparency(){
	hideParametersBar();
	var l_infoBar = document.getElementById('apply_toolbar');
	l_element = document.getElementById('infoToolbar');
	l_infoBar.style.visibility='visible';
	l_infoBar.appendChild(l_element);
	l_element.style.visibility='visible';
	var lImageLayer = document.getElementById('Layer01');
	var lImage = document.getElementById('Image_01');
	layerHTML = lImageLayer.innerHTML;
	var lObject = document.getElementById('href_1');
	var lString = "index.cfm?fa=add_transparency";
	lString = lString + "&image_path="+lImage.src;
	if(lObject==null){
		lObject = document.createElement("a");
		lObject.setAttribute("id", "href_1");
		lObject.setAttribute("href", lString+"&data=");
	}
	lImageLayer.appendChild(lObject);
	lObject.appendChild(lImage);
}

function hideTransparency(){
	var lImage = document.getElementById('Image_01');
	if(lImage==null)
		return;
	if (lImage.parentNode.id=='href_1')
		lImage.parentNode.parentNode.appendChild(lImage);
}


function showParametersMirror(pContainer,pVisible){
	l_element = document.getElementById('infoOptMirror');
	pContainer.appendChild(l_element);
	l_element.style.visibility='visible';
}

 function sendActionForm(pForce){
 	var lForm=document.ActionForm;
 	var lValue = lForm.fa.value;
 	var l_doit = true;
 	if (lValue=='crop_frame')
		l_doit = sendCropFrame(lForm);
	if (lValue=='resize')
		l_doit = sendResizeFrame(lForm);
	if (lValue=='create_text')
		l_doit = sendCreateText(lForm);
	if (lValue=='set_border')
		l_doit = sendCreateBorder(lForm);
	if (lValue=='rotate')
		l_doit = sendRotateImage(lForm);
	if (lValue=='mirror')
		l_doit = sendMirrorImage(lForm);
	if (lValue=='add_transparency'){
		l_doit = false;}
	if (lValue=='add_to_ani_sequence')
		l_doit = sendAddToAniSequence();
	if (lValue=='overlay_image'){
		l_doit = sendAddOverlayImage();}
	if (lValue=='do_overlay_image'){
		l_doit = sendDoOverlayImage(lForm);
	}
	if (lValue=='create_canvas'){
		l_doit = sendCreateCanvas();
		if(!l_doit)
			return;
		else
			pForce = true;
	}	
	if(pForce != true){
	    var lImage = getImageFromCurrentLayer();
		lForm.image_path.value=getSourcePathFor(lImage);
		}
	if (lImage==null && pForce!=true)
 		return false;
	if(l_doit==true){
		lForm.submit();
		return true;
		}
	return false;
 }

function sendCreateCanvas(){
	
	var lForm = document.ActionForm;

	var lColorSelect = lForm.Color;
	var lColor1 = lColorSelect.options[lColorSelect.selectedIndex].value;
	var lColorSelect = lForm.Color2;
	var lColor2 = lColorSelect.options[lColorSelect.selectedIndex].value;
	var lFillSelect = lForm.filltype;
	var lFill = lFillSelect.options[lFillSelect.selectedIndex].value;
	var lWidth = giveInt(lForm.width.value);
	var lHeight = giveInt(lForm.height.value);
	if (lWidth < 5 || lHeight < 5){
		alert("Width and height should be at least 5x5 !")
		return false;
	}
	if (lWidth > 1000 || lHeight > 1000){
		alert("Maximum width and hight is 1000x1000 !")
		return false;
	}
	lString = "width="+lWidth+"&height="+lHeight+"&color="+lColor1+"&color2="+lColor2+"&fill="+lFill;
	lForm.data.value=lString;
	return true;
}

 function sendAddOverlayImage(){
 	var lUploadForm = document.getElementById('OverlayUploadForm');
	lUploadForm.submit();
	return false;
 }

function ensureSmallDimensions(){
 var lImage = getImageFromCurrentLayer();
 var lWidth = giveInt(lImage.width);
 var lHeight = giveInt(lImage.height);
 if (lWidth > 150 || lHeight > 150){
 	alert("Image is not allowed to exceed width/height of 150x150! (Resize or Crop your image.)");
 	return false;
 }
 return true;
}

function sendAddToAniSequence(){
 var lImage = getImageFromCurrentLayer();
 var lWidth = giveInt(lImage.width);
 var lHeight = giveInt(lImage.height);
 if (lWidth > 250 || lHeight > 250){
 	alert("Image is not allowed to exceed width/height of 250x250! (Resize or Crop your image.)");
 	return false;
 }
 return true;
}

function sendRotateImage(pForm){
	var lValue;
	if(pForm.rotation[0].checked == true)
		lValue = 90;
	else if(pForm.rotation[1].checked == true)
		lValue = 180;
	else if(pForm.rotation[2].checked == true)
		lValue = 270;
	else if(pForm.rotation[3].checked == true)
		lValue = pForm.rotationText.value;
	lValue = giveInt(lValue);
	if(lValue < 1 || lValue > 359){
		alert("Select a value between 0 and 360");
		return false;}
	pForm.data.value = lValue;
	return true;
}

function sendMirrorImage(pForm){
	var lValue;
	if(pForm.mirror[0].checked == true)
		lValue = "horizontal";
	else if(pForm.mirror[1].checked == true)
		lValue = "vertical";
	if(lValue == null){
		alert("Select horizontal or vertical mirror");
		return false;}
	pForm.data.value = lValue;
	return true;
}

function hideParametersBar(){
	var l_element = document.getElementById('apply_toolbar');
	l_element.style.visibility='hidden';
	l_element = document.getElementById('infoWidth');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoHeight');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoOffsetX');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoOffsetY');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoBtnApply');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoBtnCancel');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoBtnHelp');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoAspect');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoText');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoFont');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoSize');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoFont');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoColor');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoColor2');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoBtnPreview');
		document.body.appendChild(l_element);
		l_element.style.visibility='hidden';
	l_element = document.getElementById('borderWidth');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoRadius');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoBorderShadow');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoBorderType');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoOptRotation');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoOptMirror');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('ExampleImage');
		l_element.style.visibility='hidden';
	l_element = document.getElementById('OverlayImage');
		l_element.style.visibility='hidden';
	l_element = document.getElementById('infoToolbar');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoOverlay');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoTextOverlay');
	l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	l_element = document.getElementById('infoFillType');
		l_element.style.visibility='hidden';
		document.body.appendChild(l_element);
	hideTransparency();
}


function addBookmark(title, url) {
		if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
			window.sidebar.addPanel(title, url, "");
		} else if(document.all) {
			window.external.AddFavorite(url, title);
		} else if(window.opera && window.print) {
			return true;
		}
	}

function getText(){
	var lForm = document.ActionForm;
	if(lForm.text == null)
		return;
	var lText = lForm.text.value;
	if(lText==null || lText=="")
		return;
	// FIX ME: For letters like +,* see http://nl.wikipedia.org/wiki/ASCII_(tekenset)
	// For FF we need to escape 1 time, for IE 2 times :-(
	lText=escape(lText);
	var lFontSelect = lForm.Font;
	var lFont = lFontSelect.options[lFontSelect.selectedIndex].value;
	var lSizeSelect = lForm.Size;
	var lSize = lSizeSelect.options[lSizeSelect.selectedIndex].value;
	var lColorSelect = lForm.Color;
	var lColor = lColorSelect.options[lColorSelect.selectedIndex].value;
	var lPreview = document.getElementById('ExampleImage');
	var lString = gRootPath;
	lString+="?fa=temp_text";
	lString+="&font="+lFont;
	lString+="&size="+lSize;
	lString+="&color="+lColor;
	if(lPreview.style.filter){
		lString+="&text="+escape(lText);
		lString+="&extension=png";
		var lFilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='";
		lFilter += lString;
		lFilter +="', sizingMethod='image')";
		lPreview.style.filter=lFilter;
		setStyle(lPreview,'cursor','move');
	}
	else{
		lString+="&text="+lText;
		lPreview.src=lString;
	}
}

function setDeskTop(){
if (isIE)
	DeskTop = document.all('DeskTop');
if (isFF)
	DeskTop = document.getElementById('DeskTop');
}

function setDeskTopWidth(){
   	var lImage = getImageFromCurrentLayer(false);
   	if(lImage){
   		var lwidth = readStyle(DeskTop,"width");
		var lImageWidth = giveInt(lImage.width);
		if(lImageWidth > gMinDeskWidth)
			lwidth = lImageWidth;
		else
			lwidth = gMinDeskWidth;
			DeskTop.parentNode.style.width = lwidth + 20;
			DeskTop.style.width = lwidth + 20;
   	}
   	else{
   		DeskTop.parentNode.style.width = DeskTop.style.width;
   		}
}

function setDeskTopHeight(){
	var lElement = document.getElementById("ExplainLayer");
	var lCurY = 10;
	if (lElement){
		var lHeight = giveInt(readStyle(lElement,'height'));
		var lDeskTopHeight = giveInt(readStyle(DeskTop,'height'));
		setStyle(DeskTop,'height',lHeight);
		lCurY = lHeight;
	}
	// matches 2, because index does not start with [0]
	if(PropertyArray.length == 2)
		yOffset=0;
 	for(i=1;i<PropertyArray.length;i++){
		lNumber=padout(i);
		lCurY=lCurY+giveInt(PropertyArray[i]['height'])+yOffset;
	}
	if(gMinDeskHeight > lCurY){
		DeskTop.style.height=gMinDeskHeight;
		lDeltaY = gMinDeskHeight-lCurY;
		lStart=lDeltaY/2;
		for(i=1;i<PropertyArray.length;i++){
			lNumber=padout(i);
			lImgLayer = document.getElementById("Layer"+lNumber);
			lImgLayer.style.top=lStart;
			clipImageToDesktop(lImgLayer);
			lStart=lStart+giveInt(PropertyArray[i]['height'])+yOffset;
		}
	}
	else{
			if(PropertyArray.length == 2)
			  lCurY=lCurY+10;
			DeskTop.style.height=lCurY;
			lCurY=10;
		 	for(i=1;i<PropertyArray.length;i++){
				lNumber=padout(i);
				lImgLayer = document.getElementById("Layer"+lNumber);
				lImgLayer.style.top=lCurY;
				clipImageToDesktop(lImgLayer);
				lCurY=lCurY+giveInt(PropertyArray[i]['height'])+yOffset;
			}
	}
}


function setLayerArray(pIndex){
	lIndex = padout(pIndex);
	lLayerName = "Layer"+lIndex;
	lLayer=document.getElementById(lLayerName);
	while(lLayer!=null){
		LayerArray[pIndex] = lLayer;
		ensureOnDesktop(lLayer);
		setLayerArray(pIndex+1);
	}
	return;
}

function Initialise(){
	setDeskTop();
	setLayerArray(1);
	setUploadLayer();
	hideParametersBar();
	showToolbarInformation();
	lelement=document.getElementById('Layer01');
	if(lelement && ImageArray.length == 2)
		changeSelection(lelement);
	setDeskTopHeight();
	setDeskTopWidth();
	deskTopResized();
}
