var backup;
var bEffect;

function wipeIn(node)
{
	dojo.query('li a', node).forEach('item.style.visibility = "hidden";');
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		dojo.byId(node).style.marginTop = '-14px';
	}
	
	bEffect = false;

	var wipe = dojo.fx.wipeIn({
		node: node, 
		duration: 1000,
		onEnd: function(){
			dojo.query('li a', node).forEach('item.style.visibility = "visible";');
			
			var properties = {
				node: node,
				words: false
			};
			currentAnimation = dojox.fx.text.blockFadeIn(dojo.mixin(properties, {
				duration: 1500,
				text: dojo.byId(node).innerHTML
			}));
			currentAnimation.onEnd = function(){
			//	dojo.byId(node).style.marginTop = '0px';				
			}
			currentAnimation.play();
			
			bEffect = true;
		}
	}).play();

	return false;
}

function wipeOut(node)
{
	dojo.byId(node).style.marginTop = '0';
	var wipe = dojo.fx.wipeOut({node: node, duration: 1000});
	wipe.play();
	return false;
}

function openSubMenu(node, link)
{	
	if (bEffect == false || currentNode == node)
	{
		return false;
	}

	if (currentNode && currentNode != node)
	{
		resetMenu(link, currentNode);
	}

	currentNode = node;
	//resetMenu(link);
	wipeIn(node);

	dojo.addClass(link, 'highlightLink');
	return false;
}

var aLightBoxImg = new Array();

function init(){
	if (trim(dojo.byId("teaser-wrapper").innerHTML) && !dojo.byId('gallery'))
	{
		dojo.byId("content-wrapper").style.margin = '0 200px 0 260px';
		dojo.byId("teaser-wrapper").style.width = '150px';
	}

	initGallery();
/*
	dojo.require("dijit.dijit");
	dojo.require('dojo.fx');
	dojo.require("dojox.fx.scroll");
	dojo.require("dojo.NodeList-fx"),
	dojo.require("dojox.fx.style");
	dojo.require("dojox.image.LightboxNano");
	dojo.require("dojox.image.MagnifierLite");
	dojo.require("dojox.fx.text");
	dojo.require("dojo.fx.easing"); */

	dojo.query('#nav-wrapper ul ul', document).forEach('item.style.display = "none";');

	initCoverFlow();

	dojo.byId("body-wrapper").style.overflow = 'hidden'; 
	dojo.byId("content").style.overflow = 'hidden';
	document.body.style.overflow = 'hidden';

	neuresize();

	registerPartnerHover();
};

var aGalleryImg = new Array();

function initGallery()
{
	if (dojo.byId('gallery'))
	{
		dojo.query('.gallery-main img', dojo.byId('gallery')).forEach(
		    function(selectTag) {
			
			aGalleryImg[selectTag.src] = new Object();
			aGalleryImg[selectTag.src]['height'] = selectTag.height;
			aGalleryImg[selectTag.src]['width']  = selectTag.width;	
	
		 	dojo.connect(selectTag, 'onmouseover', 
				function(evt){
				//	var sNewImage = evt.target.src.replace(/thumb102/g, "thumb130");

					var props = {
						 width:  aGalleryImg[selectTag.src]['width'] + (15/aGalleryImg[selectTag.src]['width'] * 100), 
						 height: aGalleryImg[selectTag.src]['height'] + (15/aGalleryImg[selectTag.src]['height'] * 100),
						 marginLeft: -6
					};
		
					dojo.anim(selectTag, props, 175, null, null, 75).play();	
				}
			);

		 	dojo.connect(selectTag, 'onmouseout', 
				function(evt){
					var props = {
						width:  aGalleryImg[selectTag.src]['width'], 
						height: aGalleryImg[selectTag.src]['height'],
						marginLeft: 0
					};
		
					dojo.anim(selectTag, props, 175, null, null, 75).play();
				}
			);
		    }
		);
	}
}

