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, position, banner_number) {
	if (banner_number == undefined) {
		banner_number = -1;
	}
	if (General.bannerRotation != undefined) {
		General.bannerRotation.stop();
		new Ajax.Request('index.php', {
			method: 'post',
			parameters: { 
				id: nav_id, 
				process: 'Ajax', 
				action: 'change_banner',
				position: position,
				bnr: banner_number,
				resize: resize, 
				desc: desc, 
				longdesc: longdesc, 
				width: width, 
				height: height },
			onSuccess: function(transport) {
				General.switchBanner(element_id, effect, transport.responseText);
			}
		});
	}
	General.bannerRotation = new PeriodicalExecuter(function(pe) {
		new Ajax.Request('index.php', {
			method: 'post',
			parameters: { 
				id: nav_id, 
				process: 'Ajax', 
				action: 'change_banner',
				position: position,
				bnr: -1,
				resize: resize, 
				desc: desc, 
				longdesc: longdesc, 
				width: width, 
				height: height },
			onSuccess: function(transport) {
				General.switchBanner(element_id, effect, transport.responseText);
			}
		});
	}, frequency);
};

General.switchBanner = function(element_id, effect, content) {
	switch (effect) {
		case 'fade':
			new Effect.Opacity(
				element_id, { 
					queue: { position: 'end', scope: 'teaser_fade' },
					afterFinish: function() {
						$(element_id).innerHTML = content;
					},
					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
				}
			);
			break;
		case 'crossfade':
			var temporaryNode = $(element_id).cloneNode(true);
			$(temporaryNode).setOpacity(0);
			$(temporaryNode).innerHTML = content;
			$(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 = content;
						$(temporaryNode).remove();
					},
					from: 0.0,
					to: 1.0,
					duration: 1.0
				}
			);
			break;
		case 'blind':
			//new Effect.BlindDown(element_id);
			// TODO: implement functionality for banner blind effect
			break;
		case 'slide':
			var element_width = $(element_id).down('img').getWidth();
			var temporaryNode = $(element_id).cloneNode(true);
			temporaryNode.writeAttribute('id', 'tmpNode');
			$(temporaryNode).update(content);
			$(temporaryNode).setStyle({
				position: 'absolute',
				top: '0px',
				left: element_width + 'px'
			});
			$(element_id).up().insert(temporaryNode);
			new Effect.Parallel([
				new Effect.Move(element_id, {sync: true, x: (element_width * -1), y: 0, mode: 'absolute'}),
				new Effect.Move(temporaryNode, {sync: true, x: 0, y: 0, mode: 'absolute'})
				], {
					duration: 1.0,
					afterFinish: (function(element_id, temporaryNode) {
						$(element_id).update(content);
						$(element_id).setStyle({ left: 0 + 'px' });
						$(temporaryNode).remove();
					}).curry(element_id, temporaryNode)
				}
			);
			break;
		default:
			$(element_id).update(content);
	}
};


General.popUpWindow;
General.openPopup = function(URLStr, width, height) {
	if (General.popUpWindow != undefined) {
		if (!General.popUpWindow.closed) General.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) {
		General.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;
		General.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+'');
	}
	General.popUpWindow.focus();
};

/**
 * 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)
 * 
 * @param string source uri
 * @return object uri elements
 */
General.parseUri = function(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;
};

/*
 * OLD STUFF
 */

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 = (document.viewport.getWidth() / 2) - (boxWidth / 2) + getXOffset() -10;
	if (yPos == undefined || yPos < 0) yPos = (document.viewport.getHeight() / 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) color = '000';
	$('layerContainer').setStyle("background-color:#"+color);
	$('layerContainer').setStyle("position:fixed");
	$('layerContainer').setStyle("height:"+height+"px");
	$('layerContainer').show();
}
function unfreeze() {
	$('layerContainer').hide();
}

/*
 * TODO:
 * NOTE: what is to do?
 */
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;
		}
	}
}

/*
 * remove comments
 * TODO: move to comment module
 */
var Comment = function() {};
Comment.deleteComment = function(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");
			}
		}
	});
};

