//set defaults

jQuery.preloadImages = function(arguments)
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback); 
};

jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};

jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };



$(window).bind('load', function() {


var myBackGrounds=new Array();
	myBackGrounds[0]="l01";
	myBackGrounds[1]="l02";
	myBackGrounds[2]="l03";
	myBackGrounds[3]="l07";
	myBackGrounds[4]="h01";
	myBackGrounds[5]="h02";
	myBackGrounds[6]="h05";
	myBackGrounds[7]="h07";
	myBackGrounds[8]="h08";
	myBackGrounds[9]="p05";

	var prevClass = "";
	var newClass = "l01";
	var olddiv = 1;
	var currentdiv = 2;
	var timebackground = 15000;

$.preloadImages('/image/background/Leisure_01.jpg','/image/background/Leisure_02.jpg','/image/background/Leisure_03.jpg','/image/background/Leisure_04.jpg','/image/background/Leisure_05.jpg','/image/background/Leisure_06.jpg','/image/background/Leisure_07.jpg','/image/background/Leisure_08.jpg','/image/background/Home_01.jpg','/image/background/Home_02.jpg','/image/background/Home_03.jpg','/image/background/Home_04.jpg','/image/background/Home_05.jpg','/image/background/Home_06.jpg','/image/background/Home_07.jpg','/image/background/Home_08.jpg','/image/background/Home_09.jpg','/image/background/Promo_01.jpg','/image/background/Promo_02.jpg','/image/background/Promo_03.jpg','/image/background/Promo_04.jpg','/image/background/Promo_05.jpg','/image/background/Promo_06.jpg','/image/box/Home_01.jpg','/image/box/Home_02.jpg','/image/box/Home_03.jpg','/image/box/Home_04.jpg','/image/box/Home_05.jpg','/image/box/Home_06.jpg','/image/box/Home_07.jpg','/image/box/Home_08.jpg','/image/box/Home_09.jpg','/image/box/Leisure_01.jpg','/image/box/Leisure_02.jpg','/image/box/Leisure_03.jpg','/image/box/Leisure_04.jpg','/image/box/Leisure_05.jpg','/image/box/Leisure_06.jpg','/image/box/Leisure_07.jpg','/image/box/Leisure_08.jpg','/image/box/Promo_01.jpg','/image/box/Promo_02.jpg','/image/box/Promo_03.jpg','/image/box/Promo_04.jpg','/image/box/Promo_05.jpg','/image/box/Promo_06.jpg');

		newIndex2 = getRandom(myBackGrounds,0);
		$('div#Background'+olddiv).fadeTo(1,0);
		$('div#Background').removeClass(newClass);
		addBackground(olddiv,currentdiv,newIndex2,0,myBackGrounds);


	$.timer(timebackground, function (timer) {

		prevClass = newClass;
		newClass = getRandom(myBackGrounds,prevClass);
		addBackground(olddiv,currentdiv,newClass,prevClass);

		if(currentdiv == 1) {
			olddiv = 1;
			currentdiv = 2;
		}
		else {
			olddiv = 2;
			currentdiv = 1;
		}
		timer.reset(timebackground);
	});

	$('div.box div.links ul li a').mouseover(function () {
	
		currentBlock = $(this).parent().parent().parent().parent().attr('id');
		currentClass = $(this).attr('id');


			if(currentClass != $('div#'+currentBlock+' div.links ul li a.selected').attr('id')) {
				$('div#'+currentBlock+' div.links ul li a.selected').removeClass('selected');
				$(this).addClass('selected');

				$('div#'+currentBlock+' div.links div.imageholder img.selected').fadeToggle(400).removeClass('selected');
				$('div.box div.links div.imageholder img.'+currentClass).fadeToggle(400).addClass('selected');

			}

	});

	function getRandom(cssArray,previous)
	{
		var s;
		i = Math.floor(Math.random()*cssArray.length);
		s = cssArray[i];
		if (s == previous) {
			if(s == cssArray[cssArray.length])
				return cssArray[0];
			else
				return cssArray[i++];
		}
		else
			return s;

	}


	function addBackground(olddiv,newdiv,newclass,prevclass){
		$('div#Background'+newdiv).addClass(newclass).removeClass('nodisplay');
		$('div#Background'+olddiv).fadeTo(500, 0, function(){
				$('div#Background'+olddiv).removeClass(prevclass).addClass('nodisplay');
				$('div#Background'+newdiv).fadeTo(500, 1);
			}
		);
		
	}


});