(function($) {
	$.fn.jqBS = function(o) {
		o = $.extend({
		}, o || {});
		return this.each(function(){
			var e = $(this);
			var b = e.find('a[rel="btn"]');
			var bs = e.find('a[rel="select"]');
			var m = e.find('ul');
			var mi = m.find('a');
			var w = m.outerWidth('true');
			e.css('width', w);
			m.hide();
			bs.click(function(){
				m.slideToggle();
				return false;
			});
			mi.click(function(){
				b.attr('href', $(this).attr('href'));
				b.html($(this).html());
				m.hide();
				return false;
			});
		});
	}
	$.fn.hideElements = function(o) {
		o = $.extend({
		}, o || {});
		return this.each(function(){
			var e = $(this);
			$(this).css('height', parseInt(e.find('.visible').outerHeight(true))+'px');
			e.find('.c-wrap').hover(function(){e.addClass('c-hover');},function(){e.removeClass('c-hover');});
		});
	}
})(jQuery);

$(document).ready(function(){
	$('.btn-select').each(function(){
		$(this).jqBS();
	});
	$('.counters').hideElements();
	$('.btn').hover(function(){
			$(this).addClass('btn-hover');
		},function(){
			$(this).removeClass('btn-hover');
	}).mousedown(function(){
			$(this).addClass('btn-press');
	}).mouseup(function(){
		$(this).removeClass('btn-press');
	});
	$('.search label').inFieldLabels();
	$('.search form, .block-actions form, .poll form, .ob-list form, .po').jqTransform();
	$('.search #search').clearableTextField();
	$('.search #search').focusin(function(){$(this).parents('form').addClass('focus');});
	$('.search #search').focusout(function(){$(this).parents('form').removeClass('focus');});
	$('input.set-rating').rating({
		required: true,
		scrolling: false
	});
	$('.block-actions').accordion({
		active: false,
		autoHeight: false,
		collapsible: true,
		icons: false
	});
	$('.more-thumbs a.cover:not([rel=movie])').fancybox({
		padding: 20,
		overlayShow: false
	});
	$('.more-thumbs a[rel=movie]').fancybox({
		padding: 20,
		overlayShow: false
	});
	if ( $('#player').length )
		flowplayer('player', 'flowplayer/flowplayer-3.2.7.swf', { wmode: 'transparent' });
	/*
	$('.short-descr td').html(function(index, oldhtml){
		$(this).html('<span class="spoiler">'+oldhtml+'<span class="shadow"></span></span>');
	});
	*/
	$('.full-timetable').each(function(){
		$(this).timetable();
	});
	$('.small-timetable').timetable({slider: false});
	$('#carousel_main').carousel({
		k: 1,
		visible: 1,
		speed: 1000,
		auto: 2000,
		afterLoad: function(a) {$(a[2]).find('.descr').show();},
		beforeStart: function(a) {$(a[2]).find('.descr').hide();},
		afterEnd: function(a) {$(a[2]).find('.descr').show();}
	});
	$('.c100').carousel({
		k: 2,
		visible: 5,
		scroll: 5
	});
	$('#carousel_news').carousel({
		visible: 1,
		speed: 1000,
		auto: 2000
	});
	$('#cbanners').carousel({
		visible: 4,
		speed: 1000,
		auto: 2000
	});
	$('.electro .btn').toggle(
		function(){$(this).siblings('.el-form').slideDown();},
		function(){$(this).siblings('.el-form').slideUp();}
	);
	
	
	/*
	set_new_page_title();
	$('#bookmarks').bookmark({
		url: document.url,
		title: document.title,
		compact: false,
		addFavorite: true,
		favoriteText: 'Избранное',
		favoriteIcon: 385,
		sites: ['vkontakte', 'facebook', 'google', 'twitter', 'hotmail', 'livejournal', 'googlereader', 'yahoo']
	});
	*/
	$('.bookmarks').each(function(){
		var e = $(this).parents('.event');
		$(this).bookmark({
			url: ('http://chastnik.ru'+e.find('a.more').attr('href')),
			title: e.find('.block-title').html()+get_title_end_text(),
			compact: false,
			addFavorite: true,
			favoriteText: 'Избранное',
			favoriteIcon: 385,
			sites: ['vkontakte', 'facebook', 'google', 'twitter', 'hotmail', 'livejournal', 'googlereader', 'yahoo']
		})
	});
	$('.unit-list ul li').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	$('.pre-footer .more').click(function(){
		$('.pre-footer .text').slideDown('slow', function(){
			$('.pre-footer .more').hide();
		});
		return false;
	});
	$('.pre-footer .less').click(function(){
		$('.pre-footer .text').slideUp('slow', function(){
			$('.pre-footer .more').show();
		});
		return false;
	});
	$.datepicker.setDefaults($.extend($.datepicker.regional["ru"]));
	$('#datepicker').datepicker({
		minDate: 0,
		showOn: 'button',
		buttonImage: 'images/calendar-btn.gif',
		buttonImageOnly: true,
		buttonText: 'Выбрать дату',
		onSelect: function() {
			$(this).parents('form').submit();
		}
	});
	$('#e-dp').datepicker({
		  dateFormat: 'dd-mm-yy'
		, altField: '.el-form form #date'
		, beforeShowDay: function(date) {
			var disable = Array(0, 2, 6);
			var day = date.getDay();
			var q = new Array();
			if($.inArray(day, disable)>-1) {
				q[0]=false;
			} else {
				q[0]=true;
			}
			return q;
		}
		, onSelect: function(dateText, inst) {	
			$('.el-form form').submit();
		}
	});
	$('.el-form input[type="checkbox"]:checked').each(function(index){
		var num = $(this).attr('name')-1;
		$('.ui-datepicker tr').each(function(){
			$(this).find('td').eq(num).addClass('select'+num);
		});
	});
	$('.el-form input[type="checkbox"]').change(function(){
		var num = $(this).attr('name')-1;
		if($(this).attr('checked')) {
			$('.ui-datepicker tr').each(function(){
				$(this).find('td').eq(num).addClass('select'+num);
			});
		} else {
			$('.ui-datepicker tr').each(function(){
				$(this).find('td').eq(num).removeClass('select'+num);
			});
		}
	});
	$('.ui-datepicker td').each(function(){
		$(this)/*.find('a')*/.append($(this).attr('title')).wrapInner('<span></span>');
		$(this).removeAttr('title');
	});
	$('.ui-datapicker .ui-state-default').hover(
		function(){$(this).siblings('.tips').show();},
		function(){$(this).siblings('.tips').hide();}
	);
});
