/*
NOTE: we could use prototype class creation style

var General = Class.create({	
	initialize : function() {
		do something
	}
});

*/

General = function() {};

General.bannerRotation;
General.mouseoverEffects = new Array();
General.mouseoutEffects = new Array();

Event.observe(window, 'load', function() {
	// executed after page is rendered
});

General.initBannerRotation = function(nav_id, element_id, frequency, effect, resize, desc, longdesc, width, height) {
	General.bannerRotation = new PeriodicalExecuter(function(pe) {
		new Ajax.Request('index.php', {
			method: 'post',
			parameters: { 
				id: nav_id, 
				process: 'Ajax', 
				action: 'change_banner', 
				resize: resize, 
				desc: desc, 
				longdesc: longdesc, 
				width: width, 
				height: height },
			onSuccess: function(transport) {
				if (effect == 'fade') {
					new Effect.Opacity(
						element_id, { 
							queue: { position: 'end', scope: 'teaser_fade' },
							afterFinish: function() {
								$(element_id).innerHTML = transport.responseText;
							},
							from: 1.0,
							to: 0.0,
							duration: 0.5
						}
					);
					new Effect.Opacity(
						element_id, {
							queue: { position: 'end', scope: 'teaser_fade' },
							from: 0.0,
							to: 1.0,
							duration: 0.5
						}
					);
				} else if (effect == 'crossfade') {
					var temporaryNode = $(element_id).cloneNode(true);
					$(temporaryNode).setOpacity(0);
					$(temporaryNode).innerHTML = transport.responseText;
					$(temporaryNode).setStyle({
						position: 'absolute',
						top: '0px',
						left: '0px'
					})
					$(element_id).up().insert(temporaryNode);
					new Effect.Opacity(
							temporaryNode, { 
								queue: { position: 'end', scope: 'teaser_fade' },
								afterFinish: function() {
									$(element_id).innerHTML = transport.responseText;
									$(temporaryNode).remove();
								},
								from: 0.0,
								to: 1.0,
								duration: 1.0
							}
						);
//					$(element_id).insert(temporaryNode);
//					alert(temporaryNode);
				} else if (effect == 'blind') {
					//new Effect.BlindDown(element_id);
					
				} else {
					$(element_id).innerHTML = transport.responseText;
				}
			}
		});
	}, frequency);
};

/*
 * OLD STUFF - needs to be rewritten/copied up
 */

function writeEmailAddress() {
	var domain = "mfa.messefrankfurt.com"; 
	var adr = "privacy" + String.fromCharCode(64) + domain; 
	document.write('<a href="mailto:' + adr + '">' + adr + '</a>');
}

function openPopup(URLStr,width,height) {
	var popUpWindow=0;
	if (popUpWindow) {
		if (!popUpWindow.closed) popUpWindow.close();
	}
	if (width > screen.width)
		width = screen.width - 12;
	if (height > (screen.height - 100))
		height = screen.height - 90;
	//var left = parseInt((screen.width/2)-(width/2));
	//var top = parseInt((screen.height/2)-(height/2));
	var left = 0;
	var top = 0;
	if (width == 0 || height == 0) {
		popUpWindow = open(URLStr, 'popUpWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=yes,copyhistory=no');
	} else {
		URLStr += '&width='+width+'&height='+height;
		popUpWindow = open(URLStr, 'popUpWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	}
	popUpWindow.focus();
}
function changeElementClass(element,newClass) {
	document.getElementById(element).className=newClass; 
}
function setNormClass() {
	if (this.className != 'input_focus')
		this.className='input'; 
}
function resetNormClass() {
	this.className='input'; 
}
function setHoverClass() {
	if (this.className != 'input_focus')
		this.className='input_hover'; 
}
function setFocusClass() {
	this.className='input_focus'; 
}
function setButtonHoverClass() {
	this.className='button_hover'; 
}
function setButtonClass() {
	this.className='button'; 
}
function renderMouseEffects() {
	var inputTags = document.getElementsByTagName("input");
	var selectTags = document.getElementsByTagName("select");
	var textareaTags = document.getElementsByTagName("textarea");
	for (var i=0; i<inputTags.length; i++) {
		var inputType = inputTags[i].getAttribute('type');
		var inputId   = inputTags[i].getAttribute('id');
		var inputClass = inputTags[i].getAttribute('class');
		if (inputClass != "search_input" && inputClass != "search_submit") {
			if (inputType == "text" || inputType == "password") {
				inputTags[i].onmouseover = setHoverClass;
				inputTags[i].onmouseout = setNormClass;
				inputTags[i].onfocus = setFocusClass;
				inputTags[i].onblur = resetNormClass;
			} else if (inputType == "submit" || inputType == "reset" || inputType == "button") {
				inputTags[i].onmouseover = setButtonHoverClass;
				inputTags[i].onmouseout = setButtonClass;
				inputTags[i].onfocus = setButtonHoverClass;
				inputTags[i].onblur = setButtonClass;
			}
		}
	}
	for (var i=0; i<selectTags.length; i++) {
		selectTags[i].onmouseover = setHoverClass;
		selectTags[i].onmouseout = setNormClass;
		selectTags[i].onfocus = setFocusClass;
		selectTags[i].onblur = resetNormClass;
	}
	for (var i=0; i<textareaTags.length; i++) {
		textareaTags[i].onmouseover = setHoverClass;
		textareaTags[i].onmouseout = setNormClass;
		textareaTags[i].onfocus = setFocusClass;
		textareaTags[i].onblur = resetNormClass;
	}
}
/* parseUri JS v0.1.1, by Steven Levithan <http://stevenlevithan.com>
Splits any well-formed URI into the following parts (all are optional):
----------------------
- source (since the exec method returns the entire match as key 0, we might as well use it)
- protocol (i.e., scheme)
- authority (includes both the domain and port)
  - domain (i.e., host; can be an IP address)
  - port
- path (includes both the directory path and filename)
  - directoryPath (supports directories with periods, and without a trailing backslash)
  - fileName
- query (does not include the leading question mark)
- anchor (i.e., fragment) */
function parseUri(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	
	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	
	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	
	return uri;
}

function changeContent(nav_id, position) {
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { id: nav_id, position:position, process:"Ajax", action:"change_content" },
		onSuccess: function(transport) {
			$('content').innerHTML = transport.responseText;
		}
	});
}

