var BASE_HREF = BASE_HREF || '../';

$.fn.inputDefaultText = function(options)
{  
	options = $.extend({
		text: 'Hledany vyraz'
	}, options);
	
	return this
		.val(options.text)
		.bind('focus', function(){ if(this.value == options.text) this.value = ''; })
		.bind('blur', function(){ if(this.value == '') this.value = options.text; });
};

/* Video controls */ 
(function()
{
	/* Pocitadlo shlednuti videa */
	var player = null;
	var playerCurrentItem = -1;
	var playerCounter = {};
	var playerCounterStop = {};
	var playlist = null;
	
	var playerReady = window.playerReady = function(obj)
	{
		player = document.getElementById(obj['id']);

		(function()
		{
			if (player) {
				player.addModelListener("TIME", "timeListener");
				player.addControllerListener("ITEM", "itemListener");
				if(obj['id'].indexOf('flash-player-playlist') == 0) player.addModelListener("STATE", "stateListener");
			} else {
				setTimeout(arguments.callee, 100);
			}
		})();
	};
	
	timeListener = function(obj)
	{ 
		var file = player.getPlaylist()[playerCurrentItem].file;
		var duration = player.getPlaylist()[playerCurrentItem].duration;
		if (playerCounter[file] == undefined) {
			playerCounter[file] = 0;
			playerCounterStop[file] = 0;
		}
		// pripocitame, jak dlouho uz se na video koukame
		playerCounter[file] += 0.1;
		if (!playerCounterStop[file] && duration > 0 && (playerCounter[file] > (duration / 2))) {
			// uz se koukame, na vic jak polovinu videa, tak pripocteme pocitadlo
			playerCounterStop[file] = 1;
			$.ajax({
				   type: "GET",
				   url: BASE_HREF + "inc/ajax_count_video.php",
				   data: "file=" + file
			});
		}
	};
	
	itemListener = function(obj)
	{
		playerCurrentItem = obj.index;
	};

	stateListener = function(obj)
	{
		switch (obj.newstate)
		{
			case "IDLE":
			case "PAUSED":
			case "COMPLETED":
				$.cookie('videoautostart', 'off', {path: "/", expires: 24 * 30 });
				break;
            case "PLAYING":
				$.cookie('videoautostart', 'on', {path: "/", expires: 24 * 30 });
				break;
		}
	};
	
})();
	
$(document).ready(function()
{
	$('a.external').click(function(){return !window.open($(this).attr("href"))});
	
	$('table tr:nth-child(even)').addClass('even');
	
	$('#q').inputDefaultText({ text: 'Hledaný výraz'});
	
	/* Set width of submenus to the end of container */
	$('#main-menu ul li .submenu ul').each(function(){ 
		$(this).css('width', 960 - $(this).parents('li').get(0).offsetLeft);
	});

	/* Main menu hovering */
	var activeItem = $('#main-menu>ul>li.active');
	var activeTimeout = null;	
	$('#main-menu>ul>li').not('.active')
		.bind('mouseenter', function(){
			clearTimeout(activeTimeout);			 
			$(this).addClass('hover');
			activeItem.removeClass('active');
	 	})
	 	.bind('mouseleave', function(){
			$(this).removeClass('hover');
			activeTimeout = setTimeout(function(){ activeItem.addClass('active'); }, 300);
		});

	// Init kfBox
	$.fn.kfBox && $('.lightbox').kfBox(); 

	$.fn.cycle && $('.partners').cycle({
		fx: 'fade',
		pause: 1,
		speed: 1000,
		timeout: 4000
		}
	);

	$.fn.cycle && $('.featured-article-slider').cycle({
		fx: 'fade',
		pause: 1,
		speed: 1000,
		timeout: 4000
		}
	);

	// Flash player
	$('.flash-player').each(function(i){
 		$(this).flashembed(
          {
               src: BASE_HREF + 'player.swf',
               width: 476,
               height: 290,
               wmode: 'opaque',
               allowfullscreen: 'true',
               allowscriptaccess: 'always',
               id: 'flash-player' + i,
               name: 'flash-player' + i
          },
          {file: $(this).find('a').attr('href'), searchbar: 'false', autostart: 'false', image: $(this).find('img').attr('src')}
     	);
	});

	/* vkládání flashe */
	$('.flash-banner').each(function(){
		flashsize = $(this).find('a').attr('data-flashsize');
		flashsize = flashsize.split("x");
		$(this).flashembed(
		{
			src: $(this).find('a').attr('href'),
			width: flashsize[0],
			height: flashsize[1],
			wmode: 'opaque',
			flashvars: {}
		});
	});
	
	$('.flash-player-playlist').each(function(i){
		var link = $(this).find('a');
		var linkautostart = link.hasClass('autostart') && $.cookie('videoautostart') != 'off' ? 'true' : 'false';
 		$(this).flashembed(
          {
               src: BASE_HREF + 'player.swf',
               width: $(this).css("width"),
               height: $(this).css("height"),
               wmode: 'opaque',
               allowfullscreen: 'true',
               allowscriptaccess: 'always',
               id: 'flash-player-playlist' + i,
               name: 'flash-player-playlist' + i
          },
          {file: link.attr('href'), playlist: 'bottom', repeat: 'list', linktarget: '_self', searchbar: 'false', autostart: linkautostart, image: $(this).find('img').attr('src')}
     	);
	});
	
	$(".share-video").click(function(){
		$(this).select();
	});
	
	/* Captcha */
	if (typeof(captcha) != "undefined") {
		$("#form-captcha").val(captcha);
		$(".captcha").hide();
	} 	
});


