/* ------------------------------------
*
*	Since : 2008-07-24
*	Editor : D_sasaki
*
-------------------------------------*/

// Set SlideClass Class

var SlideClass = function(obj){				
	//slide target index
	this.nextIndex = obj.nextIndex || 0;
	//max index
	this.maxPos = obj.maxPos;
	//content width
	this.width = obj.width;		
	//slide Speed
	this.slideSpeed = obj.slideSpeed || 800;		
	//easing type
	this.easing = obj.easing;
	
	//Individually
	this.individually = obj.individually;
	this.individuallyElm = 	obj.individuallyElm;
	
	// toIndex
	this.toIndex = obj.toIndex;
	this.toIndexLink = obj.toIndexLink;
	this.toIndexImg = obj.toIndexImg;
	this.toIndexImgName = obj.toIndexImgName;
	this.linkHash = obj.linkHash;
	
	//element
	this.slideBox = obj.slideBox;
	this.prev = obj.prev;
	this.next = obj.next;
	
	//set EventListener
	this.prev.bind("click",this,this.prevAction);
	this.prev.bind("focus",function(){
		this.blur();
	});
	this.next.bind("click",this,this.nextAction);
	this.next.bind("focus",function(){
		this.blur();
	});
	
	//set defaultAction
	this.defaultAction();
};

SlideClass.prototype = {
	setMaxWidth : function(){
		var self = this;
		var maxWidth = Number(this.maxPos + 1) * (this.width + 3);
		self.slideBox.css("width",maxWidth + "px");
	},
	nextPos: function(){
		return (this.nextIndex * this.width) 		
	},
	setToIndexLink : function (){
		this.toIndexLink
		.attr("href",this.linkHash[this.nextIndex])
		
		var orgSrc = this.toIndexImg.attr("src");
		var pos = orgSrc.lastIndexOf("/");
		var src = orgSrc.substr(0,Number(pos + 1)) + this.toIndexImgName +this.nextIndex + ".gif";
		this.toIndexImg.attr("src",src)
	},
	defaultAction : function(){
		
		//hide btn
		if(this.nextIndex == 0) {
			this.prev.addClass("hide");
		}
		if(this.nextIndex == this.maxPos) {
			this.next.addClass("hide");
		}	
		
		this.setMaxWidth();
		
		//set defaultPosition
		if(this.nextIndex != 0) {
			var defaultPosition = -(this.nextIndex * this.width)
			this.slideBox
			.css({left:defaultPosition});
		};
		
		if(this.toIndex) this.setToIndexLink();	
		
		//individually
		if(this.individually){
			this.toggleIndividually();
		};	
	},
	toggleIndividually : function(){
		if(this.individuallyElm[this.nextIndex]){
			this.individuallyElm[this.nextIndex].show();
		}else{
			for(var i = 0; i < this.individuallyElm.length; i++){
				if(this.individuallyElm[i]){
					this.individuallyElm[i].hide();
				}
			};	
		}		
	},
	slideContent : function(){
		//hide "prev" and "next"
		if(this.nextIndex == 0) {
			this.prev.addClass("hide");
			this.next.removeClass("hide");
		} else if(this.nextIndex == this.maxPos){
			this.prev.removeClass("hide");
			this.next.addClass("hide");		
		} else {
			this.prev.removeClass("hide");
			this.next.removeClass("hide");	
		}
		
		//individually
		if(this.individually){
			this.toggleIndividually();
		};
		
		// slide box
		this.slideBox
		.animate({left:-this.nextPos() + "px"},this.slideSpeed,this.easing)	
	},
	prevAction : function(event){
		
		//"self" is "SlideClass"
		var self = event.data;

		if(self.nextIndex == 0) return false;
		
		//if slideBox is animated now, stop animation.
		var now = Math.abs(self.slideBox.css("left").split("px")[0])
		var flag = (now % self.width)
		if(flag) self.slideBox.stop();
			
		self.nextIndex--;

		//slide slideBox.
		self.slideContent();
		// change toIndexLink
		if(self.toIndexLink) self.setToIndexLink();		
			
		return false;
	},
	nextAction : function(event){
		
		//"self" is "SlideClass"
		var self = event.data;
		
		if(self.nextIndex == self.maxPos) return false;
		
		//if slideBox is animated now, stop animation.
		var now = Math.abs(self.slideBox.css("left").split("px")[0])
		var flag = (now % self.width)
		if(flag) self.slideBox.stop();	
		
		self.nextIndex++;
		
		//slide slideBox.
		self.slideContent();
		// change toIndexLink
		if(self.toIndexLink) self.setToIndexLink();			
		
		return false;		
	}	
};


