var $j = jQuery.noConflict();
var cachePopupImg = null;
//ポップアップイベントの設定
function setPopup(selector){
	$j(selector).click(function(e){
		popupImage(this, e);
		return false;
	});
	$j("#popup-layer").css("display", "block");
	$j(".photo ul li .thumb").css("cursor", "url('/js/image/popup/img/zoomin.cur'), pointer");
	//削除一覧、通常一覧切り替え時にポップアップを全て閉じる
	$j("#image_list .switch a").click(function(){deleteAllPopup()});
}

//画像のポップアップ処理
function popupImage(obj, e){

	//既に表示中だと表示しない
	if(isPopupExists(obj.href)){
		return false;
	}

	//ajax request
	$j.ajax({
		type: 'GET',
		url: "/people/"+detail_id+"/pic/img/",
		data: {
			src: obj.href
		},
		cache: false,
		success: function(data){
			cachePopupImg = new Image();
			cachePopupImg.onload = function(){displayPopup(data, e)};
			cachePopupImg.onerror = function(){displayPopupNoImage(e)};
			cachePopupImg.src = obj.href;
			cachePopupImg.title = $j(obj).children("img")[0].src;
		}
	});
}
//ポップアップの削除
function removePopupImage(obj){
	$j(obj).remove();
	if($j("#popup-layer div").length == 0){
		$j("#popup-layer").css("display", "inline");
	}
}
//スクロール量の取得
function getPopupScroll(defvalue){
	if(getValue(window.scrollY)){
		return window.scrollY;
	}
	if(getValue(window.pageYOffset)){
		return window.pageYOffset;
	}
	if(getValue(document.body.scrollTop)){
		return document.body.scrollTop;
	}
	if(getValue(document.documentElement.scrollTop)){
		return document.documentElement.scrollTop;
	}
	return defvalue;
}
//undefined,null対応値取得
function getValue(val){
	if(typeof(val) == "undefined"){
		return "";
	}
	if(val == null){
		return "";
	}
	return val;
}
//既に表示されている画像かチェック
function isPopupExists(src){
	var isExists = false;
	$j("#popup-layer div img").each(function(){
		if(src == this.src || src == "/common/img/no-l.jpg"){
			isExists = true;
			return false;
		}
	});
	return isExists;
}
//フローティングNoImage表示
function displayPopupNoImage(e){
	//noImageは複数表示しない
	var isExists = false;
	$j("#popup-layer div img").each(function(){
		if(this.src.indexOf("/common/img/no-l.jpg") != -1){
			isExists = true;
			return false;
		}
	});
	if(isExists == false){
		cachePopupImg.width = 400;
		cachePopupImg.height = 400;
		displayPopup('<img src="/common/img/no-l.jpg" />', e);
	}
}
//フローティング表示
function displayPopup(data, e){
	//エレメント生成
	var div = document.createElement("div");
	div.innerHTML = data;
	div.style.left = e.clientX - (cachePopupImg.width / 2) + "px";
	div.style.top = e.clientY - (cachePopupImg.height / 2) + getPopupScroll(0) + "px";
	div.style.zIndex = "999";
	div.style.position = "absolute";
	div.style.cursor = "url('/js/image/popup/img/zoomout.cur'), pointer";
	div.id = "popup-"+$j("#popup-layer div").length;
	var act_div = document.createElement("div");
	act_div.className = "popup-act_img";
	act_div.style.width = cachePopupImg.width+"px";
	if(isDisplayExcludeImage() == false){
		//通常の一覧を表示中
		var act_pickup = document.createElement("a");
		act_pickup.id = cachePopupImg.src;
		act_pickup.className = "popup-pick";
		act_pickup.title = "PICKUP PICTUREに設定";
		var act_exclude = document.createElement("a");
		act_exclude.className = "popup-iac";
		act_exclude.title = "この画像を表示しない";
		act_exclude.rel = cachePopupImg.title;
		act_div.appendChild(act_pickup);
		act_div.appendChild(act_exclude);
		act_div.style.top = (cachePopupImg.height - 18)+"px";
	}else{
		//除外一覧を表示中
		var act_reverse = document.createElement("a");
		act_reverse.className = "popup-rac";
		act_reverse.title = "戻す";
		act_reverse.href = cachePopupImg.src;
		act_div.appendChild(act_reverse);
		act_div.style.top = (cachePopupImg.height - 15)+"px";
	}
	div.appendChild(act_div);
	$j("#popup-layer").css("display", "none");
	$j("#popup-layer").append(div);
	$j("#popup-layer").css("height", "0px");
	$j("#popup-layer").css("width", "0px");
	//サイズ調整
	setPopupSize("#popup-layer #"+div.id+" img", e);
	//位置調整
	setPopupLocation("#popup-layer #"+div.id, e);
	$j("#popup-layer").css("display", "block");
	//イベント設置
	$j("#popup-layer #"+div.id).draggable({opacity:1, containment:"document"});
	$j("#popup-layer #"+div.id).click(function(){removePopupImage(this)});
	$j("#popup-layer #"+div.id+" .popup-pick").bind("click", function(){updateTopImage(this); return false;});
	$j("#popup-layer #"+div.id+" .popup-iac").bind("click", function(){popupExcludeClick(this);return false;});
	$j("#popup-layer #"+div.id+" .popup-rac").bind("click", function(){popupReverseClick(this); return false;});
	$j("#popup-layer #"+div.id).mouseout(function(){$j(this).children(".popup-act_img").hide()});
	$j("#popup-layer #"+div.id).mouseover(function(){$j(this).children(".popup-act_img").show()});
}
//画像サイズ調整
function setPopupSize(selector, e){
	var clientY = e.clientY;
	var obj = $j(selector);
	var windowH = $j(window).height();
	var windowW = $j(window).width();
	var imageH = cachePopupImg.height;
	var imageW = cachePopupImg.width;

	var maxPerH = 0.8;//最大サイズ割合
	var maxPerW = 0.8;//最大サイズ割合

	//縦横指定割合以内のサイズかチェック
	var hPer = imageH / windowH;
	var wPer = imageW / windowW;

	//縮小する場合の割合
	var hRePer = windowH * maxPerH / imageH;
	var wRePer = windowW * maxPerW / imageW;

	//指定割合より大きい
	if(hPer > maxPerH || wPer > maxPerW){
		var height = 0;
		var width = 0;
		//縮小割合の大きいほうで縮小
		if(hRePer < wRePer){
			height = imageH * hRePer;
			width = imageW * hRePer;
		}else{
			height = imageH * wRePer;
			width = imageW * wRePer;
		}
		$j(selector).height(height);
		$j(selector).width(width);
		cachePopupImg.height = height;
		cachePopupImg.width = width;
		//縮小した場合、表示位置を再計算
		$j(selector).parent().css("left", e.clientX - (width / 2) + "px");
		$j(selector).parent().css("top", e.clientY - (height / 2) + getPopupScroll(0) + "px");

		$j(selector).parent().children(".popup-act_img").css("width", width + "px");
		$j(selector).parent().children(".popup-act_img").css("top", (height - 15) + "px");
	}
}
//画像位置調整
function setPopupLocation(selector, e){
	if(cachePopupImg.height == 0){
		return;
	}
	//下にはみ出す
	if(e.clientY + (cachePopupImg.height / 2) > $j(window).height()){
		$j(selector).css("top", getPopupScroll(0) + $j(window).height() - cachePopupImg.height - 5 + "px");
	}
	//上にはみ出す
	if(e.clientY - (cachePopupImg.height / 2) < 0){
		$j(selector).css("top", getPopupScroll(0) + 5 + "px");
	}
}
//ポップアップからの除外クリック
function popupExcludeClick(e){
	$j(e).parent().remove();
	$j(".act_img .iac").each(function(){
		if(this.rel == e.rel){
			$j(this).click();
		}
	});
}
//ポップアップからの戻すクリック
function popupReverseClick(e){
	$j(e).parent().remove();
	//除外リスト最後の１枚を戻したときはポップアップを閉じる
	if($j(".photo ul li").length == 1){
		deleteAllPopup();
	}

	$j(".act_img .rac").each(function(){
		if($j(this).parent().parent().children("a")[0].href == e.href){
			$j(this).click();
		}
	});
}
//除外一覧を表示中かチェック
function isDisplayExcludeImage(){
	if($j("#image_list .switch a").html() == "戻る"){
		return true;
	}
	return false;
}
//ポップアップ画像を全て閉じる
function deleteAllPopup(){
	$j("#popup-layer").html("");
}

