// JavaScript Document
var total = null;
window.addEvent('domready', function() {
	//Gets the ul with the highest number of eventsSlider within it so we know how much sliding we can do!
	if ($('eventsSlider')!=null) {
		var ul = $$('#eventsSlider ul');
		var margin = 0;
		total = ul[0].getChildren('li').length;
		if (total < ul[1].getChildren('li').length) {
			total = ul[1].getChildren('li').length;
		}
		total = total*104; //sets total to the width of the highest ul
		
		//store titles and text   
		$$('a.tips').each(function(element,index) {   
			var content = element.get('title').split('::');   
			element.store('tip:title', content[0]);   
			element.store('tip:text', content[1]);   
		});   
		   
		//create the tooltips   
		var tipz = new Tips('.tips',{   
			className: 'tips',   
			fixed: false,   
			hideDelay: 50,   
			showDelay: 50   
		}); 
		
		//if more than 4 shows then we need to setup the sliding
		if (total>208) {
			$('eventsSliderOuterWrapper').addEvent('mouseenter', function() {
				var Slide = new Fx.Morph('eventsSliderNavBg', {duration:250, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
				Slide.start({ 
					'margin-top': ['30px'],
					'height': ['0px']
				});	
			});
			
			$('eventsSliderOuterWrapper').addEvent('mouseleave', function() {
				var Slide = new Fx.Morph('eventsSliderNavBg', {duration:250, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
				Slide.start({ 
					'margin-top': ['0px'],
					'height': ['30px']
				});	
			});
			
			$('previous').setStyle('display','block');
			$('next').setStyle('display','none');
			
		} else {
			$('previous').setStyle('display','none');
			$('next').setStyle('display','none');
		}
			
	
		//sets up the sliding of the eventsSlider when clicking on the previous button
		$('previous').addEvent('click', function(event) {
			event.stop();									 
			var tmp = (total-208)-((total-208)*2);
			if (tmp<margin) {
				var Slide = new Fx.Morph('eventsSlider', {duration:500, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
				margin=margin-208;
				
				Slide.addEvent('complete', function() { 
					if (tmp>=margin) {
						$('previous').setStyle('display','none');
						$('next').setStyle('display','block');
					} else {
						$('previous').setStyle('display','block');
						$('next').setStyle('display','block');
					}
				});
				
				//margin=margin-208;
				Slide.start({ 'margin-left': [margin + 'px'] });	
			}
		});
		
		//sets up sliding when click on next button
		$('next').addEvent('click', function(event) {
			event.stop();
			if (margin<0) {
				var Slide = new Fx.Morph('eventsSlider', {duration:500, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
				//Slide.addEvent('complete', function() { margin=margin+208; })
				margin=margin+208;
				
				Slide.addEvent('complete', function() { 
					if (margin>=0) {
						$('previous').setStyle('display','block');
						$('next').setStyle('display','none');
					} else {
						$('previous').setStyle('display','block');
						$('next').setStyle('display','block');
					}
				});
				
				Slide.start({ 'margin-left': [margin + 'px'] });	
			}
		});
	}

}); //Ends dom ready