function changePageAreaContent(nav_id, pn) {
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { id: nav_id, pn:pn, process:"Ajax", action:"change_page_area_content" },
		onSuccess: function(transport) {
			$('content_page_area').innerHTML = transport.responseText;
		}
	});
}

function changeFaqAnswerContent(nav_id, faq_id) {
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { id: nav_id, faq_id:faq_id, process:"Ajax", action:"change_faq_answer_content" },
		onSuccess: function(transport) {
			$('faq_answers').innerHTML = transport.responseText;
		}
	});
}

function changeBanner(nav_id, frequency) {
	new Ajax.PeriodicalUpdater('banner', 'index.php', {
		frequency: frequency,
		decay: 1,
		method: 'post',
		parameters: { id: nav_id, process:"Ajax", action:"change_banner" },
		onSuccess: function(transport) {
			$('banner').innerHTML = transport.responseText;
		}
	});
}

function selectPrefix(name, prefix_value) {
	if (name == null) name = 'country';
	if (prefix_value == null) prefix_value = '';
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { process:'Ajax', action:'select_prefix', country_id:$F(name) },
		onSuccess: function(transport) {
			var prefix = transport.responseText;
			var selectTags = document.getElementsByTagName('select');
			for(var i=0; i<selectTags.length; i++) {
				if (selectTags[i].id.indexOf('_prefix') != -1) {
					var correct_tag = false;
					if (prefix_value != '') {
						if (prefix_value.indexOf(',') != -1) {
							var prefix_array = prefix_value.split(',');
							for (var k = 0; k < prefix_array.length; k++) {
								if (selectTags[i].id.indexOf(prefix_array[k]) != -1) {
									correct_tag = true;
								}
							}
						} else {
							if (selectTags[i].id.indexOf(prefix_value) != -1) {
								correct_tag = true;
							}
						}
					} else {
						correct_tag = true;
					}
					if (correct_tag) {
						for(var j=0; j<selectTags[i].options.length; j++) {
							if (selectTags[i].options[j].value == prefix) {
								selectTags[i].options[j].selected = true;
							}
						}
					}
				}
			}
		}
	});
}
function loadLightboxUrl(contentUrl, boxWidth, boxHeight, xPos, yPos) {
	uriArray = parseUri(contentUrl);
	freeze();
	new Ajax.Request(uriArray["protocol"] + "://" + uriArray["authority"] + uriArray["path"], {
		method: 'post',
		parameters: uriArray["query"],
		onSuccess: function(transport) {
			if (boxWidth == undefined || boxWidth == -1) boxWidth = 640;
			if (boxHeight == undefined || boxHeight == -1) boxHeight = 480;
			/*
			if (xPos == undefined || xPos < 0) xPos = (screen.width / 2) - (boxWidth / 2) + getXOffset() -10;
			if (yPos == undefined || yPos < 0) yPos = (screen.height / 2) - (boxHeight / 2) + getYOffset() -120;
			*/
			if (xPos == undefined || xPos < 0) xPos = (screen.width / 2) - (boxWidth / 2) -10;
			if (yPos == undefined || yPos < 0) yPos = (screen.height / 2) - (boxHeight / 2) -120;
			
			$('lightboxContainer').style.width = boxWidth + "px";
			$('lightboxContainer').style.height = boxHeight + "px";
			$('lightboxContainer').style.top = yPos +"px";
			$('lightboxContainer').style.left = xPos +"px";
			
			$('lightboxContainer').innerHTML = transport.responseText;
			$('lightboxContainer').show();
		}
	});
}
function loadLightbox(lightboxContent, boxWidth, boxHeight, xPos, yPos) {
	freeze();
	if (boxWidth == undefined || boxWidth == -1) boxWidth = 640;
	if (boxHeight == undefined || boxHeight == -1) boxHeight = 480;
	if (xPos == undefined || xPos < 0) xPos = (screen.width / 2) - (boxWidth / 2) + getXOffset() -10;
	if (yPos == undefined || yPos < 0) yPos = (screen.height / 2) - (boxHeight / 2) + getYOffset() -120;
	
	$('lightboxContainer').style.width = boxWidth + "px";
	$('lightboxContainer').style.height = boxHeight + "px";
	$('lightboxContainer').style.top = yPos +"px";
	$('lightboxContainer').style.left = xPos +"px";
	
	$('lightboxContainer').innerHTML = lightboxContent;
	$('lightboxContainer').show();
}
function hideLightbox() {
	$('lightboxContainer').innerHTML = '';
	$('lightboxContainer').hide();
	unfreeze();
}
function loadZoomedImage(imagePath, imageText) {
	freeze();
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { process:'Ajax', action:'load_image_zoom', imagePath:imagePath, imageText:imageText },
		onSuccess: function(transport) {
			$('imageContainer').style.width = "760px";
			$('imageContainer').style.left = (screen.width/2)-380+getXOffset() +"px";
			$('imageContainer').style.top = (screen.height/2)-340+getYOffset() +"px";
			$('imageContainer').innerHTML = transport.responseText;
			$('imageContainer').show();
		}
	});
}
function hideZoomedImage() {
	$('imageContainer').innerHTML = '';
	$('imageContainer').hide();
	unfreeze();
}
function loadZoomifyContent(lot) {
	freeze();
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { process:'Ajax', action:'load_zoomify_content', lot:lot },
		onSuccess: function(transport) {
			$('zoomifyBox').style.width = "720px";
			$('zoomifyBox').style.left = (screen.width/2)-370+getXOffset() +"px";
			$('zoomifyBox').style.top = (screen.height/2)-355+getYOffset() +"px";
			$('zoomifyBox').innerHTML = transport.responseText;
			$('zoomifyBox').show();
		}
	});
}
function hideZoomifyContent() {
	$('zoomifyBox').innerHTML = '';
	$('zoomifyBox').hide();
	unfreeze();
}

