// JavaScript Document

jQuery.fn.extend({
	thumView : function(maxNum) {
		var cont = 1;
		$(".thum li a").click( function() {
			var imgTgt = $(this);
			var index = $(".thum li a").index($(this));
			cont = index+1;
			chgPic( $(this).attr("href") );
			return false;
		} );
		$("li.next a").click( function() {
			if(cont == maxNum){
				return false;
			}
			cont++;
			chgPic( $(".thum li a:eq("+(cont-1)+")").attr("href") );
			return false;
		});
		$("li.back a").click( function() {
			if(cont == 1){
				return false;
			}
			cont--;
			chgPic( $(".thum li a:eq("+(cont-1)+")").attr("href") );
			return false;
		});
		function chgPic( url ) {
			$("#photo .large img").queue([]);
			$("#photo .large img").fadeTo(10, 0, function() {
				$("#photo .large img").attr("src", url );
				$("#photo .large img").fadeTo(1500, 1);
			});
			$("li.no").html(cont+"/"+maxNum);
		}
		function fillZero( num, keta ) {
			var str = new String(num);
			var cnt = keta - str.length;
			if( cnt <= 0 ) return str;
			while( cnt-- > 0 ) str = "0"+str;
			return str;
		}
	},
	tabCahnge : function() {
		var height = 160;
		var navi = $(this).find("ul.navi");
		var cont = $(this).find("dl");
		cont.find("dd").each( function() {
			if( $(this).height() > height ) {
				$(this).attr("oldHeight", $(this).height());
			} else {
				$("#thumNavi a").hide();
			}
		}).css({"height":height, "overflow":"hidden"}).filter(":not(:first)").hide();
		navi.find("li a:first img").trigger("mouseenter").unbind("mouseenter").unbind("mouseleave");
		navi.find("li a").click( function() {
			var index = navi.find("li a").index($(this));
			cont.find("dd").hide().filter(":eq("+index+")").show();
			if( cont.find("dd:eq("+index+")").attr("oldHeight") ) {
				var tgt = cont.find("dd:eq("+index+")");
				$("#thumNavi a").show();
				if( tgt.attr("oldHeight") == height ) {
					tgt.attr("oldHeight", tgt.height() );
					tgt.height(height);
					$("#thumNavi a img").attr("src", $("#thumNavi a img").attr("src").replace("_close_", "_more_") );
				}
			} else {
				$("#thumNavi a").hide();
			}
			navi.find("li a img").rollOver();
			$(this).find("img").unbind("mouseenter").unbind("mouseleave");
			navi.find("li a img").trigger("mouseleave");
			return false;
		} );
		$("#thumNavi a").click( function() {
			cont.find("dd:visible").each( function() {
				var toHeight = $(this).attr("oldHeight");
				$(this).animate( {height:toHeight}, function() { $(this).css("overflow", "hidden"); }  ).attr("oldHeight", $(this).height());
			} );
			if( $(this).find("img").attr("src").match("_close_") ) {
				$(this).find("img").attr("src", $(this).find("img").attr("src").replace("_close_", "_more_") );
			} else {
				$(this).find("img").attr("src", $(this).find("img").attr("src").replace("_more_", "_close_") );
			}
			return false;
		} );
	},
	indexWindow : function( tgtWindow ) {
		var closeBtn = document.createElement("div");
		$(closeBtn).css({"text-align":"center", "margin":"0 0 25px", "clear":"both"}).append('<a href="#close"><img src="../common/img/details/index/close_off.gif" alt="•Â‚¶‚é" /></a>');
		tgtWindow.css({"position":"absolute", "margin-left":"-300px"}).append(closeBtn).hide();
		$(this).click( function() {
			tgtWindow.show();
			var h = Math.max( document.body.clientHeight , document.body.scrollHeight );  
			h = Math.max( h , document.documentElement.scrollHeight );  
			h = Math.max( h , document.documentElement.clientHeight );
			var black = document.createElement("div");
			black.id = "black";
			document.body.appendChild( black );
			$(black).css( {
				"background":"#000",
				"opacity":"0.7",
				"position":"absolute",
				"top":"0", "left":"0",
				"width":"100%",
				"height":h,
				"z-index":0
			} );
			return false;
		} );
		$(closeBtn).click( function() {
			tgtWindow.hide();
			$("#black").remove();
			return false;
		} ).find("img").rollOver();
	}
});

$(document).ready(function(){
	$("#tab").tabCahnge();
	$("#indexToggle").indexWindow( $("#indexWindow") );
});
