function popup( url, code, width, height )
{
	if( code == null )
	{
		code = "newPopup";
	}
	
	if( width == null )
	{
		width = 800;
	}
	
	if( height == null )
	{
		height = 600;
	}
		
	var popup = window.open( url, code, "width=" + width + ", height=" + height + ", left=50, top=50, toolbar=no, menubar=yes, scrollbars=yes, location=0, status=no" );
}

//filter slider function ...
function initPage()
{
	$("div.FilterSlider a").click( 
		function() 
		{
			
			if ($("div.FilterSliderLink").parent().hasClass("Closed")) 
			{
				$("div.FilterSliderLink").parent().toggleClass( "Closed" );
				$("div.FilterTable").slideToggle( 250 );
				//$("div.FilterSlider #filterSliderBarTop").slideToggle( 10 );
				$("div.FilterSlider a").text( LOCAL_FILTERS_CLOSE );
				
			}
			else 
			{
				$("div.FilterTable").slideToggle( 250, 
					function()
					{
						$("div.FilterSliderLink").parent().toggleClass("Closed");
						$("div.FilterSlider a").text( LOCAL_FILTERS_OPEN );
					}
				);
				//$("div.FilterSlider #filterSliderBarTop").slideToggle( 10 );
			}
		}
	);
}

function searchSingleContainerId(form)
{
	var node = form.parentNode;
	while (node.className.indexOf("AjaxContainer") == -1)
	{
		node = node.parentNode;
	}
	return node.id;
}

//init page on load ...
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

//SHOWCASE on start page

var showcaseRegistry = new Array();

function showcaseInit( showcaseId, pageSize, size )
{
	showcaseRegistry[ showcaseId ] = new Array();
	showcaseRegistry[ showcaseId ][ "current" ] = 0;
	showcaseRegistry[ showcaseId ][ "pageSize" ] = pageSize;
	showcaseRegistry[ showcaseId ][ "size" ] = size;
	showcaseDraw( showcaseId );
}

function showcaseScrollUp( showcaseId )
{
	var current = showcaseRegistry[ showcaseId ][ "current" ];
	if( current > 0 )
	{
		current --;
	}
	showcaseRegistry[ showcaseId ][ "current" ] = current;
	showcaseDraw( showcaseId );
}

function showcaseScrollDown( showcaseId )
{
	var size = showcaseRegistry[ showcaseId ][ "size" ];
	var current = showcaseRegistry[ showcaseId ][ "current" ];
	var pageSize = showcaseRegistry[ showcaseId ][ "pageSize" ];
	if( current < size - pageSize )
	{
		current ++;
	}
	showcaseRegistry[ showcaseId ][ "current" ] = current;
	showcaseDraw( showcaseId );
}

function showcaseDraw( showcaseId )
{
	var showcase = showcaseRegistry[ showcaseId ];
	var size = showcaseRegistry[ showcaseId ][ "size" ];
	var pageSize = showcase[ "pageSize" ];
	var current = showcase[ "current" ];
	
	for( var i = 0; i < size; i++ )
	{
		var element = document.getElementById( showcaseId + "_" + i );
		if( element != null )
		{
			if( i < current 
					|| i >= current + pageSize )
			{
				element.style.display = "none";
			}
			else
			{
				element.style.display = "block";
			}
		}
		else
		{
			alert( "no element " + showcaseId + "_" + i);
		}
	}
	
	if( current > 0 )
	{
		document.getElementById( showcaseId + "_up_0" ).style.display = "none";
		document.getElementById( showcaseId + "_up_1" ).style.display = "block";
	}	
	else
	{
		document.getElementById( showcaseId + "_up_0" ).style.display = "block";
		document.getElementById( showcaseId + "_up_1" ).style.display = "none";
	}
	
	if( current < size - pageSize )
	{
		document.getElementById( showcaseId + "_down_0" ).style.display = "none";
		document.getElementById( showcaseId + "_down_1" ).style.display = "block";
	}	
	else
	{
		document.getElementById( showcaseId + "_down_0" ).style.display = "block";
		document.getElementById( showcaseId + "_down_1" ).style.display = "none";
	}
}

