var mashmaps_plugins = [];
//var mashsearch_plugins = ["HotPepper", "BarNavi", "Jalan", "SchoolSearch", "HotPepperBeauty", "GGeocoder", "LocoSticker"]; for Japan
var mashsearch_plugins = ["GGeocoder"];

var MashMap_Margin = 0.00003;

var MashMapDef = {
    80: "Flickr",
    81: "Panoramio"
}


var mashtools_plugins = {
    GGeocoder: { key: "", LV: 15 },
    LocoSticker: { key: "", LV: 15 },
    SchoolSearch: { key: "e7c2edb161d7ef4b", LV: 15 },
    HotPepper: { key: "e7c2edb161d7ef4b", LV: 15 },
    HotPepperBeauty: { key: "e7c2edb161d7ef4b", LV: 15 },
    Jalan: { key: "leo119da51fca3", LV: 15 },
    GoGoGs: { key: "chamap", LV: 15 },
    BarNavi: { key: "88083b92ef8e56d5a3c8006165bd52603b3c9bc49a67d768f118d4407b4edc8c", LV: 15 },
    RakutenTravel: { key: "0b5af0f72c2176e8fc426366ef396f97", LV: 15 },
    TabeLog: { key: "", LV: 15 },
    Flickr: { key: "934fb76226c34f6a9d3be891e82720de", LV: 15 },
    Panoramio: { key: "", LV: 15 },
    Zooomr: { key: "6b6ad6f3a9407bd1acfb67277ae52045", LV: 15 }
}



var MashMaps={};
MashMaps = {
    gmap: null,
    mng: null,
    items: new Array(),
    isLoad: false,
    ev: null,
    Init: function() {
        for (var k in mashtools_plugins) {
            var v = eval(k);
            v.load(mashtools_plugins[k]);
        }
    },
    load: function(m) {
        MashMaps.gmap = m;
        MashMaps.ev = GEvent.addListener(MashMaps.gmap, 'moveend', MashMaps.Search);
        if (!MashMaps.mng) MashMaps.mng = new MarkerManager(m);
        for (var k in mashtools_plugins) {
            var v = eval(k);
            v.load(mashtools_plugins[k]);
        }
        MashMaps.isLoad = true;
    },
    unload: function() {
        if (MashMaps.isLoad) {
            MashMaps.mng.clearMarkers();
            GEvent.removeListener(MashMaps.ev);
        }
        MashMaps.isLoad = false;
    },
    close: function() {

    },
    show: function() {

    },
    click: function(i) {
        MashMaps.close();
    },
    AddItem: function(r, a) {
        if (MashMaps.items[r.id] == undefined) {
            var mk = new GMarker(r.pos, a.icon);
            MashMaps.mng.addMarker(mk, a.LV);
            GEvent.addListener(mk, "click", GEvent.callbackArgs(mk, MashMaps.click, r.id));
            MashMaps.items[r.id] = { m: mk, d: MashMaps_MarkHtml(r, a), t: r.name, i: MashMapDefNumber(a) };
        }
    },
    click: function(i) {
        GMark.SetActiveInfoWindow(0);
        var s = MashMaps.items[i].d;
        //s += "<div class='mk_paste'><a href='javascript:void(0);' onclick=\"Paste.Set('" + i + "');return false;\"></a></div>";
        s += "<div class='mk_paste'><input type='button' value='" + Lang.T.QuoteAtc + "' onclick=\"Paste.Set('" + i + "');return false;\" /></div>";
        s = "<div class='mk_340'>" + s + "</div>";
        
        MashMaps.items[i].m.openInfoWindowHtml(s);
    },
    MakeIcon: function(mk) {
        var i = new GIcon();
        i.image = mk.image;
        i.iconSize = new GSize(mk.width, mk.height);
        i.iconAnchor = new GLatLng(mk.height, parseInt(mk.width / 2));
        i.infoWindowAnchor = new GLatLng(3, parseInt(mk.width / 2));
        return i;
    },
    Search: function() {
        var z = MashMaps.gmap.getZoom();
        for (var i = 0; i < mashmaps_plugins.length; i++) {
            if (mashtools_plugins[mashmaps_plugins[i]].LV <= z) {
                var v = eval(mashmaps_plugins[i]);
                v.SearchArea(MashMaps.gmap);
            }
        }
    }
}

