var shop_id = (function(a){if (a=a.match(/s=([0-9]+)/)){return a[1];}return 0;})(location.href);
function registerTags(tags){
	var tag;
	if (typeof(tags)==='object' && "length" in tags){
		t=[];
		for (var i=0,t=[]; i < tags.length; i++) {
			t.push('tag_id%5B%5D='+tags[i]);
		};
		tag = t.join('&');
		tag += "&insert=on";
	}else{tags = $("input[name=\"tag_id[]\"]:checked");tag=$(tags).serialize();}
    $.post("/api/js_mypage.php","mode=shopfavorite&action=tags&"+tag+"&shop_id="+shop_id, function(data,status){
	if (status==="success") {
		html = "";
		for (var i in data.tags){
			html += "<li><a href='/member/myshop.shtml?t%5B%5D="+data.tags[i].tag_id+"'>"+ data.tags[i].name +"</a></li>";
		}
		$("#favTagList" + shop_id).hide();
		$("#favTagList" + shop_id).html(html);
		if (html){
			$("#favTagList" + shop_id).show();
		}
		$("#favTagList" + shop_id).effect("highlight");
	}
},'json');
}

var WindowOptions = {modal:true, draggable:false, autoOpen: false,width:520,resizable:false,title:"キニイッタ！タグ編集",position:"center",buttons:{}};
WindowOptions.buttons={"保存する":function () {
$(this).dialog('close');registerTags() }, "キャンセル":function(){  $(this).dialog('close') }}

$(function(){
$("#shoptagsWindow").dialog(WindowOptions);
$("#editShoplistTag").click(function (a){
	$("#shoptagsWindow").dialog('open');
});
})

$("#addWishTag").click(function (a){registerTags([1]);$("#ShoplistTags_1").attr('checked', true)});

$("#showFavoriteList").click(function (a){
	if (!$("#winFavoriteAll").length){
		e=$("<div>").attr("id","winFavoriteAll").html("<div id='FavoriteAllContent'>loading...</div>")
		$("body").append(e);delete e;
		var WindowOptions = {modal:true, draggable:false, autoOpen: false,width:720,resizable:false,title:"キニイッタ！",position:"center",buttons:{}};
		$("#winFavoriteAll").dialog(WindowOptions);
		$.post("/api/js_mypage.php","mode=shopfavorite&action=total&shop_id="+shop_id, function(data,status){
			if (status==="success") {
				html=favo="";
				html += "<div class='fav_shop_name'>"+data.shop.shop_name+"</div>";
				html += "<div class='fav_shop_prop'>("+data.shop.area2_name+"/"+data.shop.category_name+"/"+data.shop.grapevine_disp+"件)</div>";
				i=1;
				for (var i in data.favorite){
					favo += '<li class="pos'+i+'">'+ data.favorite[i].name +'…'+data.favorite[i].count +"人</li>";
					i++;
				}
				if (favo){
					html += '<ol>'+favo+'</ol>';
				}
				$("#FavoriteAllContent").html(html);
				$("#favTagList").effect("highlight");
			}
		},'json');		
	}
	$("#winFavoriteAll").dialog('open');
});

$(".editInPlace").editInPlace({
url: "/api/js_mypage.php",
  params: "mode=shopfavorite&action=note&shop_id=" + shop_id,
show_buttons: true,
save_button: '<input type="button" class="inplace_save" value="保存する">',
cancel_button: '<input type="button" class="inplace_cancel" value="キャンセル">',
bg_over: "#FF9999",update_value: "note","textarea_cols":"40",
saving_text: '保存中です...',default_text: 'ここをクリックすると、メモがつけられます。',
success: function(data, element) {
var obj = jQuery.parseJSON(data);
note = obj.note||this.default_text;
$(element).text(note);
element.effect("highlight");
}
});
$(document).ready(function(){
	$('.imageResize').each(function(){
        var img = $(this);var constWidth = constHeight = '';
        constWidth = img.width();constHeight = img.height();
        img.click(function(){
            var imgPreloader = new Image();imgPreloader.src = img.attr('src');
            size = resizeUp(imgPreloader.width, imgPreloader.height, 320, 320);
			img.width(size[0]);img.height(size[1]);
		});
        img.mouseout(function(){img.width(constWidth);img.height(constHeight)});
    });
});

