var infos;
lock = 0;
$(document).ready( function() {		jQuery.fn.center = function () {    this.css("position","absolute");    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");    return this;}	
	$("<div class='load_pre'></div>").css({
		'position':'absolute',
		'z-index': '1500',
		'top': '0',
		'left': '0',
		'width':'100%',
		'height':'100%',
		'background':'#FFF url(../teatr/img/loading.gif) no-repeat center center'
	}).appendTo('.slash, #sponsors_content, #partners_content, #patrons_content');
	$('.calendar_widget').datepicker({		showOtherMonths: true,		beforeShowDay: selectDates,		onSelect: function(date) {
			url="/spektakle/blocks/get_events/date:"+date+"/";
			jQuery("#calendar_events").html("Pobieram...").addClass("ajax-loading");
			jQuery("#calendar_events").load(url,null, function(data) {
				jQuery("#calendar_events").removeClass("ajax-loading");
			});			return false;
		}	});
	$('.link_newtab').click( function() {
		window.open(this.href);
		return false;
	});	$('#terminy_select').bind("change", function() {
		if($(this).val() !== "-1") {
			window.location = '/front/page/get/9/termin_id:'+$(this).val()+'/#terminy_select';
		}
	});	formChange();
	$('#option').change( function() {
		formChange();
	});	$('.white_bar_car_tab').mouseenter( function() {
		moveWhiteTab(1);
	});	$('.controls span').hide();
});function moveWhiteTab(mode) {
	if(mode == 1 && lock == 0) {
		lock = 1;
		$('.white_bar_car').animate({			bottom: '+=50'		}, 200, function() {			setTimeout( function() {				moveWhiteTab(0);			}, 2000);		});	} else if(mode == 0 && lock == 1) {
		$('.white_bar_car').animate({			bottom: '-=50'		}, 700, function() {			lock = 0;		});	}
}function formChange() {
	if($('#option').val() == "0") {
		$('#email').show();
		$('#phone').hide();
	} else {
		$('#phone').show();
		$('#email').hide();
	}
}//To jest po załadowaniu wszystkich obrazków
$(window).load( function() {
	$('.load_pre').fadeTo("slow", 0, function() {		$(this).hide();	});	var carousel = $('#carousel_banners').jcarousel({
		auto: 4,
		start: 1,
		visible: 1,
		wrap: 'both',
		offset: 1,
		easing: 'easeOutExpo',
		animation: 'slow',
		scroll: 1,
		initCallback: serveCarousel,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemVisibleInCallback:  highlight,
		itemVisibleOutCallback: removehighlight
	});
	var wdt = 0;
	$('#sponsors_carousel li a').children().each( function(index) {
		wdt += $(this).width();
	});	var set = 0;
	var rw = wdt + ($('#sponsors_carousel li a').children().size()*40);
	if(rw < 930) {
		set = ((930 - wdt) / $('#sponsors_carousel li a').children().size()) / 2;
	} else {
		set = 20;
	}
	$('#sponsors_carousel li a:first-child').children().each( function(index) {
		$(this).parent().parent().width($(this).width());
		(set != 0) ? $(this).parent().parent().css('margin', '0 '+set+'px') : '';
		($(this).height() != 0) ? $(this).css('margin-top', (74-$(this).height())/2) : '';
	});	var wdt = 0;
	$('#patrons_carousel li a').children().each( function(index) {
		wdt += $(this).width() +40;
	});	var set = 0;
	var rw = wdt + ($('#patrons_carousel li a').children().size()*40);
	if(rw < 280) {
		set = ((280 - wdt) / $('#patrons_carousel li a').children().size()) / 2;
	} else {
		set = 20;
	}
	$('#patrons_carousel li a:first-child').children().each( function(index) {
		$(this).parent().parent().width($(this).width());
		(set != 0) ? $(this).parent().parent().css('margin', '0 '+set+'px') : '';
		($(this).height() != 0) ? $(this).css('margin-top', (74-$(this).height())/2) : '';
	});	var wdt = 0;
	$('#partners_carousel li a').children().each( function(index) {
		wdt += $(this).width()+40;
	});	var set = 0;
	var rw = wdt + ($('#partners_carousel li a').children().size()*40);
	if(rw < 610) {
		set = ((610 - wdt) / $('#partners_carousel li a').children().size()) / 2;
	} else {
		set = 20;
	}
	$('#partners_carousel li a:first-child').children().each( function(index) {
		$(this).parent().parent().width($(this).width());
		(set != 0) ? $(this).parent().parent().css('margin', '0 '+set+'px') : '';
		($(this).height() != 0) ? $(this).css('margin-top', (74-$(this).height())/2) : '';
	});	var carousel2 = $('#sponsors_carousel').jcarousel({
		initCallback: sponsCar,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: "circular",
		scroll: 1,
		visible: 5,
		auto: .01,
		animation: 2000,
		easing: null
	});
	var carousel3 = $('#partners_carousel').jcarousel({
		initCallback: partCar,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: "circular",
		scroll: 1,
		visible: 3,
		auto: .01,
		animation: 2000,
		easing: null
	});
	var carousel4 = $('#patrons_carousel').jcarousel({
		initCallback: patrCar,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: "circular",
		auto: .01,
		visible: 1,
		scroll: 1,
		animation: 2000,
		easing: null
	});
	$('#dla_dzieci, #dla_doroslych').bind("click", function(e) {
		var ad = false;
		$('.dla_dzieci, .dla_doroslych').toggle(800, function() {
			if(ad === false) {				$('#dla_dzieci, #dla_doroslych').toggleClass('active');				ad = true;			};
		});		e.preventDefault();
	});	$('.fancybox:not(.nofancy), .gallery li a:not(.nofancy)').fancybox();
	$('.rounded_corner').corner();
	$(".img-zaokraglony").corner("bevelfold  5px");
});function sponsCar(jc) {
	$('#sponsors_right a').bind("click", function(e) {		jc.next();		e.preventDefault();	});	$('#sponsors_left a').bind("click", function(e) {		jc.prev();		e.preventDefault();	});}function partCar(jc) {
	$('#partners_right a').bind("click", function(e) {		jc.next();		e.preventDefault();	});	$('#partners_left a').bind("click", function(e) {		jc.prev();		e.preventDefault();	});}function patrCar(jc) {
	$('#patrons_right a').bind("click", function(e) {		jc.next();		e.preventDefault();	});	$('#patrons_left a').bind("click", function(e) {		jc.prev();		e.preventDefault();	});}function serveCarousel(jc) {
	var paused = 0;
	$('.carousel .controls li.c a').bind('click', function() {
		jc.scroll($.jcarousel.intval($(this).text()));
		return false;
	});	$('.carousel .controls li#c_p a').bind('click', function() {
		if(paused) {			jc.startAuto();			paused = 0;		} else {			jc.stopAuto();			paused = 1;		}
		return false;
	});}function highlight(carousel, obejctli,liindex,listate) {
	var c = '';
	for(i in infos) {
		// console.log('i+1: '+(parseInt(i)+1)+', liindex: '+liindex);
		if((parseInt(i)+1) == liindex) {
			if(infos[i].type == 2) {
				//  console.log('Type 2');
				c = infos[i].mode2;
			} else {
				//console.log('Other type');
				c = infos[i].title;
			}
		}
	}
	$('.white_bar_car_cont').html(c);
}function removehighlight(carousel, obejctli,liindex,listate) {
	return true;
}function selectDates(date) {
	for (i = 0; i < calendar_dates.length; i++) {
		if (date.getMonth() == calendar_dates[i][1] - 1 && date.getFullYear() == calendar_dates[i][2]  && date.getDate() == calendar_dates[i][0]) {
			return [true, 'sprite_child s1'];
		}
	}
	return [false, ''];
}