//MODAL WINDOW
/**
 * @param contentDivId id of the modal window div to be shown (class = ModalWindow) 
 * @param isClickOnCurtainHides if true, the modal window will be closed if you click on the curtain
 */
function modalShow( contentDivId, isClickOnCurtainHides, marginTop )
{
	var body = document.getElementsByTagName( "body" )[0];
	var curtain = document.getElementById( "modalCurtain" );
	
	if( curtain == null )
	{
		//create on demand
		curtain = document.createElement( "div" );
		curtain.id = "modalCurtain";
		if( body != null )
		{
			body.appendChild( curtain );
		}
	}
	makeVisible( curtain );
	if( isClickOnCurtainHides )
	{
		curtain.onclick = function ( ) { modalHideAll(); };
	}
	
	var modalWindow = getTopLevelElement( contentDivId );
	_displayWindow( modalWindow, marginTop );
}
/**
 * works like modalShow(..) but without curtain 
 * @param contentDivId id of the modal window div to be shown (class = ModalWindow) 
 */
function popupShow( contentDivId, marginTop )
{
	var modalWindow = getTopLevelElement( contentDivId );
	_displayWindow( modalWindow, marginTop );
}

function _displayWindow( modalWindow, marginTop )
{
	if( modalWindow != null )
	{	
		makeVisible( modalWindow );
		if( !marginTop )
		{
			marginTop = Math.max ( 10, ( getClientHeight() - modalWindow.offsetHeight ) / 2 );
		}
		//alert( getScrollTop()  + " " + marginTop + " " + modalWindow.offsetHeight +" "+ getClientHeight());
		
		modalWindow.style.top = marginTop + getScrollTop() + "px";
	}
}

/**
 * removes the modal window if shown before
 * @param contentDivId name of modal window content div (class == ModalWindow)
 */
function modalHide( contentDivId )
{
	var windows = document.getElementsByTagName( "div" );
	var openWindowCount = 0;
	for( var windowIndex in windows )
	{
		//Test auf "windowsIndex ist eine Zahl"
		//Opera erkennt sonst einen Modalen Dialog mehrfach,
		//einmal �ber den Index als Z�hler und einmal �ber den Namen
		if (/\b\d+\b/.exec(windowIndex)) {
			var modalWindow = windows[ windowIndex ];
			if( modalWindow != null  
					&& modalWindow.className != null
					&& ( modalWindow.className.indexOf( "ModalWindow " ) != -1
							|| modalWindow.className.indexOf( " ModalWindow" ) != -1  
							|| modalWindow.className == "ModalWindow" ) 
					&& modalWindow.style.display == "block" )
			{
				openWindowCount++;
			}
		}
	}
	
	if( openWindowCount <= 1 )
	{
		var curtain = document.getElementById( "modalCurtain" );
		if( curtain != null )
		{
			makeInvisible( curtain );			
		}
	}
	
	var modalWindow = getTopLevelElement( contentDivId );
	if( modalWindow != null )
	{
		makeInvisible( modalWindow );
	}
}

/**
 * removes all modal windows that have ever been openend
 * used when clicking on curtain 
 */
function modalHideAll()
{
	var curtain = document.getElementById( "modalCurtain" );
	if( curtain != null )
	{
		makeInvisible( curtain );		
	}
	
	var windows = document.getElementsByTagName( "div" );
	for( var windowIndex in windows )
	{
		var modalWindow = windows[ windowIndex ];
		if( modalWindow != null  
				&& modalWindow.className != null
				&& ( modalWindow.className.indexOf( "ModalWindow " ) != -1
						|| modalWindow.className.indexOf( " ModalWindow" ) != -1  
						|| modalWindow.className == "ModalWindow" ) )
		{
			makeInvisible( modalWindow );
		}
	}
}

//VIEW SWITCH

