/**
 * Jquery for Promo Box
 *
 * @author		Dave Newson <dave@4mation.com.au>
 * @version		20100616
 */



/**
* Promobox
*
*/

function PromoBox()
{
}

// Delay between cycles
PromoBox.prototype.CycleDelay = 4000;

// DIV container for the box
PromoBox.prototype.Container = null;
// DIV Fader for fading in/out of items
PromoBox.prototype.Fader = null;

// Timeout timer
PromoBox.prototype.Timer = null;

// Fade duration
PromoBox.prototype.FadeDuration = 500;

// Items container
PromoBox.prototype.Items = null;

// Allow cycles?
// Mostly used for on-mouse-over catches
PromoBox.prototype.AllowCycle = true;

// Currently selected item
PromoBox.prototype.CurrentItem = 0;


/**
* Initialise
* Prepares the following items
* - List buttons to choose which item
* - Timer to rotate promo item
*/

PromoBox.prototype.Initialise = function ( container )
{
	// Set up List buttons
	this.Container = $(container);
	
	// Add on-mouse-over event
	this.Container.mouseover( bind(this, this.BoxMouseOver) );
	
	// Add on-mouse-out event
	this.Container.mouseout( bind(this, this.BoxMouseOut) );

	// Add List items Container
	this.Container.append('<ul></ul>');
	
	// add list items
	for (id in this.Items)
	{
		var li = document.createElement( 'li' );
		
		// :WARN: JS Closure
		$(li).click( function( classref, num )
		{
			return function()
			{
				classref.SwitchTo(num);
			}
		}(this, id)
		);
		
		var digit = parseInt(id)+1;
		
		$(li).text( digit );
		
		// Add LI to elements
		this.Container.children('ul').append( li );
		
		// Resize LI in a funky way.
		var margin = parseInt( $(li).css('margin-left').substring(0, 2) );	// Margins
		var width = this.Container.width() - margin;	// Account for right margin
		width = width / this.Items.length;				// Number of items
		width = width - 2;								// Border element
		width = width - margin;							// Margins for each
		
		$(li).width( width );

		
	}
	
	// Fade in the UL
	this.Container.children('ul').fadeIn( this.FadeDuration );
	
	// Set up fade DIV
	this.Container.append('<div class="fader"></div>');
	this.Fader = this.Container.children('.fader');

	// Set up the link-on-click
	this.Fader.click( bind(this, function()
	{
		this.FollowLink();
	}));
	
	// Pick a random start image
	this.SwitchTo( Rand(0, this.Items.length) );
	
	// Delay until next cycle
	this.TriggerDelay();
}


/**
* Cycle
* Cycles the promo box onto the next item
*/

PromoBox.prototype.Cycle = function()
{
	// If we're not allowed to perform the cycle of images
	// Don't do anything and bail out.
	if (!this.AllowCycle)
	{
		return;
	}
	
	// Increment current item
	if (this.CurrentItem+1 == this.Items.length)
	{
		this.CurrentItem = 0;
	}
	else
	{
		this.CurrentItem++;
	}

	// Roll onto next item
	this.SwitchTo (this.CurrentItem);
	
	// Delay until next cycle
	this.TriggerDelay();
}


/**
* TriggerDelay
* Prepares a timeout to trigger the next cycle
*/

PromoBox.prototype.TriggerDelay = function()
{
	// Set up delay until next cycle
	// Clear the cached timer first.
	clearTimeout(this.Timer);
	this.Timer = setTimeout( bind(this, this.Cycle), this.CycleDelay);
}


/**
* Switch To
* Switches the currently selected item to that specified
*/

PromoBox.prototype.SwitchTo = function( id )
{
	// Set the current item ID
	this.CurrentItem = id;
	
	// Put old item to background and hide the fader
	this.Container.css({'background-image': this.Fader.css('background-image') });
	this.Fader.hide();
	
	// Set new fader image
	this.Fader.css({'background-image': 'url('+this.Items[ this.CurrentItem ][0]+')'});
	
	// Fade in fader
	this.Fader.fadeIn( this.FadeDuration );
	
	// Fade all controls except the active one.
	this.Container.children('ul').children('li').each( function(num)
	{
		if (num == id)
		{
			$(this).fadeTo(0, 1);
			$(this).addClass('a');
			$(this).removeClass('ia');
		}
		else
		{
			$(this).removeClass('a');
			$(this).addClass('ia');
		}
	});
	
}


/**
* FollowLink
* Goes to the location specified in the link
*/

PromoBox.prototype.FollowLink = function()
{
	// Go to location
	location.href = this.Items[ this.CurrentItem ][1];
}


/**
* Box Mouse Over
* Stops the allowing of cycling when the mouse is over the image
*/

PromoBox.prototype.BoxMouseOver = function ()
{
	// Disable the ability to cycle
	this.AllowCycle = false;
}


/**
* Box Mouse Out
* Allows the cycling of images and fires the timer back up.
*/

PromoBox.prototype.BoxMouseOut = function ()
{
	// Enable the ability to cycle
	this.AllowCycle = true;
	
	// reinstigate the delay
	this.TriggerDelay();
}


/**
* Set Items
* Pointless data joiner thing.
*/

PromoBox.prototype.SetItems = function ( list )
{
	this.Items = list;
}



/**
* Helpers
*/

// Bind helper
function bind(scope, fn)
{
    return function ()
    {
        fn.apply(scope, arguments);
    }
}

// Randomiser
function Rand( arg1, arg2 )
{
	if (arg2)
	{
		return Math.floor(Math.random() * (arg2 - arg1)) + arg1;
	}
	else
	{
		return Math.floor(Math.random() * arg1);
	}
}