(function($){
	$.extend({
		ChangeModel : function(obj){
			
			//　注意！！！！！
			//　htmlページ内で複数の商品で使用する場合、その数ぶんnewする必要がある
			//	その際、各エレメントのIDを変更させる必要あり（#selectColor,#selectColor2　など）
			
			
			//element
			var selectMenu = obj.selectMenu;
			var targetImg = obj.targetImg;
			var targetH2 = obj.targetH2;
			var targetP = obj.targetP;
			var targetMain = obj.targetMain;
			var targetIcon = obj.targetIcon;
			
			//text
			var h2Text = obj.h2Text || [];
			var pText = obj.pText || [];
			var mainText = obj.mainText || [];
			
			//set utility
			var targetSrc = targetImg.attr("src");
			var pos = targetSrc.lastIndexOf("/");
			
			//change icon
			var changeIcon = obj.changeIcon;
			var showIconIndex = obj.showIconIndex;
					
			//set model [img text]
			function setModel(model){				
				var src = targetSrc.substr(0,Number(pos + 1)) + model.imgName;
				targetImg.attr("src",src);
				if(targetH2){
					targetH2.html(model.h2Inner);
				}
				if(targetP){
					targetP.html(model.pInner);
				}
				if(targetMain){
					targetMain.html(model.mainInner);	
				}
			}
			
			//changeIcon function
			function changeIconFunc(i){
				
				for(var j = 0; j < showIconIndex.length; j++){
					if(showIconIndex[j] == i) {
						targetIcon.show();
						break;
					}else {
						targetIcon.hide();
					}
				}
		
			}
			
			// toggle Btn 
			function toggleBtn(node){
				node.addClass("on");
				node.parent().siblings().children().removeClass("on");			
			}
			
			//event
			selectMenu.each(function(i){
				$(this).click(function(){
					if(changeIcon){
						changeIconFunc(i);
					}
					var model ={
						imgName : $(this).attr("href"),
						h2Inner : h2Text[i],
						pInner : pText[i],
						mainInner : mainText[i]
					}		
					setModel(model);
					toggleBtn($(this));
					return false;
				})
			}).focus(function(){
				$(this).blur();
			})							
		},
		ChangeArt : function(obj){
			// authorHashは、a,bで同じ作者の場合は、値を同じにする。↓例
			// a : "http://www.sunfield.ne.jp/nrtabata/",
			// b : "http://www.sunfield.ne.jp/nrtabata/"
			
			var obj = $.extend({
				authorHash : [
					{	
						a : "http://www.sunfield.ne.jp/nrtabata/",
						b : "http://www.padico.co.jp/gallery/takahasi_y/index.html"
					},
					null,
					{	
						a : "http://www.wovn.com/faicraft/",
						b : "http://www.padico.co.jp/gallery/tokoro/index.html"
					},
					{
						a : "http://www.geocities.jp/kyozo_hayashi/"
					},
					{	
						a : "http://www.padico.co.jp/gallery/inomata/index.html"
					},
					null,
					null
				],
				hash : hash,
				nextIndex : nextIndex,
				content : $(".moveContent"),
				sum : $(".sum")
						
			}, obj || {});	
			
			if(obj.hash.indexOf("ex") > -1){
				var author = $(obj.content).eq(Number(obj.nextIndex)); //なぜかobj.nextIndexがstringになるのです
				
				author
				.find("h1")
				.children("img")
				.attr("src","../images/art_h1_" + obj.nextIndex + "_b.jpg")
				.end()
				.end()
				.find(".bigImg")
				.children("img")
				.attr("src","../images/art_pic_" + obj.nextIndex + "_b.jpg")
				
				//作者名とurlをセット
				if(obj.authorHash[obj.nextIndex]){
					var authorImg = author
						.find(".sum")
						.next("p")
						.find("img")
					if(obj.authorHash[obj.nextIndex]["b"]) {
						authorImg
						.attr("src","../images/author_" + obj.nextIndex + "_b.gif")
						.parent("a")
						.attr("href",obj.authorHash[obj.nextIndex]["b"]);
					}else{
						authorImg.hide();		
					}			
				};	
				
				obj.sum.eq(Number(obj.nextIndex)).find("a").toggleClass("on");	
			}	
			
			$(obj.sum).each(function(){
				var bigImg = $(this).parent().parent().children(".bigImg").children("img");
				var titleImg = $(this).siblings("h1").children("img");
				var authorImg = $(this).next("p").find("img");
				
				$(this).find("a").click(function(){
					var href = $(this).attr("href");
					var bigImgSrc = "../images/art_pic_" + href + ".jpg";
					var titleImgSrc = "../images/art_h1_" + href + ".jpg";
					var authorImgSrc = "../images/author_" + href + ".gif";
					var nowIcon = href.split("_")[1]
					var nextIndex = $(".sum").index($(this).parent().parent("ul"));
					
					if(obj.authorHash[nextIndex]){
						var authorlink = obj.authorHash[nextIndex][nowIcon];
					}
		
					bigImg.attr("src",bigImgSrc);
					titleImg.attr("src",titleImgSrc);
								
					if(authorImg && authorlink) {
						authorImg
						.show()
						.attr("src",authorImgSrc)
						.parent("a")
						.attr("href",authorlink);
					}else{
						authorImg.hide();
					}			
					
					$(this)
					.addClass("on")
					.parent()
					.siblings("li")
					.children("a")
					.removeClass("on");
					
					return false;
				}).focus(function(){
					$(this).blur();
				})
			})			
			
			
		}
	})
})(jQuery);








