/*
*	DEBUGGING
*/
var Debugger = {};
Debugger.VERBOSE = false;
Debugger.NOTICE = "notice";
Debugger.WARNING = "notice";
Debugger.ERROR = "error";
Debugger.UNDEFINED_ERROR = "undefined error";
Debugger.message = function(messageString, messageType)
{
	if(messageType == undefined) var messageType = Debugger.UNDEFINED_ERROR;
	if(Debugger.VERBOSE) alert("\"" + messageType + "\": " + messageString);
}

/*
*	swaps a displayed image to an image defined by file
*	f.e. rollOver effect
*
*	@id		string, node of the image to swap
*	@file	string, path and filename of image that should be displayed
*/
function swapImage(id, file)
{
	if(document.getElementById(id)) document.getElementById(id).src = file;
	else Debugger.message("swapImage: node " + id + " does not exist", Debugger.NOTICE);
}
/*
*	show or hide a video info
*	@buttonId	string, id of the button that should be affected
*	@infoId		string, id of the info layer that shuld be affected
*/
function setVideoInfoDisplay(buttonId, infoId)
{
	if(document.getElementById(buttonId) && document.getElementById(infoId))
	{
		if(document.getElementById(infoId).style.display == "block")
		{
			document.getElementById(buttonId).src = "images/buttons/v_info_plus.jpg";
			showLayer(infoId, false);
		}
		else
		{
			document.getElementById(buttonId).src = "images/buttons/v_info_minus.jpg";
			showLayer(infoId, true);		
		}
	}
	else
	{
		Debugger.message("layer not available", Debugger.WARNING);
	}
}
/*
*	show or hide a layer
*	@id		string, id of layer which should be affected
*	@show	bool, true if block is display:block, false for display:none
*/
function showLayer(id, show)
{
	if(document.getElementById(id))
		if(show)
		{
			document.getElementById(id).style.display = "block";
		}
		else
		{
			document.getElementById(id).style.display = "none";
		}
}
/*
*	image opacity
*/
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
/*
*	JBroadcaster
*/
var JBroadcaster = {};
JBroadcaster.broadcastList = new Array();
JBroadcaster.addListener = function (functionName, objectName)
{
	JBroadcaster.broadcastList.push({func:functionName, obj:objectName});
}
JBroadcaster.removeListener = function (functionName, objectName)
{
	for(var i = 0; i < JBroadcaster.broadcastList.length; i++)
	{
		var cItem = JBroadcaster.broadcastList[i];
		if(cItem.func == functionName && cItem.obj == objectName) JBroadcaster.broadcastList.splice(i, 1);
	}
}
JBroadcaster.broadcastMessage = function(functionName)
{
	for(var i = 0; i < JBroadcaster.broadcastList.length; i++)
	{
		var cItem = JBroadcaster.broadcastList[i];
		if(cItem.func == functionName) cItem.obj[functionName]();
	}
}
/*
*	EmotionBanner
*	
*	a class based on the scriptaculous library
*/
function EmotionBanner(layerId)
{
	this.layer = layerId;
	this.teaserList = new Array();
	this.currentAnimationId = null;
	this.count = 0;
	this.cBegin = '<div class="teaserGallery">';
	this.c = '';
	this.cEnd = '</div>';
	this.width = 970;
	this.height = 300;
	if(EmotionBanner.instanceCount == undefined)
	{
		EmotionBanner.instanceCount = 0;
		EmotionBanner.instances = new Array();
		EmotionBanner.instances.push(this);
	}
	else
	{
		EmotionBanner.instances.push(this);
	}
	this.instanceId = EmotionBanner.instanceCount;
	EmotionBanner.instanceCount ++;
}
EmotionBanner.duration = 10000;
EmotionBanner.speed = 1;
EmotionBanner.textSpeed = 2000;
EmotionBanner.fadeIn = function(imgId)
{
	new Effect.Opacity(document.getElementById(imgId), {duration:EmotionBanner.speed, fps:33, from:0.0, to:1.0});
}
EmotionBanner.fadeOut = function(imgId)
{
	new Effect.Opacity(document.getElementById(imgId), {duration:EmotionBanner.speed, fps:33, from:1.0, to:0.0});
}
EmotionBanner.fadeTeaser = function(instanceId)
{
	var newImg = null;
	var newTxt = null;
	var oldImg = null;
	var oldTxt = null;

	var link = EmotionBanner.instances[instanceId];

	if(link.count < link.teaserList.length - 1 && link.teaserList.length > 1)
	{
		oldImg = link.teaserList[link.count].id;
		oldTxt = link.teaserList[link.count].id + "_t";
		link.count++;
		newImg = link.teaserList[link.count].id;
		newTxt = link.teaserList[link.count].id + "_t";
	}
	else
	{
		link.count = 0;
		if(link.teaserList.length > 1)
		{
			oldImg = link.teaserList[link.teaserList.length - 1].id;
			oldTxt = link.teaserList[link.teaserList.length - 1].id + "_t";
		}
		newImg = link.teaserList[0].id;
		newTxt = link.teaserList[0].id + "_t";
	}
	EmotionBanner.fadeIn(newImg);
	
	var tId = link.teaserList[link.count].id + "_t";
	setTimeout("EmotionBanner.fadeIn('" + String(tId) + "')", EmotionBanner.textSpeed);
	setTimeout("EmotionBanner.fadeOut('" + String(tId) + "')", EmotionBanner.duration - EmotionBanner.textSpeed);
	
	if(link.teaserList.length > 1)
	{
		EmotionBanner.fadeOut(oldImg);
	}
}
// prototypes
var o = EmotionBanner.prototype;
o.addBanner = function (imageUrl, text, _x, _y)
{
	this.teaserList.push({url:imageUrl, txt:text, x:_x, y:_y, id:"teaserItem" + (this.teaserList.length + 1)});
	this.c += '<div class="item">';
	this.c += '<img id="' + this.teaserList[this.teaserList.length - 1].id + '"  src="' + this.teaserList[this.teaserList.length - 1].url + '" >';
	this.c += '<div id="' + this.teaserList[this.teaserList.length - 1].id + '_t" class="text" style="left:' + this.teaserList[this.teaserList.length - 1].x + ';top:' + this.teaserList[this.teaserList.length - 1].y + ';">';

	if(navigator.appName == "Microsoft Internet Explorer" && parseFloat(navigator.appVersion) == 4)
	{
		this.c += '<img width="' + this.width + '" height="' + this.height + '" src="images/emobanner/placeholder.gif" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.teaserList[this.teaserList.length - 1].txt + '\', sizingMethod=\'scale\')" >';
	}
	else
	{
		this.c += '<img width="' + this.width + '" height="' + this.height + '" src="' + this.teaserList[this.teaserList.length - 1].txt + '" >';
	}
	
	this.c += '</div></div>';
}
o.run = function()
{
	if(this.teaserList.length > 0)
	{
		this.layer.innerHTML = this.cBegin + this.c + this.cEnd;
		
		if(this.teaserList.length > 1) for(var i=0; i < this.teaserList.length; i++)
		{
			setOpacity(document.getElementById(this.teaserList[i].id), 0);
			setOpacity(document.getElementById(this.teaserList[i].id + "_t"), 0);
		}
		
		EmotionBanner.fadeIn(this.teaserList[0].id);
		setTimeout("EmotionBanner.fadeIn('teaserItem1_t')", EmotionBanner.textSpeed);
		setTimeout("EmotionBanner.fadeOut('teaserItem1_t')", EmotionBanner.duration - EmotionBanner.textSpeed);
		
		this.currentAnimationId = setInterval("EmotionBanner.fadeTeaser(" + this.instanceId + ")", EmotionBanner.duration);
	}
	else
	{
		Debugger.message("no teaser added to EmotionBanner.teaserList, use addTeaser:function before running EmotionBanner", Debugger.WARNING);
	}
}
/*
*	create a JBroadcaster for window.onload
*/
window.onload = function()
{
	JBroadcaster.broadcastMessage("siteLoaded");
}
/*
*	clearTextField
*/
function clearTextField(id)
{
	if(document.getElementById(id)) document.getElementById(id).value = "";
}
/*
*	checkOnTextInput
*/
function checkOnTextInput(id, defaultText)
{
	if(document.getElementById(id))
	{
		if(document.getElementById(id).value == defaultText && document.getElementById(id).value != "") return true;
		else false;
	}
}

