
if(isIE6) {
	DD_belatedPNG.fix("img, #thumbnail");
}




var promo = new Promo();

function Promo(){
	var instance = this;
	var isMoving = true;
	var currentKey = 0;
	var promoArea = document.getElementById("promo");
	var divs = promoArea.getElementsByTagName("div");
	
	
	var promoImages = new Array();
	for(var i=0; i<divs.length; i++) {
		var div = divs[i];
		if(div.className == "promo-image") {
			promoImages.push(div);
			div.style.position = "absolute";
		}
	}
	
	var thumbnailArea = document.getElementById("thumbnail");
	var thumbnailList = thumbnailArea.getElementsByTagName("ul")[0];
	var thumbnails = thumbnailList.getElementsByTagName("li");
	
	var nav = thumbnailArea.getElementsByTagName("ul")[1];
	var prevBtn = nav.getElementsByTagName("li")[0];
	var nextBtn = nav.getElementsByTagName("li")[1];
	
	
	
	this.preload = function() {
		var images = ["arrow_l_left_active.gif", "arrow_l_right_active.gif"];
		for(var i=0; i<images.length; i++) {
			var img = new Image();
			img.src = "/shared/img/" + images[i];
		}
	}
	
	
	
	
	this.fadeIn = function(node) {
		instance.isMoving = true;
		new Effect.Opacity(node, {
			from: 0.0,
			to: 1.0,
			fps: 30,
			duration: 1.0,
			delay: 0,
			restoreAfterFinish: true,
			afterSetup: function(effect) {
				instance.setIndexAllImages(effect.element);
				effect.element.style.display = "block";
				effect.element.style.zIndex = "20";
			},
			beforeStartInternal: function(effect) {
			},
			afterFinishInternal: function(effect) {
				instance.hideAllImages(effect.element);
				instance.isMoving = false;
			}
		});
	}
	
	
	this.setIndexAllImages = function(ignoreNode) {
		for(var i=0; i<promoImages.length; i++) {
			var div = promoImages[i];
			if(ignoreNode && div !== ignoreNode) {
				div.style.zIndex = "10";
			}
		}
	}
	
	
	this.hideAllImages = function(ignoreNode) {
		for(var i=0; i<promoImages.length; i++) {
			var div = promoImages[i];
			if(ignoreNode && div !== ignoreNode) {
				div.style.display = "none";
			}
		}
	}
	
	this.showImage = function(key, mode) {
		var div = promoImages[key];
		
		if(mode == "direct") {
			this.hideAllImages(div);
		}
		else {
			this.fadeIn(div);
		}
		
		this.toggleThumbnail(key);
	}
	
	
	this.toggleThumbnail = function(key) {
		for(var i=0; i<thumbnails.length; i++) {
			var thumb = thumbnails[i];
			var btn = thumb.getElementsByTagName("a")[0];
			if(i == key) {
				btn.className = "active";
			}
			else {
				btn.className = "";
			}
		}	
	}
	
	
	
	
	
	this.switchImage = function(key) {
		if(this.isMoving) {
			return;
		}
		
		currentKey = key;
		this.showImage(currentKey);
	}
	
	
	this.gotoImage = function(mode) {
		if(this.isMoving) {
			return;
		}
		
		if(mode == "prev") {
			currentKey--;
			if(currentKey < 0) {
				currentKey = thumbnails.length - 1;
			}
		}
		else if(mode == "next") {
			currentKey++;
			if(currentKey > thumbnails.length - 1) {
				currentKey = 0;
			}
		}
		this.showImage(currentKey);
	}
	
	

	this.setThumbnails = function() {
		var thumbnailWidth = 110 * thumbnails.length;
		var offset = Math.floor( (900 - thumbnailWidth) / 2 );
		thumbnailList.style.marginLeft = offset + "px";
		prevBtn.style.left = (offset - 30) + "px";
		nextBtn.style.left = (offset + thumbnailWidth) + "px";
		
		for(var i=0; i<thumbnails.length; i++) {
			var thumb = thumbnails[i];
			var btn = thumb.getElementsByTagName("a")[0];
			btn.key = i;
			btn.onclick = function() {
				instance.switchImage(this.key);
			}
		}
		
		prevBtn.onclick = function() {
			instance.gotoImage("prev");
		}
		nextBtn.onclick = function() {
			instance.gotoImage("next");
		}
		
		prevBtn.onmouseover = this.mouseoverBtn;
		prevBtn.onmouseout = this.mouseoutBtn;
		nextBtn.onmouseover = this.mouseoverBtn;
		nextBtn.onmouseout = this.mouseoutBtn;
	}
	
	

	this.mouseoverBtn = function() {
		var img = this.getElementsByTagName("img")[0];
		img.src = img.src.replace(".gif", "_active.gif");
	}
	this.mouseoutBtn = function() {
		var img = this.getElementsByTagName("img")[0];
		img.src = img.src.replace("_active.gif", ".gif");
	}
	
	
	this.preload();
	this.setThumbnails();
	this.showImage(0, "direct");
}



