var BASE_HREF = BASE_HREF || '../';


(function($){
	
	$.fn.fbGet = function(get, dimension, callback) {
		if(!this.length){
			return this;
		}
		
		var result;
		var m = Math[get];
		this.each(function(index) {
			var newResult = $(this)[dimension]();
			result = index ? m(result, newResult) : newResult;
		});
		
		if(typeof callback == 'function'){
			callback.call(this, result);
			return this;
		}
		
		return result;
	};
	
	$(document).ready(function(){

		// lightbox
		$.fn.kfBox && $('.lightbox').kfBox();

		// specialni odkazy
		$('a.external').click(function(){return !window.open($(this).attr("href"))});
		$("a.print").bind("click", function() { window.print(); return false; });
		
		$('.news-row-1').fbGet('max', 'height', function(val){
			$(this).height(val);
		});
		$('.news-row-2').fbGet('max', 'height', function(val){
			$(this).height(val);
		});
		
		// embedovani flashovych banneru
		$('.flash').each(function(){
			var url = $(this).attr('data-url');
			flashembed(this, url, {
				wmode: 'opaque'
			});
		});
		
		// prehravac v diskografii
		/*$('#jwplayer').size() && jwplayer("jwplayer").setup({
			flashplayer: BASE_HREF + 'player.swf',
			width: 20,
			height: 20,
			events: {
				onComplete: function() {
					// po dokonceni zrusime stop tlacitko
					$('a.play-btn').each(function() {
						$(this).removeClass('stop-btn');
					});
				}
			}
		});
		
		function eq($element, item, count){
			$element.each(function(){
				var $items = $(this).find(item);
				var end = $items.length;
				for (var i=0; i < end; i=i+count) {
					$items.slice(i, i+count).fbGet('max', 'height', function(val){
			    		this.height(val)	
			    	});
				};
			});	
		};
		eq($('.crossroad'), '>ul>li', 3)
		 */
		
		
		$('table')
			.not('.reset')
			.wrap('<div class="table-wrap"></div>')
			.find('tbody tr:odd')
				.addClass('even')
				.end()
			.find('tr td:first-child, tr th:first-child')
				.addClass('first')
				.end()
			.find('tr td:last-child, tr th:last-child')
				.addClass('last')
				.end();
				
		// Instance galerie carouselu	
	    var galleryCarousel = new kff.widgets.Carousel('.photoshow .list ul', {
	    	'visible': 8,
	    	'scroll': 8,
	    	'animation': 500
	    })
	    galleryCarousel.init();		
	    
		// Instance galerie
		var gallery = new kff.widgets.GalleryImg('.photoshow .photo', '.photoshow .list ul', {
			htmlPrev: null,
			htmlNext: null
		})
		gallery.init();
		gallery.$element.bind('next.galleryImg prev.galleryImg', function(event, index){
			if(!galleryCarousel.isVisible(index)){
				if(Math.abs(index - galleryCarousel.current) < galleryCarousel.scroll ){
					if(index > galleryCarousel.current){
						galleryCarousel.next()
					}
					else{
						galleryCarousel.prev();
					}
				}
				else{
					galleryCarousel.scrollTo(index)	
				}
			};
		});
		
		// calendar
		var calendarController = new CalendarController();
		calendarController.init();
				

		/* IE */
    	$('.ie6ie7 a .img img')
    		.click(function(){
    			this.parentNode.click()
    		});	
		
	});

	
	window.onload = function(){
		if($.fn.cycle)
		{
			$('.slideshow')
				.after('<p class="pager"></p>')
				.cycle({
					fx: 'fade',
					speed: 500,
					timeout: 6000,
					pause: true,
					pager: '.pager',
					slideExpr: '.slides'
				});
		}		
	};
	
	var CalendarController = function()
	{
		this.$calendar = $('.calendar').eq(0);
		this.$monthPaging = $('.month-paging').eq(0);
		//this.$dayTimeSwitch = $('.daytime-switch').eq(0);
		this.monthNames = ['Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'];
		this.ajaxUrl = BASE_HREF+'ajax-calendar/';
		//this.daytime = this.$dayTimeSwitch.find('.active').attr('data-daytime') || 'afternoon';
	}
	
	CalendarController.prototype.init = function()
	{
		this.$loader = $('<div class="calendar-loader"/>');
		this.$monthPaging.find('.prev').bind('click', $.proxy(function()
		{
			this.prev();
			return false;
		}, this));
		this.$monthPaging.find('.next').bind('click', $.proxy(function()
		{
			this.next();
			return false;
		}, this));
		
		/*this.$dayTimeSwitch.find('li').bind('click', $.proxy(function(event)
		{
			this.$dayTimeSwitch.find('li').removeClass('active');
			$(event.currentTarget).addClass('active');
			this.setDayTime($(event.currentTarget).attr('data-daytime'));
			return false;
		}, this));             */
	}
	
	CalendarController.prototype.prev = function()
	{
		var data = {
			month: this.$calendar.attr('data-month'),
			year: this.$calendar.attr('data-year')//,
			//part: this.daytime
		};
		
		if(data.month == 1)
		{
			data.month = 12;
			data.year--;
		}
		else
		{
			data.month--;
		}	
		this.ajaxLoad(data);
	}
	
	CalendarController.prototype.next = function()
	{
		var data = {
			month: this.$calendar.attr('data-month'),
			year: this.$calendar.attr('data-year')//,
			//part: this.daytime
		};
		
		if(data.month == 12)
		{
			data.month = 1;
			data.year++;
		}
		else
		{
			data.month++;
		}	
		this.ajaxLoad(data);
	}
	
	/*CalendarController.prototype.setDayTime = function(daytime)
	{
		this.daytime = daytime;
		var data = {
			month: this.$calendar.attr('data-month'),
			year: this.$calendar.attr('data-year'),
			part: this.daytime
		};
		
		this.ajaxLoad(data);
	} */
	
	CalendarController.prototype.ajaxLoad = function(data)
	{
		this.showLoader();
		
		$.ajax({
			url: this.ajaxUrl, 
			data: data,
			dataType: 'html',
			success: $.proxy(function(response)
			{
				var $monthName = this.$monthPaging.find('.month-name');
				
				this.$calendar.attr('data-month', data.month);
				this.$calendar.attr('data-year', data.year);
				
				$monthName.html(this.monthNames[data.month - 1] + ' ' + data.year);
				
				this.$calendar.find('.calendar-table').empty().append(response);
				
				this.hideLoader();		
	        }, this)
		});
	}
	
	CalendarController.prototype.showLoader = function()
	{
		this.$calendar.find('.bd').append(this.$loader);
	}
	
	CalendarController.prototype.hideLoader = function()
	{
		this.$loader.detach();
	}


})(jQuery);