function viewSelect( viewId, selectedId )
{
	var view = document.getElementById( viewId );
	if( view != null )
	{
		for( var childIndex in view.childNodes )
		{
			var child = view.childNodes[ childIndex ];
			if( child.id != null )
			{
				if( child.id == selectedId )
				{
					makeVisible( child );
				}
				else
				{
					makeInvisible( child );
				}
			}
		}
	}
}

function viewHide( viewId, hideId )
{
	var view = document.getElementById( viewId );
	if( view != null )
	{
		for( var childIndex in view.childNodes )
		{
			var child = view.childNodes[ childIndex ];
			if( child.id != null )
			{
				if( child.id == hideId )
				{
					makeInvisible( child );
				}
				else
				{
					makeVisible( child );
				}
			}
		}
	}
}

//INFO BUBBLES

function showInfoBubble( id, relativeElement )
{	
	var element = document.getElementById( id );
	var bubble = getTopLevelElement( "info_bubble" );
	if( element != null
			&& relativeElement != null )
	{
		bubble.style.top = getTop( relativeElement ) + 10 + "px";
		bubble.style.left = getLeft( relativeElement ) - 209 + "px";
		
		makeVisible( bubble );
		makeVisible( element );
	}
}

function hideInfoBubble( id )
{
	var bubble = getTopLevelElement( "info_bubble" );
	makeInvisible( bubble );
	makeInvisible( id );
}

//TOOLS

function getLeft( element )
{
	return accumulateProperty( element, "offsetLeft" );
}

function getTop( element )
{
	return accumulateProperty( element, "offsetTop" );
}

function getScrollTop() {
	if( window.pageYOffset )
	{
		return window.pageYOffset;
	}
	if( document.documentElement )
	{
		return document.documentElement.scrollTop;
	}
	if( document.body ) 
	{
		return document.body.scrollTop;
	}
}

function getScrollLeft() {
	if( window.pageXOffset )
	{
		return window.pageXOffset;
	}
	if( document.documentElement )
	{
		return document.documentElement.scrollLeft;
	}
	if( document.body ) 
	{
		return document.body.scrollLeft;
	}
}

function getClientHeight() {
	if( window.innerHeight  )
	{
		return window.innerHeight ;
	}
	if( document.documentElement )
	{
		return document.documentElement.clientHeight;
	}
	if( document.body ) 
	{
		return document.body.clientHeight;
	}
}

function accumulateProperty( element, attribute )
{
	var result = 0;
	while( element )
	{
		result += element[ attribute ];
		element = element.offsetParent;
	}
	return result;
}

function makeVisible( idOrElement ) {
	var element = null;
	if( typeof(idOrElement) == "string" )
	{
		element = document.getElementById( idOrElement );
	}
	else if( typeof(idOrElement) == "object" )
	{
		element = idOrElement;
	}
	if( element != null )
	{	
		element.style.display = "block";
	}
}

function makeInvisible( idOrElement ) {
	var element = null;
	if( typeof(idOrElement) == "string" )
	{
		element = document.getElementById( idOrElement );
	}
	else if( typeof(idOrElement) == "object" )
	{
		element = idOrElement;
	}
	if( element != null )
	{	
		element.style.display = "none";
	}
}

/*Mouse tracking and hover functionality*/

var IE = document.all ? true : false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

var mouseX = 0;
var mouseY = 0;

