

$(document).ready(function(){
	if($('input.focused:first,textarea.focused:first').length == 1){
		$('input.focused:first,textarea.focused:first').focus();
	}
	
	$('form.stdForm').submit(function(){
		if(!stdFormCheck(this)) return false;
		b = $(this).find('button.saveButton').attr('disabled', 'disabled');
		
		if($(b).val()) $(b).html($(b).val());
		
		$(this).find('button.loadIndicator').append('...');
	});
	 
	 
	$('.tabContainer .tab').click(function(){
		var par = $(this).parents('.tabContainer:first');
		$(par).find('.tab').removeClass('activeTab');
		$(this).addClass('activeTab');
		var id = $(this).attr('id');
		$(par).find('.tab-content').removeClass('activeContent');
		$(par).find('.'+id).addClass('activeContent');
	});

	$('.clearOnFocus').focus(function(){
		$(this).css('background-image', 'none');
	});
	
	
	$('.photo-with-text .text-on-photo').hover(function(){
		$(this).stop().animate({
			top:'140px'
		},{
			queue:false,
			duration:160
		});
	}, function() {
		$(this).stop().animate({
			top:'173px'
		},{
			queue:false,
			duration:160
		});
	});
	
	$('.photo-with-text img').hover(function(){
		$(this).parents('.photo-with-text').find('.text-on-photo').stop().animate({
			top:'140px'
		},{
			queue:false,
			duration:160
		});
	}, function() {
		$(this).parents('.photo-with-text').find('.text-on-photo').stop().animate({
			top:'173px'
		},{
			queue:false,
			duration:160
		});
	});

				
	$('#v-menu td').hover(function(){
		if($(this).hasClass('active')) $(this).addClass('always-active'); else	$(this).addClass('active');
	}, function(){
		if(!$(this).hasClass('always-active'))$(this).removeClass('active');
	});
			
			
			
	if($('#leftCalendar').length > 0){
			
		$('#leftCalendar')
		.datePicker({
			inline:true,
			startDate:'01/10/2009',
			endDate: (new Date()).asString()
		})
		.bind(
			'dateSelected',
			function(e, selectedDate)
			{
				document.location = '/news/archive/' + selectedDate.asString();
			//alert(selectedDate.asString());
			}
			);
		if(selectedDay) {
			$('#leftCalendar').dpSetSelected(selectedDay);
			
		}
	}


	if($('#afishaCalendar').length > 0){
		$('#afishaCalendar')
		.datePicker({
			inline:true,
			startDate: (new Date()).asString()
		})
		.bind(
			'dateSelected',
			function(e, selectedDate)
			{
				var sections = '';
				if(afishaSections.length){
					sections = '?sections=';
					for(i in afishaSections)
						sections+='+'+afishaSections[i];
				}
				document.location = '/afisha/date/' + selectedDate.asString() + sections;
			}
			);

	}
});

var afishaSections = new Array();
function afishaFilterClick(obj){
	var name = obj.getAttribute('name');
	if((i = afishaSections.indexOf(name)) >= 0)
		delete afishaSections[i];
	else
		afishaSections.push(name);
}

function stdFormCheck(f){
	$(f).find('.needed').removeClass('wrong');
	$(f).find('.needed').each(function(){
		if($(this).val().match(/^\s*$/)) $(this).addClass('wrong');
	});	
	
	if($(f).find('.wrong').length){
		$(f).find('.wrong:first').focus();
		return false;
	}
	
	return true;	
	
}

var curBanPos = 0;

function banScroll(p){
	if(curBanPos < -3400 && p < 0) return false;
	if(curBanPos >= 0 && p > 0) return false;

	curBanPos += p;
	$('#scrollBanner table').animate({
		left: curBanPos
	}, 1000);
}