// This script shold be UTF-8: äöü
// var definition
var currentPlan;
var currentSite;
var currentAddress;
var currentWorkgroupFile;
var workgroupFiles;
var sites = new Array();
var addresses = new Array();
var addressFieldNames = new Array("Company", "Street", "Street2", "Zip", "City", "Phone", "Mobile", "Lastname", "Firstname", "Mail");

// current path info
var pathArray = location.pathname.split( '/' );
var contextPath = "/" + pathArray[1];

function PrintServerPlan(uuid, name, title, width, height, isColor, siteUUID) {
	this.uuid = uuid;
	this.name = name;
	this.title = title;
	this.width = width;
	this.height = height;
	this.isColor = isColor;
	this.siteUUID = siteUUID;
}

function PrintServerSite(uuid, name) {
	this.uuid = uuid;
	this.name = name;
}

function WorkgroupFile(uuid, title, width, height, description, filename, fileextension, filesize) {
	this.uuid = uuid;
	this.title = title;
	this.width = width;
	this.height = height;
	this.description = description;
	this.filename = filename;
	this.fileextension = fileextension;
	this.filesize = filesize;
}

function PrintServerAddress(uuid, company, street1, street2, zip, city, phone, fax, email, firstname, lastname, sex) {
	this.uuid = uuid;
	this.company = company;
	this.street1 = street1;
	this.street2 = street2;
	this.zip = zip;
	this.city = city;
	this.phone = phone;
	this.fax = fax;
	this.email = email;
	this.contactFirstname = firstname;
	this.contactLastname = lastname;
	this.contactSex = sex;
}

function openWorkgroupFileDialog(dialogID, uuid) {
}

//currentWorkgroupFile = new WorkgroupFile('${currFile.node.UUID}', '${planTitle}', '${planWidth}', '${planHeight}', '${description}', '${currFile.fileName}', '${currFile.fileExtension}', '${currFile.fileSize}'); $('#editPlanDialog').dialog('open');

function validateWorkgroupFileEditForm() {
/*	if ((!$('#editFolderForm input[name=document]').val() || $('#editFolderForm input[name=document]').val() == "") && (!$('#editFolderForm input[name=filename]').val() || $('#editFolderForm input[name=filename]').val() == "")) {
		alert("Bitte geben Sie eine Datei ein");
	} else {
		alert("document: " + $('#editFolderForm input[name=document]').val() + ", filename: "+ $('#editFolderForm input[name=filename]').val());
	}*/
	var missingValues = new Array();
	
	// plan name is mandatory
	if (!$('#editPlanForm input[name=title]').val().length > 0) {
		missingValues.push("Planname");
	}
	
	// make sure a file has been uploaded before or the user has selected one
	if ($('#editPlanForm input[name=document]').val().length > 0 || currentWorkgroupFile.filename.length > 0) {
		// a file was selected or has been uploaded previously
	} else {
		missingValues.push("Datei");
	}
	
	if (missingValues.length > 0) {
		var alertText = "Bitte füllen Sie folgende obligatorischen Felder aus:\n";
		for (var i=0; i<missingValues.length; i++) {
			alertText += "- " + missingValues[i] + "\n";
		}
		alert(alertText);
		return false;
	} else {
		return true;
	}
}

function toggleCollapsibleParagraph(id, type, uuid) {
	var paragraph = document.getElementById(id);
	var status;
	if (paragraph) {
		var currentClass = paragraph.getAttribute("class");
		if (!currentClass) {
			currentClass = paragraph.getAttribute("className");
		}
		if (currentClass == "closed") {
			paragraph.setAttribute("class", "open"); 
			paragraph.setAttribute("className", "open");
			status = "open";
		} else {
			paragraph.setAttribute("class", "closed"); 
			paragraph.setAttribute("className", "closed");
			status = "closed";
		}
		if (type && uuid) {
//			alert("setting status " + status + " for " + type + " with uuid "+uuid);
			// add the collapsible page marker url to the context path
			var collapsibleMarkerPagePath = contextPath + "/.magnolia/pages/clientSideService.html";
			// update the marker of this collapsible element
//			alert("url: "+collapsibleMarkerPagePath);
			$.post(collapsibleMarkerPagePath, {type: type, uuid: uuid, status: status, command: "markCollapsibleElementStatus"} );
		}
	}
}