function getMouseXY( e ) {
	if (IE) { 
		mouseX = event.clientX + getScrollLeft();
		mouseY = event.clientY + getScrollTop();
		
	}
	else { 
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	return true;
}

var hoverShowTimeoutEvents = new Array();
var ajaxCallTimeoutEvents = new Array();

function mouseHoverShow( id ) {
	if( ! CMS_ISENABLED
			&& LAST_DOM_NODE_LOADED )
	{
		var element = getTopLevelElement( id );
		if( element.style.display != "block" )
		{
			if( hoverShowTimeoutEvents[ id ] == null )
			{
				hoverShowTimeoutEvents[ id ] = new Array();
			}
			hoverShowTimeoutEvents[ id ].push( window.setTimeout( "mouseHoverShowInternal( '" + id + "' )", 800 ) );
		}
		else
		{
			mouseHoverShowInternal( id );
		}
	}
}

function mouseHoverShowInternal( id ) {
	var element = getTopLevelElement( id );
	
	var hoverY = mouseY;
	var maxTop = getClientHeight() - 400;
	if( (hoverY - getScrollTop()) > maxTop )
	{
		hoverY = getScrollTop() + maxTop;
	}
	
	element.style.top = hoverY + "px";
	element.style.left = mouseX + 10 + "px";
	makeVisible( element );
}

function mouseHoverHide( id ) {
	if( ! CMS_ISENABLED
			&& LAST_DOM_NODE_LOADED )
	{
		if( hoverShowTimeoutEvents[ id ] != null && hoverShowTimeoutEvents[ id ].length > 0 )
		{
			for( var timeOutIndex in hoverShowTimeoutEvents[ id ] )
			{
				window.clearTimeout( hoverShowTimeoutEvents[ id ][ timeOutIndex ] );
			} 
			hoverShowTimeoutEvents[ id ] = new Array();
		}		
		var element = getTopLevelElement( id );
		makeInvisible( element );
	}
	
	if (loadingAjaxArticleZoomTimeout != null) {
		window.clearTimeout( loadingAjaxArticleZoomTimeout );	
		loadingAjaxArticleZoomTimeout = null;
	}
	loadingAjaxArticleZoomContainer = null;	
}

var loadedAjaxArticleZoomContainers = new Array();
var loadingAjaxArticleZoomContainer = null;
var loadingAjaxArticleZoomTimeout = null;

function loadArticleZoomImage( requestURL, ajaxContainerId )
{
	if( loadedAjaxArticleZoomContainers[ ajaxContainerId ] == null && loadingAjaxArticleZoomContainer != ajaxContainerId)
	{		
		loadingAjaxArticleZoomContainer = ajaxContainerId;
		loadingAjaxArticleZoomTimeout = window.setTimeout("doLoadArticleZoomImage('"+requestURL+"', '"+ajaxContainerId+"')", 2000) ;
	}
}

function doLoadArticleZoomImage(requestURL, ajaxContainerId) {
			
	if (loadingAjaxArticleZoomContainer == ajaxContainerId) {
		loadedAjaxArticleZoomContainers[ ajaxContainerId ] = true;
		ajaxCall( requestURL, ajaxContainerId );
		loadingAjaxArticleZoomContainer = null;
	}
}

function resetLoadedArticleZoomImages()
{
	loadedAjaxArticleZoomContainers = new Array();
}

/* TOP LEVEL ELEMENTS */

function addToTopLevelContainer( element )
{
	if( element != null )
	{
		var body = document.getElementsByTagName( "body" )[0];
		var topLevelContainer = document.getElementById( "__topLevelContainer" );
		
		if( topLevelContainer == null )
		{
			//create on demand
			topLevelContainer = document.createElement( "div" );
			topLevelContainer.id = "__topLevelContainer";
			if( body != null )
			{
				body.appendChild( topLevelContainer );
			}
		}		
		topLevelContainer.appendChild( element );
	}
}

function getTopLevelElement( id )
{
	var element = document.getElementById( id );
	var tlcElement = document.getElementById( id + "_tlc" );
	if( element != null )
	{
		if( tlcElement != null )
		{
			//remove existing tlc element
			var topLevelContainer = document.getElementById( "__topLevelContainer" );
			if( topLevelContainer != null 
					&& topLevelContainer == tlcElement.parentNode )
			{
				topLevelContainer.removeChild( tlcElement );
			}
		}
		element.id = id + "_tlc";
		addToTopLevelContainer( element );		
		return element;
	}
	else if( tlcElement != null )
	{
		return tlcElement;
	}
	else
	{
		return null;
	}
}

function displayText(div) {
	var temp = document.getElementById('deliveryZoneInfo').innerHTML;
	document.getElementById(div).innerHTML = temp;
}
