(function( window, undefined ) {
	var _fshslide = {
		max: 5
		,current: 1
		,timer: false
		,interval: 7000
		,slide_box: 'Top_MainSlide_image'
		,remove_class_name: '__remove_image'
		,switch_prefix: 'Top_MainSlide_switch_'
		,switch_off_image: '/img/slide/switch_off.png'
		,switch_on_image: '/img/slide/switch_on.png'
		,binding: {1:{image:'http://www.fourseasons-tokyo.com/img/slide/index_xmass2012.jpg', url:'http://www.fourseasons-tokyo.com/event/000438.html', target:'_self'}}
		,setImage: function(image_no) {
			if (image_no - this.current == 0) {
				return;
			}
			if (this.binding[image_no]) {
				var item = this.binding[image_no];
				var newImage = $("<img src=\""+item.image+"\" alt=\"メインイメージ\" class=\"__new_image\" />");
				var link = $("#"+this.slide_box+" a").attr("href", item.url).attr("target", item.target);
				link.find("img").addClass(this.remove_class_name);
				link.append(newImage);
				var temp = this.remove_class_name;
				newImage.fadeIn("slow", function() {
					$("."+temp).remove();
				});
				$("#"+this.switch_prefix+this.current+" img").attr("src",this.switch_off_image);
				$("#"+this.switch_prefix+image_no+" img").attr("src",this.switch_on_image);
				this.current = image_no;
				this.restart();
			}
		}
		,nextImage: function() {
			var temp = this.current+1;
			if (temp > this.max) {
				temp = 1;
			}
			this.setImage(temp);
		}
		,prevImage: function() {
			var temp = this.current-1;
			if (temp <= 0) {
				temp = 5;
			}
			this.setImage(temp);
		}
		,dataBind: function(context) {
			this.binding = context;
			for(var i = 1; i <= this.max; i++) {
				var img = new Image();
				img.src = this.binding[i].image;
			}
		}
		,start: function() {
			var _this = this;
			if (!this.timer) {
				this.timer = setInterval(function(){_this.nextImage()}, this.interval);
			}
		}
		,stop: function() {
			if(this.timer) {
				clearTimeout(this.timer);
			}
			this.timer = false;
		}
		,restart: function() {
			this.stop();
			this.start();
		}
	};
	window.fshslide = _fshslide;
})(window);

$(function(){
	if(links) {
		fshslide.dataBind(links);
	}
	$(".Top_MainSlide_button").mouseover(function(){
		$(this).css("cursor", "pointer");
		$("#Top_MainSlide_prev").show();
		$("#Top_MainSlide_next").show();
	}).mouseout(function(){
		$(this).css("cursor", "default");
		$("#Top_MainSlide_prev").hide();
		$("#Top_MainSlide_next").hide();
	}).click(function(){
		var id = $(this).attr("id");
		if (id == "Top_MainSlide_next") {
			fshslide.nextImage();
		}
		else if (id == "Top_MainSlide_prev") {
			fshslide.prevImage();
		}
		else {
			var num = id.match(/[0-9]+/i);
			if (!num) {
				num = 1;
			}
			num = parseInt(num);
			fshslide.setImage(num);
		}
	});
	$("#Top_MainSlide_image").mouseover(function(){
		$("#Top_MainSlide_prev").show();
		$("#Top_MainSlide_next").show();
	}).mouseout(function(){
		$("#Top_MainSlide_prev").hide();
		$("#Top_MainSlide_next").hide();
	});
	fshslide.start();
});
