//      $ site.js, v 1.0, 2011-03-30 19:12 YEKST, key@kosmatov.su Exp $

var $j = jQuery.noConflict();
var placemarks = new Array;

(function($) { $(function() {

	var slideshow_timeout = 30000;
	var timer = null;

	if ( $('#gallery').length && $('.gallery:first').length ) {

		$('.gallery:first').clone(flag).appendTo( $('#gallery') );

		if ( $('.gallery:first').hasClass('no-lightbox') ) {

			$('#gallery a').removeClass('lightbox').unbind();
		}

		$('.gallery:first').remove();
	}

	$(".to-placemark").hover(function() {

		var i = parseInt( YMaps.jQuery(this).attr('id').replace('to-placemark-','') );
		placemarks[i].openBalloon();

	},function() {

			var i = parseInt( YMaps.jQuery(this).attr('id').replace('to-placemark-','') );
			placemarks[i].closeBalloon();
	});

	$('.switcher li').click( function() {

		if ( $(this).hasClass('this') )
			return false;

		var act = $(this).parent().children('.this').removeClass('this');

		if ( act.attr('id') == $(this).attr('id') )
			return false;

		if ( timer ) {

			clearTimeout(timer);
			timer = setTimeout(slideShow, slideshow_timeout*2);
		}

		$('#'+act.attr('id').replace('to-','')).hide();
		$(this).addClass('this');
		$('#'+$(this).attr('id').replace('to-','')).fadeIn(1000);

		return false;
	});

	var sw_rand = Math.floor( ( Math.random()*11 + $('.switcher li').length ) % ( $('.switcher li').length + 1 ) );
	$('#to-slide-'+sw_rand).click();

	var slideShow = function() {

		var next = $('.switcher li.this').next('li');

		if ( next.length ) {

			next.click();
		}

		else {

			$('.switcher li:first').click();
		}

		if ( timer )
			clearTimeout(timer);
		timer = setTimeout(slideShow, slideshow_timeout);
	}

	timer = setTimeout(slideShow, slideshow_timeout);

	$('.slide .last').click( function() {

		$('.switcher li:first').click();
		timer = setTimeout(slideShow, slideshow_timeout);

		return false;
	});

	var conth = function() {

		$('div.helping h4').animate({opacity:.5}, 1000, function() {

			$(this).animate({opacity:1}, 1000);
			setTimeout(conth,5000);
		});
	}

	conth();

}); })(jQuery);