function getXOffset() {
	return (document.all) ? document.body.scrollLeft : window.pageXOffset;
}
function getYOffset() {
	return (document.all) ? document.body.scrollTop : window.pageYOffset;
}

function freeze(color) {
	var height = screen.availHeight;
	var pageHeight = $('page').getHeight()+80;
	if (pageHeight > height) {
		height = pageHeight;
	}
	if (color == undefined) var color = '000';
	$('layerContainer').setStyle("background-color:#"+color);
	$('layerContainer').setStyle("position:fixed");
	$('layerContainer').setStyle("height:"+height+"px");
	$('layerContainer').show();
}
function unfreeze() {
	$('layerContainer').hide();
}
function toggleProducts(parent) {
	var products = document.getElementsByClassName("pgroup" + parent);
	if (products.length > 0) {
		for(var i = 0; i < products.length; i++) {
			products[i].toggle();
		}
	}
	var checkbox_parent = document.getElementsByClassName("pelement" + parent);
	var checkboxes = document.getElementsByClassName("pclass" + parent);
	if (checkboxes.length > 0) {
		for(var j = 0; j < checkboxes.length; j++) {
			checkboxes[j].checked = checkbox_parent[0].checked;
		}
	}
}

function deleteComment(commentId) {
	new Ajax.Request('index.php', {
		method: 'post',
		parameters: { process:'Ajax', action:'deleteComment', commentId:commentId },
		onSuccess: function(transport) {
			if (transport.responseText == "1") {
				$('comment_'+commentId).remove();
			} else {
				alert("fail");
			}
		}
	});
}