var MashSearch={};
MashSearch = {
    gmap: null,
    ggeo: null,
    isLoad: false,
    load: function(m) {
        MashSearch.gmap = m;
        MashSearch.gmap.removeControl(cmGLargeMapControl);
        MashSearch.gmap.addControl(ms_z15srv);
        MashSearch.gmap.addControl(cmGLargeMapControl);
        MashSearch.gmap.addControl(ms_box);
        
        $('msearch').onkeypress = MashSearch.hookEnter;
        if (!MashSearch.ggeo) MashSearch.ggeo = new GClientGeocoder();

        MashSearch.isLoad = true;
    },
    unload: function() {
        if (MashSearch.isLoad) {
            MashSearch.gmap.removeControl(ms_z15srv);
            MashSearch.gmap.removeControl(ms_box);
        }
        MashSearch.isLoad = false;
    },
    hookEnter: function(e) {
        ev = (!e) ? event : e;
        key = ev.keyCode;
        mf = (ev.modifiers) ? ev.modifiers : ((ev.altKey) ? 1 : 0) + ((ev.ctrlKey) ? 2 : 0) + ((ev.shiftKey) ? 4 : 0);
        k = key + '_' + mf;
        if (k == '13_0') { MashSearch.Search(); }
    },
    Search: function() {
        var s = $('msearch').value;
        if (isNull(s)) return;
        MashSearch.ggeo.getLatLng(s, f1);
        function f1(p) {
            if (p) {
                MashSearch.gmap.setCenter(p, 16);
                Ctls.Map.TStop();
                $('msearch').style.color = "";
            } else {
                $('msearch').style.color = "#f00";
            }
        }
    }
}

var splits=['周辺の','近くの','近辺の','周りの',',','、','\s','　','\t','の'];
function Splitter(s){
    if(isNull(s))return false;
    var a=-1,d;
    for(var i=0;i<splits.length;i++ ){
        d=splits[i];
        a =s.lastIndexOf(d);
        if(a!=-1)break;
    }
    if(a==-1||a==0||a==s.length-1)return false;
    return [s.substr(0,a),s.substr(a+d.length)]
}
	
	

function MashMaps_MarkHtml(r,a){
    var s = "";
    s+= "<div class='mm_title'>";
    s+= r.url && isURL(r.url) ? "<a href='"+r.url+"' target='_blank'>"+r.name+"</a>":"<span>"+r.name+"</span>";
    s+= "</div>";

    if(r.img){s+="<div class='mm_img'><a href='"+r.url+"' target='_blank'><div style='background-image:url("+r.img+");'></div></a></div>";}
    s+= "<div class='mm_desc' >"+r.desc+"</div>";
    s += "<div class='mm_cdt' >Powerd By <a href='" + a.url + "' target='_blank'>" + a.credit + "</a></div>";
    return s;
}


function MashMapGetMarker(i, op) {
    var a = eval(MashMapDef[i]);
    return a.marker.image ? "<img src='" + a.marker.image + "' " + op + "/>" : "";
}
function MashMapGetTitle(i) {
    var a = eval(MashMapDef[i]);
    return a.title ? a.title : "";
}
function MashMapGetAPIName(i) {
    var a = eval(MashMapDef[i]);
    return a.APIName ? a.APIName : "";
}


function MashMaps_ApiList(my,f){
        var i,d,s="";
        for(i in MashMapDef){
            if(f||my.contains(i)){
                d=eval(MashMapDef[i]);
                s+="<div class='apic'>";
                s+="<label>";
                if(f)s+="<input type='checkbox' name='api' id='api' value='"+i+"' "+(my.contains(i)?'checked':'')+">";
                s+="<img src='"+d.marker.image+"' align='absmiddle'/>&nbsp;"+d.title;
                s+="</label>";
                s+="<div><a href='"+d.url+"' target='_blank'>"+d.credit+"</a></div>";
                s+="</div>";
            }
        }
        return s;
}

function MashMaps_IconList(my){
        if(!my)return "";
        var i,d,s="";
        for(i in MashMapDef){
            if(my.contains(i)){
                d=eval(MashMapDef[i]);
                s+="<img src='"+d.marker.image+"' align='absmiddle'/>&nbsp;";
            }
        }
        return s;
}
function MashMap_Set(my){
    if(MashMaps.mng)MashMaps.mng.clearMarkers();
    mashmaps_plugins=new Array();
    var c=0,i;
    for(i in MashMapDef){
        if(my.contains(i)){
            mashmaps_plugins[c]=MashMapDef[i];
            c++;
        }
    }
}

function MashMapDefNumber(o) {
    for (i in MashMapDef) {
        if (eval(MashMapDef[i]) == o) return i;
    }
    return 0;
}




function MashMapsZ15Srv() { };
MashMapsZ15Srv.prototype = new GControl();
MashMapsZ15Srv.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(4, 90)); }
MashMapsZ15Srv.prototype.initialize = function(m) {
    var d = document.createElement("div");
    d.innerHTML = "<img src='/images/zoom15srv.gif' />";
    m.getContainer().appendChild(d);
    return d;
}
var ms_z15srv = new MashMapsZ15Srv();

function MashSearchbox() { };
MashSearchbox.prototype = new GControl();
MashSearchbox.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(70,7)); }
MashSearchbox.prototype.initialize = function(m) {
    var d = document.createElement("div");
    d.innerHTML = "<div id='locsearch'>" + Lang.T.LocSearch + "&nbsp;<input type='text' id='msearch' maxlength='36' size='36'/></div>";
    m.getContainer().appendChild(d);
    return d;
}
var ms_box = new MashSearchbox();


function DateToStr(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();

    if (m < 10) { m = "0" + m; }
    if (d < 10) { d = "0" + d; }

    return y + "/" + m + "/" + d;
}