var map;
var directionsPanel;
var directions;

initialize = function () {
	if (GBrowserIsCompatible()) {
		function createMarker(point, icoImage, titleText) {
			// Create a lettered icon for this point using our icon class
			var letteredIcon = new GIcon(new GIcon(G_DEFAULT_ICON));
			letteredIcon.image = "imgs/" + icoImage + ".png";
			letteredIcon.shadow = "imgs/" + icoImage + "_shadow.png";
			letteredIcon.iconSize = new GSize(16, 29);
			letteredIcon.shadowSize = new GSize(41, 29);
            letteredIcon.iconAnchor = new GPoint(8, 29);


			// Set up our GMarkerOptions object
			markerOptions = { icon:letteredIcon, title: titleText };
			var marker = new GMarker(point, markerOptions);

			return marker;
		}

		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(55.763851, 37.681603), 16);
		//map.setUIToDefault();
		map.addOverlay(createMarker(new GLatLng(55.762204,37.6807),'marker', ''));
	}
}

showPopup = function(popup,i) {
    $('#popup').show();
    $('#popup .name').html(popup.find('.name').html());
    $('#popup .header').html(popup.find('.header').html());
    $('#popup .subheader').html(popup.find('.subheader').html());
    $('#popup .scroll').html(popup.find('.text').html());
    $('#popup .scroll').jScrollPane({showArrows:true, scrollbarWidth: 7});
    $('#popup #video').flash({
        swf: '/bitrix/templates/adwatch/swf/adwatch-layer.swf',
        width: 428,
        height: 257,
        flashvars: {
            contentURL: popup.find('.video').html()
        }
    });
    $('#popup .press a').attr('href', popup.find('.press').html());
    $('#popup .download a').attr('href', popup.find('.download').html());
    $('#popup #currentItem').html(i+1);
    $('#popup #totalItems').html($('.tdc-element').length);
    $('#popup #prev').click( function() {
        if(i==0) {
            showPopup($($('.tdc-element')[$('.tdc-element').length-1]),$('.tdc-element').length-1);
        }
        else {
            showPopup($($('.tdc-element')[i-1]),i-1);
        }
    });
    $('#popup #next').click( function() {
        if (i==$('.tdc-element').length-1) {
            showPopup($($('.tdc-element')[0]),0);
        }
        else {
            showPopup($($('.tdc-element')[i+1]),i+1);
        }
    });
}

$().ready( function() {
    $('.tdc-element').each( function(i) {
        var popup = $(this);
        $(this).find('.open-popup').click( function(event) {
            showPopup(popup,i);
            event.preventDefault();
        });
    });
    $('.tdc-element').each( function(i) {
        var popup = $(this);
        $(this).find('.image').click( function() {
            if($(this).parent().css('zIndex')==200) {
                showPopup(popup,i);
            }
        });
    });
    $('#close').click( function(event) {
        $('#popup').hide();
        event.preventDefault();
    });
    $('#zoomout').click( function(event) {
        $('#popup').hide();
        event.preventDefault();
    });
});

