// Hover Behaviour for Nav etc.
var HoverBehavior = Class.create({
	initialize: function() {
		$A(arguments).each( function(arg) {
			$$(arg).each( function(tag) {
				Event.observe(tag, 'mouseover', function() { Element.addClassName(tag, 'hover'); });
				Event.observe(tag, 'mouseout', function() { Element.removeClassName(tag, 'hover'); });
			});
		});
	}
});


var SearchField = Class.create({
	
	initialize: function(field, options) {
		
		// Ensure field exists
		if (!$(field)) { return false; }
	
		// Override default options
		this.options = Object.extend({
			defaultValue: "search"
		}, options || {});
		this.field = field;
		
		// Clear field on click
		Event.observe(this.field, "click", function(){
			if ($F(this.field)==this.options.defaultValue) { 
				$(this.field).clear(); 
			}
		}.bind(this));
		
		// Restore field's default text when empty
		Event.observe(this.field, "blur", function(){
			if (!$(this.field).present()) { 
				$(this.field).setValue(this.options.defaultValue); 
			}
		}.bind(this));
	}
});


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');	
		
		// Set start and end frame
		this.startSlide = 0;
		this.endSlide = this.slides.length-1;
		
		// Hide extra slides
		this.hideSlides();
		
		// 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);
	}		
});

// Mark external and download links!
function markLinks(container) {

	// Ensure container exists
	if (!$(container)) { return false; }	
	
    // Mark external links    
	$$('#'+container+' a[href^=http]').each( function(link) {
		link.addClassName('external');
		link.addClassName('new-window');
		link.target='_BLANK';
	});		
	
    // Mark download links
    var filetypes = ['pdf','mpg'];
    var d = new Date(); var timestamp = (d.getTime()-d.getMilliseconds())/1000;

    // Loop each filetype    
    $A(filetypes).each(function(filetype){

      // Append a timestamp to all file downloads
      $$('#'+container+' a[href$=.'+filetype+']').each(function(link){
        link.href = link.href+'?'+timestamp;
		link.addClassName('file');
		link.addClassName('new-window');
		link.target='_BLANK';
      });

    });	
}

function applyCSS () {
	// Add class to product intro so it lines up with image.
	//$$('.bodyimagevideo + .text > p:first-child').each(function(p){p.addClassName('product-intro')});
}

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

	// Clear/reset default text in search field
	new SearchField('search');
	
	// Start slide on homepage
	new SlideShow('slideshow');
	
	// Mark external or download links to open in a new window
	markLinks('content');
	
	// Do any advanced css
	applyCSS();

	// IE6 menu
	new HoverBehavior('#nav-top-level > li'); // keep at the end!	
});