var map;
var list_div;
var detail_div;
var map_div;
var info;
var current_id;
var paid_marker = "/pc/common/img/paid_marker.gif";
var normal_marker = "/pc/common/img/normal_marker.gif";
var paid_json;

$(function(){

	list_div = $(".search_display02 > .position01");
	detail_div = $(".search_display02 > .position02");
	map_div = $(".search_display02 > .position03");

	google.maps.Map.prototype.markers = new Array();

	google.maps.Map.prototype.addMarker = function(id, marker) {
    this.markers[id] = marker;
	};

	google.maps.Map.prototype.clearMarkers = function() {

    if(info) {
      info.close();
    }
		for(var i in this.markers){
			if(this.markers[i].icon != paid_marker){
				this.markers[i].set_map(null);
			}
		}
		this.markers = {};
  };

	map = new google.maps.Map(map_div[0], {
		zoom: 14,
		center: new google.maps.LatLng(35.689722, 139.692222),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false
	});

	$.getJSON("/pc/all_paid_store.js", function(json){ 
		create_markers(json["stores"]);
	});

});


function hide_map(){
	map_div.hide();
}

function view_list(type, page, id, params){

	params = params || "";
	page = page || 1;

	if(map){ map.clearMarkers(); }

	hide_info();
	detail_div.animate({ scrollTop:0}, "slow");
	$('html,body').animate({ scrollTop: $(".search_area01").offset().top }, 'slow');

	url = "/pc/" + type + ".js?page=" + page;
	if(params != ""){ url += "&" + encodeURI(params); }

	$.getJSON(url, function(json){ 
		create_markers(json["stores"]);
		view_store(id || json["stores"][0]["id"]);	
		list_div.html(json["html"]);
	});

	$(list_div).animate({ scrollTop: 0}, "slow");
}

function create_markers(stores){

	if(map){ map.clearMarkers(); }

	for(var i = 0; stores && i < stores.length; i++){
		create_marker(stores[i]);
	}
}

function create_marker(store){

	var marker = map.markers[store["id"]];

	if(!marker){
		var marker = new google.maps.Marker({
			map: map,
			position: new google.maps.LatLng(store["lat"], store["lon"])
		});
		if(store["users_id"] != 0){
			marker.icon = paid_marker;
		}else{
			marker.icon = normal_marker;
		}
		marker.store_id = store["id"];

		google.maps.event.addListener(marker, "click", function(){
			if(info){ info.close(); }
			info = new google.maps.InfoWindow({
				content: store["name"] + "<br />" + store["address"]
			});
			info.open(map, marker);
			if(marker.store_id != current_id){
				view_store(marker.store_id, true);
			}
		});
		map.addMarker(store["id"], marker);
	}

	return(marker);
}

function hide_info(){
  $(".search_display01").hide();
}


function view_store(id, only_detail){

	current_id = id;
	only_detail = only_detail || false;

	detail_div.animate({ scrollTop:0}, "slow");

	$.getJSON("/pc/store.js?id=" + id, function(json){
		detail_div.html(json["html"]);
		$("#paid_comment").marquee({loop: -1});
		if(!only_detail){
			var marker = create_marker(json["store"]);
			google.maps.event.trigger(marker, "click");
			if(json["nearest_paid_store"]){
				var paid_marker = create_marker(json["nearest_paid_store"]);
			}
		}
	});
}

function search(q){

	if(q == ""){
		alert("検索ワードを入力してください");
		return false;
	}

	$.getJSON("/pc/search.js?q=" + encodeURI(q), function(json){
		hide_info();
		$(list_div).animate({ scrollTop: 0}, "slow");
		list_div.html(json["html"]);
		if(json["stores"].length != 0){
			view_store(json["stores"][0]["id"]);
			create_markers(json["stores"]);
		}
	});
}


function switch_block(block){
	$(".position02 .block").hide();
	$(".position02 #" + block).show();
}

