﻿function fadeOutHeaderBg() {
	$( "#headerimg div.rotate" ).fadeOut( fadeDuration, fadeOutReady );
}

function fadeInHeaderBg() {
	$( "#headerimg div.rotate" ).fadeIn( fadeDuration, fadeInReady );
}

function changeImage( sElement )
{
	bgImage = ( bgImage + 1 ) % fadeImages.length;
	$( sElement ).css( "backgroundImage", "url(" + bgDir + fadeImages[bgImage] + ")" );
	checkPreload();
}

function fadeOutReady() {
	changeImage( "#headerimg div.rotate" );
	setTimeout( fadeInHeaderBg, fadeTimeout );
}

function fadeInReady() {
	changeImage( "#headerimg" );
	setTimeout( fadeOutHeaderBg, fadeTimeout );
}

function checkPreload()
{
	if (!allImagesPreloaded)
	{
		var nextImage = bgImage + 1;
		if (nextImage == fadeImages.length)
		{
			allImagesPreloaded = true;
			return;
		}
		preloadImage(bgDir + fadeImages[nextImage]);
	}
}

function chooseImageCollection()
{
	var aReturn;

	if ( document.location.href.match( "/about/consumer" ) )
		aReturn = consumerImages;
	else if ( document.location.href.match( "/about/commercial" ) )
		aReturn = commercialImages;
	else
		aReturn = consumerImages.concat( commercialImages );

	//  Shuffle the Array.. randomizing the order
	for ( var j, x, i = aReturn.length; i; j = parseInt( Math.random() * i ), x = aReturn[ --i ], aReturn[ i ] = aReturn[ j ], aReturn[ j ] = x );
	return aReturn;
}

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function preloadImage(src) {
	var plImg = new Image();
	plImg.src = src;
}


function Search()
{
	this._object  = document.getElementById( "search" );
	if ( this._object )
	{
		this._default = this._object.value;

		this._object._control = this;
		this._object.onfocus  = this.__onfocus;
		this._object.onblur   = this.__onblur;
	}
};
Search.prototype.__onfocus = function()
{
	this._control.state( true );
};
Search.prototype.__onblur = function()
{
	this._control.state( false );
};
Search.prototype.state = function( bActive )
{
	if ( bActive && this._object.value == this._default )
		this._object.value = "";
	else if ( !bActive && this._object.value.match( /\s*/ ) )
		this._object.value = this._default;
};


function CountrySelect( oElement, nIndex )
{
	this._object = oElement;
	window[ ( this._self = "CountrySelectObject" + ( nIndex || 0 ) ) ] = this;

	this._object._control   = this;
	this._object.onclick    = this.__toggle;
	this._object.onmouseout = this.__disable;

	this._state = false; // not open

	var aLI = $( "li", this._object );
	for ( var i = 0; i < aLI.length; ++i )
		if ( !aLI[ i ].className.match( /first/ ) )
		{
			aLI[ i ]._control    = this;
			aLI[ i ].onmouseover = this.__enable;
		}
};
CountrySelect.prototype.__toggle = function( e )
{
	this._control.toggle();
};
CountrySelect.prototype.__enable = function( e )
{
	this._control.show();
};
CountrySelect.prototype.__disable = function( e )
{
	this._control.hide();
};
CountrySelect.prototype.toggle = function()
{
	if ( this._state )
		this.hide( 1 );
	else
		this.show();

	this._state = !this._state;
};
CountrySelect.prototype.show = function()
{
	clearTimeout( this._timer );
	this._addActiveClass();
};
CountrySelect.prototype.hide = function( nDelay )
{
	clearTimeout( this._timer );

	this._timer = setTimeout( this._self + "._stripActiveClass();", nDelay || 1000 );
};
CountrySelect.prototype._addActiveClass = function()
{
	if ( !this._object.className.match( /active/ ) )
		this._object.className += " active";	
};
CountrySelect.prototype._stripActiveClass = function()
{
	this._object.className = this._object.className.replace( /\s*active/, "" );
};



var bgDir              = "/media/image/layout/header/";
var timerOn            = false;
var fadeDuration       = 1000;
var fadeTimeout        = 7000;
var commercialImages   = [
	"header_04.jpg", 
	"header_05.jpg", 
	"header_07.jpg", 
	"header_08.jpg", 
	"header_09.jpg" 
];
var consumerImages     = [
	"header_01.jpg", 
	"header_02.jpg", 
	"header_03.jpg", 
	"header_06.jpg", 
	"header_10.jpg" 
];
var fadeImages;
var allImagesPreloaded = false;
var bgImage            = 0;


$(document).ready(function() {
	new Search();

	//  Images are divided into two groups, consumer and commercial, on most pages all images may 
	//  be used, though for specific consumer or commercial pages (/about/consumer*, /about/commercial*) the specific collection is used
	fadeImages = chooseImageCollection();
	bgImage    = Math.round( Math.random() * fadeImages.length ) % ( fadeImages.length - 1 );

	preloadImage(bgDir + fadeImages[0]);
	changeImage( "#headerimg" );

	var aList = $( "ul.countryselect" );
	for ( var i = 0; i < aList.length; ++i )
		new CountrySelect( aList[ i ], i );

	var aPath = document.location.href.split( "/" );
	if ( aPath.length == 4 && aPath[ 3 ] == "" )
		setTimeout(fadeOutHeaderBg, fadeTimeout);

	if ( typeof Gallery != "undefined" )
		new Gallery();
});
