/**
 * ...
 *
 * @author $Author$
 * @version $Revision$
 *
 * $Log$
 * Revision 1.19  2010/02/22 12:07:26  ronny
 * merged with development
 *
 * Revision 1.18.2.7  2010/02/18 13:35:34  daniel
 * added function openConfirmAjax()
 *
 * Revision 1.18.2.6  2009/12/03 15:26:39  ronny
 * move js function
 *
 * Revision 1.18.2.5  2009/12/03 13:36:33  daniel
 * updated animation
 *
 * Revision 1.18.2.4  2009/12/03 13:30:42  daniel
 * disable animation if shopping cart is not visible (first item)
 *
 * Revision 1.18.2.3  2009/12/03 12:58:18  daniel
 * activate animation
 *
 * Revision 1.18.2.2  2009/12/02 15:51:36  ronny
 * deactivate animation
 *
 * Revision 1.18.2.1  2009/12/02 15:37:05  ronny
 * animate shopping cart
 *
 * Revision 1.18  2007/03/29 14:02:10  daniel
 * updated demoversion (screenshot): opens popup window
 *
 * Revision 1.17  2006/12/19 13:10:44  daniel
 * initial values
 *
 * Revision 1.16  2006/12/11 20:02:33  daniel
 * added keyword selector
 *
 * Revision 1.15  2006/12/11 15:46:59  daniel
 * in progress: new profisuche
 *
 * Revision 1.14  2006/12/11 09:09:23  daniel
 * in progress: new profisuche
 *
 * Revision 1.13  2006/12/08 11:28:24  daniel
 * updated shopping cart shortview (display issues on ie6)
 *
 * Revision 1.12  2006/12/04 11:51:22  daniel
 * moved item actions (ajax shopping cart add, remove) to separate template
 *
 * Revision 1.11  2006/12/01 17:24:26  daniel
 * fixed: ajax call is case sensitiv
 *
 * Revision 1.10  2006/12/01 17:11:23  daniel
 * updated shoping card ajax functions (no tiny ajax)
 *
 * Revision 1.9  2006/10/29 17:45:39  daniel
 * updated navigation
 *
 * Revision 1.8  2006/10/04 14:18:34  daniel
 * in progress: profisuche
 *
 * Revision 1.7  2006/09/24 08:39:23  daniel
 * updated quicksearch (suggestions)
 *
 * Revision 1.6  2006/09/13 09:35:38  daniel
 * do not close expanded nodes at onMouseOut
 *
 * Revision 1.5  2006/09/12 17:59:39  daniel
 * javascript workaround for ie (expand and collapse submenu)
 *
 * Revision 1.4  2006/09/12 11:45:34  daniel
 * *** empty log message ***
 *
 * Revision 1.3  2006/09/04 11:40:41  ronny
 * add comments
 *
 *
 */
function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function ShowScreenshot(URL)
{
	var now = new Date();
	var popupURL = URL;
	var popup = window.open(popupURL,"Screenshot",'screenX=100,screenY=100,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=540,height=400');
	if( navigator.appName.substring(0,8) == "Netscape" )
	{
		popup.location = popupURL;
	}
	popup.focus();
}

function ShowInfo(URL)
{
	var now = new Date();
	var popupURL = URL;
	var popup = window.open(popupURL,"Hilfe",'screenX=100,screenY=100,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=540,height=400');
	if( navigator.appName.substring(0,8) == "Netscape" )
	{
		popup.location = popupURL;
	}
	popup.focus();
}

function itemMouseOver(item, color)
{
	item.bgColor = color;
}

function itemMouseOut(item, color)
{
	item.bgColor = color;
}

function itemToggleVisibility(itemID)
{
	var x=document.getElementsByName(itemID)
	for(var i=0; i<x.length; i++)
	{
		var current = x[i].style.visibility;
		if(x[i].style.visibility=='collapse')
			x[i].style.visibility='visible';
		else
			x[i].style.visibility='collapse';
	}
}

