﻿
//--------------------------------------------------------------------
// グローバル変数宣言部
//--------------------------------------------------------------------

var arrDownloadList 	= ($.cookie("JntoDownloadList")==null? new Array(): $.cookie("JntoDownloadList").split(",")); // ダウンロードリスト格納用配列。
var intLimitDLList	 	= 30;			// ダウンロードリストの登録個数制限
var arrCheckedPhotoId 	= ($.cookie("JntoCheckedPhotoId")==null? new Array(): $.cookie("JntoCheckedPhotoId").split(","));	// 画像一覧のチェック画像
var intCountDownloadList = arrDownloadList.length;

/*******************************************
// 以下、JQuery用の関数
********************************************/

$(document).ready(function() {
						   
	// カテゴリツリーの設定
	$("#category_tree").treeview({
		animated: "fast",	// アニメーション速度
		collapsed: true, 	// 初期折畳み表示か否か
		unique: true,		// 複数ブランチ展開可か否か
		persist: "cookie"	// 展開状態の保存
	});
	
	// 詳細検索ボタンクリック時　
	$('#btn_advanced').click(function(){
		$("#search > *").filter(":lt(12)").filter(":gt(1)").slideToggle("fast"); // 各要素展開・縮小
		var alt = $("#btn_advanced").attr("alt");
		$("#btn_advanced").attr("alt", $("#btn_advanced").val());
		$("#btn_advanced").val(alt);				
	});
	
	// 地図から探すボタンクリック時　------------------------------
	$('#btnSearchMap').click(function(){  
		if( $('#divMap').css('display') == 'none'){
			$('#divMap').animate({ width: 'show', height: 'show', opacity: 'toggle'}, 400); 
		} else {
			$('#divMap').animate({ width: 'hide', height: 'hide', opacity: 'toggle'}, 200);   
		}  
	});
	
	// 地図　都道府県・閉じるクリック時
	$('#m_map_choose_pref > area').click(function(){
		if( $(this).attr("id")=="mapClose" ){ // 閉じるボタン
			$('#divMap').animate({ width: 'hide', height: 'hide', opacity: 'toggle'}, 200); 
		} else { // 都道府県
			var pref_id = $(this).attr("id").substr(5,2); //console.log("pref_id=" + pref_id);
			$("#lstPref").val(pref_id);
		}
	});
	
	// Web拍手ボタンクリック時
	$(".clap_button").one("click",function(){	
		if (!$(this).hasClass("clap_done")){ // 拍手済みでなければ
			$.getJSON("../feed/ajax_receiver.php",{ type:"web_clap", photo_id : $(this).attr("clap:id")},function(json){
				intReturn = json.returnValue;
				if( parseInt(intReturn) > 0) { // 戻り値がエラーでなければ、拍手回数を更新
					$(".clap_button span").html(intReturn);
				}
			});			
			$(this).addClass("clap_done");
		}
	});
	
	// Web拍手ボタンの初期値設定
	$(".clap_button").each(function(){					
		if( $.cookie("JntoWebClap"+ $(this).attr("clap:id"))=="true" ){
			$(this).addClass("clap_done");
		}
	});
	
	// 画像一覧画面のチェックボタン変更時 ------------------------------
	$("input[id^='chkCheckPhoto_']").click(function(){
		if( $(this).attr("checked") ){ // チェック時
			arrCheckedPhotoId.push($(this).val()); // 配列に追加
		} else { // 解除時
			for( i=0; i<arrCheckedPhotoId.length; i++ ){ // 配列走査を行い、該当IDを削除
				if( arrCheckedPhotoId[i]==$(this).val() ) { arrCheckedPhotoId.splice(i,1); }
			}
		}
		// 整形された値をCookieに書込
		$.cookie("JntoCheckedPhotoId", arrCheckedPhotoId.join(","), {path:"/"});
	});
		
	// 言語切り替えドロップダウン変更時 
	$("#lstLanguage").change(function(){
		url = location.pathname.replace(/\/(eng|jpn|chs|chc|kor)\//, "/" + $(this).val().toLowerCase() + "/"); // URLの言語部分を書き換え
		console.log("url=" + url + location.search);
		location.href = url + location.search;
	});

}); 



/*******************************************
// 以下、JQueryではない関数
********************************************/

/* ダウンロードリストCookieへの追加関数 *********/

function downloadListEntry(photo_id){
	var arrPhotoId 	= new Array();
	console.log("$.cookie('JntoDownloadList')=" + $.cookie("JntoDownloadList"));
	console.log("arrDownloadList.length=" + arrDownloadList.length);
	// 保存データ読み込み
	/*if( arrDownloadList.length == 0 ){
		arrDownloadList = ($.cookie("JntoDownloadList")==null? new Array(): $.cookie("JntoDownloadList").split(","));
	}*/
	
	// 処理用配列に写真IDを格納
	if( photo_id instanceof Array ){ // 引数が配列ではない場合
		arrPhotoId = photo_id; 
	} else {
		arrPhotoId.push(photo_id);
	}

	// 配列を回して各写真IDについて処理
	for (j = 0; j < arrPhotoId.length; j++) {
		// 制限を超えていたら処理を中止
		if( arrDownloadList.length >= intLimitDLList )	break;
		
		var duplicate = false;
		// 既に登録されているか確認
		for (i = 0; i < arrDownloadList.length; i++) {
			if( arrDownloadList[i] == arrPhotoId[j] ) duplicate = true;
		}
		// 重複がなければ追加
		if (!duplicate) arrDownloadList.push( arrPhotoId[j] ); console.log(arrPhotoId[j]+"をダウンロードリストに追加しました");
	}
	
	// Cookieに書込
	$.cookie("JntoDownloadList", arrDownloadList.join(","), { expires:7, path:"/"}); console.log(arrDownloadList.join(","));
	
	console.log("arrDownloadList.length=" + arrDownloadList.length);
	
	// リストの数を返して終了
	return arrDownloadList.length;
}

// ヘッダーのダウンロードリスト更新関数　*********

function updateHeaderDLList(){
	var arrPhotoId = new Array();
	
	// 処理用配列に写真IDを格納
	arrPhotoId = arrDownloadList;
	if( arrPhotoId > 4 ) arrPhotoId = arrPhotoId.slice(0,3);
	
	// 配列を回して各写真IDについて処理
	for (j = 0; j < arrPhotoId.length; j++) {
		path 		= Math.floor(arrPhotoId[j] / 1000); // 画像フォルダ名
		// ヘッダーのリスト更新
		if( !$("#tblDownloadList td").is("[id='" + arrPhotoId[j] + "']") ){ // 既に追加されていた場合の判定処理　→　Cookieの更新タイミングの違いに起因
			$("#tblDownloadList td:last").remove();
			$("#tblDownloadList td:first").before("<td id=" + arrPhotoId[j] + "><a href='photo_detail.php?PI=" + arrPhotoId[j] + "'><img src='../photo/ss_" + path + "/" + arrPhotoId[j] + ".jpg'></a></td>");
		}
	}
	
	// リスト個数の更新
	$("#cntDownloadList").text( arrDownloadList.length );
	
}

// console.logに非対応ブラウザの対策
if (typeof window.console != 'object'){
window.console = {log:function(){},debug:function(){},info:function(){},warn:function(){},error:function(){},assert:function(){},dir:function(){},dirxml:function(){},trace:function(){},group:function(){},groupEnd:function(){},time:function(){},timeEnd:function(){},profile:function(){},profileEnd:function(){},count:function(){}};
}

// *****
// PHPのvar_dumpと同等
function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}//end function var_dump

