//new Control.Slider('scrollbar-handle','scrollbar-track');
//document.observe('dom:loaded', function () { new Lightbox(); });
if(navigator.userAgent.indexOf('Gecko'))  {
   window.onload = function(){ init(); }
}  else  {
   document.observe('dom:loaded', function () { init() });
}

var scrollMax = 0;

Position.getWindowSize = function(w) {
   var width, height;
   w = w ? w : window;
   width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
   height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
   return { width: width, height: height };
}

function optimizeForMobileDevices()  {
   
	new Lightbox();
   
}


function optimizeForStandardDevices()  {

   $$('a.video').each(function(anchor,index){
		anchor.setAttribute('href','javascript:showMovie();');   
   });
   
   
   optimizeScrolling();
   
   new Lightbox();
   
   
   
}

function init()   {
   
   if(navigator.userAgent.indexOf('iPhone') != -1)   {
      
      optimizeForMobileDevices();
      
   }  else  {
   
      optimizeForStandardDevices();
      
   }
   
}

function optimizeScrolling()	{
	
	var content_height = 600;

	   $('inhalt').setStyle({position:'relative',overflow:'hidden',height:content_height+'px'});
	   $('inhalt-mask').setStyle({position:'absolute',width:'811px',height:'auto'});

	   //center content vertically
	   var content_top = Math.ceil((Position.getWindowSize().height - content_height)/2);
	   content_top = content_top > 10 ? content_top : 10;
	   $('screen').setStyle({marginTop:content_top+'px'});
	var glyphs = $$('div#inhalt div.glyph');
	   scrollMax = parseInt(glyphs[glyphs.length-1].cumulativeOffset()[1]);
	   scrollMax += parseInt(glyphs[glyphs.length-1].getStyle('height'));
	   scrollMax -= parseInt($('inhalt').getStyle('height')); //600
	   scrollMax -= parseInt($('inhalt').cumulativeOffset()[1]); //65;

	   if(scrollMax > 0) {
	      $('scrollbar').setStyle({display:'block'});
	      $('screen').setStyle({width:parseInt($('screen').getStyle('width'))+13+'px'});
	      $('inhalt').setStyle('padding-right','0px');

	      var step = 0.005; 

	      Event.observe(document, "mousewheel", function(event){
	         if(Event.wheel(event)>=1){
	            //scroller.setValueBy(-0.025);
	            scroller.setValueBy(-step);
	         }  else if(Event.wheel(event)<0){
	            //scroller.setValueBy(0.025);
	            scroller.setValueBy(step);
	         }
	      }, false);
	      Event.observe(document, "DOMMouseScroll", function(event){
	          if(Event.wheel(event)>=1){
	            //scroller.setValueBy(-0.04);
	            scroller.setValueBy(-step);
	         }  else if(Event.wheel(event)<0){
	            //scroller.setValueBy(0.04);
	            scroller.setValueBy(step);
	         }  
	      }, false); // Firefox


	      var scroller = new Control.Slider(
	      $('scrollbar-handle'),
	      $('scrollbar-track'),
	      {
	         axis:'vertical',
	         sliderValue:0,
	         onSlide:function(value)  {
	            var scrollY = Math.floor( value * scrollMax );
	            //alert(scrollMax);
	            $('inhalt-mask').setStyle({top:'-'+scrollY+'px'});
	         },
	         onChange:function(value)  {
	            var scrollY = Math.floor( value * scrollMax ); 
	            $('inhalt-mask').setStyle({top:'-'+scrollY+'px'});
	         }
	      });

	      $('scroll-up').observe('mousedown',function(event){
	           //scroller.setValueBy(-0.05);
	           scroller.setValueBy(-0.01);
	      });
	      $('scroll-down').observe('mousedown',function(event){
	           //scroller.setValueBy(0.05);
	           scroller.setValueBy(0.01);
	      });

	   }
}


function hideScrollbars()  {
   $('inhalt').setStyle('overflow','hidden');  
}

function showMovie() {
   var pageSize = getPageSize();
   $('moverlay').setStyle('top:0;left:0;width:100%;height:'+pageSize[1]+'px;');
   $('moverlay').observe('click', (function() { 
      hideMovie();     
   }));
   $('movie').setStyle('z-index:100;top:'+Math.floor((pageSize[1]-344) / 2)+'px;left:'+Math.floor((pageSize[0]-425)/2)+'px;opacity:1;');
   //alert($('movie').getStyle('top'));
   /*onYouTubePlayerReady('myytplayer') = function() {
      $('myytplayer').playVideo():Void;
   }*/
   playMovie();
}

var MOVIELOADED = false;
function playMovie() {
   
   if(MOVIELOADED == true
   && playMovie.arguments.length > 0
   &&  playMovie.arguments[0] == false
   )  {
      $('myytplayer').pauseVideo();
   }  else if(MOVIELOADED == true)  {
      $('myytplayer').playVideo();
   }
}

function onYouTubePlayerReady(playerId) {
      
      MOVIELOADED = true;
}


function getPageSize() {
	        
	  var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}

function hideMovie() {
     $('moverlay').setStyle('position:absolute;z-index:100;left:-1000px;top:150px;width:425px;height:344px;overflow:hidden;background:rgb(0,0,0);opacity:0.8;');
     $('movie').setStyle('position:absolute;z-index:100;left:-1000px;top:150px;width:425px;height:344px;overflow:hidden;');
     playMovie(false);
}

Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			//delta = event.wheelDelta/120;
         delta = event.wheelDelta/120;
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3;	}
		return Math.round(delta); //Safari Round
	}
});


/*******/