function registerPartnerHover()
{
	var i = 0;

	dojo.query('#content .partner', document).forEach(
	    function(selectTag) {
	 	dojo.connect(selectTag, 'onmouseover', 'hoverPartner');
		selectTag.id = 'partner_' + i;
		i++;
	    }
	);

	dojo.connect(dojo.byId("content"), 'onmouseout', 'hoveroutPartner');
}

function hoverPartner(evt)
{
	var id = evt.target.id;

	if (id && dojo.byId(id))
	{
		dojo.query('#content .partner', document).forEach(
		    function(selectTag) {
			dojo.addClass(selectTag, 'dehighlightpartner');
		    }
		);
		dojo.removeClass(id, 'dehighlightpartner');
	}
}

function hoveroutPartner(evt)
{
	if (evt.relatedTarget && evt.relatedTarget.id && evt.relatedTarget.id != 'content')
	{
		dojo.query('#content .dehighlightpartner', document).forEach(
		    function(selectTag) {
			dojo.removeClass(selectTag, 'dehighlightpartner');
		    }
		);
	}
}

function neuresize()
{
	dojo.byId("music-player").style.top = (document.documentElement.clientHeight - 80) + 'px';
	dojo.byId("music-player").style.left = '10px';

	dojo.byId("meta-list").style.top = (document.documentElement.clientHeight - 80) + 'px';
	dojo.byId("meta-list").style.left = '-40px';
}

window.onresize = neuresize;

var currentNode = null;

function resetMenu(link, node)
{
	wipeOut(node);

	/*dojo.query('#nav-wrapper ul ul', document).forEach(
		    function(selectTag) {
		    	if (selectTag.id != currentNode)
		    	{
		    		wipeOut(selectTag);
		    	}
		    }
		);*/
	
	dojo.query('#nav-wrapper ul a span', document).forEach(
		    function(selectTag) {
		    	dojo.removeClass(selectTag, 'highlightLink');
		    }
		);
	
	dojo.query('span', link).forEach(
		    function(selectTag) {
		    	dojo.addClass(selectTag, 'highlightLink');
		    }
		);
}

var blubb;
var bSroll = false;

function scroll(node, mode)
{
	var objDiv = dojo.byId(node);

	var iPos = objDiv.scrollTop;

	if(mode == 'up')
	{
		iPos -= 5;
		if (iPos >= 0)
		{
			moveUp(node, iPos);
		}
	}
	else
	{
		iPos += 5;
		if (objDiv.offsetHeight >= iPos - 210)
		{
			moveDown(node, iPos);
		}
	}

	if(bSroll == true)
	{
		setTimeout('scroll("'+node+'", "'+mode+'")', 50);
	}

	return false;
}

var timer;

function moveUp(node, targetYPos)
{
    var y =  dojo.byId(node).scrollTop;

    if (y>targetYPos){
         dojo.byId(node).scrollTop -= 2;

        timer = setTimeout('moveUp("'+node+'", '+targetYPos+')', 50);
    }
    else clearTimeout(timer);
    return false;
}

function moveDown(node, targetYPos)
{
    var y =  dojo.byId(node).scrollTop;

    if (y<targetYPos){
         dojo.byId(node).scrollTop += 2;

        timer = setTimeout('moveDown("'+node+'", '+targetYPos+')', 50);
    }
    else clearTimeout(timer);
    return false;
}

var test;

var handles 	   = new Array();
var currentPicture = 0;
var bSlideShowLink = false;

function Lesezeichen(){

  if (window.sidebar)
    {
    // firefox
    window.sidebar.addPanel(Text,URL, "");
    }
  else if(window.opera && window.print)
    {
    // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',URL);
    elem.setAttribute('title',Text);
    elem.setAttribute('rel','sidebar');
    elem.click();
    }
  else if(document.all)
    {
    // ie
    window.external.AddFavorite(URL,Text);
    }
}


var sLastPage;

