﻿/*
Copyright (c) 2010 by 5 Limes Pty Limited. All rights reserved.
This material may not be duplicated for any purpose without the
express written consent of 5 Limes Pty Limited of Sydney, Australia.
http://www.5Limes.com.au
*/

function CBackground() { }

CBackground.prototype =
{
	_arrImg: null, // Array of background skin image URLs
	_bgIndex: 1, // Background skin index is from 1 to the number of skins
	_speed: 400, // Duration of fade animation in milliseconds
	_totalBg: 0, // Number of background skins
	GetIndex: function () {
		return gBackground._bgIndex;
	},
	GetCurrentImageSrc: function () {
		return (gBackground._bgIndex > 0 && gBackground._bgIndex <= gBackground._totalBg) ? gBackground._arrImg[gBackground._bgIndex - 1] : "";
	},
	ShowNext: function () {
		var nextImg = gBackground._bgIndex + 1;

		if (nextImg > gBackground._totalBg) nextImg = 1;

		gBackground.Show(nextImg);
	},
	Show: function (index) {
		// Only perform transition if index is different to current background index
		if (gBackground._bgIndex != index) {
			jQuery("#divBgCurtain").fadeIn(gBackground._speed / 2, function () {
				jQuery("body").css("background-image", "url('" + gBackground._arrImg[index - 1] + "')").ready(function () { jQuery("#divBgCurtain").fadeOut(gBackground._speed); });
			});
			gBackground._bgIndex = index;
		}
	},
	LoadBgImages: function () {
		// Prompt the browser to preload the background skins by inserting img tags into the hidden "image cache" div
		// Start with the skin for the next banner in sequence (initial banner's skin is already loaded as body background)
		if (gBackground._bgIndex < gBackground._totalBg)
			jQuery("#divPageBackgroundImgCache").append("<img src='" + gBackground._arrImg[gBackground._bgIndex] + "' />");
		// And then the rest
		jQuery(gBackground._arrImg).each(function () {
			jQuery("#divPageBackgroundImgCache").append("<img src='" + this + "' />");
		});
	},
	Initialise: function (bgImages) {
		this._arrImg = bgImages.split("|");
		this._totalBg = this._arrImg.length;
		this._bgIndex = Math.ceil(this._totalBg * Math.random()); // 1-based

		// Write out a new style tag to set the first skin
//		document.write("<style type=\"text/css\">body { background-image:url(" + this._arrImg[this._bgIndex - 1] + "); }</style>");

		// Delay caching of the background skins
		setTimeout(gBackground.LoadBgImages, 1000);
	}
}

var gBackground = new CBackground();

