(function($){


	$.fn.kfImageRotator = function(options)
	{
		var o = $.extend({
			rowTimeOut: 3000,
			imageTimeOut: 20,
			fadeOut: 150,
			fadeIn: 150,
			imagesList: [],
			imagesPath: ''
	  	}, options);

		var $elems = $(this);
		var length = $elems.size();
		var imgBuffer = [];
	    var rotIndex = length-1;
	    var imagesListLength = o.imagesList.length;

	    var rotateCountdown = length;
	    var preloadCountdown = 0;
	    var timeOutReady = false;

	    init();
	    return this;

	    function init()
	    {
			prepareNextRound();

	    };

		function rotate()
		{
			rotateCountdown = length;
			$elems.each(function(i)
			{
				var that = this;
				setTimeout(function(){ rotateSingle($(that), i); }, o.imageTimeOut * (i+1));
			});
		};

		function rotateSingle($e, i)
		{
			var targetHeight = imgBuffer[i][0].height;
			var targetWidth = imgBuffer[i][0].width;

			$('img', $e).animate({ height: '0', width: 100, marginTop: 37 }, o.fadeOut, 'easeOutCirc' , function(){
					var href = $.data(imgBuffer[i][0], 'href');
					if(href) $e.filter('a').attr('href', href);
					else $e.filter('a').removeAttr('href');
					imgBuffer[i]
						.css({ height: '0', width: targetWidth, marginTop: 37})
						.appendTo($e.empty())
						.animate({ height: targetHeight + 'px', marginTop: (75 - targetHeight)/2 + 'px' }, o.fadeIn, 'easeInCirc');
				});

			var that = this;
			if(--rotateCountdown <= 0) setTimeout( prepareNextRound, o.imageTimeOut * (i+1));
		};

		function prepareNextRound()
		{
			preloadCountdown = $elems.size();
			imgBuffer = [];
			timeOutReady = false;
			var that = this;
			for(var i = 0, l = preloadCountdown; i <l; i++)
			{
				rotIndex = (rotIndex + 1) % imagesListLength;
				var img = document.createElement('img')
				imgBuffer[i] = $(img)
					.bind('load', function(){ --preloadCountdown; preloadCallback(); })
					.attr({
						'src': o.imagesPath + o.imagesList[rotIndex].file,
						'alt': o.imagesList[rotIndex].alt,
						'title': o.imagesList[rotIndex].title
					});

				if(o.imagesList[rotIndex].href) $.data(img, 'href', o.imagesList[rotIndex].href);
			}
			setTimeout(function(){ timeOutReady = true; preloadCallback(); }, o.rowTimeOut);
		};

		function preloadCallback()
		{
			if(preloadCountdown <= 0 && timeOutReady) rotate();
		};


	};


	$(document).ready(function(){

    	// lightbox
		$.fn.kfBox && $('.lightbox').kfBox();

		$('.messages-list').each(function(i){
			$(this).find('.lightboxreport').kfBox({minWidth: 700, maxWidth: 700});
		});

		$('.bandvote').each(function(i){
			$(this).find('.lightboxbands').kfBox({minWidth: 750, maxWidth: 750});
		});

        // table
    	$('.wysiwyg table')
    		.wrap('<div class="table-wrap"></div>')
    		.find('tbody tr:odd')
    			.addClass('even');

    	// separator
    	$('.wysiwyg hr')
    		.wrap('<div class="separator"></div>')
    		.hide();


    	// IE
    	$('.ie6ie7 a .img img, .ie6ie7 .filter-box img, .ie8 .filter-box img')
    		.click(function(){
    			this.parentNode.click()
    		});

    	// Home carousel
		var homeCarousel = new fb.widgets.Carousel('.band-list ul', {
	    	'visible': 5,
	    	'scroll': 5,
	    	'animation': 250
	    })
	    homeCarousel.init();
	    
	    $('#fake-thickbox').each(function(){
	    	var $box = $(this);
	    	var $overlay = $('#fake-thickbox-overlay');
	    	$overlay.add($box.find('.close, a.fr')).bind('click', function(){
	    		$overlay.remove();
	    		$box.remove();
	    		return false;
	    	});
	    });

		/* vkládání flashe */
		$('.flash-banner').each(function(){
			flashsize = $(this).find('a').attr('data-flashsize');
			flashsize = flashsize.split("x");
			$(this).flash(
			{
				src: $(this).find('a').attr('href'),
				width: flashsize[0],
				height: flashsize[1],
				wmode: 'opaque',
				flashvars: {}
			});
			$(this).find('.alt').css("display","none");
		});

		$('.flash-player').each(function(){
	 		flashsize = $(this).find('a').attr('data-flashsize');
	 		flashsize = flashsize.split("x");
	 		$(this).flash(
	        {
	             src: '/inc/flash/player.swf',
	             width: flashsize[0],
	             height: flashsize[1],
	             wmode: 'opaque',
	             allowfullscreen: true,
	             flashvars: {file: $(this).find('a').attr('href'), searchbar: 'false', autostart: 'false', frontcolor: '0x666666', image: $(this).find('img').attr('src')}
	        });
	     	// nebudeme zobrazovat alt
	     	$(this).find('.alt').css("display","none");
		});
		/* vkládání mp3 */
		$('.flash-mp3player').each(function(){
			$(this).flash(
			  {
				   src: '/inc/flash/playermp3.swf',
				   width: 18,
				   height: 20,
				   wmode: 'opaque',
				   flashvars: {file: $(this).find('a').attr('href'), searchbar: 'false', autostart: 'false', frontcolor: '0x666666'}
			  }
			);
		});
		/* Postupné vybírání kraj-město-škola-fakulta selectů */
		if($('#form-county').size())
		{
			$('#form-county').bind('change', function(){
				$('#form-cityx, #form-school, #form-faculty').parent().addClass('hidden');
				var id = $(this).val();
				if(id != '')
				{
					$('#form-cityx').parent().addClass('loading').removeClass('hidden');
					$.get('/get/cities/' + id + '/', function(html){ $('#form-cityx').parent().removeClass('loading').html(html); bindCity(); })
				}
			});

			var bindCity = function()
			{
				$('#form-cityx').bind('change', function(){
					$('#form-school, #form-faculty').parent().addClass('hidden');
					var id = $(this).val();
					if(id != '')
					{
						$('#form-school').parent().addClass('loading').removeClass('hidden');
						$.get('/get/schools/' + id + '/', function(html){ $('#form-school').parent().removeClass('loading').html(html); bindSchool(); })
					}
				});
			};

			var bindSchool = function()
			{
				$('#form-school').bind('change', function(){
					$('#form-faculty').parent().addClass('hidden');
					var id = $(this).val();
					if(id != '')
					{
						$('#form-faculty').parent().addClass('loading').removeClass('hidden');
						$.get('/get/faculties/' + id + '/', function(html){
							$('#form-faculty').parent().removeClass('loading').html(html);
							if($('#form-faculty').parent().find('option').size() <= 1) $('#form-faculty').parent().addClass('hidden');
						})
					}
				});
			};
		}
		if($('#form-county2').size())
		{
			$('#form-county2').bind('change', function(){
				$('#form-cityx2, #form-school2, #form-faculty2').parent().addClass('hidden');
				var id = $(this).val();
				if(id != '')
				{
					$('#form-cityx2').parent().addClass('loading').removeClass('hidden');
					$.get('/get/cities2/' + id + '/', function(html){ $('#form-cityx2').parent().removeClass('loading').html(html); bindCity(); })
				}
			});

			var bindCity = function()
			{
				$('#form-cityx2').bind('change', function(){
					$('#form-school2, #form-faculty2').parent().addClass('hidden');
					var id = $(this).val();
					if(id != '')
					{
						$('#form-school2').parent().addClass('loading').removeClass('hidden');
						$.get('/get/schools2/' + id + '/', function(html){ $('#form-school2').parent().removeClass('loading').html(html); bindSchool(); })
					}
				});
			};

			var bindSchool = function()
			{
				$('#form-school2').bind('change', function(){
					$('#form-faculty2').parent().addClass('hidden');
					var id = $(this).val();
					if(id != '')
					{
						$('#form-faculty2').parent().addClass('loading').removeClass('hidden');
						$.get('/get/faculties2/' + id + '/', function(html){
							$('#form-faculty2').parent().removeClass('loading').html(html);
							if($('#form-faculty2').parent().find('option').size() <= 1) $('#form-faculty2').parent().addClass('hidden');
						})
					}
				});
			};
		}

		/*
        if(!logoList){
			var logoList = [
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-palacecinemas_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-gambrinus2_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-praha_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-axe_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-carrot-euro_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-fajnradio_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-tservis_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-dpp_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-neoluxor_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-metropolislive_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-studenta_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-studentska-leta_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-poslouchejnet_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-vsem_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-bozkov_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-tshock_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-bmpr_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-joyride_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-ticketportal_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-ukmedia_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-ukacko_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-su-uk_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-su-cvut_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-fanonline_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-libimseti_resize_100_75.jpg', alt: '', title: '', href: ''},
				{file: 'http://brno.majales2011.cz.kappa.nen.cz/cache/images/wl-social_resize_100_75.jpg', alt: '', title: '', href: ''}
			];
		}*/
		if(typeof logoList != 'undefined'){

			setTimeout(function(){
				$('#partners li>a').kfImageRotator({
			      rowTimeOut: 5000,
			      imageTimeOut: 100,
			      fadeOut: 250,
			      fadeIn: 250,
			      imagesPath: '',//../img/samples/
			      imagesList: logoList
			    });
			}, 5000);
		}
    });

	
	 /*Raphael.fn.drawGrid = function (x, y, w, h, wv, hv, color) {
	    color = color || "#000";
	    var path = ["M", Math.round(x) + .5, Math.round(y) + .5, "L", Math.round(x + w) + .5, Math.round(y) + .5, Math.round(x + w) + .5, Math.round(y + h) + .5, Math.round(x) + .5, Math.round(y + h) + .5, Math.round(x) + .5, Math.round(y) + .5],
	        rowHeight = h / hv,
	        columnWidth = w / wv;
	    for (var i = 1; i < hv; i++) {
	        path = path.concat(["M", Math.round(x) + .5, Math.round(y + i * rowHeight) + .5, "H", Math.round(x + w) + .5]);
	    }
	    for (i = 1; i < wv; i++) {
	        path = path.concat(["M", Math.round(x + i * columnWidth) + .5, Math.round(y) + .5, "V", Math.round(y + h) + .5]);
	    }
	    return this.path(path.join(",")).attr({stroke: color});
	};*/
	
	window.onload = function(){
		if(typeof Raphael != 'undefined' && $('.status-box .chart').length){
			var $holder = $('.status-box .chart').css({'position':'relative'}),
				$tooltip = $('<span class="chart-tooltip"></span>').appendTo($holder),
				labels = $.map($('.status-box .chart thead th'), function(that, i){ return $(that).text()}),
				values = $.map($('.status-box .chart tbody td'), function(that, i){ return $(that).text()}),
				
				rounding = 1000,
				txt = {font: '10px Helvetica, Arial', fill: "#093c5e"},
				txts = {font: '10px Helvetica, Arial', fill: "#f232b1"},
		        txt1 = {font: '8px Helvetica, Arial', fill: "#093c5e", 'text-anchor':'end'},
		        txt2 = {font: '12px Helvetica, Arial', fill: "#fff"},
		        stroke = {stroke: "#a3bf40", 'stroke-width': 3},
		        circle = {fill: "#a3bf40", stroke: "#fcfeff", 'stroke-width': 2, 'cursor': 'pointer'},
	
				maxValue = Math.max.apply(Math, values),
				rows = Math.ceil(maxValue/rounding),
				cols = labels.length - 1,
				width = $holder.width(),
				height = $holder.height(),
				gutter = {
					top: 10,
					bottom: 22,
					left: 25,
					right: 16
				},
				X = Math.round((width - gutter.left - gutter.right) / cols),
				Y = Math.round((height - gutter.top - gutter.bottom) / rows),
				
				R = Raphael($holder[0], width, height);
	
			// generate grid
			//R.drawGrid(gutter.left, gutter.top, width - gutter.left - gutter.right, height - gutter.top - gutter.bottom, cols, rows);
			
			// x labels
			for (var i = 0, ii = cols + 1; i < ii; i++) {
		        var x = Math.round( gutter.left + X * (i)),
		            t = R.text(x, height - gutter.bottom  + 13, labels[i]).attr(labels[i] == '29.4' ? txts : txt).toBack();
			}
			
			// y labels
			for (var i = 1, ii = rows; i <= ii; i++) {
		        var y = Math.round( height - gutter.bottom - Y * (i)),
		            t = R.text(gutter.left - 8, y, rounding*i).attr(txt1).toBack();  
			}
			
			// path + circle
			path = [];
			for (var i = 0, ii = cols + 1; i < ii; i++) {
				var x = gutter.left + X * i,
					y = height - gutter.bottom - ( rows / maxValue * values[i] ) * Y;
				path = path.concat([i ? 'L' : 'M', x, y])
				
				// circle	
	   			var c = R.circle(x, y, 6).attr(circle);
	   			
	   			(function(circle, x, y, value){
	   				circle.hover(function() {
	   					this.attr({'r': 8})
	   					$tooltip
	   						.stop(true, true)
	   						.text(value)
	   						.css({'left': x - ($tooltip.outerWidth() / 2) + 'px', top: y - $tooltip.outerHeight() - 10  + 'px'})
	   						.animate({'opacity': 1}, 250);	
	   				}, function() {
	   					this.attr({'r': 6})
	   					$tooltip
	   						.stop(true, true)
	   						.animate({'opacity': 0}, 250, function(){
	   							$(this).css('left', '');
	   						});	
	   				});	
	   			})(c, x, y, values[i])
	   			
			}
			var p = R.path(path.join(",")).attr(stroke).toBack();
		}
	};


})(this.jQuery);