function updateContent(page, LinkText, link, bMainMenu)
{
	if(dojo.byId("imgdiv"))
	{
		dojo.byId("imgdiv").style.display = 'none';
	}

	if (currentNode && dojo.byId(currentNode).style.display != 'none' && bMainMenu == 1)
	{
		wipeOut(currentNode);
		currentNode = null;
	}
	
	dojo.query('#nav-wrapper li ul a', document).forEach(
		    function(selectTag) {
		    	selectTag.style.color = '#929292';
		    }
		);
	
	dojo.query('#nav-wrapper a span', document).forEach(
		    function(selectTag) {
		    	dojo.removeClass(selectTag, 'highlightLink');
		    }
		);
	
	dojo.query('span', link).forEach(
		    function(selectTag) {
		    	dojo.addClass(selectTag, 'highlightLink');
		    }
		);
	
	if (sLastPage != page)
	{		
		dojo.byId("content").style.overflow = 'hidden';
  	  	sLastPage =page;

		var aUrlParts = page.split('/');
		var sPage = aUrlParts[3];

		dojo.xhrPost( { // ➂
	    // The following URL must match that used to test the server.
			url: page, 
			handleAs: "text",

			timeout: 5000, // Time in milliseconds
		
		    // The LOAD function will be called on a successful response.
		    load: function(response, ioArgs) { // ➃

			//dojo.byId("body-wrapper").style.backgroundImage = '';
		
			if (dojo.byId("slideshow_img"))
			{
				dojo.byId("slideshow_img").src = '';
			}

			if (dojo.byId("scroll-top"))
			{
				dojo.byId("scroll-top").style.visibility    = 'hidden'; 
			}

			if (dojo.byId("scroll-bottom"))	
			{
				dojo.byId("scroll-bottom").style.visibility = 'hidden';
			}

			dojo.byId("content-wrapper").style.visibility = 'visible'; 
			dojo.byId("teaser-wrapper").style.visibility = 'visible'; 
			dojo.byId("content-wrapper").style.borderWidth = '1px';
		
			currentPicture = 0;
			dojo.byId('content').scrollTop = 0;
		
			if(dojo.byId("slideshow_link"))
			{
				dojo.byId("slideshow_link").style.visibility = 'hidden';
			}
		
			dojo.byId("left-wrapper").style.backgroundImage = '';

			if (handles[0] != null)
			{
				dojo.disconnect(handles[0]);
				dojo.disconnect(handles[1]);
				dojo.disconnect(handles[2]);
				dojo.disconnect(handles[3]);

				handles[0] = null;
				handles[1] = null;
				handles[2] = null;
				handles[3] = null;
			}
		
			response = response.split('<div id="main-wrapper">');
			response = response[1];
		
			var teaser = response.split('<div id="nav-wrapper">');
			teaser = teaser[0];
			teaser = teaser.split('<div id="teaser-wrapper">');
			teaser = teaser[1];
			teaser = teaser.substr(0, teaser.length - 10);

			if (trim(teaser))
			{
				dojo.byId("content-wrapper").style.margin = '0 200px 0 260px';
				dojo.byId("teaser-wrapper").style.width = '150px';
			}
			else
			{
				
				dojo.byId("content-wrapper").style.margin = '0 0 0 260px';
				dojo.byId("teaser-wrapper").style.width = '50px';
			}

			response = response.split('<div id="teaser-wrapper">');
			response = response[0];
		
			response = response.split('<div id="content-wrapper">');
			response =response[1].split('<div id="content">');
			response =response[1].split('<a id="scroll-bottom"');
			main = response[0].substr(0, response[0].length - 12);
/*	
			dojo.fadeOut({ 
					node:"content", 
					onEnd: function() {
						dojo.byId("content").innerHTML = main;
						dojo.fadeIn({ node:"content" }).play();	
					} 
			}).play();*/
		
			if (dojo.byId("content").innerHTML == '')
			{
				dojo.byId("content").innerHTML = '&nbsp;';
			}

			var properties = {
				node: "content",
				words: true
			};
				
			if (aUrlParts[2] == 'print' || aUrlParts[2] == 'multimedia' || dojo.byId("content").innerHTML == '' || dojo.byId("gallery"))
			{
				dojo.byId("content").innerHTML = '&nbsp;';
			}

			main = main + '&nbsp;';
			
			properties.onEnd = function(){
				currentAnimation = dojox.fx.text.blockFadeIn(dojo.mixin(properties, {
					onEnd: function (){
						registerPartnerHover();

						var instanceOne = new ImageFlow();
						instanceOne.init({ ImageFlowID:'myImageFlow' });
						initCoverFlow();
						initGallery();
	
						if (sPage == 'impressum')
						{
							if (dojo.byId("scroll-top"))
							{
								dojo.byId("scroll-top").style.visibility    = 'visible'; 
							}

							if (dojo.byId("scroll-bottom"))
							{
								dojo.byId("scroll-bottom").style.visibility = 'visible'; 
							}
						}
					},
					text: main
				}));
				currentAnimation.play();
			};
			currentAnimation2 = dojox.fx.text.blockFadeOut(properties);
			/*currentAnimation2.onEnd = function(){
			
				initGallery();
				
			};*/
			currentAnimation2.play();


			dojo.fadeOut({ 
				node:"teaser-wrapper", 
				onEnd: function() {
					dojo.byId("teaser-wrapper").innerHTML = teaser;
					dojo.fadeIn({ node:"teaser-wrapper" }).play();	

		/*	dojo.byId("teaser-wrapper").innerHTML = teaser;
					dojo.fadeIn({ node:"teaser-wrapper" 
						onEnd: function (){
							
							initGallery();
							
						}
					}).play();	*/	

				} 
			}).play();

			initGallery();

			return response;
		    },

		    // The ERROR function will be called in an error case.
		    error: function(response, ioArgs) { // ➃
		      console.error("HTTP status code: ", ioArgs.xhr.status); // ➆
		      return response; // ➅
		      }
	    });
	}
}

