/*toaruR gallary ('A`) kluge*/

$(document).ready(function(){

	jQuery.easing['jswing'] = jQuery.easing['swing'];
	jQuery.extend( jQuery.easing, {
		easeInOutQuad: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t + b;
			return -c/2 * ((--t)*(t-2) - 1) + b;
		}
	});
	
	
	
	//カテゴリ選択ナビ
	$(".basedata li").click(
		function () {
			$(".basedata li span").removeClass('supercurrent');
			$("span",this).addClass('supercurrent');
			G.show($("span",this).attr('id').replace('category-',''));
		}
	);
	$(".basedata li span").hover(
		function () {
			$(this).addClass('current');
		},
		function () {
			$(this).removeClass('current');
		}
	);
	//メインナビ
	$("#main-back").click(
		function () {
			G.watchimageback();
		}
	);
	$("#main-back").hover(
		function () {
			$("#main-back").css('background-image','url(/images/main-back-over.gif)');
		},
		function () {
			$("#main-back").css('background-image','url(/images/main-back.gif)');
		}
	);
	$("#main-next").click(
		function () {
			G.watchimagenext(); 
		}
	);
	$("#main-next").hover(
		function () {
			$("#main-next").css('background-image','url(/images/main-next-over.gif)');
		},
		function () {
			$("#main-next").css('background-image','url(/images/main-next.gif)');
		}
	);
	//サムネイルナビ
	$("#thumb-back").hover(
		function () {
			$("#thumb-back").css('background-position','-36px 0');
			G.scrollthumbs('right');
		},
		function () {
			$("#thumb-back").css('background-position','0 0')
			G.scrollthumbs('stop');
		}
	);
	$("#thumb-next").hover(
		function () {
			$("#thumb-next").css('background-position','-36px 0');
			G.scrollthumbs('left');
		},
		function () {
			$("#thumb-next").css('background-position','0 0');
			G.scrollthumbs('stop');
		}
	);

	var Rgallery = function() {
		this.campuswidth = 963;	//キャンパス幅
		this.currentbook = 1;	//カレントブック番号
		this.book = [];			//ブックデータ
		this.setup();
	}

	var Rbook = function( num,campuswidth ) {
		this.mynum = num;
		this.image = [];	//画像データ
		this.thumbid;		//サムネイルセットのID
		this.currentimg = 1;//カレント画像番号
		this.maximg;		//最大画像番号
		this.bookwidth;		//サムネイルズの幅
		
		this.campuswidth = campuswidth;
		this.setup();
	}

	var Rimage = function( num,url,id,campuswidth,bookwidth ) {
		this.mynum = num;	//画像番号
		this.url = url;		//拡大データのURL
		this.thumbid = id;	//サムネイルセットのID
		
		this.campuswidth = campuswidth;
		this.bookwidth = bookwidth;
		this.setup();
	}

	Rgallery.prototype = {
		setup: function() {
			//カテゴリ一覧取得
			var self = this;
			$(".basedata li span").each(function(){
				var temp = $(this).attr('id').replace('category-','');
				self.book[temp] = new Rbook(temp,self.campuswidth);
			});
		},
		show: function( num,img ) {
			if ( Number(num) <= 0 || num == null ) {
				num = this.currentbook;
			} else {
				this.currentbook = Number(num);
			}
			if ( Number(img) <= 0 || img == null ) {
				img = this.book[num].currentimg;
			} else {
				this.book[num].currentimg = Number(img);
			}
			//サムネイルセット切り替え
			this.book[num].show();
			//メインナビ表示
			this.show_mainnavi();
			//サムネイルの移動と表示
			this.reposition(img);
		},
		show_mainnavi: function( num ) {
			var targetbook = this.book[this.currentbook];
			var targetimg = targetbook.image[targetbook.currentimg];
			if ( num == null ) num = targetimg.mynum;
			
			if (Number(num) > 1) {
				$("#main-back").fadeIn("fast");
			} else {
				$("#main-back").fadeOut("fast");
			}
			if (Number(num) < targetbook.maximg) {
				$("#main-next").fadeIn("fast");
			} else {
				$("#main-next").fadeOut("fast");
			}
		},
		reposition: function( num ) {
			var self = this;
			var targetbook = this.book[this.currentbook];
			var targetimg = targetbook.image[targetbook.currentimg];
			if ( num == null ) num = targetimg.mynum;
			
			var selecter = targetbook.thumbid+' a.t'+num;
			
			//全部絶対位置。移動後の相対位置ではない
			var position = $(selecter).position();
			var leftend = $(targetbook.thumbid+' a.t1').position().left;
			var rightend = leftend + targetbook.bookwidth;
			var mywidth = $(selecter).innerWidth();
			var targetposition = Math.floor(  (this.campuswidth + mywidth)/2  );
			
			/*
			alert(
				 'selecter'+':'+selecter+'\n'
				+'position.left'+':'+position.left+'\n'
				+'leftend'+':'+leftend+'\n'
				+'rightend'+':'+rightend+'\n'
				+'bookwidth'+':'+targetbook.bookwidth+'\n'
				+'campuswidth'+':'+this.campuswidth+'\n'
			);
			*/
			var head;
			
			//ブック幅がキャンパス幅以下の場合左寄せ
			if(targetbook.bookwidth <= this.campuswidth) {
				head = 0;
				//alert('miman');
			} else {
				//中央に寄せる
				//左が余白になる場合は左寄せ
				if (targetposition > position.left-mywidth/2 ) {
					head = 0;
					temp=position.left-mywidth/2;
					//alert('leftset'+':'+leftend+':'+targetposition+'>'+temp);
				//右が余白になる場合は右寄せ
				} else if (targetposition > rightend - position.left) {
					//head = -1*( rightend - this.campuswidth);
					head = -1*( targetbook.bookwidth - this.campuswidth);
					//alert('rightset');
				} else {
					head = targetposition - position.left - mywidth;
					//alert('centerset');
				}
			}
			
			$(targetbook.thumbid).animate({ left:head },1000,function() {
				self.show_thumbnavi();
			});
		},
		show_thumbnavi: function( num ) {
			//仮アニメを入れてそのコールバックにすることで待機
			//$("#header").show(0,this.show_thumbnavimain(num) );
			//$(this.book[this.currentbook].thumbid).show(0,this.show_thumbnavimain(num) );
			//},
			//show_thumbnavimain: function( num ) {
			var targetbook = this.book[this.currentbook];
			var targetimg = targetbook.image[targetbook.currentimg];
			if ( num == null ) num = targetimg.mynum;
			
			//var relativetop = $("#thumbnails .mask").offset().left;
			//temp = relativetop - $(targetbook.thumbid).offset().left;
			
			var nowtop = $(targetbook.thumbid).position().left;
			var nowtail = nowtop + targetbook.bookwidth;
			
			//左に飛び出ていれば左サムナビを出す
			if ( Number(nowtop)  < 0 ) {
				$("#thumb-back").fadeIn("slow").fadeTo("slow", 0.80);
			} else {
				$("#thumb-back").fadeOut("slow");
			}
			//同、右
			if ( Number(nowtail) > this.campuswidth ) {
				$("#thumb-next").fadeIn("slow").fadeTo("slow", 0.80);
			} else {
				$("#thumb-next").fadeOut("slow");
			}
			
		},
		watchimageback: function() {
			var targetbook = this.book[this.currentbook];
			var num = targetbook.currentimg;
			
			if ( Number(num) > 1 ) {
				num -= 1;
				targetbook.currentimg = num;
				//メイン画像とサムネイル枠表示
				targetbook.image[num].show();
				//メインナビ表示
				this.show_mainnavi();
				//サムネイルの移動と表示
				this.reposition(num);
			}
		},
		watchimagenext: function() {
			var targetbook = this.book[this.currentbook];
			var num = targetbook.currentimg;
			
			if ( Number(targetbook.maximg) > 1 ) {
				num += 1;
				targetbook.currentimg = num;
				//メイン画像とサムネイル枠表示
				targetbook.image[num].show();
				//メインナビ表示
				this.show_mainnavi();
				//サムネイルの移動と表示
				this.reposition(num);
			}
			/*
			alert(num+':'
			+this.book[this.currentbook].image[this.book[this.currentbook].currentimg].url+'\n'
			+this.book[this.currentbook].currentimg+'\n'
			);*/
		},
		scrollthumbs: function(direction) {
			var self = this;
			var targetbook = this.book[this.currentbook];
			var to_position;
			var speed;
			
			if (direction == 'left') {
				to_position = this.campuswidth-targetbook.bookwidth;
				speed = this.get_speed(to_position);
				$(targetbook.thumbid).stop()
				.animate({ left:to_position-7 },speed,'easeInOutQuad' )
				.animate({ left:"+=7px" },200 ,function() {
					self.show_thumbnavi();
				});
			} else if (direction == 'right') {
				to_position = 0;
				speed = this.get_speed(0);
				$(targetbook.thumbid).stop()
				.animate({ left:to_position+7 },speed,'easeInOutQuad' )
				.animate({ left:"-=7px" },200 ,function() {
					self.show_thumbnavi();
				});
			} else {
				$(targetbook.thumbid).stop();
				self.show_thumbnavi();
			}
		},
		get_speed: function(to_position) {
			from_position = $(this.book[this.currentbook].thumbid+" li:first").offset().left - $("#thumbnails .mask").offset().left;
			var speed = from_position - to_position;
			if (speed < 0) speed *= -1;
			speed *= 2.5;
			return speed;
		}
	}
	

	Rbook.prototype = {
		setup: function() {
			//サムネイル一覧取得
			var self = this;
			self.thumbid = "#thumbnail-"+self.mynum;
			self.bookwidth = $(self.thumbid+" li:last").position().left + $(self.thumbid+" li:last").innerWidth();
			var thumbnum;
			$(self.thumbid +" a").each( function(){
				thumbnum = Number($(this).attr('class').replace('t',''));
				//var temp = $(this).attr('href').match(/.*\(\'(.*?)\'.*/);
				//self.image[thumbnum] = new Rimage(thumbnum,temp[1],'#thumbnail-'+self.mynum,self.campuswidth,self.bookwidth);
				
				var temp = $(this).attr('href');
				$(this).attr({
					className: 't'+thumbnum,
					href:'#zoom'+thumbnum
				});
				self.image[thumbnum] = new Rimage(thumbnum,temp,self.thumbid,self.campuswidth,self.bookwidth);
				$(self.thumbid+' li a.t'+thumbnum).click( function() {
					G.show(0,$(this).attr('href').replace('#zoom',''));
				});
				
			});
			self.maximg = thumbnum;
			//変則非表示の解除
			$(self.thumbid).hide();
			$(self.thumbid).css('padding','0');
		},
		show: function() {
			var num = this.mynum;
			var adjust = 0;
			var thumid = '#thumbnail-'+num;
			$("#thumbnails ul:not("+thumid+")").fadeOut("slow");
			$(thumid).show();
			//カレント画像表示
			this.image[this.currentimg].show();
		}
	}

	Rimage.prototype = {
		setup: function() {
			var self = this;
		},
		show: function() {
			var self = this;
			//メイン画像
			$("#mainimage").fadeOut("fast",function() {
				$("#mainimage").css('background-image','url('+ self.url +')');
				$("#mainimage").fadeIn("fast");
			});
			//サムネイル枠
			$(this.thumbid+" a:not(.t"+this.mynum+') img').css('border-color','#000000');
			$(this.thumbid+' a.t'+this.mynum+' img').css('border-color','#ffffff');
		}
	}
	
	//プロフィールオープン
	$("a.openprofile").attr({ href:'#openprofile' });
	$("a.openprofile").click(
		function () {
			P.show();
		}
	);
	//クローズ
	$("a.closeprofile,").attr({ href:'#closeprofile' });
	$("a.closeprofile,#outermat").click(
		function () {
			P.hide();
		}
	);
	//プリントアウト
	/*
	$("a.printprofile").attr({ href:'#printprofile' });
	$("a.printprofile").click(
		function () {
			openprintpageSelf();
		}
	);
	*/
	
	
	var Rprofile = function() {
		this.setup();
	}

	Rprofile.prototype = {
		setup: function() {
			var self = this;
		},
		show: function() {
			$("#profiletext").fadeIn("slow");
		},
		hide: function() {
			$("#profiletext").fadeOut("slow");
		}
	}
	
	$(".emaillink span").html('@');
	
	
	var G = new Rgallery();
	var P = new Rprofile();
	
	if (window.location.href.indexOf('#openprofile') > 0) {
		$("#profiletext").fadeIn("slow");
	}
	
	G.show();

});


