var j$ = jQuery;

//name space
var jp;
if (typeof(jp) == "undefined") jp = {};
if (typeof(jp.italiaIn) == "undefined") jp.italiaIn = {};
if (typeof(jp.italiaIn.nw) == "undefined") jp.italiaIn.nw = {};
(function() {
var ns = jp.italiaIn.nw;


/**
 * プロモーション広告
 */
ns.PromotionAd = function () {
    this.initialize.apply(this, arguments);
}
ns.PromotionAd.prototype = {
    initialize: function() {
		this.images = j$(".mainph #mainPh li");
		this.images.each(function(){
			j$(this).hide();
		});
		this.count = 0;
		this.images.eq(this.count).show();
		
		j$(".mainph")
		.bind("mouseenter", { "self":this }, function(event){ 
			event.data.self.stopChange();
			return false;
		})
		.bind("mouseleave", { "self":this }, function(event){ 
			event.data.self.startChange();
			return false;
		});

		this.nav = j$(".mainph .nav li");
		for(var i=0; i<this.nav.length; i++){
			this.nav.eq(i).bind("click", {"self":this }, function(event){
				var id = j$(this).attr("id");
				//console.info(id);
				event.data.self.changeImage(id-1);
				return false;
			})
			this.setMouseEvent(this.nav.eq(i));
		}
		var img = this.nav.eq(this.count).find("a img");
		img.attr("src", img.attr("src").replace("_out.", "_over."));
		
		this.startChange();
    },
	
	setMouseEvent : function(btn){
		btn.bind("mouseenter", {"self":this }, function(event){
			event.data.self.setIamgeOver(this);
			return false;
		}).bind("mouseleave", {"self":this }, function(event){
			event.data.self.setIamgeOut(this);
			return false;
		});
	},
	
	setIamgeOver: function(btn){
		var img = j$(btn).find("a img");
		img.attr("src", img.attr("src").replace("_out.", "_over."));
	},
	
	setIamgeOut: function(btn){
		var img = j$(btn).find("a img");
		img.attr("src", img.attr("src").replace("_over.", "_out."));
	},
	
    changeImage: function(val) {
		var id = val;
		if(this.count == id){
			return;
		}
		var prev = this.count;
		if (typeof id == 'undefined') {
			this.count ++;
		}else {
			this.count = id;
		}
		if( this.count >= this.images.length ){
			this.count = 0;
		}
		this.images.eq(prev).fadeOut("slow");
		this.images.eq(this.count).fadeIn("slow");
		
		var img = this.nav.eq(prev).find("a img");
		img.attr("src", img.attr("src").replace("_over.", "_out."));
		
		this.setMouseEvent(this.nav.eq(prev));
		
		var img = this.nav.eq(this.count).find("a img");
		img.attr("src", img.attr("src").replace("_out.", "_over."));
		
		this.nav.eq(this.count).unbind("mouseenter");
		this.nav.eq(this.count).unbind("mouseleave")
		
		var self = this;
		clearTimeout(this.itv);
		this.itv = setTimeout( function(){ self.changeImage() }, 15000);
    },
	
	startChange: function(){
		var self = this;
		clearTimeout(this.itv);
		this.itv = setTimeout( function(){ self.changeImage() }, 15000);
	},
	
	stopChange: function(){
		clearTimeout(this.itv);
		this.itv = null;
	}
};


})();

function updateList(date){
	alert(date);
};

j$(document).ready(function(){
	if (j$(".mainph").get()[0]) {
		var promotionAd = new jp.italiaIn.nw.PromotionAd();
	}
});

