/*-----------------------------------------------
 *
 *	@Name : common.js
 *				-IMPORT SHARE HTML
 *				-BLANK WINDOW
 *				-SMOOTH SCROLL
 *				-SWAP IMAGES
 *				-PATH NAVIGATION
 *				-GLOBAL NAVIGATION
 *	@Auther : cyocun.koiwahara
 *	@Modified : 2010.06.14
 *	@Copyright : Copyright 2010, cycoun(TM).koiwahara 
 *
-----------------------------------------------*/

jQuery.noConflict();

jQuery(function() {

	/* IMPORT SHARE HTML
	-----------------------------------------------*/
	var LoadBox = jQuery('#importShare')
	var PageName = jQuery('body').attr('id')
	reSetting();

	jQuery(LoadBox).hide().load(
		// ショッピングページはstore.jsに記載
		shareFile(),
		function(){shareCallBack();}
	);

	//読み込むファイルを設定
	function shareFile(){
		switch(PageName){
			case "inquiry":
				return('../shareSSL.html');
			case ""://shopping
				return('/html/newpage.html?code=4');
		default:
				return('/share.html');
		}
	}
	

	//shareファイルが読み込み終わったら実行
	function shareCallBack(){
		LoadBox.show().find("."+PageName).remove();
		setHeight();
		reSetting();
	}


	/* BASE SETTING
	-----------------------------------------------*/
	function reSetting(){
		// BLANK WINDOW
		jQuery("a.blank").click(function(){
			this.target="_blank";
		});

		// SWAP IMAGES
		var postfix = "_o";
		jQuery(".swap").not('[src*="'+ postfix +'."]').each(function() {
			var img = jQuery(this);
			var src = img.attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.')) + postfix + src.substring(src.lastIndexOf('.'));
			jQuery('<img>').attr('src', src_on);
			img.hover(function() {img.attr('src', src_on);},function() {img.attr('src', src);});
		});
	}


	/* SMOOTH SCROLL
	-----------------------------------------------*/
	jQuery('a.scroll').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
			var target = jQuery(this.hash);
			target = target.length && target;
			if (target.length)
				var pos = 30;
				var dur = 1000;
				var offSet = target.offset().top - pos;
				jQuery('html,body').stop().animate({scrollTop: offSet},{duration: dur, easing: 'easeOutQuint'});
				return false;
	});


	/* PATH NAVIGATION
	-----------------------------------------------*/
	jQuery("#pathNav li:last-child")
		.css({paddingLeft:"25px",color:"#3e3e3e"})
		.animate({paddingLeft:"30px",color:"#909090"},500);


	/* GLOBAL NAVIGATION
	-----------------------------------------------*/
	if (jQuery.browser.msie && jQuery.browser.version <=6 ){
			jQuery("#gNav span").hide();
		}else{//IE6以下ははじく。

		var inS=70;
		var outS=75;
		
		
		//about
		var rAbout='rect(25px, 120px, 25px, 120px)';
		var about=jQuery('#gAbout span');
		var nAbout=jQuery('#gAbout img');
		//about.css({clip:rAbout,opacity:'toggle'});
		
		//apply
		var rApply='rect(25px, 76px, 25px, 76px)';
		var apply=jQuery('#gApply span');
		var nApply=jQuery('#gApply img');
		//apply.css({clip:rApply,opacity:'toggle'});
		
		//participate
		var rParticipate='rect(25px, 76px, 25px, 76px)';
		var participate=jQuery('#gParticipate span');
		var nParticipate=jQuery('#gParticipate img');
		//participate.css({clip:rParticipate,opacity:'toggle'});
		
		//winner
		var rwinner='rect(25px, 69px, 25px, 69px)';
		var winner=jQuery('#gwinner span');
		var nwinner=jQuery('#gwinner img');
		//winner.css({clip:rwinner,opacity:'toggle'});
		
		//store
		var rStore='rect(25px, 105px, 25px, 105px)';	
		var store=jQuery('#gStore span');
		var nStore=jQuery('#gStore img');
		//store.css({clip:rStore,opacity:'toggle'});
		
	
		about.css({clip:rAbout,opacity:'toggle'});
		apply.css({clip:rApply,opacity:'toggle'});
		participate.css({clip:rParticipate,opacity:'toggle'});
		winner.css({clip:rwinner,opacity:'toggle'});
		store.css({clip:rStore,opacity:'toggle'});
	
	
		
		//about
		jQuery('#gAbout a').bind({
			mouseover:function(){
				nAbout.css("visibility","hidden");
				about.css("opacity","toggle").stop(true, false).animate({clip:"auto"},inS)
			},
			mouseout:function() {
				nAbout.css("visibility","visible");
				about.stop(true, false).animate({clip:rAbout},outS,function(){about.css("opacity","toggle")})
			}
		});
		
		//apply
		jQuery('#gApply a').bind({
			mouseover:function(){
				nApply.css("visibility","hidden");
				apply.css("opacity","toggle").stop(true, false).animate({clip:"auto"},inS)
			},
			mouseout:function() {
				nApply.css("visibility","visible");
				apply.stop(true, false).animate({clip:rApply},outS,function(){apply.css("opacity","toggle")})
			}
		});
		
		//participate
		jQuery('#gParticipate a').bind({
			mouseover:function(){
				nParticipate.css("visibility","hidden");
				participate.css("opacity","toggle").stop(true, false).animate({clip:"auto"},inS)
			},
			mouseout:function() {
				nParticipate.css("visibility","visible");
				participate.stop(true, false).animate({clip:rParticipate},outS,function(){participate.css("opacity","toggle")})
			}
		});
		
		//winner
		jQuery('#gwinner a').bind({
			mouseover:function(){
				nwinner.css("visibility","hidden");
				winner.css("opacity","toggle").stop(true, false).animate({clip:"auto"},inS)
			},
			mouseout:function() {
				nwinner.css("visibility","visible");
				winner.stop(true, false).animate({clip:rwinner},outS,function(){winner.css("opacity","toggle")})
			}
		});
		
		//store
		jQuery('#gStore a').bind({
			mouseover:function(){
				nStore.css("visibility",'hidden');
				store.css("opacity","toggle").stop(true, false).animate({clip: "auto"},inS)
			},
			mouseout:function() {
				nStore.css("visibility",'visible');
				store.stop(true, false).animate({clip:rStore},outS,function(){store.css("opacity","toggle")})
			}
		});
	}	



	/*--------------------------------------------------------------------------------------------------------
	*
	* WINNER LIST MOTION SETTING
	*
	--------------------------------------------------------------------------------------------------------*/
	if(jQuery("#winner").length<=0){return};//#winnerじゃなければ実行しない。


	
	/* FARST SETTING
	-----------------------------------------------*/
	var activeTab	//現在アクティブなリスト[#yyyy]
	jQuery(".listBackBtn").hide();

	/* RIGHT SERACH SELECTER
	-----------------------------------------------*/
	jQuery("select").jQselectable({
		style:"simpleBox",
		callback:
			function(){
				if(jQuery(this).val().length>0){
					selectGo("#" + jQuery(this).val())
				}
			}
	})


	//戻るボタン
	jQuery(".listBackBtn").click(function() {
		changeTitle();//タイトル変更
		jQuery(activeTab).children().remove();
		jQuery(activeTab).css({display:"none"});
		sortListBoxToggle("show");
		jQuery(".listBackBtn").hide();
		//年表リストの開閉トグル
		jQuery("#loadErr").hide();
		return false;
	});

	//年表からの移動開始
	jQuery("ul.yearList>li>a").click(function() {
		activeTab = jQuery(this).attr("href");//クリックされたhrefを格納
		sortListBoxToggle("hide");//年表の開閉Toggle
		changeTitle(activeTab);//タイトル変更
		listView(activeTab);//年間受賞者表示
		return false;
	});

	//年間受賞者表示
	function listView(ID){
		activeTab =ID;//クリックされたhrefを格納
		jQuery("#loadErr").hide();
		jQuery("#loading").fadeIn();
		jQuery(ID).show();
		if(jQuery(ID).children().length){being();return false;};//存在するなら中断
		jQuery(ID).load(
			ID.slice(2,6)+".html",
				being = function(text, status, xhr){
					//CALL BACK->
					jQuery(ID).find("th:empty").remove();
					jQuery(ID).children().css({opacity:0,display:'block'}).scale(.95);
					 if(status=="success"){	//読み込みに成功した場合
						setTimeout(function(){

							var leng=jQuery(ID).children().length;
							var i,num,spd=50,del=150;
							if(leng==0){noTable();return;};
							for(i=0;i<leng;i++){
								num=i*del;
								jQuery(ID +" dl:eq("+i+")").delay(num).animate({opacity:1,scale:1},200).css({display:'block'});
								if(leng-1==i)
									jQuery("#loading").delay(num).fadeOut();
							}
						}, 250);
					 }else if(status){
						jQuery("#loadErr").html(status+" : "+xhr.status + " " + xhr.statusText)
						jQuery("#loading").fadeOut("fast");
						jQuery("#loadErr").fadeIn();
						jQuery(".year dl").remove();
					 }else{
						jQuery("#loading").fadeOut("fast");
						jQuery("#loadErr").html("エラーが発生しました。再度年代を選択してください。")
						jQuery("#loadErr").fadeIn();
						jQuery(".year dl").remove();
					 }
					 function noTable(){
						jQuery("#loading").fadeOut("fast");
						jQuery("#loadErr").html("データが見つかりませんでした。")
						jQuery("#loadErr").fadeIn();
						jQuery(".year dl").remove();
					 }
					 //アコーディオン発動！
						jQuery(ID).children().each(function(){
							var toggle= false;
							////////////////////////////
							var ele = jQuery(this);
							var listsBlock = ele.find("dl");
							var liststitle = ele.find("dt p");
							var listsCredits = ele.find("dd");
							var tableHeight = ele.find("table").outerHeight()+1;
							var trHeight = ele.find("tr:eq(1)").outerHeight()+1;
							///////////////////////////
							var postfix = "_o";
							var postOpen = "_c";
							var img = liststitle.find("#icon");
							//console.log(jQuery(ele));
							var src = img.attr("src");
							var src_on = src.substr(0, src.lastIndexOf('.')) + postfix + src.substring(src.lastIndexOf('.'));
							var src_open = src.substr(0, src.lastIndexOf('.')) + postOpen + src.substring(src.lastIndexOf('.'));
							var src_open_on = src.substr(0, src.lastIndexOf('.')) + postOpen + postfix + src.substring(src.lastIndexOf('.'));
							///////////////////////////

							listsCredits.height(trHeight);
							
							jQuery(liststitle).click(function(){
								if(listsCredits.height()==trHeight){
									//OPEN
									img.attr('src', src_open);
									listsCredits.stop(true,true).animate({height:tableHeight},150);
								}else{
									//CLOSE
									img.attr('src', src);
									listsCredits.stop(true,true).animate({height:trHeight},150);
								}
								toggle=!toggle
								return false;
							});
	
							jQuery(liststitle).hover(function(){
								if(!toggle){img.attr('src', src_on)}else{img.attr('src', src_open_on)};
							},function() {
								if(!toggle){img.attr('src', src)}else{img.attr('src', src_open)};
								//console.log(toggle)
							});

						});
			//changeHight(ID);
			//<-CALL BACK
				}
			);
	}


	//年表リストの開閉
	var flags = true
	function sortListBoxToggle(set){
		baseHeight=jQuery("#yearListBox").height();
		switch(set) {
			case "hide":
					flags = false
					jQuery("#yearListBox").animate({opacity:0,scale:.95},150,function(){
					jQuery("#yearListBox").hide();
					});
			break;
			case "show":
					flags = true
					jQuery("#yearListBox").css({display:'block'}).delay(300).animate({opacity:1,scale:1},200);
					jQuery("#yearListBox").height(baseHeight);
			break;
		}
	}
	
	//付随要素の切り替え
	function changeTitle(y){
		if(y!=undefined){	//一覧から一覧へ移動
			setTimeout(function(){jQuery(".listBackBtn").fadeIn()}, 500);
			jQuery(".scrollBox").animate({marginTop:'-45px'},300);
			jQuery("#titleYear span").text(y.slice(2));
		}else{	//戻る。
			jQuery(".listBackBtn").hide();
			jQuery(".scrollBox").animate({marginTop:'0'},300,function(){jQuery("#titleYear span").text("")});
		}
	}

	//プルダウンメニューから
	function selectGo(href) {
		if(activeTab!=href){
			flags = false
			jQuery("#yearListBox").animate({opacity:'0',scale:'.95'},150,function(){jQuery("#yearListBox").hide()});
			changeTitle(href);
			jQuery(activeTab +" dl").remove();
			jQuery(".year").hide();
			listView(href);
			return false;
		}
	};

});

jQuery(window).load(setHeight=function () {
	if(jQuery("#index").length<=0)
		jQuery(".container").css({minHeight: jQuery("#side").height()-98});
});

