// statics

BannerSection.LEFT = "left";
BannerSection.RIGHT = "right";
BannerSection.TOP = "top";
BannerSection.BOTTOM = "bottom";

BannerSection.speed = 200;

// class

function BannerSection(holder, mainImage, images, text, link, sideIn, sideOut) {
	
	constructor();
	
	// private vars
	
	var overImage = null, textHolder, textBg;
	
	// constructor

	function constructor() {
		if(typeof(sideIn) == 'undefined') sideIn = BannerSection.LEFT;
		if(typeof(sideOut) == 'undefined') sideOut = sideIn;
		mainImage.css("position", "absolute").hide();
		holder.css({ position:"relative", overflow:"hidden" }).append(mainImage).append("<div class=\"textBg\"></div>").append("<div class=\"text\"></div>");
		if(typeof(link) != 'undefined' && link != "") holder.css({ cursor:"pointer" }).click(redirect);
		textBg = $(".textBg", holder);
		textBg.css({ background:"#000000", opacity:0.8, width:holder.width(), height:"26px", top:holder.height()-26 - 3, position:"absolute", zIndex:2 });
		textHolder = $(".text", holder);
		textHolder.html(text).css({ position:"absolute", zIndex:5, width:holder.width(), top:holder.height()-textHolder.height()-10, textAlign:"center", color:"#f4f4f4" });
		mainImage.fadeIn(1000);	
	}
	
	// private methods
	
	function redirect() {
		window.location = link;	
	}
	
	function remove() {
		overImage.remove();
	}
	
	// public methods

	this.changeIn = function() {
		var top = 0, left = 0;
		overImage = $(images[Math.floor(Math.random()*images.length)]);
		switch(sideIn) {
			case BannerSection.LEFT :
				left = -holder.width();
			break;
			case BannerSection.RIGHT :
				left = holder.width();
			break;
			case BannerSection.TOP :
				top = -holder.height();
			break;
			case BannerSection.BOTTOM :
				top = holder.height();
			break;	
		}
			overImage.css({ position:"absolute", left:left, top:top });
			holder.append(overImage);
			overImage.stop().animate({ left:0, top:0 }, BannerSection.speed);
	}
	
	this.changeOut = function() {
		switch(sideOut) {
			case BannerSection.LEFT :
				overImage.stop().animate({ left:-holder.width() }, BannerSection.speed, remove);
			break;
			case BannerSection.RIGHT :
				overImage.stop().animate({ left:holder.width() }, BannerSection.speed, remove);
			break;
			case BannerSection.TOP :
				overImage.stop().animate({ top:-holder.height() }, BannerSection.speed, remove);
			break;
			case BannerSection.BOTTOM :
				overImage.stop().animate({ top:holder.height() }, BannerSection.speed, remove);
			break;	
		}
	}
}


//////////////////

var Banner = {
	loadedSets : 0,
	loadCount : 0,
	loadCountTotal : 0,
	loadTotal : 0,
	
	loadBar : null,
	loadText : null,
	
	bannerSectionHolders : null,
	bannerSections : [],
	images : [ [], [], [], [], [] ],
	
	texts : null,
	links : null,
	
	imageSets : null,
	
	setup : function(holders, mainImages, imageSet1, imageSet2, imageSet3, imageSet4, texts, links) {
		Banner.bannerSectionHolders = holders;
		Banner.texts = texts;
		Banner.links = links;
		Banner.loadBar = $("#loadBar");
		Banner.loadText = $("#loadText");
		Banner.loadTrack = $("#loadTrack");
		
		Banner.imageSets = [ mainImages, imageSet1, imageSet2, imageSet3, imageSet4 ];
		
		for(var i in Banner.imageSets) Banner.loadTotal += Banner.imageSets[i].length;	
		
		$(Load).bind("complete", function() {
			Banner.images[Banner.loadedSets].push(Load.getImage(Banner.imageSets[Banner.loadedSets][Banner.loadCount]));
			Banner.loadCount ++;
			Banner.loadCountTotal ++;
			Banner.loadBar.width(Banner.loadTrack.width() * ( Banner.loadCountTotal / Banner.loadTotal ));
			Banner.loadText.html("Loading : "+(Math.ceil(Banner.loadCountTotal / Banner.loadTotal*100))+"%");
			if(Banner.loadedSets == Banner.imageSets.length -1 && Banner.loadCount == Banner.imageSets[Banner.imageSets.length-1].length) {
				Banner.loadTrack.animate({ top:"-=20px", opacity:0 }, 1000, function() {
					Banner.loadTrack.remove();	
				});
				Banner.setupSections();
			} else {
				if(Banner.loadCount == Banner.imageSets[Banner.loadedSets].length || Banner.imageSets[Banner.loadedSets][Banner.loadCount] == undefined) {
					Banner.loadCount = 0;
					Banner.loadedSets++;
					Load.load(Banner.imageSets[Banner.loadedSets][Banner.loadCount]);
				} else {
					Load.load(Banner.imageSets[Banner.loadedSets][Banner.loadCount]);
				}
			}
		});
		Load.load(Banner.imageSets[Banner.loadedSets][Banner.loadCount]);
	},
	
	setupSections : function() {
		for(var i = 0 ; i < 4 ; i++) {
			var side = i < 2 ? BannerSection.LEFT : BannerSection.RIGHT,
			bannerSection = new BannerSection(Banner.bannerSectionHolders.eq(i), $(Banner.images[0][i]), Banner.images[i+1], Banner.texts[i], Banner.links[i], side);
			Banner.bannerSections.push(bannerSection);
		}
		Banner.bannerSectionHolders.mouseenter(function(e) { 
			Banner.bannerSections[Banner.bannerSectionHolders.index(this)].changeIn();
		}).mouseleave(function(e) { 
			Banner.bannerSections[Banner.bannerSectionHolders.index(this)].changeOut();
		});
	}
}
