// JavaScript Document
var Site = {
	
	start: function() {
		
		if( News.initialize()){
			Tradeshows.hideAll();
			News.start();
		} else {
			Tradeshows.start();
		}
	}
	
}

var News = {
	
	initialize: function() {
		var items = $$('.news');
		if(items.length>0){
			return true;
		}
		return false;
	}, 
	
	start: function() {
		var items = $$('.news');
		var timer = 0;
		var slidefxs = [];
		items.each( 
			function(el, i) { 
				timer += 200;
				el.setStyle('margin-left', '-179px');
				slidefxs[i] = new Fx.Style(el, 'margin-left', {
										   duration: 1000, 
										   transition: Fx.Transitions.backOut, 
										   wait: false, 
										   onComplete: News.end.pass([el, i])
										   });
				slidefxs[i].start.delay(timer, slidefxs[i], 0);
			}
		, this);
	},
	
	end: function(el, i) {
		if(!Tradeshows.initialize() ){
			return;
		}
		timer = 10000;
		if(i!= $$('.news').length-1) {
			var slidefxs = new Fx.Style(el, 'margin-left', {
									   duration: 1000, 
									   transition: Fx.Transitions.backIn, 
									   wait: false});
		}else{
			var slidefxs = new Fx.Style(el, 'margin-left', {
									   duration: 1000, 
									   transition: Fx.Transitions.backIn, 
									   wait: false, 
									   onComplete: function() {
										   		News.hideAll();
										   		Tradeshows.showAll();
										  		Tradeshows.start();
									   		}
										});
		}
		slidefxs.start.delay(timer, slidefxs, 179);
	}, 
	
	hide: function(el) {
		el.setStyle('display', 'none');
	}, 
	
	hideAll: function() {
		var items = $$('.news');
		items.each(function(el){News.hide(el)},this);
	},
	
	show: function(el) {
		el.setStyle('display', 'block');
	}, 
	
	showAll: function() {
		var items = $$('.news');
		items.each(function(el){News.show(el)},this);
	}
}


var Tradeshows = {
	
	initialize: function() {
		var items = $$('.tradeshows');
		if(items.length>0){
			return true;
		}
		return false;
	}, 
	
	start: function() {
		var items = $$('.tradeshows');
		var timer = 0;
		var slidefxs = [];
		items.each( 
			function(el, i) { 
				timer += 200;
				el.setStyle('margin-left', '-179px');
				slidefxs[i] = new Fx.Style(el, 'margin-left', {
										   duration: 1000, 
										   transition: Fx.Transitions.backOut, 
										   wait: false, 
										   onComplete: Tradeshows.end.pass([el, i]) });
				slidefxs[i].start.delay(timer, slidefxs[i], 0);
			}
		, this);
	},
	
	end: function(el, i) {
		if(!News.initialize()){
			return;
		}
		
		timer = 10000;
		if(i!= $$('.tradeshows').length-1) {
			var slidefxs = new Fx.Style(el, 'margin-left', {
									   duration: 1000, 
									   transition: Fx.Transitions.backIn, 
									   wait: false});
		}else{
			var slidefxs = new Fx.Style(el, 'margin-left', {
									   duration: 1000, 
									   transition: Fx.Transitions.backIn, 
									   wait: false, 
									   onComplete: function() {
										   Tradeshows.hideAll();
										   News.showAll();
										   News.start();
									   }
								});
		}
		slidefxs.start.delay(timer, slidefxs, 179);
	}, 
	
	hide: function(el) {
		el.setStyle('display', 'none');
	}, 
	
	hideAll: function() {
		var items = $$('.tradeshows');
		items.each(function(el){Tradeshows.hide(el)},this);
	},
	
	show: function(el) {
		el.setStyle('display', 'block');
	},
	
	showAll: function() {
		var items = $$('.tradeshows');
		items.each(function(el){Tradeshows.show(el)},this);
	}

}

//----------------------------------------------------------
//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}


addLoadEvent(Site.start);