/*
*	clearTextAreaOnInput
*/
function clearTextAreaOnInput(textAreaID, defaultText)
{
	if(document.getElementById(textAreaID))
	{
		if(document.getElementById(textAreaID).innerHTML == defaultText) document.getElementById(textAreaID).innerHTML = "";
	}
}

/*
*	for comment preview only
*/
function clearOnTextInput(id, defaultText)
{
	if(document.getElementById(id))
	{
		if(document.getElementById("previewComment").style.display == "block") hidePreview("previewComment");
		if(document.getElementById('validadtionFailureMessage').style.display != "none") Effect.SlideUp('validadtionFailureMessage');
	
		Effect.Appear(document.getElementById('previewBtn'), {duration:0.5}); 
		Effect.Fade(document.getElementById('sendBtn'), {duration:0.5});
	
		if(checkOnTextInput(id, defaultText))
		{
			document.getElementById(id).value = "";
		}
	}
}
/*
*	loadUserComments
*/
function loadUserComments(id, _setCode, _userName)
{
	if(document.getElementById(id))
	{
		new Ajax.Request('/includes/comments/getComments.php',
		  {
			method:'post',
			parameters: {setCode:_setCode, userName:_userName},
			onSuccess: function(transport){
			  var response = transport.responseText || "No comments yet.";
			  
			  document.getElementById(id).innerHTML = response;			  
			},
			onFailure: function(){
				Debugger.message('error during comment load', Debugger.WARNING);
			}
		  });
	}
}
/*
*	previewUserComment
*/
function previewUserComment(previewID, sendBtnId, previewBtnId, id, _setCode, _userName, _action, _type, _userIP, _publish, _nickName, _comment)
{
	var commentAllowed = true;
	new Ajax.Request('/includes/comments/commentAllowed.php',
	  {
		method:'post',
		parameters: {userName:_userName},
		onSuccess: function(transport){
			if(transport.responseText === "true") commentAllowed = true;
			else commentAllowed = false;
			
			if(commentAllowed)
			{
				if(document.getElementById(previewID) && document.getElementById("usercommentfield").value.length > 2 && document.getElementById("usercommentfield").value != "Write here...")
				{
					Effect.Fade(document.getElementById(previewBtnId), {duration:0.5}); 
					Effect.Appear(document.getElementById(sendBtnId), {duration:0.5});
					
					if(document.getElementById(previewID).style.display == "none") Effect.SlideDown(previewID);
			
					var nn = "";
					(_nickName == "" || _nickName == "Enter your nickname here...") ? nn = "member comment" : nn = _nickName;
			
					document.getElementById('userPreviewNickName').innerHTML = nn;
					document.getElementById('userPreviewComment').innerHTML = _comment;
				}
				else
				{
					if(document.getElementById('validadtionFailureMessage').style.display == "none")
					{
						if(document.getElementById(previewID).style.display != "none") Effect.SlideUp(previewID);
						Effect.SlideDown('validadtionFailureMessage');
					}
				}
			}
			else
			{
				if(document.getElementById("tooMuchComments"))
				{
					Effect.SlideDown('tooMuchComments');
				}
			}			
		}
	  });
}
/*
*	hidePreview
*/
function hidePreview(previewId)
{
	Effect.SlideUp(previewId);
}
/*
*	sendUserComment
*/
function sendUserComment(id, _setCode, _userName, _action, _type, _userIP, _publish, _nickName, _comment)
{
	if(document.getElementById(id))
	{
		document.getElementById("nicknamefield").value = "";
		document.getElementById("usercommentfield").value = "";
	
		var nn = "";
		(_nickName == "" || _nickName == "Enter your nickname here..." || _nickName == "Enter your nickname here") ? nn = "member comment" : nn = _nickName;
	
		new Ajax.Request('/includes/comments/sendUserComment.php',
		  {
			method:'post',
			parameters: {setCode:_setCode, userName:_userName, action:_action, nickName:nn, type:_type, comment:_comment, userIP:_userIP, publish:_publish},
			onSuccess: function(transport){
			
				Effect.Appear(document.getElementById('previewBtn'), {duration:0.5}); 
				Effect.Fade(document.getElementById('sendBtn'), {duration:0.5});				
			
				hidePreview("previewComment");
			
				loadUserComments(id, _setCode, _userName);
			},
			onFailure: function(){
				Debugger.message('error during comment send', Debugger.WARNING);
			}
		  });	
	}	
}
/*
*	getFormValue
*/
function getFormValue(formName, elementName)
{
	var rs = document.forms[formName].elements[elementName].value;
	
	return rs;
}

