/**
 * @author David Abdul - 25/10/2010
 */
function eGallery() {
	//Properties
	this.images = Array(
		"/WebRoot/BT4/Shops/BT3551/MediaGallery/Rotator/Rotator1.png",		
		"/WebRoot/BT4/Shops/BT3551/MediaGallery/Rotator/Rotator2.png",
		"/WebRoot/BT4/Shops/BT3551/MediaGallery/Rotator/Rotator3.png",
		"/WebRoot/BT4/Shops/BT3551/MediaGallery/Rotator/Rotator4.png"
	

	);
	this.imageObj = Array();
	this.urls = Array(
		"?ObjectPath=/Shops/BT3551/Categories/Accessories",
		"?ObjectPath=/Shops/BT3551/Categories/Menswear",
		"?ObjectPath=/Shops/BT3551/Categories/Shoes",
		"?ObjectPath=/Shops/BT3551/Categories/Womenswear"
	);
	this.directory = "";
	this.canvas;
	this.loadClass;
	this.linkClass;
	this.loading;
	this.interval;
	this.intervalTime = 8000;
	this.intervalTrack = 0;
	
	//Method Declares
	this.init = init;
	this.showLoader = showLoader;
	this.load = load
	this.loadSuccess = loadSuccess;
	this.loadFail = loadFail;
	this.buildGallery = buildGallery;
	this.animate = animate;
	
	//Methods
	function init(parent,loader,link) {
		this.linkClass = link;
		this.canvas = document.getElementById(parent);
		this.showLoader(loader);
		this.loadCount = 0; 
		this.load();
	}
	
	function showLoader(className) {
		this.loading = document.createElement("div");
		this.loading.className =className;
		this.canvas.appendChild(this.loading);
	}
	
	function load() {
		obj = this;
		this.imageObj[this.loadCount] = new Image()
		this.imageObj[this.loadCount].onload = function() {
			obj.loadSuccess();
		};
		this.imageObj[this.loadCount].onerror = function(){
			obj.loadFail();
		};
		this.imageObj[this.loadCount].src = this.directory + this.images[this.loadCount];
	}
	
	function loadSuccess() {
		this.loadCount++;
		this.load();
	}
	
	function loadFail() {
		//this.loadCount--;
		this.buildGallery();
	} 
	
	function buildGallery() {
		var obj = this;
		for (var i=0; i<this.loadCount; i++) {
			this.imageObj[i] = document.createElement("a");
			this.imageObj[i].href = this.urls[i];
			this.imageObj[i].className = this.linkClass;
			var img = document.createElement("img");
			img.src = this.directory + this.images[i];
			img.style.border = "0px";
			this.imageObj[i].appendChild(img);
			if (i == 0) this.imageObj[i].style.zIndex = "6";
			else {
				this.imageObj[i].style.zIndex = "5";
				this.imageObj[i].style.display = "none";
			}
			this.canvas.appendChild(this.imageObj[i]);
		}
		jQuery(this.loading).fadeOut();
		this.interval = setInterval(function () {
			obj.animate();
		},this.intervalTime); 
	}
	
	function animate() {
		var obj = this;
		var next = "";
		var current = this.intervalTrack;
		if (this.intervalTrack < (this.loadCount - 1)) {
			next = this.intervalTrack + 1;
			this.intervalTrack++;
		} 
		else {
			
			next = 0;
			this.intervalTrack = 0;
		}
		this.imageObj[next].style.zIndex = 4;
		this.imageObj[next].style.display = "block";
		
		jQuery(this.imageObj[current]).fadeOut('slow',function () {
			obj.imageObj[next].style.zIndex = 6;
		});
	}
}
jQuery(document).ready(function() {
	var gallery = new eGallery();
	gallery.init("galleryCanvas","loadBox","galleryLink");
});
