$extend(Browser,{ 
	
	enableHTML5: function(){ 
		elems = 'article aside footer header menu nav section'; 
		elems.split(' ').each(function(tag){ 
			new Element(tag); 
		}) 
		mooModernizr.enableHTML5 = $lambda(true); 
		return true; 
	}
}); 
Browser.enableHTML5();
window.addEvent("load", function(){

	$each(Browser.Features, function(item,index){
		if(!item){
			$(document.body).addClass('no-'+index);
		}
	});
});	
var page_loaded = false;
window.addEvent("domready", function(){
                if(Cookie.read('image'))
				{
					if(document.getElements('div.holder ul li img')[0])
					{
						src_first = document.getElements('div.holder ul li img')[0].get('src');
						document.getElements('div.holder ul li')[0].setStyle('top',300);
					}
					else
					{
						src_first = document.getElements('div.holder img')[0].get('src');
						$("header_image").setStyle('top',300);
					}
					
					if(src_first != Cookie.read('image'))
					{
						startup_image = new Element("img");
						startup_image.set('src',Cookie.read('image'));
						startup_image.set('align','top');
						startup_image.set('id','startup');
						$$(".holder").grab(startup_image,'top');
						$('startup').spin();
					}
				}
				
				if($('splash_top'))
				{
					var height_bottom = $(window).getSize().y-$('splash_top').getSize().y;
					$('splash_bottom').setStyle('height',height_bottom);
					$$('#splash_bottom div.inner_splash').setStyle('height',height_bottom-20);	
					var timer_splash = function()
					{
						if(page_loaded)
						{						
							var splash_top = new Fx.Morph($('splash_top'),{duration: 1000, onComplete: function(){$('splash_top').setStyle("display",'none');}});
							splash_top.start({'height':0});
							var splash_bottom = new Fx.Morph($('splash_bottom'),{duration: 1000, onComplete: function(){$('splash_bottom').setStyle("display",'none');}});
							splash_bottom.start({'height':0});
							var splash_bottom_inner = new Fx.Morph($('splash_bottom_inner'),{duration: 1000});
							splash_bottom_inner.start({'margin-top':-(height_bottom-20)});
						}
						else
						{
							timer_splash.delay(500);
						}
						   
					}
					timer_splash.delay(5000);
				}
                      
});
window.addEvent("load", function(){
	
	page_loaded = true;

	var gallery = document.getElements('div.holder ul li');
	if(gallery.length > 1)
	{
		var paging = new Element("ul");
		var paging_container = $$('.paging')[0];
		paging.injectInside(paging_container);
		var fadeout = new Array();
		var fadein = new Array();
		gallery.each(function(el, i) {     
			li =  new Element("li");
			li.innerHTML = '<a hidefocus="true">' + '<span></span>' + '</a>';
			li.injectInside(paging);
			fadeout[i] = new Fx.Tween(el,{duration: 1000, link:'ignore', onComplete: function(){el.setStyle("left",'960px'); moving = false;  }});
			fadein[i] = new Fx.Tween(el,{ duration: 1000, link:'ignore'});
			
		});
		
		var aantal = gallery.length;
		var current = 1;
		var previous = 5;
		var moving = false;
		var stop = false;
		var next;
		
		var pagers = document.getElements(".paging ul li a");
		pagers.each(function(el, i) {
			el.addEvent('click', function() {
				if(current != i+1)
				{
					nextSlide(i+1);		
					return false;
				}
			}.bind(this));
		}.bind(this));
		paging_container.addEvent('mouseover', function() {
			$clear(next);
		}.bind(this));
		paging_container.addEvent('mouseout', function() {
			next = nextSlide.periodical(7500);
		}.bind(this));
		
		var title = new Element('h3');
		new_title = gallery[current-1].getElement('img').get('alt');
		if(new_title != null)
		{
			title.innerHTML = new_title;
		}
		paging_container.grab(title, 'top');
		
		var fxTitle = new Fx.Tween.CSS3(title, {
			property: "opacity",
			duration: 1000/2,
			transition: 'linear',
			link: "ignore",
			fps: 100,
			onComplete: function(){
				new_title =  gallery[current-1].getElement('img').get('alt');
				if(new_title != null)
				{
					title.set('html',new_title);
					fxTitle.start(1);
				}}
		});
		var image_cookie = Cookie.write('image', gallery[current-1].getElement('img').get('src'));
		gallery[0].setStyle('left',0);
		pagers[0].addClass('active');
	}
	else
	{
		var image_cookie = Cookie.write('image', $("header_image").get('src'));
		
	}
	
	var nextSlide = function(i)
	{
		if(!moving)
		{
			moving = true;
			if(i == null)
			{
				previous =  current-1;
				if(current < aantal)
				{
					current++;
				}
				else
				{				
					current = 1;
				}
			}
			else
			{
				previous = current-1;
				current = i;
			}
			
			fxTitle.start(0);
			$$("div.paging ul li a.active").removeClass('active');
			pagers[current-1].addClass('active'); 
			image_cookie = Cookie.write('image', gallery[current-1].getElement('img').get('src'));
			fadeout[previous].start('left','-960');
			fadein[current-1].start('left', '0');
		
		}
	
	}

	
								
	

	

	

	
	$$('header nav a').each(function(a){
		a.addEvent('click', function(e){
									 //gallery[current-1].getElement('img').spin();
									 $clear(next);
									 
									 })
		});
	/*								
			if(gallery.length > 1)
			{
				 var href = this.get('href');
				$clear(next);
				//frm.getElement('input[name="image"]').set('value',gal.items[gal.current].getElement('img').get('src')).getParent().set('action', this.get('href')).submit();
				var ajax = new Request({
				  url: '../php/setsession.php',
				  method: 'post',
				  data:{'current_image':gallery[current-1].getElement('img').get('src')},
				  onComplete:function(){ window.location = href; }
				  
				});
				ajax.send();
				return false;

			}else if($("header_image").get('src')){
				$clear(next);
				var href = this.get('href');
				var ajax = new Request( {
				  url: '../php/setsession.php',					   
				  method: 'post',
				  data:{'current_image':$("header_image").get('src')},
				  onComplete:function(){ window.location = href; }
				  
				});
				ajax.send();
				return false;
			}
		});
	});
	*/

	if($('startup')){
		$$('.spinner').dispose();
		var fx3 = new Fx.Tween($('startup'),{
			duration: 1000,
			onComplete: function(){
						   if(gallery.length > 1)
						   {
								next = nextSlide.periodical(7500);
						    }
							
						}
		});
		fx3.start('top','-300');
		
		if(gallery.length > 1){
			fadein[current-1].start('top', '0');
		}
		else
		{
			var fx4 = new Fx.Tween($("header_image"),{duration: 1000});
			fx4.start('top','0');
		}
	}
	else
	{
		if(gallery.length > 1)
	   {
			next = nextSlide.periodical(7500);
		}
	}
	$$('img.over').addEvents({
		'mouseover': function(){this.set('src', this.get('srcalt'))},
		'mouseout': function(){this.set('src', this.get('srcorg'))},
		'click': function(){this.set('src', this.get('srcalt'))}
	});
});