/*
*	resetForm
*/
function resetForm(formID)
{
	if(document.getElementById(formID)) document.getElementById(formID).innerHTML = "";
}

/*
*	sendQuickFeedback
*/
function sendQuickFeedback(qfID, responseMessageID, userName)
{
	if(document.getElementById(qfID))
	{
		var feedBackText = document.getElementById(qfID).value;
		document.getElementById(qfID).value = "";
	
		new Ajax.Request('/includes/quickFeedback/quickFeedback.php',
		  {
			method:'post',
			parameters: {feedbackText:feedBackText, user:userName},
			onSuccess: function(transport)
			{
				if(transport.responseText == "ok")
				{
					if(document.getElementById(responseMessageID))
					{
						Effect.Appear(responseMessageID);
					}
				}
				else
				{
					Debugger.message('quickFeedback text could not be send.');
				}
			},
			onFailure: function(){
				Debugger.message('error during quickfeedback send', Debugger.WARNING);
			}
		  });	
	}
}

/*
*	custom radios
*	
*	Create a list (f.e. <ul><li></li><li></li></ul>) and insert <div id="xxx"><img /></div> statements into it. afterwards create a hidden input field and
*	name it like custom radioButton group. Now if you click on a custom radioButton, the hidden field will be changed immediatly.
*/

