var _t = 0;
var _timer = 5000;

function slide_next() {
  var _slide = $('.slideshow li.selected');
  var _next = $(_slide).next();

  if (_next.length <= 0) {
    _next = $('.slideshow li:first');
  }

  if (_slide.attr('id') == _next.attr('id')) return;

  $(_next).addClass('selected');
  $(_slide).attr('class', '');
  $(_next).fadeIn(1000);
  $(_slide).hide();

  clearTimeout(_t);
  _t = setTimeout("slide_next()", _timer);
}

function slide_prev() {
  var _slide = $('.slideshow li.selected');
  var _prev = $(_slide).prev();

  if (_prev.length <= 0) {
    _prev = $('.slideshow li:last');
  }

  if (_slide.attr('id') == _prev.attr('id')) return;

  $(_prev).addClass('selected');
  $(_slide).attr('class', '');
  $(_prev).fadeIn(1000);
  $(_slide).hide();

  clearTimeout(_t);
  _t = setTimeout("slide_next()", _timer);
}

$(document).ready(function() {
    var _done = false;
    $('.menu .expanded a').each(function(index) {
	if (($(this).hasClass('active') || $(this).parent('li').hasClass('active-trail')) && _done == false) {
            $(this).parent().find('ul').show();
	    _done = true;
	}
    });
    Cufon.now();

    $('.slideshow li:first').addClass('selected');
    $('.slideshow .button-prev').click(function() { slide_prev(); return false; });
    $('.slideshow .button-next').click(function() { slide_next(); return false;});
    $('.slideshow .button-next').click();

    $('.imagecache-image_page_thumb').click(function() {
	var id = $(this).attr('name');
	$(this).parent().parent().parent().find('.larges img').hide();
	$(this).parent().parent().parent().find('.larges img[name="'+id+'"]').show();
    });

    if ($('#gmap').length) {
	$('#gmap').height('400px');
	$('#gmap').width('500px');
	my_map = new GMap2(document.getElementById('gmap'));
	my_map.setCenter(new GLatLng(55.7402, 37.5978), 17);
	my_map.addControl(new GOverviewMapControl());
	my_map.addControl(new GLargeMapControl());
	my_map.addControl(new GMapTypeControl());
	my_map.enableDoubleClickZoom();
	my_map.enableContinuousZoom();
	var latlng = new GLatLng(55.7402, 37.5977);
	var office_marker = new GMarker(latlng);
	my_map.addOverlay(office_marker);
	GEvent.addListener(office_marker, 'click', function() {
	    office_marker.openInfoWindowHtml('<img style="display:block; position:relative; top:5px; left:-5px; height:225px; width:300px;" src="/sites/default/files/building_foto.jpg" alt="Здание Центра Оперного Пения Галины Вишневской"><br>');
	});
    }
});