function openprintpage(htmllines) {
	var windowWidth  = 800;
	var windowHeight = 660;
	var targtetWindow = window.open("","","width="+windowWidth+",height="+windowHeight+",resizable=yes,status=yes,location=yes,toolbar=no,menubar=yes,titlebar=yes,scrollbars=yes");
	htmllines = htmllines.replace(/=am/g,'&amp;');
	htmllines = htmllines.replace(/=br/g,'<br />');
	htmllines = htmllines.replace(/=lt/g,'<');
	htmllines = htmllines.replace(/=gt/g,'>');
	htmllines = htmllines.replace(/=qu/g,'"');
	htmllines = htmllines.replace(/=39/g,'\'');
	htmlhead  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'+"\n";
	htmlhead += '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'+"\n";
	htmlhead += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">'+"\n\n";
	htmlhead += '<head>'+"\n";
	htmlhead += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+"\n";
	htmlhead += '<title>SIGNO official website</title>'+"\n";
	htmlhead += '<link rel="stylesheet" type="text/css" href="/css/print.css" />'+"\n";
	htmlhead += '</head>'+"\n";
	htmlhead += '<body>'+"\n";
	htmlhead += '<h1>';
	
	htmlfoot  = '<img src="/images/footer-print.gif" alt="SIGNO .Inc 1-15-15 MINAMIAOYAMA.MINATO-KU.TOKYO.JAPAN.107-0062 TEL : +81-3-5414-2852 FAX : +81-3-5414-2853" width="334" height="64" />'+"\n";
	htmlfoot += '</body>'+"\n";
	htmlfoot += '</html>'+"\n";
	
	htmllines = htmlhead + htmllines  +htmlfoot;
	
	targtetWindow.document.open();
	targtetWindow.document.write(htmllines);
	targtetWindow.document.close();
	targtetWindow.focus();
	setTimeout(function(){targtetWindow.print();}, 500);
}


