window.addEvent('load',function() {
	
	Element.implement( 
  { 
      removeStyle: function(ele) 
      { 
          regex  = new RegExp('[~;\\s]' + ele + '.*?[;~]', "i"); 
          style = this.get('style')+';'; 
          this.set('style', style.replace(regex, '')); 
      } 
  }); 
	
	if($('flashhome')) {
        
		
		
		$('homeSlider').removeClass('hide');
		
		if(Browser.ie7) {
  		var myFxSlider = new Fx.Tween($('homeSlider'), {
  		   duration: 2500,
  		   property: 'opacity'
  		});
  		myFxSlider.start('0', '1');
  	} 
		
		
		if ($('snow'))
		{
			var aSnow = new mooSnow({container: $("snow")});
		}
		
		var itemsContainer = $('sliderItems');
		var allImages = itemsContainer.getElements('div.item');
		var maxImages = allImages.length;
		var currentImage = 1;
		var periodical = null;
		var oldActive = null;
		var allowClick = true;
		
		
		var loadingBar = $('loadingBar'+currentImage);
		
		var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		if (version == 7) {
		  $('flashhome').setStyle('position', 'relative');
		  $('flashhome').setStyle('position', 'absolute');
		}
		
		//Blokjes geheel linken
		$$('.sliderBlock').addEvent("click",function(elm){
			var location = this.get('rel');
			window.location = location;
		});
		
		if(maxImages > 1) {
		
			var myFxLoading = new Fx.Tween(loadingBar, {
			   duration: 12000,
			   property: 'margin-left',
			   onComplete: function() {
				   $$('#sliderItems .loadingBar').each(function(el) {
						el.setStyle('margin-left', '-1200px');				   
					});
			   }.bind(loadingBar)
			});
			myFxLoading.start('-1200px', '0'); 
			
			$('sliderNext').addEvent('click', function(e) {
				if (e) e.preventDefault();
				if(allowClick == true) {
					slide('next');
					
					clearInterval(periodical);
					periodical = slide.periodical(12500, null, 'next');
				}
			});
			
			$('sliderPrevious').addEvent('click', function(e) {
				if (e) e.preventDefault();
				if(allowClick == true) {
					slide('previous');
					
					clearInterval(periodical);
					periodical = slide.periodical(12500, null, 'next');
				}
			});
		
			periodical = slide.periodical(12500, null, 'next');
		
		} else {
			$('sliderNext').addClass('hide');
			$('sliderPrevious').addClass('hide');
		}
	}
	
	function slide(direction) {
		
		allowClick = false;
		
		//$$('#sliderItems .loadingBar').each(function(el) {
			 
		//});
				
		var oldActive = $('item'+currentImage);
		
		if(direction == 'next') {
			if (currentImage < maxImages) {
				currentImage++;
			}
			else {
				currentImage = 1;
			}
		} else {
			if (currentImage == 1) {
				currentImage = maxImages;			
			}
			else {
				currentImage--;
			}
		}
		
		var newActive = $('item'+currentImage);
		loadingBar = $('loadingBar'+currentImage);
				
		var myFxLoading = new Fx.Tween(loadingBar, {
		   duration: 11500,
		   property: 'margin-left',
		   onComplete: function() {
			   $$('#sliderItems .loadingBar').each(function(el) {
					el.setStyle('margin-left', '-1200px');				   
				});
		   }.bind(loadingBar)
		});
		
		var myFxOld = new Fx.Tween(oldActive, {
		   duration: 'long',
		   property: 'margin-left',
		   onComplete: function() {
			   this.removeClass('active');
			   $$('#sliderItems .item').each(function(el) {
					el.setStyle('margin-left', '0');				   
				});
			   allowClick = true;
			    myFxLoading.start('-1200px', '0'); 
		   }.bind(oldActive)
		});
		
		var myFxNew = new Fx.Tween(newActive, {
		   duration: 'long',
		   property: 'margin-left',
		   onComplete: function() {
			   $$('#sliderItems .item').each(function(el) {
					el.setStyle('margin-left', '0');				   
				});
			   allowClick = true;
			   initData(newActive);
		   }.bind(newActive)
		});
		
		var elm = $('item'+currentImage);
		elm.setStyle('margin-left', '-1200px');
		elm.addClass('active');
		
		if(direction == 'next') {
			myFxOld.start('0', '-1200px'); 
			myFxNew.start('1200px', '0'); 
		} else {
			myFxOld.start('0', '1200px'); 
			myFxNew.start('-1200px', '0'); 
		}
		
	}
	
	function initData(newActive) {
		var title = newActive.get('rel');
		$('sliderLogoText').set('html', title);
	}
	
});
