$(function(){
	$('p.entrance a.login').click(function(){
		var $body = $('body');
		var $loginform = $('#loginform');
		$loginform.toggle();
		$loginform.mousedown(function(event){
			event.stopPropagation();
		}).show();
		$body.mousedown(function(event){
			$loginform.hide(); $body.unbind('mousedown');
		});
		return false;
	});
});

function choice_init( form_id, com_form_id )
{
	$(function(){
		var $form = $('#' + form_id);
		var $com_form = $('#' + com_form_id);
		
		var $day_select = $('#s_day');
		var $month_select = $('#s_month');
		var $year_select = $('#s_year');
		
		$form.ajaxForm({
			data:{ ajax: 1 },
			beforeSubmit: function(arr, $form, options) {
				o.push({ url: '/choice-birthday', vars: {
					user_day: $day_select.val(), user_month: $month_select.val(), user_year: $year_select.val()
				} });
			},
			success: function(data){
				if ( !data )
					location.reload();
				
				if ( jQuery.browser.msie && jQuery.browser.version.substr(0,1) == '6' )
					$('div.dateselector').find('select').css('visibility', 'hidden');
				$('div.dateselector').fadeOut(2000);
				
				if ( data == '1' ) {
					$('div.18years').fadeIn(2000);
					/*window.location.href = 'http://www.alcofact.ru/';*/
				} else if ( data == '2' ) {
					$('div.21years').fadeIn(2000);
					/*window.location.href = 'http://www.talkingalcohol.com/';*/
				} else {
					$('div.secondbg').fadeIn(2000);
					window.setInterval( function(){ location = data }, 3000 );
				}
			}
		});
		
		if ( !jQuery.browser.msie &&
				!( navigator.userAgent.match( /iPhone/i ) || navigator.userAgent.match( /iPod/i ) ) ) {
			$day_select.sSelect({ddMaxHeight: '200px'});
			$month_select.sSelect({ddMaxHeight: '200px'});
			$year_select.sSelect({ddMaxHeight: '200px'});
		} else {
			$day_select.css( 'display', 'block' );
			$month_select.css( 'display', 'block' );
			$year_select.css( 'display', 'block' );
		}
		
		var check_date = function( year, month, day ){
			var tmp_date = new Date( year, month - 1, day );
			
			var valid =
				( tmp_date.getFullYear() == year ) &&
				( tmp_date.getMonth() == month - 1 ) &&
				( tmp_date.getDate() == day );
			
			return valid;
		};
		
		var select_change = function(){
			var day = $day_select.val();
			var month = $month_select.val();
			var year = $year_select.val();
			
			if ( check_date( year, month, day ) )
				$form.find('input.enter').removeAttr('disabled');
			else
				$form.find('input.enter').attr('disabled', 'disabled');
		};
		
		$day_select.change(select_change);
		$month_select.change(select_change);
		$year_select.change(select_change);
		
		select_change();
	});
}

function Promoblock(selector, delay) {
    this.selector = selector;
    this.count = $(selector + ' .promo').size();
	this.current = $(selector + ' .promo').index($(selector + ' .promo.current'));
	this.delay = delay;
	this.timeout = "";
	this.animationspeed = 500;
	
	if(this.current < 0){
		this.current = 0;
		$(selector + ' .promo').eq(0).addClass('current');
	}
	
	$(selector + ' .promo').each(function(){
		if($(this).is('.current')) $(selector + ' .pager').append('<a class="dot current"></a>'); else $(selector + ' .pager').append('<a class="dot"></a>');
	});
	
	for(var i = 0; i < this.count; i++){
		$(selector + ' .pager .dot').eq(i).click({obj: this, index: i}, function(e) {
			e.data.obj.set(e.data.index);
		});
	}
	
	this.set = function(index) {
		$(this.selector + ' .promo.current').fadeOut(this.animationspeed, function() {
			$(this).removeClass('current');
		});
		$(this.selector + ' .pager .dot.current').removeClass('current');
		this.current = index;
		$(this.selector + ' .promo').eq(index).fadeIn(this.animationspeed, function() {
			$(this).addClass('current')
		});
		$(this.selector + ' .pager .dot').eq(index).addClass('current');
		clearTimeout(this.timeout);
	}
	
	this.rotate = function() {
		$(this.selector + ' .promo.current').fadeOut(this.animationspeed, function(){
			$(this).removeClass('current');
		});
		$(this.selector + ' .pager .dot.current').removeClass('current');
		this.current++;	
		if(this.current >= this.count) this.current = 0;
		$(this.selector + ' .promo').eq(this.current).fadeIn(this.animationspeed, function() {
			$(this).addClass('current');
		});
		$(this.selector + ' .pager .dot').eq(this.current).addClass('current');
		var self = this;
		this.timeout = setTimeout(function(){self.rotate()}, this.delay);
	}
	
	this.start = function(crossdelay) {
		if(!crossdelay) crossdelay = 0;
		var self = this;
		this.timeout = setTimeout(function(){self.rotate()}, parseFloat(this.delay) + parseFloat(crossdelay));
	}
}

function initPromos(){
	$(function() {
		if($('.promoblocks').size() == 0) return;
		var delay = $('.promoblocks').attr('delay');
		var crossdelay = $('.promoblocks').attr('crossdelay');
		var startleft = true;
		var startright = true;
		if($('.promoblocks .left .promo.current').size() > 0) startleft = false;
		if($('.promoblocks .right .promo.current').size() > 0) startright = false;
		
		window.promoLeft = new Promoblock('.promoblocks .left', delay);
		window.promoRight = new Promoblock('.promoblocks .right', delay);
		
		if(startleft) window.promoLeft.start();
		if(startright) window.promoRight.start(crossdelay);
		
		$(".mainpopup #close").click(function(){
			$(".firstatsite").css("display", "block");
			$(".mainpopup").slideUp("slow");
			
			document.cookie="mainpopup=close; path=/; expires=Wed, 01-Jan-2020 00:00:00 GMT";
		})
		
		$(".firstatsite").click(function(){
			$(".firstatsite").css("display", "none");
			$(".mainpopup").slideDown("slow");
			
			document.cookie="mainpopup=; path=/; expires=Mon, 01-Jan-1970 00:00:00 GMT";
		})
	});
}