function searchUL(UL)
{
	do
	{
		if(UL) UL = UL.nextSibling;
		if(UL && UL.nodeName == "UL") return UL;
	} while(UL);
	return false;
}

function showBlock(id)
{
	var LI = document.getElementById(id);
	var UL = searchUL(LI.firstChild);
	UL.style.display = "block";
	UL.style.visibility = "visible";
}

function hideBlock(id)
{
	var LI = document.getElementById(id);
	var UL = searchUL(LI.firstChild);

	var check = LI.className;
	if(!check.match(/expanded/i))
	{
		//aktiven zweig nicht schliessen!
		UL.style.display = "none";
		UL.style.visibility = "hidden";
	}
}

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}//end function var_dump

var MAX_DUMP_DEPTH = 10;

function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];

  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}


       function dumpObj(obj, name, indent, depth) {

              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + name + "\n";

                     indent += "\t";

                     for (var item in obj)

                     {

                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "\n";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }

function showElement(element_id)
{
	var element = document.getElementById(element_id);
	if(element)
	{
		element.style.display = "block";
	}
}

function hideElement(element_id)
{
	var element = document.getElementById(element_id);
	if(element)
	{
		element.style.display = "none";
	}
}

function getAbsoluteX(element) {
	var result = 0;
	while(element != null) {
		result += element.offsetLeft;
		element = element.offsetParent;
	}
	return result;
}

function getAbsoluteY(element) {
	var result = 0;
	while(element != null) {
		result += element.offsetTop;
		element = element.offsetParent;
	}
	return result;
}

function updateShoppingCart(cmd, id, element)
{
	if (document.getElementById)
	{
		var shortview = document.getElementById('shopping_cart_shortview');
		if (shortview)
		{
			//update shopping card
			new Ajax.Request('/rpc/UpdateShoppingCart?cmd=' + escape(cmd) + '&id=' + escape(id),
							{asynchronous:false, evalScripts:true});
			/*
			//var el = document.getElementById('shopping_cart_shortview');
			/alert($('shopping_cart_shortview').cumulativeOffset());
			alert($('shopping_cart_shortview_thumbnail_wrapper').cumulativeOffset());
			alert($('shopping_cart_shortview_thumbnail').cumulativeOffset());
			var	targetX = getAbsoluteX(document.getElementById('shopping_cart_shortview'));
			var	targetY = getAbsoluteY(document.getElementById('shopping_cart_shortview'));
			alert(targetX + ',' +targetY);

			var thumbnail = document.getElementById('thumbnail_' + id);
			var	startX = getAbsoluteX(thumbnail);
			var	startY = getAbsoluteY(thumbnail);
			alert('thumbnail org: ' + startX + ',' +startY);

			alert('thumbnail proto: ' + $('thumbnail_' + id).cumulativeOffset());
			var animation = document.getElementById('shopping_cart_animation');
				animation.style.top = startY + 'px';
				animation.style.left = startX + 'px';
				animation.style.width = 80 + 'px';
				animation.style.height = 150 + 'px';
				animation.style.display = 'block';
				animation.style.backgroundColor = 'red';
*/
			// animate shopping adding: deactivated
			//update short view
			new Ajax.Updater('shopping_cart_shortview',
							'/rpc/UpdateShoppingCart?cmd=shortview',
							{asynchronous:false, onComplete:function() {
								if (cmd == 'add') {
									showElement('shopping_cart_shortview');
									var target = $('shopping_cart_shortview_thumbnail');
									if(target) {
										var targetX = target.cumulativeOffset()[0];
										var targetY = target.cumulativeOffset()[1];
										var animation = document.getElementById('shopping_cart_animation');
										var animation_thumbnail = document.getElementById('animation_thumbnail');
										var thumbnail = document.getElementById('thumbnail_' + id);
										//animation.innerHTML = thumbnail.innerHTML;
										animation_thumbnail.src = thumbnail.src;
										var startX = getAbsoluteX(thumbnail);
										var startY = getAbsoluteY(thumbnail);
										//alert('' + startX + ':' + startY + ' -> ' + targetX + ':' + targetY);
										animation.style.top = startY + 'px';
										animation.style.left = startX + 'px';
										animation.style.width = 80 + 'px';
										animation.style.height = 150 + 'px';
										animation.style.display = 'block';
										//Effect.Fade('shopping_cart_animation', { duration: 0.5, from: 0, to: 1 });
										//new Effect.Move('shopping_cart_animation', { x: targetX, y: targetY, mode: 'absolute', duration: 1 });
										new Effect.Parallel([
											new Effect.Move('shopping_cart_animation', { sync: true, x: targetX, y: targetY, mode: 'absolute' }),
											new Effect.Scale('shopping_cart_animation', 41, { sync: true }) // from 80x127 to 33x52
										], {
											duration: 2.0,
											//queue: 'end',
											transition: Effect.Transitions.sinoidal
										});
										Effect.Fade('shopping_cart_animation', { duration: 0.5, queue: 'end' });
										//animation_thumbnail.src = '/images/spacer.gif';
										//animation.style.display = "none";
									}
								}
								showElement('shopping_cart_shortview');
							}, evalScripts:true});
			//update content elements
			var shopping_cart = document.getElementById('shopping_cart');
			var item = document.getElementById('itemactions_' + id);
			if(shopping_cart)
			{
				//update shopping card
				new Ajax.Updater('main2',
								'/rpc/UpdateShoppingCart?cmd=shoppingcart',
								{asynchronous:false, evalScripts:true});
			}
			else if(item)
			{
				//update item
				new Ajax.Updater('itemactions_' + id,
								'/rpc/UpdateShoppingCart?cmd=item' + '&id=' + escape(id),
								{asynchronous:false, evalScripts:true});
			}
		}
	}
}

function setAutocompleter(autocomleter, inputID, suggestID, selectorID, resetInput, keywordsID)
{
	if(resetInput=='true')
	{
		var inputField = document.getElementById(inputID);
		inputField.value='';
	}
	var selector = document.getElementById(selectorID);
	search_query_opt = selector.options[selector.selectedIndex].value;
	autocomleter = new Ajax.Autocompleter(inputID,suggestID,'/rpc/search_suggest', {tokens: ' ', select:'selectme', method:'get', parameters:'type=relations&word_key='+search_query_opt, paramName:'query', autoSelect:false, asynchronous:false});
	if(keywordsID!='')
	{
		var keywordsField = document.getElementById(keywordsID);
		if(search_query_opt!='' && search_query_opt=='schlagwoerter')
		{
			keywordsField.style.display = 'block';
		}
		else
		{
			keywordsField.style.display = 'none';
		}
	}
}

function setSearchKeyword(selectorID, inputID)
{
	var selector = document.getElementById(selectorID);
	var inputField = document.getElementById(inputID);
	inputField.value=selector.options[selector.selectedIndex].value;

}

function clearFormField(field, checkInput)
{
	if(field.value==checkInput)
	{
		field.value='';
	}
}

function toggleVisibility($target) {
	Effect.toggle($target, 'blind', { duration: 0.5 });
	return false;
}

function openConfirmAjax(content_url, target) {
	Dialog.confirm(
		{ url: "/rpc/provider_hint", options: {method: 'get'}},
		{
			//top: 100,
			width:250,
			title: 'Hinweis',
			className: "reclam", //"bluelighting",
			okLabel: "Weiter",
			cancelLabel:"Zur&uuml;ck",
			resizable: false,
			closable: false,
			minimizable: false,
			maximizable: false,
			draggable: false,
			//showEffect: Element.show,
			//hideEffect: Element.hide,
			cancel: function(win) { return false; },
			ok: function(win) {window.location = target; return true;}
		}
	);
	return false;
}