var x = 1;
var fx;
var taginfo;
var winSize;

var nextTag = function(){
	
	new Fx.Tween(taginfo, {
		duration: 		600,
		transition: 	'sine:out',
		onComplete: function(){
			
			fx.start(taglines[x].title);
			
			taginfo.set('text', taglines[x].content);
			new Fx.Tween(taginfo, {
				duration: 		1400,
				transition: 	'sine:out'
			}).start('opacity', 1);
			
			x++;
			if(x >= taglines.length)	x = 0;
			
		}
	}).start('opacity', 0);		
	
};

var sizeBestelknop = function(){
	
	knopWidth = (winSize.x - 970) / 2;
	$$('header a.bestel').setStyle('padding-right', knopWidth);
	
}

var sizeLanguageBar = function(){
	
	barWidth = (winSize.x - 970) / 2;
	$$('div.language').setStyle('padding-left', barWidth);
	
}

var sizeImageSlide = function(){
	
	slideWidth = (winSize.x - 970) / 2;
	$('imageslide').setStyles({
		'margin-right': -slideWidth,
		'width': 		270 + slideWidth
	});
}

var makeScrollbar = function(content, scrollbar, handle, horizontal, ignoreMouse){

	var steps = (horizontal ? (content.getScrollSize().x - content.getSize().x) : (content.getScrollSize().y - content.getSize().y));

	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal ? 'horizontal' : 'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal ? step : 0);
			var y = (horizontal ? 0 : step);
			content.scrollTo(x,y);
		}
	}).set(0);
	
	if(!(ignoreMouse)){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e.stop();
			var step = slider.step - e.wheel * 20;	
			slider.set(step);
		});
	}
	
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
	
	// correct scroll if done through JS / #
	(function(){
		
		currentScroll  = $('contentarea').getScroll();
		slider.set(currentScroll.y);
		
	}).periodical(500);
	
}

var correctScrollbar = function(){
	
	
	
}

window.addEvent('domready', function(){
	
	winSize = window.getSize();
	
	$$('a[rel=external]').setProperty('target', '_blank');
	
	//taglines
	if(taglines.length){
		taginfo = $('taginfo');
	
		fx = new Fx.Text($('tagline'), {
		    duration: 1000,
		    transition: Fx.Transitions.Sine.easeOut
		});
	
		nextTag.periodical(7000);
	}
	
	//balkjes op maat maken
	sizeBestelknop();
	sizeLanguageBar();
	sizeImageSlide();
	
	//scrollende plaatjes
	handleRoll();	
	
	//en op resize laten reageren
	window.addEvent('resize', function(){
		
		winSize = window.getSize();
		
		sizeBestelknop();
		sizeLanguageBar();
		sizeImageSlide();
		
	});
	
	scroller = new Fx.Scroll('contentarea',{
		transition: 	Fx.Transitions.Quad.easeInOut,
		duration: 		2500
	});
	$$('ul.nav a').addEvent('click', function(e){
	
		e.stop();
		link = this.get('href').split('#');
		scroller.toElement(link[1]);
		
	});
	
	//effectjes
	$$('header a.bestel').addEvent('mouseover', function(){
		new Fx.Tween(this, {
			duration: 		200,
			transition: 	'sine:out'
		}).start('background-position', '6px 0');
	}).addEvent('mouseout', function(){
		new Fx.Tween(this, {
			duration: 		200,
			transition: 	'sine:out'
		}).start('background-position', '3px 0');
	});
	
	//content scroller
	if($('scrollbar')){
		makeScrollbar($('contentarea'), $('scrollbar'), $('handle'), false, false);
	}
	
	if(Browser.Engine.trident && Browser.Engine.version <= 4){
		fixIE();
	}
	
});

var handleRoll = function(){
	
	var slider = $('imageslide');
	list = slider.getElements('li');
	
	if(list.length){
	
		slider.v = 2;
	
		slider.over = false;
		slider.addEvent('mouseenter', function(){
			slider.over = true;
		}).addEvent('mouseleave', function(){
			slider.over = false;
		});
		
		bewegen.periodical(30, slider);
		var handle = slider.getElement('.handle');
		
		new Drag(handle, {
			limit: {x: [-1000, 0], y: [0,0]},
		    snap: 6,
			onStart: function(el){
		        el.addClass('dragging');	
		    }
		});
	
		handle.getElements('a').addEvent('click', function(e){
			if(!handle.hasClass('dragging')){
				SqueezeBox.open(this.href, {
					size: 	JSON.decode(this.rel)
				});
				e.stop();
			}else{
				handle.removeClass('dragging');
				e.stop();
			}
		});
		
	}
	
}

var bewegen = function(){
	
	if(!this.over){
		
		var handle = this.getElement('.handle');
		var first = handle.getElement('li');
		
		if(first){
			dim = first.getSize();

			newmargin = handle.getStyle('left').toInt();
		
			newmargin -= this.v;
			handle.setStyle('left', newmargin);
		
			if(dim.x < (-newmargin)){

				first.dispose().inject(handle);
				handle.setStyle('left', (newmargin + dim.x));

			}
		}
		
	}
	
}

var newsInit = function(){
	
	window.addEvent('domready', function(){
	
		$$('h2 a, a.readmore').addEvent('click', function(e){

			SqueezeBox.open(this.href, {
				ajaxOptions: {
					method: 'post',
			        data: 	'sq=1'
			    },
				size: 	{x: 500, y: 380}
			});
			e.stop();
			
		});
		
	});	
	
}

var fixIE = function(){
	
	$$('ul.menu li').addEvent('mouseenter', function(){
		this.addClass('hover');
	}).addEvent('mouseleave', function(){
		this.removeClass('hover');
	});
	
}
