﻿// Avoid framing
if(top!=self) top.location=self.location;

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

// Slide effect (MooTools)
function sbgClass() {
	var myScroll;
	var imgIntro;
	var imageWidth = 750;
	var stagediv;
  var menudiv;
	var content;
	var from = 0;

	if ((document.location).toString().indexOf('/news') > 0) from = 1;
	if ((document.location).toString().indexOf('/standorte') > 0) from = 2;
	if ((document.location).toString().indexOf('/born-to-wake') > 0) from = 3;
	if ((document.location).toString().indexOf('/shop') > 0) from = 4;
	if ((document.location).toString().indexOf('/angebot') > 0) from = 5;
	if ((document.location).toString().indexOf('/ueber-uns') > 0) from = 6;

	this.afterLoad = function(){
		stagediv = document.getElementById('largestage');
		content = document.getElementById('content');
		imageWidth = imgIntro.width;
		stagediv.style.background = 'url('+imgIntro.src+') #ffffff no-repeat';
		stagediv.style.width = (imageWidth + 1500).toString()+'px';
		content.style.marginLeft = imageWidth.toString()+'px';

		myScroll = new Fx.Scroll('stage', {wait: true, duration: ((imageWidth / 2) + 500)});
		myScroll.scrollTo(imageWidth,0);
	}

	window.addEvent('load', function(){
	  if (querySt('from')==undefined) {
        var coverElem = document.getElementById('cover');
        var coverFade = new Fx.Style(coverElem,"width", {wait: false, duration: 1000}); 
        coverFade.addEvent('onComplete', function() { afterIntro2(1); });
        coverFade.start(window.document.body.clientWidth,0);
    }
    else
    {
      var coverElem = document.getElementById('cover');
      coverElem.width = '0px';
      var frameElem = document.getElementById('frame');
      frameElem.style.height = '387px';
      var togglerElem = document.getElementById('toggler');
      if (togglerElem)
        togglerElem.style.visibility = 'visible';
      afterIntro2(0);
		}
	}); 
  
	this.scrollOut = function(newSite){
    myScroll.addEvent('onComplete', function() { 
      window.location.href=newSite+'?from='+from;			
    });
    myScroll.options.duration = 500;
    myScroll.scrollTo((imageWidth + 1500),0);
  }

  this.afterIntro = function(intro){
    var menuDiv = document.getElementById('menu');
    if (intro==1) {
      var frameElem = document.getElementById('frame');
      var frameFade = new Fx.Style(frameElem,"height", {wait: false, duration: 1000, transition: Fx.Transitions.Bounce.easeOut}); 
      frameFade.addEvent('onComplete', function() { afterIntro2(2); });
      frameFade.start(0,387);
    }
    else if (intro==2) {
      var buttonMover = new Array(6);
      var width = window.document.body.clientWidth;
      var spacer = 48;
      var imageWidth = new Array(36,72,98,58,33,63);
      var currentPosition = Math.floor(width / 2)-Math.floor((imageWidth[0]+spacer+imageWidth[1]+spacer+imageWidth[2]+spacer+imageWidth[3]+spacer+imageWidth[4]+spacer+imageWidth[5]) / 2);
      for (i = 0; i < 6; i++) {
        var newdiv = document.createElement('div');
        var divIdName = 'introdiv'+i.toString();
        newdiv.setAttribute('id',divIdName);
        newdiv.style.cssText = 'position:absolute; z-index:9; left:0px; margin-left:-10px; top:5px;';
        menuDiv.parentNode.appendChild(newdiv);
        var newimg = document.createElement('img');
        newimg.src = "/assets/templates/wba/images/menu/but.gif";
        newdiv.appendChild(newimg);
        if (i < 5) {
          buttonMover[i] = new Fx.Style(newdiv,"margin-left", {wait: false, duration: 1000}); 
          buttonMover[i].start(width+(2000*i),currentPosition+Math.floor(imageWidth[i] / 2) - 4);
        } else {
          buttonMover[5] = new Fx.Style(newdiv,"margin-left", {wait: false, duration: 1000}); 
          buttonMover[5].addEvent('onComplete', function() { afterIntro2(3); });
          buttonMover[5].start(width+(2000*i),currentPosition+Math.floor(imageWidth[i] / 2) - 4);
        }
        
        currentPosition = (currentPosition+imageWidth[i]+spacer);
        
      }
    } 
    else if (intro==3) {
      var togglerElem = document.getElementById('toggler');
      if (togglerElem)
        togglerElem.style.visibility = 'visible';
      for (i = 0; i < menuDiv.firstChild.childNodes.length; i++) {
        var menuCol = menuDiv.firstChild.childNodes[i];
        var imgBut = menuCol.firstChild.firstChild;
        var imgText = menuCol.firstChild.nextSibling.firstChild; // .firstChild;
        var menuFade = new Fx.Style(imgText,"opacity", {wait: false, duration: 500}); 
        if ((i+1) == menuDiv.firstChild.childNodes.length)
        {
          menuFade.addEvent('onComplete', function() { afterIntro2(0); });
        }
        menuFade.set(0);
        menuFade.start(1);
      }
    } 
    else
    {
      for (i = 0; i < menuDiv.firstChild.childNodes.length; i++) {
        var newdiv = document.getElementById('introdiv'+i.toString());
        if (newdiv) newdiv.style.visibility = 'hidden';
        var menuCol = menuDiv.firstChild.childNodes[i];
        var imgBut = menuCol.firstChild.firstChild;
        var imgText = menuCol.firstChild.nextSibling.firstChild; 
        
        imgBut.style.visibility = 'visible';
        var menuFade = new Fx.Style(imgText,"opacity", {wait: false, duration: 0}); 
        menuFade.start(1);
      }
      
      if (querySt('from') != from) {
        imgIntro = new Image();
        imgIntro.onload = function() { slideIn(); };
        imgIntro.src = 'assets/templates/wba/images/intro_'+from+'.jpg';
      }	
      else
      {
		  	stagediv = document.getElementById('largestage');
	  		stagediv.style.width = '2250px';
  			content = document.getElementById('content');
		  	content.style.marginLeft = '750px';
		  	myScroll = new Fx.Scroll('stage', {wait: true, duration: 500});
	  		myScroll.scrollTo(750,0);
  		}
    }
                        
		
	}
}

