$(document).ready(function() {

    $("#hotMapCity a[name='chsName']").click(function() {
        onClickHotMapCity($(this).attr("cityName"), $(this).attr("countryCode"), $(this).attr("enuName"), $(this).attr("cityCode"));
    })

     $("#mapCityName").click(function() {
        showHotMapCity();
    })

    $("#mapCityName").keydown(function(event) {
        if(event.keyCode==13){
            return false;
        }
        $("#mapCityCode").val('');
        $("#mapCountryCode").val('');
        $("#mapCityEnuName").val('');
        $("#hotMapCity").hide();
    })

    $("#mapCityName").blur(function() {
        setTimeout('doValidateMapCountryCode()', 200);
    })

    initHotMapCityWindow();

    hide_popcityform();

    $("body").click(function(event) {
        var mapCityNameId = (event.target || event.srcElement).id;
        if (mapCityNameId != "mapCityName") {
            $("#hotMapCity").hide();
        }
    })

})

function doValidateMapCountryCode() {
    if ($("#mapCountryCode").val() == null || $("#mapCountryCode").val() == "") {
        $("#mapCityName").val("");
        $("#mapCityCode").val("");
        $("#mapCountryCode").val("");
    }
}

function hide_popcityform() {
    $("#hotMapCity ul li").mouseover(function() {
        $(this).addClass("hover");
    })
    $("#hotMapCity ul li").mouseout(function() {
        $(this).removeClass("hover");
    })
    $("#hotMapCity ul li").click(function() {
        $("#hotMapCity").hide();
    })
}


function removeNoneMapCityName() {
    if ($("#mapCountryCode").val() == "") {
        $("#mapCityCode").val("");
        $("#mapCityName").val("");
    }
}

function showHotMapCity() {
    removeNoneMapCityName();
    $("#hotMapCity").css("z-index", "9999");
    $("#hotMapCity").toggle();
    var pos = findPos(document.getElementById("mapCityName"));
    $("#hotMapCity").css("top", (pos.y + document.getElementById("mapCityName").offsetHeight) + "px");
    $("#hotMapCity").css("left", pos.x);
}

function findPos(obj) {
    var curleft = obj.offsetLeft || 0;
    var curtop = obj.offsetTop || 0;
    while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft
        curtop += obj.offsetTop
    }
    return {x:curleft,y:curtop};
}


function initHotMapCityWindow() {
    if ($.browser.msie) {
        var hotCity = $("#hotMapCity");
        var mapCityName = $("#mapCityName");
        hotCity.css("top", (mapCityName.position().top + mapCityName.height() + 5) + "px");
        hotCity.css("left", mapCityName.position().left);

        iframeDecorateDiv("hotMapCity");
    }
}

function onClickHotMapCity(name, countryCode, cityEnuName, cityCode) {
    $("#mapCountryCode").val(countryCode);
    $("#mapCityName").val(name);
    $("#mapCityEnuName").val(cityEnuName);
    $("#mapCityCode").val(cityCode);
    $("#hotMapCity").hide();
}