$(function() {
	window.name = "mup_top";
	var webroot_path = "";
	var bubbleImagePath = '/img/bg.png';

	//ドロップ
  $(".dropzone").Droppable({
    accept: "top_block", 
		activeclass: "dropzone_active",
		hoverclass: "dropzone_hover",
    onDrop: function(top_block) {
      $(this).prepend(top_block);
    }
  });

	//ドラッグ
  $(".top_block").Draggable({
    handle: '.block_title',
    revert: true,
    opacity: 0.5
  });

	var useservice = {
		"yahoowebunitserch" : webroot_path + "/block/yahoos/webunitserch/",
		"yahoowebserch" : webroot_path + "/block/yahoos/webserch/",
		"yahooimageserch" : webroot_path + "/block/yahoos/imageserch/",
		"yahoovideoserch" : webroot_path + "/block/yahoos/videoserch/",
		"yahooauctionserch" : webroot_path + "/block/yahoos/auctionserch/",
		"yahoolocalsearch" : webroot_path + "/block/yahoos/localsearch/",
		"hatenasimilar" : webroot_path + "/block/hatenas/similar/",
		"hatenahotkeyword" : webroot_path + "/block/hatenas/hotkeyword/",
		"hatenahoturl" : webroot_path + "/block/hatenas/hoturl/",
		"hatenaserch" : webroot_path + "/block/hatenas/serch/",
		"hatenaphotolife" : webroot_path + "/block/hatenas/photolife/",
		"hatenabookmark" : webroot_path + "/block/hatenas/bookmark/",
		"taggynewsserch" : webroot_path + "/block/taggys/newsserch/",
		"taggyvideoserch" : webroot_path + "/block/taggys/videoserch/",
		"taggyblogserch" : webroot_path + "/block/taggys/blogserch/",
		"taggybookmarkserch" : webroot_path + "/block/taggys/bookmarkserch/",
		"taggyphotoserch" : webroot_path + "/block/taggys/photoserch/",
		"taggypodcastserch" : webroot_path + "/block/taggys/podcastserch/",
		"hotpeppergourmetsearch" : webroot_path + "/block/hotpeppers/gourmetsearch/",
		"jalanhotelsearch" : webroot_path + "/block/jalans/hotelsearch/",
		"jalanonsensearch" : webroot_path + "/block/jalans/onsensearch/",
		"gurunavirestsearch" : webroot_path + "/block/gurunavis/restsearch/"
	};

	//jQueryオブジェクトに関数を追加する
	jQuery.fn.extend({
		blockload:function() {
			//AJAX検索をする
			var timekey = (new Date()).getTime();
	    $(this).html("<div style='text-align: center;'><img src='" + webroot_path  + "/img/loading.gif' /></div>");
			$('#serchform select').each(function () {
				metropolis = this.options[this.selectedIndex].value;
			});
			if($("#serchaddressitem").attr("latitude")) {
				lat = $("#serchaddressitem").attr("latitude");
			} else {
				lat = "";
			}
			if($("#serchaddressitem").attr("longitude")) {
				lng = $("#serchaddressitem").attr("longitude");
			} else {
				lng = "";
			}
			url = useservice[$(this).attr("id")] + "?" + $.param({
				'serch' : $("#serchword").val(),
				'metropolis' : metropolis,
				'latitude' : lat,
				'longitude' : lng,
				't' : timekey
			});
			$(this).load(url, function(html, status){
				setaction('#' + $(this).attr("id"));
			});
		}
	});

	//チェックボックス
	$(".useservice input").each(function(){
		$(this).click(function() {
			if($(this).attr("checked")) {
				$('#' + $(this).attr("value")).blockload();
			} else {
				$('#' + $(this).attr("value")).html('');
			}
		})
		if($(this).attr("checked")) $('#' + $(this).attr("value")).blockload();
	});

	//AJAX検索ボタン
	$("#button_serch").click(function(){
		$(".useservice input").each(function(){
			if($(this).attr("checked")) {
				$('#' + $(this).attr("value")).blockload();
			} else {
				$('#' + $(this).attr("value")).html('');
			}
		});
		//QRコードを表示する
		$("#qrimage").html("<img src='qrcodes/qrrender/" + encodeURIComponent($("#serchword").val()) + "' />");
	});

	setaction("");
//	$("#gmapwindow").parent(".block_contents").hide();
});

