(function($){$().ready(function(){

	var main_photos = new Array();
	var timer = null;
	var timeout = 10000;
	var gallery = new Array();

	$('#photos ul.switch li a').each(function(){

		var image = new Image();
		image.src = $(this).attr('href');
		main_photos.push(image);

		$(this).click(function() {

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

			if ( timer != null )
				clearTimeout(timer);

			var id = $(this).attr('class').replace('s','');

			$('#photos').css({backgroundImage:'url('+main_photos[id].src+')'});

			$('#photos-describe').children().hide();
			$('#photos-describe .s'+id).fadeIn();

			$('#photos .this').removeClass('this');
			$(this).parent().addClass('this');

			return false;
		});
	});

	$('#slide-show ul.switch li a').each(function(){

		var image = new Image();
		image.src = $(this).attr('href');
		gallery.push(image);

		$(this).click(function(){

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

			if ( timer != null && ! $('#photos').length )
				clearTimeout(timer);

			$('#slide-show img').attr('src',$(this).attr('href'));
			$('#slide-show ul.switch li.this').removeClass('this');
			$(this).parent().addClass('this');

			return false;
		});
	});
	
	$('#tag-cloud li.vi').hide();
	
	$('#tag-cloud a.more').click( function() {
			
		if ( $(this).hasClass('vi') ) {
		
			$(this).removeClass('vi').html('ещё&nbsp;&rarr;').parent().parent().children('li.vi').hide();
		}
		
		else {
			
			$(this).addClass('vi').html('&larr;&nbsp;скрыть').parent().parent().children('li.vi').show();
		}
		
		return false;
	});

	switch_photos = function() {

		if ( $('#photos').length )
			id = '#photos';
		else
			id = '#slide-show';

		var nxt_link = $(id+' ul.switch li.this').next().children('a');
		if ( ! nxt_link.length )
			nxt_link = $(id+' ul.switch li:first a');
		nxt_link.click();

		timer = setTimeout(switch_photos,timeout);
	}

	if ( $('#photos').length || $('#slide-show').length ) {

		timer = setTimeout(switch_photos,timeout);
	}

	var hide_order = function() {

		$('#order .reset').click();
	}

	var load_order = function() {

		if ( ! $('#order .reset').length )
			$('#order .inner div').append('<button class="reset"></button>');

		$('#order .reset').click(function(){$('#order').fadeOut();return false;});

		if ( $('#order form').length ) {

			$('#order form').submit(function(){

				$.post($(this).attr('action'), $(this).serialize(), function(data) {

					$('#order .inner div').html(data);
					load_order();
				});

				return false;
			});
		}

		else setTimeout(hide_order,6000);
	}

	$('.app,.call').click(function(){

		$.post('/','interface=ajax_form&action='+$(this).attr('class'),function(data) {

			$('#order .inner div').html(data);
			$('#order').fadeIn();
			load_order();
		});
	});

	if ( $('#fphone').length ) {

		var load_phone_form = function() {

			if ( ! $('#fphone .reset').length )
				$('#fphone').append('<button class="reset"></button>');

			$('#fphone .reset').click(function(){$('#fphone').fadeOut();return false;});

			if ( $('#fphone form').length ) {

				$('#fphone form').submit(function(){

					$.post($(this).attr('action'), $(this).serialize(), function(data) {

						$('#fphone div').html(data);
						load_phone_form();
					});

					return false;
				});
			}

			else setTimeout( function(){$('#fphone').fadeOut();}, 6000 );
		}

		var show_phone_form = function() {

			$.post('/','interface=ajax_form&action=phone',function(data) {

				var fm = $('#fphone');
				fm.children('div').html(data);
				var t = $(document).scrollTop()+100;
				var l = ( $(document).width() - fm.width() ) / 2;
				fm.css({top:t,right:l}).fadeIn();
				load_phone_form();
			});
		}

		setTimeout( show_phone_form, fphone_timeout );
	}

	$('#menu li a').click(function() {

		var u = $(this).parent().children('ul');
		if ( u.length ) {

			u.slideToggle();
			if ( u.hasClass('active') ) {

				u.removeClass('active');
				return false;
			}

			$(this).parent().parent().find('ul.active').slideToggle().removeClass('active');
			u.addClass('active');

			return false;
		}

		return true;
	});

});})(jQuery)

