/*
Billboard Banner Roator
Version: 1.1
Author: Ian Nielsen (ian.nielsen@me.com)
Dependancies: jQuery
*/

var BillboardRotator = function() {
	
	// Check for presence of jQuery. Bail out if not found
	if(!jQuery) { return false; };

	// Local variables
	var arBillboards = [];							// array to hold each billboard
	var count = 0;									// Counter to reference the current billboard
	var oBillboard = jQuery('.billboard');			// billboard display space
	var oTimer;										// variable to hold our timer
	var delay = 4000;								// default delay time between transitions
	
	this.setDelay = function(val) {
		// allows us to set the delay time externally
		delay = parseInt(val) ? val : delay;
	};
	
	var Methods = {
		
		load : function() {
			// Loads all the billboard content we require
			
			jQuery('.jsonly').each(function(i,e) {
				jQuery(e).removeClass('jsonly');
				jQuery(e).hide();
			});

			// Grab the panels and get the href and image locations for the billboard link and image
			jQuery('.product').each(function(i,e) {
			
				// Load the data into our array so it can be referenced throughout the script
				arBillboards.push(e);											// the panel element
				arBillboards[i].href = jQuery(e).find("a").attr('href');		// href for the billboard
				arBillboards[i].bbID = jQuery(e).find("a").attr('rel');			// id of the billboard
				arBillboards[i].index = i;										// index position of the billboard
				
				// set mouse over listener
				jQuery(e).mouseover(function() {
					// clear the timer
					try { clearInterval(oTimer) } catch(err) {};
					// show whichever billboard panel the user has moused over
					Methods.show(this.index);
				});
				
				// set onclick event for the panel
				jQuery(e).click(function() {
					// go to which ever page is required
					document.location.href = this.href;
				});
				
				// If this is the initially selected panel, then set the counter to it's index so that the billboards start rotating from the correct place
				if(e.className.match('active')) { count = i; };
				
			});
			
			// Show the first billboard ad
			Methods.show(count);

		},
		
		show : function(i) {
			// displays the billboard requested
			try {
				// set the global counter variable so that the next ad is displayed the next time the rotator loops
				if(count < arBillboards.length-1) { count++; } else { count = 0; };

				jQuery('.billboard').hide();
				jQuery('#'+arBillboards[i].bbID).show();
				
				// remove any currently selected active states
				jQuery('.product').each(function(i,e) { jQuery(e).removeClass('active'); });
				// set the active state of the current billboard ad
				jQuery(arBillboards[i]).addClass('active');
			} catch(err) {};
		}
		

	};
	
	this.init = function() {
		// initialise the script
		Methods.load();
		// start the timer
		oTimer = setInterval(function() { Methods.show(count); }, delay);
	};
	
};

var $billboard = new BillboardRotator();
try { jQuery(window).load($billboard.init); } catch(err) {};
