var SlideShow = Class.create({	
	
	initialize: function(container, options){

		// Ensure container exists
		if (!$(container)) { return false; }

		// Override default options
		this.options = Object.extend({
			delay: 5000
		}, options || {});
		this.container = container;
		
		// Get array of slides
		this.slides = $$('#'+this.container+' li');	
		
		// Stop this slideshow if there is only one slide
		if (this.slides.length<2) return false;
		
		// Set start and end frame
		this.startSlide = 0;
		this.endSlide = this.slides.length-1;
		
		// Hide extra slides
		this.hideSlides();
		
		// Set li's to position absolute
		$(container).down('ul').addClassName('ready');
		
		// Start the slide show
		setTimeout(this.fadeInOut.bind(this,this.startSlide),this.options.delay);		
	},
	
	// Hide all but the first slide
	hideSlides: function() {
		var firstSlide = true;
		this.slides.each( function(slide) {
			if (!firstSlide) { slide.setStyle({ display:'none' }); }
			firstSlide = false;
		});		
	},
	
	// Fade from one frame to the next
	fadeInOut: function(slide) {
		Effect.Fade(this.slides[slide]);
		if (slide == this.endSlide) { slide = this.startSlide; } else { slide++; }
		Effect.Appear(this.slides[slide]);
		setTimeout(this.fadeInOut.bind(this,slide), this.options.delay + 1850);
		
	}		
});



function styleTables () {
	// Add Last to Each Last
	var tables = document.getElementsByTagName('table');
	for (var i=0; i < tables.length; i++) {
		var rows = tables[i].getElementsByTagName('tr');
		for (var i=0; i < rows.length; i++) {
			var theads = rows[i].getElementsByTagName('th');
			var cells = rows[i].getElementsByTagName('td');
			if(theads.length > 0){
				theads[0].className = 'first';
				theads[theads.length - 1 ].className = 'last';
				}
			if(cells.length > 0){
				cells[0].className = 'first';
				cells[cells.length - 1 ].className = 'last';
				}
		};
	};
}


function addStyles () {
	
	//styleTables();
	// Add Class to top sub nav item for styling
	$$('#sidenav #root > ul > .active a').each(function(link) {
		link.addClassName('subactive');
	});
	
	// Fix Image margins beside lists
	$$('.bodyimage + .text').each(function(text) {
		if(text.down('ul')){
			text.adjacent('.bodyimage').each(function(img) {
				img.setStyle('margin-right: 15px;');
			});
		}
	});
	
}

// Global DOM onload
document.observe("dom:loaded", function() {
	addStyles();
		
	// Check for the search form	
	if ($('product_search_form')) {	
		
		// On submit, ensure at least one of the 3 fields has a value
		$('product_search_form').observe('submit', function(e){
			if ( ($F('product_search_name')=='') && ($F('product_search_brand')=='') && ($F('product_search_application')=='') ) {				
				alert('Please enter a product name or choose a brand or application.');
				e.stop();
			}						
		});
	}
	
	// if (location.hash != '#test') return;
	// if (!$('banner')) return; 
	// 
	// console.log(location.href);
	// var url = location.href;
	// url = url.split('http://agriumat.com/ca')[1]; // strip domain
	// url = url.split('#test')[0]; // strip hash
	// url = '/images/banners'+url;
	// console.log(url);
	// 
	// new Ajax.Request(url+'1.jpg',{
	// 	onSuccess:function(t){
	// 		console.log('success!'); 
	// 		$('banner').update('<img src="'+url+'1.jpg" />');
	// 	}, 
	// 	onFailure:function(t){
	// 		console.log('failure!');
	// 	} 
	// });
	
});


// Global window onload
Event.observe(window,'load',function(){ 

	new SlideShow('banner', {delay:5000} );	
	
});