function updateAddress(site, addressType, element) {
	if (element) {
//		alert("value: "+element.value);
		if (element.value == 'orderAddress' || element.value == 'shippingAddress' || element.value == 'billingAddress') {
			for (var i=0; i<addressFieldNames.length; i++) {
				var sourceID = site + '_' + element.value + addressFieldNames[i];
				var targetID = site + '_' + addressType + addressFieldNames[i];
				$('#' + targetID).val($('#'+sourceID).val());
			}			
		} else if (element.value.substring(0, 5) == 'uuid_') {
			var uuid = element.value.substring(5);
			var address = addresses[uuid];
			if (address) {
				$('#' + site + '_' + addressType + 'Company').val(address.company);
				$('#' + site + '_' + addressType + 'Street').val(address.address1);
				$('#' + site + '_' + addressType + 'Street2').val(address.address2);
				$('#' + site + '_' + addressType + 'Zip').val(address.zip);
				$('#' + site + '_' + addressType + 'City').val(address.city);
				$('#' + site + '_' + addressType + 'Phone').val(address.phone);
				$('#' + site + '_' + addressType + 'Mobile').val(address.fax);
				$('#' + site + '_' + addressType + 'Lastname').val(address.contactLastname);
				$('#' + site + '_' + addressType + 'Firstname').val(address.contactFirstname);
				$('#' + site + '_' + addressType + 'Mail').val(address.email);
			}
		}
		element.options[0].selected = true;
	}
}

function resizeImage(image, width, height, isMax, center) {
	if (image) {
		var origWidth = image.width;
		var origHeight = image.height;
		var scaleFactor = 1;
		if (width && width > 0 && height && height > 0) {
			// Bild-Seitenverhältnis
			var ratioImage = origWidth / origHeight;
			// Box-Seitenverhältnis
			var ratioBox = width / height;			
			if (isMax) {
				if (ratioBox > ratioImage) {
					// Wenn das Box-Seiten-Verhältnis > Bild-Seitenverhältnis muss die Höhe übereinstimmen
					scaleFactor = height / origHeight;
				} else {
					// Wenn das Box-Seiten-Verhältnis < Bild-Seitenverhältnis muss die Breite übereinstimmen
					scaleFactor = width / origWidth;
				}
			} else {
				if (ratioBox < ratioImage) {
					// Wenn das Box-Seiten-Verhältnis < Bild-Seitenverhältnis muss die Höhe übereinstimmen
					scaleFactor = height / origHeight;
				} else {
					// Wenn das Box-Seiten-Verhältnis > Bild-Seitenverhältnis muss die Breite übereinstimmen
					scaleFactor = width / origWidth;
				}
			}
			var newWidth = Math.ceil(origWidth * scaleFactor);
			var newHeight = Math.ceil(origHeight * scaleFactor);
			$(image).css("width", "" + newWidth + "px");
			$(image).css("height", "" + newHeight + "px");
			$(image).css("position", "relative");
			if (isMax && center) {
				if (newWidth < width) {
					$(image).css("left", Math.round((width - newWidth) / 2) + "px");
				}
				if (newHeight < height) {
					$(image).css("top", Math.round((height - newHeight) / 2) + "px");
				}
			}
			if (!isMax && center) {
				if (newWidth > width) {
					$(image).css("left", "-" + Math.round((newWidth - width) / 2) + "px");
				}
				if (newHeight > height) {
					$(image).css("top", "-" + Math.round((newHeight - height) / 2) + "px");
				}
			}
		}
	}
}