var radioGroups = new Array();

function addCustomRadio(id, normal, over, active, group, formName, availablePricecodes, defaultPricecode, isActive)
{
	if(document.getElementById(id))
	{
		var n = document.getElementById(id);
		if(radioGroups[group] == null || radioGroups[group] == undefined) radioGroups[group] = new Array();		
		var radio = { node:n, out:normal, over:over, clicked:active, formName:formName, availablePricecodes:availablePricecodes, defaultPricecode:defaultPricecode, isActive:isActive };
		radioGroups[group].push(radio);
		
		radio.node.style.cursor = "pointer";
		
		radio.node.onmouseover = function() { if(!radio.isActive) radio.node.getElementsByTagName("img")[0].src = radio.over; }
		radio.node.onmouseout = function() { if(!radio.isActive) radio.node.getElementsByTagName("img")[0].src = radio.out; }
		radio.node.onclick = function()
		{
			for(var i = 0; i < radioGroups[group].length; i++)
			{
				if(radioGroups[group][i].node != this)
				{
					radioGroups[group][i].node.getElementsByTagName("img")[0].src = radioGroups[group][i].out;
					radioGroups[group][i].isActive = false;
				}
			}
			document.getElementsByName("formName")[0].value = radio.formName;
			document.getElementsByName("allowedTypes")[0].value = radio.availablePricecodes;
			document.getElementsByName("subscriptionTypeId")[0].value = radio.defaultPricecode;
			radio.node.getElementsByTagName("img")[0].src = radio.clicked;
			radio.isActive = true;
		}
	}
}
