$(document).ready(function(){
	$("#superDiscount_joinLink").jHelperTip({
		trigger: "hover", 
		dC: "#superDiscount_joinPanel",
		opacity: 0.9
	});
	
	$("#priceBattle_joinLink").jHelperTip({
		trigger: "hover", 
		dC: "#priceBattle_joinPanel",
		opacity: 0.9
	});		
    
	AdRollImage.run();
	ProductAd.run();
	CommonAd.run();
	PriceBattle.run();
});

var AdRollImage = new Object();
AdRollImage.imageList;
AdRollImage.indexList;
AdRollImage.processor;
AdRollImage.interval = 3000;
AdRollImage.selected = 0;
AdRollImage.run = function(){
	this.imageList = $("#adRollImagePanel ul.imageList li");
	this.indexList = $("#adRollImagePanel ul.indexList a");
	
	this.imageList.first().show();
	this.indexList.first().addClass("selected");
	this.processor = window.setTimeout("AdRollImage.goNext()", this.interval);
}
AdRollImage.go = function(index){
	if(index != this.selected){
		this.imageList.hide();
		
		var next = this.imageList.eq(index);
		next.fadeIn("slow");
		this.indexList.removeClass("selected");
		this.indexList.eq(index).addClass("selected");	
		
		this.selected = index;
		window.clearTimeout(this.processor);
		this.processor = window.setTimeout("AdRollImage.goNext()", this.interval);
	}
}
AdRollImage.goNext = function(){
	var index = this.selected + 1;
	if(index >= this.imageList.size()){
		index = 0;
	}	
	this.go(index);
}

var ProductAd = new Object();
ProductAd.panel;
ProductAd.target;
ProductAd.processor;
ProductAd.size;
ProductAd.index = 0;
ProductAd.itemWidth = 155;
ProductAd.itemNum = 3;
ProductAd.delay = 6000;
ProductAd.speed = 1000;
ProductAd.run = function(){
	this.panel = $("#productAdPanel");
	this.target = $("#productAdPanel ul");
	this.size = this.target.find("li").size();
	
	this.panel.css("width", this.itemWidth*this.itemNum+"px").css("position", "relative").css("overflow", "hidden");
	this.target.css("width", this.itemWidth*this.size+20+"px").css("position", "absolute").css("left", "0px");

	this.processor = window.setTimeout("ProductAd.active()", this.delay);

	this.panel.hover(
		function(){
			ProductAd.pause();
		},
		function(){
			ProductAd.resume();
		}
	);
}
ProductAd.active = function(){
	this.index = parseInt(this.index + this.itemNum);
	if(this.index >= this.size){
		this.index = 0;
		this.target.animate({left: "-"+parseInt(this.size*this.itemWidth)+"px"}, this.speed, function(){
			ProductAd.target.css("left", ProductAd.itemWidth*ProductAd.itemNum+"px");
			ProductAd.target.animate({left: "0px"}, ProductAd.speed);
		});
	}
	else{
		this.target.animate({left: "-"+parseInt(this.index*this.itemWidth)+"px"}, this.speed);
	}
	this.processor = window.setTimeout("ProductAd.active()", this.delay);
}
ProductAd.pause = function(){
	window.clearTimeout(this.processor);
}
ProductAd.resume = function(){
	this.processor = window.setTimeout("ProductAd.active()", this.delay);
}

var CommonAd = new Object();
CommonAd.panel;
CommonAd.target;
CommonAd.processor;
CommonAd.size;
CommonAd.index = 0;
CommonAd.itemHeight = 90;
CommonAd.itemNum = 3;
CommonAd.delay = 6000;
CommonAd.speed = 1000;
CommonAd.run = function(){
	this.panel = $("#commonAdPanel");
	this.target = $("#commonAdPanel ul");
	this.size = this.target.find("li").size();
	
	this.panel.css("height", this.itemHeight*this.itemNum+"px").css("position", "relative").css("overflow", "hidden");
	this.target.css("position", "absolute").css("top", "0px");

	this.processor = window.setTimeout("CommonAd.active()", this.delay);

	this.panel.hover(
		function(){
			CommonAd.pause();
		},
		function(){
			CommonAd.resume();
		}
	);
}
CommonAd.active = function(){
	this.index = parseInt(this.index + this.itemNum);
	if(this.index >= this.size){
		this.index = 0;
		this.target.animate({top: "-"+this.size*this.itemHeight+"px"}, this.speed, function(){
			CommonAd.target.css("top", CommonAd.itemHeight*CommonAd.itemNum+"px");
			CommonAd.target.animate({top: "0px"}, CommonAd.speed);
		});
	}
	else{
		this.target.animate({top: "-"+this.index*this.itemHeight+"px"}, this.speed);
	}
	this.processor = window.setTimeout("CommonAd.active()", this.delay);
}

CommonAd.pause = function(){
	window.clearTimeout(this.processor);
}
CommonAd.resume = function(){
	this.processor = window.setTimeout("CommonAd.active()", this.delay);
}

var PriceBattle = new Object();
PriceBattle.itemList;
PriceBattle.indexList;
PriceBattle.processor;
PriceBattle.interval = 8000;
PriceBattle.selected = 0;
PriceBattle.run = function(){
	this.itemList = $("#priceBattlePanel ul.itemList");
	this.indexList = $("#priceBattlePanel ul.indexList a");
	
	this.itemList.first().show();
	this.indexList.first().addClass("selected");
	this.processor = window.setTimeout("PriceBattle.goNext()", this.interval);
}
PriceBattle.go = function(index){
	if(index != this.selected){
		this.itemList.hide();
		
		var next = this.itemList.eq(index);
		next.fadeIn("fast");
		this.indexList.removeClass("selected");
		this.indexList.eq(index).addClass("selected");	
		
		this.selected = index;
		window.clearTimeout(this.processor);
		this.processor = window.setTimeout("PriceBattle.goNext()", this.interval);
	}
}
PriceBattle.goNext = function(){
	var index = this.selected + 1;
	if(index >= this.itemList.size()){
		index = 0;
	}	
	this.go(index);
}