function leer()
{
	
}

function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}


function initCoverFlow()
{	
	dojo.query('.imageflow img', document).forEach(
		    function(node) {
			if(node.id)
			{
			    	var w = new dojox.image.LightboxNano({
						href: node.src
					}, node.id+'2');
					aLightBoxImg[node.id] = w; 
				}
		    }
	);	
}

var lastslide;

function slideshow(pictures, current)
{
	if (lastslide && lastslide != current && current)
	{
		dojo.query('span', lastslide).forEach(
			    function(selectTag) {
			    	dojo.removeClass(selectTag, 'highlightLink');
			    }
			);
	}

	if (current)
	{
		lastslide = current;
	}	

	dojo.byId("teaser-wrapper").innerHTML = '';
	dojo.byId("content").innerHTML = '';

	if (dojo.byId("scroll-top"))
	{
		dojo.byId("scroll-top").style.visibility    = 'hidden'; 
	}

	if (dojo.byId("scroll-bottom"))
	{
		dojo.byId("scroll-bottom").style.visibility = 'hidden'; 
	}

	var aPictures = pictures.split(',');

	var img = '/root/img/pool' + aPictures[currentPicture];

	if (!dojo.byId("imgdiv"))
	{
		var oImgdiv = document.createElement("div");
		var oImg = document.createElement("img");
		oImg.id  = 'slideshow_img';

		oImgdiv.id = 'imgdiv';
		oImgdiv.style.zIndex = 400;
		oImgdiv.appendChild(oImg);

		dojo.byId("body-wrapper").appendChild(oImgdiv);
	}
	else
	{
		dojo.byId("imgdiv").style.display = 'block';
	}

	dojo.fadeOut({ 
		node:"imgdiv", 
		onEnd: function() {
		//	dojo.byId("body-wrapper").style.backgroundImage    = 'url(' + img + ')';
		//	dojo.byId("body-wrapper").style.backgroundRepeat   = 'no-repeat';
	//		dojo.byId("body-wrapper").style.backgroundPosition = 'right';

			dojo.byId("slideshow_img").src = img;
			dojo.fadeIn({ node:"imgdiv",
				onEnd: function() {
					if (handles[0] == null)
					{
						handles[0] = dojo.connect(dojo.byId("left-wrapper"), 'onmouseover', 'hoverSlideShowLeft');
						handles[1] = dojo.connect(dojo.byId("nav-wrapper"), 'onmouseover', 'hoverSlideShowLeft');
						handles[2] = dojo.connect(dojo.byId("left-wrapper"), 'onmouseout', 'outhoverSlideShowLeft');
						handles[3] = dojo.connect(dojo.byId("nav-wrapper"), 'onmouseout', 'outhoverSlideShowLeft');
					}
					//dojo.byId("slideshow_img").height = '605';

					if (dojo.byId("slideshow_img").width < 600)
					{
						dojo.disconnect(handles[0]);
						dojo.disconnect(handles[1]);
						dojo.disconnect(handles[2]);
						dojo.disconnect(handles[3]);

						//dojo.byId("slideshow_img").width = '408';

						handles[0] = null;
					}
					else
					{
						//dojo.byId("slideshow_img").width = '880';
					}
			}}).play();	

			dojo.byId("content-wrapper").style.visibility = 'hidden'; 
			dojo.byId("teaser-wrapper").style.visibility = 'hidden'; 
			dojo.byId("content-wrapper").style.borderWidth = '0px';
				
			if(bSlideShowLink == false)
			{
				var a = document.createElement("a");
				var span = document.createElement("span");

				span.innerHTML = 'Weiter';

				a.href 	  = 'javascript:void(0); nextSlideShowPicture("' + pictures + '");';
				a.id = 'slideshow_link';
		
				a.appendChild(span);
				dojo.byId("meta-wrapper-list").appendChild(a);
				bSlideShowLink = true;
			}
			else
			{
				if(dojo.byId("slideshow_link"))
				{
					dojo.byId("slideshow_link").href = 'javascript:void(0); nextSlideShowPicture("' + pictures + '");';
				}
			}
			dojo.byId("slideshow_link").style.visibility = 'visible';
		} 
	}).play();

	if (current)
	{
		dojo.query('span', current).forEach(
		    function(selectTag) {
		    	dojo.addClass(selectTag, 'highlightLink');
		    }
		);
	}
}

