window.addEvent('domready', function() {
	initBackground();
	if($$('img.rotatorimage').length > 0){
		initSmallRotator();
	}
	if($$('div.tabbed').length > 0) {
		initTabs();
	}
	
	$$('div.stageoverview div.lineupitem').each(function(elm) {
		elm.setStyle('cursor','pointer');
		elm.addEvent('click', function() {
			link = $(this).getElement('a');
			window.location = link;
		});
		
	});
});

function initBackground() {
	var containerCoords = $('container').getCoordinates();
	$('flash').setStyles({
		height: containerCoords.height,
		top: containerCoords.top
	})
	flashVars = $('flash').get('rel');
	if(Browser.ie) {
		var sFlashHtml = '<object height="100%" width="100%" type="application/x-shockwave-flash" data="/application/assets/default/swf/background_new.swf"><param name="quality" value="high"><param name="allowScriptAccess" value="always"><param name="wMode" value="transparent"><param name="movie" value="/application/assets/default/swf/background_new.swf"><param name="FlashVars" value="currentPage='+flashVars+'"><embed src="/application/assets/default/swf/background_new.swf" wmMode="transparent" width="100%" height="100%"></embed></object>';
		$('flash').set('html',sFlashHtml);
	} else {
		var obj = new Swiff('/application/assets/default/swf/background_new.swf', {
	    id: 'background',
	    width: '100%',
	    height: '100%',
	    params: {
	        wMode: 'transparent',
	        bgcolor: '#ffffff'
	    },
	    vars: {
	    		currentPage: flashVars
	    }
		});
		obj.inject($('flash'));
	}
}

function initTabs() {
	$$('div.tabbed').each(function(elm){
		elm.setStyles({
			'overflow':'hidden',
			'height':'24px'
		});
		
		elm.set('tween', {
			    duration: 'short',
			    property: 'height'
			});
		
		elm.addEvent('click', function(e) {
			if(e != null) {
				e.preventDefault();
			}
			if(!Browser.ie) {
				$$('div.tabbed object').each(function(elm){
					elm.addEvent('click', function(e) {
						if(e!= null) {
							e.stopPropagation();
						}
					});
				});
			}
			elmTween = $(this).get('tween');
			if($(this).hasClass('activetab')) {
				elmTween.start('24px');
				$(this).set('id','');
				$(this).removeClass('activetab');
			} else {
				if($('activetab') != null) {
					elmTween2 = $('activetab').get('tween');
					$('activetab').removeClass('activetab');
					$('activetab').set('id','');
					elmTween2.start('20px');
				}
				$(this).set('id','activetab');
				$(this).addClass('activetab');
				scrollSizes = $(this).getScrollSize();
				elmTween.start(scrollSizes.y);
				(function(){var myFx = new Fx.Scroll(window).toElement($(this))}).delay(600, this);
			}

		});
	});
}

function initSmallRotator() {
  var images = $$('img.rotatorimage');
  var currentImageNr = 0;
  var maxImage = images.length-1;
  var newImageNr = 0;
  var timer = null;
  
  currentImage = images[currentImageNr];
  currentImage.setStyle('visibility','visible');
  relValue = currentImage.get('rel');
  splitValue = relValue.split('|');
  if(splitValue[0] != null) {
  	relValue = splitValue[0];
  }
  
  if($('sliderText') != null && relValue != null) {
    $('sliderText').set('html'
    , relValue);
  }
  
  if(splitValue[1] != null) {
	  if($('sliderStageText') != null && relValue != null) {
	    $('sliderStageText').set('html'
	    , splitValue[1]);
	  }
	  $('slider').addEvent('click',function(){
	  	window.location = 'http://www.stereosunday.nl/lineup';
	  });
	  $('slider').setStyle('cursor','pointer');
  }
  
  if(splitValue[2] != null) {
  	$('slider').removeEvents('click');
  	$('slider').addEvent('click',function(){
	  	window.location = 'http://www.stereosunday.nl'+splitValue[2];
	  });
  }
  
  previousButton = $('controls').getElement('a.previous');
  nextButton = $('controls').getElement('a.next');
  
  if(images.length > 1) {
    
    previousButton.addEvent('click', function(e) {
      if(e != null) {
        e.preventDefault();
        e.stopPropagation();
      }
      if(images.indexOf(currentImage) == 0) {
        newImageNr = maxImage;
      } else {
        newImageNr--;
      }
      
      if(timer != null) {
      	clearInterval(timer);
      }
      
      var newImage = images[newImageNr];
      switchImage(images[newImageNr], newImageNr, currentImage, currentImageNr);
      currentImage = newImage;
      currentImageNr = newImageNr;
    });
    
    nextButton.addEvent('click', function(e) {
      if(e != null) {
        e.preventDefault();
        e.stopPropagation();
      }
      if(images.indexOf(currentImage) == maxImage) {
        newImageNr = 0;
      } else {
        newImageNr++;
      }
      
      if(timer != null && e != null) {
      	clearInterval(timer);
      }

      var newImage = images[newImageNr];
      switchImage(images[newImageNr], newImageNr, currentImage, currentImageNr);
      currentImage = newImage;
      currentImageNr = newImageNr;
    });
    
    timer = (function(){  
			nextButton = $('controls').getElement('a.next');
  		nextButton.fireEvent('click');
 		}).periodical(3000);	
    
  } else {
    previousButton.setStyle('display','none');
    nextButton.setStyle('display','none');
  }
}

function switchImage(newImage, newImageNr, currentImage, currentImageNr) {
  
  
  newImage.setStyle('z-index',91);
  
  if(Browser.ie) {
  	newImage.setStyle('visibility','visible');
	  currentImage.setStyle('z-index', currentImageNr);
	  currentImage.setStyle('visibility', 'hidden');
	  
	  relValue = newImage.get('rel');
	  newImage.setStyle('z-index',90);
	  
	  if($('sliderText') != null && relValue != null) {
	    $('sliderText').set('html', relValue);
	  }
  } else {
  	newImage.setStyle('opacity','0');
  	newImage.setStyle('visibility','visible');
	  var myEffects = new Fx.Tween(newImage, {
	  	duration: 500, 
	  	transition: Fx.Transitions.Sine.easeOut,
	  	onComplete: function(elm) {
	  		newImage.setStyle('visibility','visible');
			  currentImage.setStyle('z-index', currentImageNr);
			  currentImage.setStyle('visibility', 'hidden');
			  
			  relValue = newImage.get('rel');
			  newImage.setStyle('z-index',90);
			  
			  splitValue = relValue.split('|');
			  if(splitValue[0] != null) {
			  	relValue = splitValue[0];
			  }
			  
			  if($('sliderText') != null && relValue != null) {
			    $('sliderText').set('html'
			    , relValue);
			  }
			  
			  if(splitValue[1] != null) {
				  if($('sliderStageText') != null && relValue != null) {
				    $('sliderStageText').set('html'
				    , splitValue[1]);
				  }
			  }
			  
				if(splitValue[2] != null) {
			  	$('slider').removeEvents('click');
			  	$('slider').addEvent('click',function(){
				  	window.location = 'http://www.stereosunday.nl'+splitValue[2];
				  });
			  }
			  
	  	}
	  });
	  	
	  myEffects.start('opacity',0,1);
  }
}