sbgObject = new sbgClass();

function afterIntro2(intro) {
	sbgObject.afterIntro(intro);
}

function slideIn() {
	sbgObject.afterLoad();
}

function slideOut(newSite) {
	sbgObject.scrollOut(newSite);
}

// Menu image mouseover effect
function mo(e, over_out,pageid,wrap) {
  if(wrap.className != "on") {
    var image = document.getElementById("menu"+pageid);
	  var src = image.src, ext = src.substring(src.lastIndexOf('.'),src.length);
	  image.src= (over_out == 1 ? src.replace(ext,'_on' + ext) : src.replace('_on' + ext,ext));
	  
	  var butimage = document.getElementById("but"+pageid);
		if (butimage!=null) {
		  var butsrc = butimage.src, ext = butsrc.substring(butsrc.lastIndexOf('.'),butsrc.length);
		  butimage.src= (over_out == 1 ? butsrc.replace(ext,'_on' + ext) : butsrc.replace('_on' + ext,ext));
		}
	}
	return;
}

var menuTimer;
var openMenus = new Array();
var currentMenuIndex;

// Menu image mouseover effect
function moc(e, over_out,pageid,wrap) {
  if (!e) var e = window.event;
	var target = e.relatedTarget || e.toElement || false;
	var source = e.srcElement || e.target || false;

  var elem = wrap;
  while (elem.className != 'event level1')
		elem.parentNode;

  if(elem.className != "on") {
    if (over_out == 1) {
      for (ci = 0; ci < elem.childNodes.length; ci++) {
        if (elem.childNodes[ci].className == 'event submenu-wrap') {
          var subMenu = elem.childNodes[ci];
		     	if (subMenu.style.visibility != "visible") {
            for(i = 0; i < openMenus.length; i++) {
              openMenus[i].style.visibility = 'hidden';
            }
            if (menuTimer) clearTimeout(menuTimer);
		    
            var arrChilds = subMenu.childNodes;
	          var nChildHeight = subMenu.firstChild.clientHeight;
            subMenu.style.height = (arrChilds.length*nChildHeight+14).toString()+"px"; 
    
            if ((arrChilds.length*nChildHeight+9+51) > 145) {
              var menuDiv = document.getElementById("menu");
              menuDiv.style.height = (arrChilds.length*nChildHeight+9+51).toString()+'px';
            }
          
 		        for(i = 0; i < arrChilds.length; i++) {
  	          arrChilds[i].style.marginTop = "-"+nChildHeight.toString()+"px";
		        }
		    
 		        subMenu.style.visibility = "visible";
 		          
            for(i = 0; i < arrChilds.length; i++) {
  	          menuSlide = new Fx.Style(arrChilds[i],'margin-top', {wait: false, duration: 100+(50*i)});
              menuSlide.start(nChildHeight*(-1),(nChildHeight*i+9));
    	      }
          
            openMenus.push(subMenu);
		      
            currentMenuIndex = openMenus.length;
            menuTimer = setTimeout('hideMenu('+currentMenuIndex+')',5000);
		      }
		      else
		      {
		        if (menuTimer) {
		          clearTimeout(menuTimer);
		          menuTimer = setTimeout('hideMenu('+currentMenuIndex+')',5000);
		        }
		      }
		    }
 		  }
 	  }
	}
	return;
}

function hideMenu(index) {
  if (openMenus) {
    openMenus[index-1].style.visibility = 'hidden';
    openMenus.pop();
  }
}

// Gallery mouseover effect
function galleryZoom(wo) {
	if(wo.firstChild.src) {
		var image = wo.firstChild;
		var src = image.src, ext = src.substring(src.lastIndexOf('.'),src.length);
		document.getElementById("zoom").src = src.replace('_small' + ext,ext);
	}
}

//	Manipulate links
window.addEvent('load', function(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for(var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		/*anchor.tabindex = i;*/
		if (anchor.getAttribute("href")) {
			anchor.onfocus = function() {
				this.blur();
			}
			if (anchor.getAttribute("rel") == "ext") {
				anchor.target = "_blank";
				if (document.getElementsByTagName("html")[0].lang == "en") {
					if (!anchor.title) anchor.title = "open in new window"
					else anchor.title += " (new window)";					
				}else {
					if (!anchor.title) anchor.title = "in neuem Fenster öffnen"
					else anchor.title += " (neues Fenster)";
				}
			}
		}
	}
});


