var YMap;

function function_exists( function_name ) { // Return TRUE if the given function has been defined
    if (typeof function_name == 'string')
        return (typeof window[function_name] == 'function');
    else
        return (function_name instanceof Function);
}

function getParam (name, location)
{
    location = location || window.location.search;
    var res = location.match(new RegExp('[?&]' + name + '=([^&]*)', 'i'));
    return (res && res[1] ? res[1] : '');
}

Array.prototype.in_array = function(p_val) {
    for(var i = 0, l = this.length; i < l; i++)  {
        if(this[i] == p_val) {
            return true;
        }
    }
    return false;
}


YMaps.jQuery(function () {
    // Инициализация карты
    YMap = new YMaps.Map(document.getElementById("YMapsID"));
    YMap.enableRightButtonMagnifier();
    YMap.enableDragging();
    YMap.enableScrollZoom();
    YMap.addCopyright("© «Банкоматы Саратова на карте», 2010");
    var zoomControl = new YMaps.SmallZoom({smooth: false});
    YMap.addControl(zoomControl,
        new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT, new YMaps.Point(10, 10)));
    var params = {
        ll : getParam("ll"),    // Центр карты
        spn : getParam("spn")   // Линейный размер области
    };
    var bounds = YMaps.GeoBounds.fromCenterAndSpan(
        YMaps.GeoPoint.fromString(params.ll),   // Центр карты
        YMaps.Size.fromString(params.spn)       // Линейный размер области в градусах
        );

    $(".YMapsLink").each(function(){
        $(this).attr("oldhref", $(this).attr("href"));
    });
    if (params.ll && params.spn)
    {
        YMap.setBounds(bounds);
        $(".YMapsLink").each(function(){
            $(this).attr("href",
                $(this).attr("oldhref")+'?'
                +"&ll=" + YMap.getCenter().toString()
                +"&spn=" + YMap.getBounds().getSpan().toString(6)
            );
        });
    }
    else if (Boolean($.cookie('home')))
    {
        var home = String($.cookie('home')).split(','),
        center = new YMaps.GeoPoint(home[0],home[1]);
        YMap.setCenter(center, 15)
    }
/*    else if (YMaps.location)
        YMap.setCenter(new YMaps.GeoPoint(YMaps.location.longitude, YMaps.location.latitude), 14)*/
    else
        YMap.setCenter(new YMaps.GeoPoint(46.034549,51.533269), 14)

    YMaps.Events.observe(YMap, [YMap.Events.BoundsChange, YMap.Events.TypeChange],  function () {
        $(".YMapsLink").each(function(){
            $(this).attr("href",
                $(this).attr("oldhref")+'?'
                +"&ll=" + YMap.getCenter().toString()
                +"&spn=" + YMap.getBounds().getSpan().toString(6)
            );
        });
    });
    YMaps.Events.observe(YMap, YMap.Events.BalloonOpen,  function () {
        YMap.getBalloon().setOptions({
            maxWidth: 400,
            maxHeight: 300,
            margin: [10, 10, 10, $('#leftContainer').width()+50]
        });
    });

    $('.closeDivButton').click(function(){
        $(this).parent().slideUp('fast');
        return false;
    });

    $('input[placeholder!=][type!=submit]')
    .blur(function(){
        if ($(this).val() == '')
        {
            $(this).val($(this).attr('placeholder'));
            $(this).addClass('inactive');
        }
    })
    .focus(function(){
        $(this).removeClass('inactive');
        if ($(this).val() == $(this).attr('placeholder'))
            $(this).val('');
    })
    .change(function(){
        if ($(this).val() != $(this).attr('placeholder') && $(this).val() != '')
            $(this).removeClass('inactive');
    })
    .each(function(){
        if(!$(this).val())
        {
            $(this).val($(this).attr('placeholder'));
            $(this).addClass('inactive');
        }
    });
    $('form').submit(function(){
        $('.inactive').val('');
    });

    if ($('#infoMessage ul li').size())
        $('#infoMessage').slideDown('fast');

    if (function_exists('behaviour'))
        behaviour(YMap);

});