//AJAXでロード後、機能を割り当てなおす
function setaction(name) {
	$(name + " .block_more").hide();
	$(name + " .block_more_toggle").css("background-position", "top left");

	$(name + " .block_icon_toggle").unbind("click");
	$(name + " .block_more_toggle").unbind("click");

  //ブロックの表示内容を開閉する
  $(name + " .block_icon_toggle").click(function(){
		if($(this).parent().siblings(".block_contents").css("display") == "block") {			$(this).css("background-position", "top left");
			if($(this).parent().siblings(".block_more").css("display") == "block") {
				$(this).parent().siblings(".block_nav").children(".block_more_toggle").css("background-position", "top left");
		    $(this).parent().siblings(".block_more").hide();
			}
		} else {
			$(this).css("background-position", "bottom left");
		}
    $(this).parent().siblings(".block_contents").slideToggle("slow");
  });

  //フッターブロックの表示内容を開閉する
  $(name + " .block_more_toggle").click(function(){
		if($(this).parent().siblings(".block_more").css("display") == "block") {
			$(this).css("background-position", "top left");
		} else {
			$(this).css("background-position", "bottom left");
		}
    $(this).parent().siblings(".block_more").slideToggle("slow");
  });

	if(name != '') {
	  $(name).DraggableDestroy();
		//ドラッグ
	  $(name).Draggable({
	    handle: '.block_title',
	    revert: true,
	    opacity: 0.5
	  });

		//はてな。フォトライフの画像リンクにtarget属性を付ける
		$(".photolife a").attr("target", "_blank");
		$(".photolife a").attr("class", "snap_shots");
		$(name + " img").css("text-align","right");

		//Snapでリンク先サムネイルを得る
		SNAP_COM.shot.onload();
	}
	genLocations();
}

//検索セットを設定する
function serchset(num) {
	switch (num) {
		case 1:
			//検索セット
$(".useservice").children().each(function() {
	if($(this).attr("value") == "yahoowebserch") $(this).attr("checked","checked"); //YahooWeb検索
	if($(this).attr("value") == "hatenaserch") $(this).attr("checked","checked"); //はてな検索
	if($(this).attr("value") == "taggyblogserch") $(this).attr("checked","checked"); //Taggyブログ検索
	if($(this).attr("value") == "taggynewsserch") $(this).attr("checked","checked"); //Taggyニュース検索
});
    	break;
		case 2:
			//動画像セット
$(".useservice").children().each(function() {
	if($(this).attr("value") == "yahooimageserch") $(this).attr("checked","checked"); //Yahoo画像検索
	if($(this).attr("value") == "yahoovideoserch") $(this).attr("checked","checked"); //Yahoo動画検索
	if($(this).attr("value") == "hatenaphotolife") $(this).attr("checked","checked"); //はてなフォトライフ
	if($(this).attr("value") == "taggyphotoserch") $(this).attr("checked","checked"); //Taggy写真検索
	if($(this).attr("value") == "taggyvideoserch") $(this).attr("checked","checked"); //Taggy動画検索
	if($(this).attr("value") == "taggypodcastserch") $(this).attr("checked","checked"); //Taggyポッドキャスト検索
});
    	break;
		case 3:
			//地図セット
$(".useservice").children().each(function() {
	if($(this).attr("value") == "yahoolocalsearch") $(this).attr("checked","checked"); //Yahooローカルサーチ
	if($(this).attr("value") == "hotpeppergourmetsearch") $(this).attr("checked","checked"); //ホットペッパーグルメサーチ
	if($(this).attr("value") == "jalanhotelsearch") $(this).attr("checked","checked"); //じゃらん宿サーチ
	if($(this).attr("value") == "jalanonsensearch") $(this).attr("checked","checked"); //じゃらん温泉サーチ
	if($(this).attr("value") == "gurunavirestsearch") $(this).attr("checked","checked"); //ぐるなびレストラン検索
});
    	break;
		case 4:
			//便利セット
$(".useservice").children().each(function() {
	if($(this).attr("value") == "yahoowebunitserch") $(this).attr("checked","checked"); //Yahoo関連語
	if($(this).attr("value") == "hatenasimilar") $(this).attr("checked","checked"); //はてな連想語
	if($(this).attr("value") == "hatenahotkeyword") $(this).attr("checked","checked"); //はてな注目キーワード
	if($(this).attr("value") == "hatenahoturl") $(this).attr("checked","checked"); //はてな注目url
	if($(this).attr("value") == "hatenabookmark") $(this).attr("checked","checked"); //はてなブックマーク
	if($(this).attr("value") == "taggybookmarkserch") $(this).attr("checked","checked"); //Taggyブックマーク検索

});
    	break;
		case 5:
			//お買い物セット
$(".useservice").children().each(function() {
	if($(this).attr("value") == "yahooauctionserch") $(this).attr("checked","checked"); //Yahooオークション検索
});
    	break;
		case 6:
			//クリアー
$(".useservice").children().each(function() {
	$(this).attr("checked","");
	$('#' + $(this).attr("value")).html('');
});

    	break;
		default:
  	  break;
	}
}

