(function($)
{  

	function UnWrap(sel)
	{
		var cnt = $(sel).contents();
		$(sel).replaceWith(cnt);
	}

	// Contests fix
	$(function()
	{
		var video_contest = $('.view-id-contests_works.view-display-id-block_3 .view-content > .item-list, ' +
			'.view-id-contests_works_end.view-display-id-block_3 .view-content > .item-list');
		video_contest.attr('class', 'contest video');
		$('li span.field-content', video_contest).unwrap();
		$('li', video_contest).each(function()
		{
			UnWrap($('span.field-content', this));
			UnWrap($('span.vud-widget', this));
		});
	});


	$(function()
	{
		function setCookie (name, value, expires, path, domain, secure) {
		document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
		};
		Cufon.replace('#left-column h2, #right-column h2');
		
		if ($('.concert-info').length > 0)
		{
		$('#content-column > h1:first-child').hide();
		}

		$('ul.jcarousel-skin-film').each(function()
		{
			var gal = $(this);
			var count = $('li', gal).length;
			if (count >= 4)
				return;
			for (;count<4; count++)
				gal.append('<li></li>');
		});

	
	  // Top menu
		$('#top-menu > ul.cykooz-menu').cykooz_menu(
		{
			overduration: 50,
			outduration: 100
		});
    $('#top-menu > ul.cykooz-menu > li.has-submenu > a').removeAttr('href');
    
		// City selectore
		$('#page-header .city > div span').click(function()
		{
		  var selector = $('#page-header .city > div');
			if (selector.hasClass('opened'))
			{
      	$('#page-header .city > div > div').hide('fast', function()
				{
					selector.removeClass('opened');
				});
			}
			else
			{
				selector.addClass('opened');
      	$('#page-header .city > div > div').show('fast');
			}
      return false;
		});
		//city select
		/*
		$('a#izh').click(function()
		{
				setCookie("city", "1");				
				setCookie("city-name", "Ижевск");
			//	top.location.href='/about';
				window.location.reload(true);
		});
		$('a#votk').click(function()
		{
				setCookie("city", "2");				
				setCookie("city-name", "Воткинск");				
				window.location.reload(true);
		});
		$('a#perm').click(function()
		{
				setCookie("city", "3");			
				setCookie("city-name", "Пермь");								
				window.location.reload(true);
		});
		$('a#kaz').click(function()
		{
				setCookie("city", "4");				
				setCookie("city-name", "Казань");				
				window.location.reload(true);
		});
		*/
		// Login form
		$('#login-btn').click(function()
		{
			var login_menu = $(this).parents('.login-menu');
			if (login_menu.hasClass('opened'))
			{$		('form#auth_form').submit();
			    //getElementById('auth_form').submit(); 
				return false;
				// 
			
			}
			else
			{
        login_menu.animate({height: 162}, 500, function()
        {
          login_menu.addClass('opened');
				});
			}
			return false;
		});
		$('.login-menu .close').click(function()
		{
			var login_menu = $(this).parents('.login-menu');
			if (login_menu.hasClass('opened'))
			{
        login_menu.removeClass('opened');
        login_menu.animate({height: 41}, 500);
			}
			return false;
		});
		/* Fade slider (banners and actions) */
		$('.fade-slider').cycle(
		{
			fx: 'fade',   // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed: 1000,  // speed of the transition (any valid fx speed value)
			timeout: 5000 // milliseconds between slide transitions (0 to disable auto advance)
		});		
	
	/* Film gallery */
		
		try
		{
			var film_galleries = $('.film-gallery');
			if (film_galleries.length > 0)
			  film_galleries.jcarousel(
				{
        	scroll: 4
				});
		}
		catch (err)
		{
			alert('Please, include jCarousel JavaScript file');
		}

		/* Fancybox */
		
    /*$('.lightbox').fancybox();*/

		/* Contest fancybox */
		
  /*  $('.contest-lightbox').fancybox(
		{
      titlePosition: 'inside',
      titleFormat: function(title, currentArray, currentIndex, currentOpts)
      {
				var a = $(currentArray[currentIndex]);
				var author = a.parents('li').find('> div > b').text();
				var votes = a.parents('li').find('.votes').text();
				return '<b>' + title + '</b><br />Автор: ' + author +'<br /><span class="votes">' + votes + '</span>';
			}
		});*/

		/* Gallery */

		$('.gallery h1 a').click(function()
		{
			var gallery = $(this).parents('.gallery');
			if (gallery.hasClass('opened'))
			{
				$('ul', gallery).hide('fast', function()
				{
				  gallery.removeClass('opened');
				});
			}
			else
			{
				$('ul', gallery).show('fast', function()
				{
				  gallery.addClass('opened');
				});
			}
			return false;
		});
		$('.gallery:first h1 a').click();
		
		 


		/* Pay method */

		$('.pay-method > a').click(function()
		{
			var paymethod = $(this).parents('.pay-method');
			if (paymethod.hasClass('opened'))
			{
			  $('.details', paymethod).slideUp(700, function()
				{
          paymethod.removeClass('opened');
				});
			}
			else
			{
			  paymethod.addClass('opened');
			  $('.details', paymethod).slideDown(700);
			}
			return false;
		});

	});


	// Sophit animation

	var cur_frame_RE = new RegExp('sophit-(\\d)', 'i');

	function sophit_inverse(lis, delay)
	{
		$(lis).each(function()
		{
			var stop_frame = 4;
			var delta = 1;
			var li = $(this);
			if (!li.is('li[class*=sophit-]'))
				li.addClass('sophit-0');

			var li_class = li.attr('class');
			var cur_frame = cur_frame_RE.exec(li_class)[1];
			cur_frame = parseInt(cur_frame);
			if (cur_frame > 0)
			{
				delta = -1;
				stop_frame = 0;
			}
			li.data({cur_frame: cur_frame, stop_frame: stop_frame, delta: delta});
		});

		function animation()
		{
			var stop_animation = true;
			$(lis).each(function()
			{
				var li = $(this);
				var data = li.data()
				if (data.cur_frame == data.stop_frame)
					return;
				li.removeClass('sophit-' + data.cur_frame);
				var cur_frame = data.cur_frame + data.delta;
				li.addClass('sophit-' + cur_frame);
				li.data('cur_frame', cur_frame);
				stop_animation = cur_frame == data.stop_frame;
			});
			if (!stop_animation)
				setTimeout(animation, delay);
		}

		animation();
	}

	var min_timeout = 3000;    // Минимальная задержка между переключениями софитов
	var max_timeout = 7000;    // Максимальная задержка между переключениями софитов
	var animation_delay = 150; // Время анимацчии включений/выключения софита

	function sophits(lis)
	{
		if (lis.length <= 0)
			return;
		
		var first_step = true;
		function next_sophit()
		{
			/*
			var r = Math.round(Math.random() * (lis.length-1));
			sophit_inverse(lis[r], animation_delay);
			*/
			if (first_step)
			{
				sophit_inverse(lis[0], animation_delay);
				sophit_inverse(lis[2], animation_delay);
				first_step = false;
			}
			else
			{
				sophit_inverse(lis, animation_delay);
			}
			var timeout = min_timeout; //Math.floor(Math.random() * (max_timeout - min_timeout + 1)) + min_timeout;
			setTimeout(next_sophit, timeout);
		}
		setTimeout(next_sophit, min_timeout);
	};


	$(function()
	{
		var lis = $('body.main .events-cards li').slice(0, 4);
		sophits(lis);
	});

})(jQuery);