$(window).load(function()
{
/*
	var $prevnext = $('<div class="prevnext"><a href="#" class="btn-prev">předchozí</a> <a href="#" class="btn-next">další</a></div>');
    $('.intro-box .article-list').prepend($prevnext);
    var introScroller = new kff.widgets.Scroller('.intro-box .article-list .scroll-box', {
		scrollWidth: 243/3,
		carouselWidth: 243,
		prev: '.intro-box .article-list .btn-prev',
		next: '.intro-box .article-list .btn-next',
		axis: 'y',
		speed: 600
	});
	introScroller.activate();
*/

	var autoScrollTimer = null;
	var bindAutoScroll = function()
	{
		clearInterval(autoScrollTimer);
		autoScrollTimer = setInterval(function(){
			// introScroller.scrollNext();
			$next = $('.col-news-home1 ul li.active').next();
			if($next.size() == 0)
			{
				$('.col-news-home1 ul li:first-child').triggerHandler('mouseenter');
				// introScroller.scrollFirst();
			}
			else
			{
				$('.col-news-home1 ul li.active').next().triggerHandler('mouseenter');
			}
		}, 5000);
	}

	$('.col-news-home1 ul li').bind('mouseenter', function(){
		if($(this).hasClass('active')) return false;
		$('.col-news-home1 ul li.active').removeClass('active');
		$(this).addClass('active');
		var fullImageUrl = this.getAttribute('data-fullimage');
		var title = this.getAttribute('title');
		var $oldImg = $('.col-videobox .video-box img').fadeOut(500);
		$('.col-videobox a.btn-play').attr('href', $('a', this).attr('href'));
		var $fullImage = $('<img />');
		$fullImage.bind('load', function(){
			$fullImage.css('opacity', 0);
			$oldImg.after($fullImage);
			$oldImg.remove();
			$fullImage.fadeTo(500, 1);
			$('.col-videobox .video-desc').html(title);
		}).attr('src', fullImageUrl);
	});
	$('.col-news-home1 .fnews ul li').bind('click', function(event){
		window.location = $('a', this).attr('href');
	});
	$('.col-news-home1 .fnews ul li a').bind('click', function(event){
		event.preventDefault();
	});
	$('.col-news-home1 .fnews').bind('mouseenter', function(event){
		clearInterval(autoScrollTimer);
	}).bind('mouseleave', function(event){
		bindAutoScroll();
	});

	bindAutoScroll();
});

