/* Gets the page / window width and height */
function getPageSize() {
	var scrollWidth = 0;
	var scrollHeight = 0;
	var winHeight = 0;
	var winWidth = 0;
	
	if (window.innerHeight && window.scrollMaxY) {	
		scrollWidth = document.body.scrollWidth;
		scrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { 
		scrollWidth = document.body.scrollWidth;
		scrollHeight = document.body.scrollHeight;
	} else {
		scrollWidth = document.body.offsetWidth;
		scrollHeight = document.body.offsetHeight;
	}
	
	if (self.innerHeight) {
		winWidth = self.innerWidth;
		winHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}	
	
	var pageHeight = scrollHeight < winHeight ? winHeight : scrollHeight;
	var pageWidth = scrollWidth < winWidth ? winWidth : scrollWidth;

	return { pageWidth: pageWidth, pageHeight: pageHeight, winWidth: winWidth, winHeight: winHeight };
}

/* Get the page scroll values */
function getPageScroll() {
	var scrollHeight = 0;
	
	if (self.pageYOffset) {
		scrollHeight = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	
		scrollHeight = document.documentElement.scrollTop;
	} else if (document.body) {
		scrollHeight = document.body.scrollTop;
	}
	
	return { scrollWidth: 0, scrollHeight: scrollHeight };
}

/* Modal methods */
var modal_mode = "";
var showModalCallback = false;

function validateRequest() {
	var ajax = new Ajax.Request("/admin/scripts/allow_request.php", { method: 'get', asynchronous:false });
	var allowed = ajax.transport.responseText == "";
	if (!allowed) {
		document.location.href = document.location.href;	
	}
	return allowed;
}

function showModal(title, mode, params, width, height, callback) {
	if (modal_mode == "") {
		if (validateRequest()) {
			modal_mode = mode;
			width -= 20;
			var contentHeight = height - 82;
			var contentWidth = width - 20;
			var pageSize = getPageSize();
			var pageScroll = getPageScroll();
			var top = pageScroll.scrollHeight + ((pageSize.winHeight - height) / 2);
			var left = ((pageSize.pageWidth - width) / 2);
			showModalCallback = callback;
	
			$("modalOverlay").style.width = pageSize.pageWidth + "px";
			$("modalOverlay").style.height = pageSize.pageHeight + "px";
			$("modalOverlay").style.display = "block";
			Element.setOpacity("modalOverlay", 0.0);
			
			$("modalWindow").style.width = width + "px";
			$("modalWindow").style.height = height + "px";
			$("modalWindow").style.top = (top < 0 ? 0 : top) + "px";
			$("modalWindow").style.left = (left < 0 ? 0 : left) + "px";
			$("modalWindowContent").style.width = contentWidth + "px";
			$("modalWindowContent").style.height = contentHeight + "px";
	
			Element.update("modalWindowTitle", title);
			
			/*
			var debug = "Params\n";
			for(var i in params)
			{
				debug += i + " = " + params[i] + "\n";
			}
			alert(params);
			*/
			
			if(modal_mode == "image_view")
			{
				params += "&contentWidth=" + contentWidth + "&contentHeight=" + contentHeight;
			}
			
			if(modal_mode == "body_preview")
			{
				new Effect.Opacity("modalOverlay", { duration: 0.2, transition: Effect.Transitions.linear, from: 0.0, to: 0.75 });	
				new Effect.BlindDown("modalWindow");
			}
			else if(modal_mode == "image_edit")
			{
				new Effect.Opacity("modalOverlay", { duration: 0.2, transition: Effect.Transitions.linear, from: 0.0, to: 0.75 });	
				new Effect.BlindDown("modalWindow");
			} 
			else {
				new Ajax.Updater("modalWindowContent", "/admin/interfaces/" + modal_mode + ".php", { method: "post", parameters: params, asynchronous: true, evalScripts: true, onComplete: loadModalComplete }); 		
			}
		}
	}
}

function loadModalComplete(request) {
	if (request.responseText != "") {
		new Effect.Opacity("modalOverlay", { duration: 0.2, transition: Effect.Transitions.linear, from: 0.0, to: 0.75 });	
		new Effect.BlindDown("modalWindow")
	} else {
		modal_mode = "";	
	}
	if(showModalCallback) showModalCallback(request.responseText);
	request = null;
	delete request;
}

function hideModal() {
	new Effect.BlindUp("modalWindow", { afterFinish: hideModalComplete });
	new Effect.Opacity("modalOverlay", { duration: 0.2, transition: Effect.Transitions.linear, from: 0.75, to: 0.0, afterFinish: hideModalComplete });
	if (typeof onModalDispose == "function") {
		onModalDispose();
	}
	onModalFormInit = null;
	delete onModalFormInit;
	onModalSaveComplete = null;
	delete onModalSaveComplete;
	onModalValidationError = null;
	delete onModalValidationError;
	onModalDispose = null;
	delete onModalDispose;
}

function hideModalComplete(effect) {
	$(effect.element.id).style.display = "none";
	if (effect.element.id == "modalWindow") {
		modal_mode = "";	
		$("modalWindowContent").innerHtml = "";
	}
	effect = null;
}

function getModalForm() {
	return document.modal_form;
}

function cancelModal() {
	// Check for changes?
	hideModal();	
}

function saveModal() {
	//alert(modal_mode + "   &&   " + Form.serialize(getModalForm()));
new Ajax.Request("/admin/scripts/" + modal_mode + "_save.php", { method: "post", parameters: Form.serialize(getModalForm()), asynchronous: true, onComplete: saveModalComplete });
}

function saveModalComplete(request) {
	var success = false;
	//alert(request.responseText);
	
	var response = eval('(' + request.responseText + ')');
	switch (response.type) {
		case "success":
			success = true;
			Element.update("admin_publish_status", "UNPUBLISHED");
			break;
		case "validation":
			if (typeof onModalValidationError == "function") {
				onModalValidationError(response.message);
			}
			break;
		case "error":
			break;
	}
	if (success) {
		if (typeof onModalSaveComplete == "function") {
			//alert(response.message.html);
			onModalSaveComplete(response.message);
		}
		hideModal();
	}
	response = null;
	delete response;
	request = null;
	delete request;
}

