var maps = new Array(), google, googleAttached = 0;

function loadGMap(callback) {
    if(!googleAttached) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.google.com/maps/api/js?v=3.4&sensor=false" + (callback ? "&callback=" + callback : "");
        document.body.appendChild(script);
        googleAttached = 1;
    }
}

function attachGMap(elements) {
    var center  = new google.maps.LatLng(56.8379, 60.5983);
    var mapOptions = {
        zoom: 16,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: false,
        center: center
    };

    elements = elements || ".gmap";
    $(elements).each(function(i) {
        var $this       = $(this);
        var coords      = $this.data('coords');
        var geoAddress  = $this.data('address');
        var marker;

        var mapsx = new google.maps.Map(this, mapOptions);

        if(coords && coords.latitude && coords.longitude) {
            var point = new google.maps.LatLng(coords.latitude, coords.longitude);
            marker = new google.maps.Marker({
                map: mapsx,
                position: point
            });
            mapsx.setCenter(point);
        }
        else if (geoAddress) {
            var geocoder = new google.maps.Geocoder();
            console.log('Start ' + geoAddress);
            geocoder.geocode( {
                'address': geoAddress,
                'latLng': center
            }, function(results, status) {
                console.log('Loaded' + geoAddress);
                if (status == google.maps.GeocoderStatus.OK) {
                    mapsx.setCenter(results[0].geometry.location);
                    marker = new google.maps.Marker({
                        map: mapsx,
                        position: results[0].geometry.location
                    });
                }
            })
        }
    });
}

jQuery.support.placeholder = (function(){
    var i = document.createElement('input');
    return 'placeholder' in i;
})();

function attachSMap() {
    $('a.map-popup').each(function() {
        var id      = '#' + $(this).data('map-id');
        var title   = $(this).attr('title') || '';

        $(this).click(function() {
            Shadowbox.open({
                content:    id,
                player:     "inline",
                title:      title,
                width:      9300,
                height:     5800,
                options: {
                    onFinish: function() {
                        attachGMap($('.gmap', '#' + Shadowbox.playerId));
                    }
                }
            });
        });
    });
}

$(function(){
    loadGMap("attachSMap");

    Shadowbox.init({
        skipSetup: true
    });

    Shadowbox.setup("a.popup", {
        gallery: "grinvich"
    });

    $('a.inline-popup').each(function() {
        var id      = '#' + $(this).data('inline-id');
        var title   = $(this).attr('title') || '';

        $(this).click(function() {
            Shadowbox.open({
                content:    id,
                player:     "inline",
                title:      title,
                width:      9300,
                height:     5800
            });
        });
    });
    
    // This initialises carousels on the container elements specified, in this case, carousel1.
    $carusel = $("#carousel");
    $carusel
        .removeClass('no-js')
        .CloudCarousel(     
        {
            xPos: $carusel.width() / 2,
            yPos: 33,
            xRadius: 489/2,
            yRadius: 100/2,
            minScale: 0.6,
            reflHeight: 40,
            reflGap:2,
            buttonLeft: $("#left-but"),
            buttonRight: $("#right-but"),
            /*FPS:30,*/
            autoRotateDelay: 5200,
            speed:0.2
            /*bringToFront:true*/
        }
    );

    $("#left-but").show();
    $("#right-but").show();

    $(".catalog dl dt a").click(function() {
        if (!$(this).hasClass('no-sub')) {
            var $parent = $(this).closest("dl");
            if( $parent.is(".open") ){
                $parent.removeClass("open");
            } else {
                $parent.addClass("open");
            }
            return false;
        }
        return true;
    });

    $('.open-text').click(function() {
        $('#hidden-text').animate({ height: 'toggle', opacity: 'toggle' }, 400);
    });

    if (!$.support.placeholder) {
        $('INPUT[placeholder], TEXTAREA[placeholder]').blur(function(){
            if ($(this).val()=='') {
                $(this).val($(this).attr('placeholder'));
                $(this).addClass('m-placeholder');
            }
        }).focus(function(){
            $(this).removeClass('m-placeholder');
            if ($(this).val()==$(this).attr('placeholder'))
                $(this).val('');

        }).each(function(){
            if ( ($(this).val()=='') || ($(this).val()==$(this).attr('placeholder')) ) {
                $(this).val( $(this).attr('placeholder') );
                $(this).addClass('m-placeholder');
            }
            var form = $(this).closest('FORM');
            if (form.length)
                form.submit(function(){
                    if ($(this).val()==$(this).attr('placeholder'))
                        $(this).val('');
                });
        });
    }
});