function nextSlideShowPicture(pictures)
{
	var aPictures = pictures.split(',');
	currentPicture++;
	
	if(aPictures.length <= currentPicture)
	{
		currentPicture = 0;
	}
	slideshow(pictures);
}

function hoverSlideShowLeft()
{
	dojo.byId("left-wrapper").style.backgroundImage  = 'url(/root/img/Verlauf.png)';
	dojo.byId("left-wrapper").style.backgroundRepeat = 'no-repeat';
}

function outhoverSlideShowLeft()
{
	dojo.byId("left-wrapper").style.backgroundImage = '';
}

function showMediumImg(id)
{	
	hideMediumImg();
	
	dojo.byId(id).style.display = 'block';
}


function hideMediumImg(id)
{	
	dojo.query('.img_medium', document).forEach(
	    function(selectTag, id) {
	    	selectTag.style.display = 'none';
	    }
	);
}

function raiseEvent (eventType, elementID)  
{   
	var o = document.getElementById(elementID);   
	if (document.createEvent) {   
   		var evt = document.createEvent("Events");   
   		evt.initEvent(eventType, true, true);   
  		o.dispatchEvent(evt);   
	}   
	else if (document.createEventObject)   
	{  
	   var evt = document.createEventObject();   
	   o.fireEvent('on' + eventType, evt);   
	}   
	o = null;  
}   

function showBigImg(alt)
{
	raiseEvent('click', alt + '2');
	
	window.setTimeout("initMagnifierLite()", 1000);
}

function initMagnifierLite()
{
	dojo.query('body > img', document).forEach(
		function(node) {
			if (!node.id)
			{
				new dojox.image.MagnifierLite({ scale:2.2, glassSize:130 }, dojo.byId(node));
			}
	});
}