// *****
// クエリ文字から任意の項目を追加もしくは外す関数

function getQueryString(arrExclude) {
	
	var QS 			= new Array;
	var key_value 	= new Array;
	var arrRet 		= new Array;
	
	// クエリ文字列を配列に格納
	if (location.search.length > 1) {
		var m_Array = location.search.substr(1).split("&"); 
		for (idx in m_Array) { 
			key_value = m_Array[idx].split("=");
			QS[key_value[0]] = key_value[1];
		}
	}

	// 排除キー
	if( arrExclude ){
		for (var i = 0; i < arrExclude.length; i++){
			if( arrExclude[i] in QS ){ delete QS[arrExclude[i]]; }
		}
	}
	// クエリ配列の結合
	for( key in QS ){
		arrRet.push(key + "=" + QS[key]);
	}
	//console.log(arrRet.join("&"));
	return arrRet.join("&");
}

// 指定したキーのクエリ値を返す関数

function getQueryValue(key) {
	var QS 			= new Array;
	// クエリ文字列を配列に格納
	if (location.search.length > 1) {
		var m_Array = location.search.substr(1).split("&"); 
		for (idx in m_Array) {
			key_value = m_Array[idx].split("=");
			QS[key_value[0]] = key_value[1];
		}
	}
	
	if (QS[key]) {
		return QS[key];
	} else {
		return null;
	}
}

// HTML特殊文字の変換関数
function htmlspecialchars(ch) { 
    ch = ch.replace(/&/g,"&amp;") ;
    ch = ch.replace(/"/g,"&quot;") ;
    ch = ch.replace(/'/g,"&#039;") ;
    ch = ch.replace(/</g,"&lt;") ;
    ch = ch.replace(/>/g,"&gt;") ;
    return ch ;
}
