var mapLoaded = false;
$(document).ready(function() {

    /* This code is executed on the document ready event

     var left = 0,
     top = 0,
     sizes = { retina: { width:130, height:130 },
     webpage:{ width:285, height:332} },
     webpage = $('#zoom'),
     offset = { left: webpage.offset().left, top: webpage.offset().top },
     retina = $('#full');

     $(window).resize(function() {
     offset = { left: webpage.offset().left, top: webpage.offset().top };
     });

     if (navigator.userAgent.indexOf('Chrome') != -1) {
     /*	Applying a special chrome curosor,
     as it fails to render completely blank curosrs.

     retina.addClass('chrome');
     }

     webpage.mousemove(
     function(e) {

     left = (e.pageX - offset.left);
     top = (e.pageY - offset.top);

     if (retina.is(':not(:animated):hidden')) {
     /* Fixes a bug where the retina div is not shown
     webpage.trigger('mouseenter');
     }

     if (left < 0 || top < 0 || left > sizes.webpage.width ||
     top > sizes.webpage.height) {
     /*	If we are out of the bondaries of the
     webpage screenshot, hide the retina div

     if (!retina.is(':animated')) {
     webpage.trigger('mouseleave');
     }
     return false;
     }

     /*	Moving the retina div with the mouse
     (and scrolling the background)

     retina.css({
     left                : left - sizes.retina.width / 2,
     top                    : top - sizes.retina.height / 2,
     backgroundPosition    : '-' + (1.8 * left) + 'px -' + (1.8 * top) + 'px'
     });

     }).mouseleave(
     function() {
     retina.stop(true, true).fadeOut('fast');
     }).mouseenter(function() {
     retina.stop(true, true).fadeIn('fast');
     });*/
    $('.zoom').retina({preload: true, css:{ width: 130, height: 130, "z-index": 999 }});

    var table = $(".cities table");
    var cities = new Array();
    var current_city = 0;
    var cities_menu = $("<div></div>")/*.css({display:"none"})*/.addClass("cities_menu");
    var list = $("<ul></ul>");
    var i = 0;
    $("tr", table).each(function() {
        cities.push({city:$("td:first-child", this).text(),phone:$("td:last-child", this).text()});
        list.append($("<li></li>").text($("td:first-child", this).text()).attr("rel", i));
        i++;
    });
    var cities_count = cities.length;
    $("#header .city").append(cities_menu.append(list));

    apply_city();

    $("ul li", cities_menu).click(function() {
        current_city = $(this).attr("rel");
        apply_city();
        $(".cities_menu").slideUp();
    });

    function apply_city() {
        var city = cities[current_city];
        $("#header .city .name").text(city.city);
        $("#header .city .phone").text(city.phone);
    }

    $("#cities").click(function() {
        $(".cities_menu").slideToggle();
    });
    /**MAPS**/

    if ($("#map").length > 0) {
        // var script = document.createElement("script");
        //script.src = "http://maps.googleapis.com/maps/api/js?sensor=false";
        //script.type = "text/javascript";
        //document.getElementsByTagName("head")[0].appendChild(script);
        mapLoaded = true;
        showMap(55.643838, 37.593718);
    }


    $("#map a.city").click(function(event) {
        event.preventDefault();
        $("#map a.city.active").removeClass("active");
        $(this).addClass("active");
        $("#map .google a").attr("href", $(this).attr("href"));
        $("#map .address").html($(this).attr("rel"));
        showMap($(this).attr("data-x"), $(this).attr("data-y"), $(this).attr("rel").replace("<br />", "\n"));

    });

    function showMap(x, y, text) {
        if (!mapLoaded) {
            //('API карт Google еще не загружен. Нажмите на кнопке "Загрузить API карт Google"');
            return;
        }
        else {
            var myLatlng = new google.maps.LatLng(x, y);

            var myOptions = {
                zoom: 15,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title:text
            });
        }
    }

});

function loadMaps() {
    google.load("maps", "2", {"callback" : loaded});
}

function loaded() {
    mapLoaded = true;
}
