scroller = {
	distance: 147,
	speed: 900,
	itemCnt: null,
	itemsVisible: 3,
	containerId: 'scroller',
	itemClass: 'scrollItem',
	leftId: 'scrollLeft',
	rightId: 'scrollRight',
	position: 0,
	move:3,
	
	init: function(){
		var items = $('#'+this.containerId+' .'+this.itemClass)
		this.itemCnt=items.length;
//		this.distance=items[0].offsetWidth+items[0].style.marginLeft+items[0].style.marginRight;
		if(!document.getElementById(this.containerId)
			|| !document.getElementById(this.leftId)
			|| !document.getElementById(this.rightId)) return false; 
		this.setButtons();
	},
	setButtons: function(){
		if(this.position+this.itemsVisible>=this.itemCnt){
			$('#'+this.rightId).addClass('disabled');
			document.getElementById(this.rightId).onclick=null;
		} else {
			$('#'+this.rightId).removeClass('disabled');
			document.getElementById(this.rightId).onclick=function(){scroller.right();};
		}
		if(this.position>0){
			$('#'+this.leftId).removeClass('disabled');
			document.getElementById(this.leftId).onclick=function(){scroller.left();};
		} else {
			$('#'+this.leftId).addClass('disabled');
			document.getElementById(this.leftId).onclick=null;
		}
	},
	end: function(){
		this.position=this.itemCnt>this.itemsVisible?this.itemCnt-this.itemsVisible:0;
		document.getElementById(this.containerId).style.left=-this.position*this.distance+'px';
		this.setButtons();
	},
	right: function(){
		var move = ( this.position+2*this.move < this.itemCnt ? this.move : this.itemCnt-this.position-this.move );
		$('#'+this.containerId).animate({left:'-='+move*this.distance},this.speed*move/this.move);
		this.position+=move;
		this.setButtons();
	},
	left: function(){
		var move = ( this.position >= this.move ? this.move : this.position );
		$('#'+this.containerId).animate({left:'+='+move*this.distance},this.speed*move/this.move);
		this.position-=move;
		this.setButtons();
	}
}

$(document).ready(function() {
	$("a.lightbox").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': false
	});

	var fake_search_value = "Hledání ...";
	var vyska_fake_value = "výška (cm)";
	var vaha_fake_value = "váha (kg)";
	var autor_fake_value = "Vaše jméno";
	var koment_fake_value = "Váš komentář ...";
	var formcenamenuskoly_fake_value = "V toleranci vyhlášky o školním stravování.";
	var formpokrmyskoly_fake_value = "surovina + hmotnost/objem";
	
	if ($("#search-field").val() == "") {
		$("#search-field").val(fake_search_value);
	}
	
	if ($("#author").val() == "") {
		$("#author").val(autor_fake_value);
	}
	
	if ($("#comment").val() == "") {
		$("#comment").val(koment_fake_value);
	}

	if ($("#vyska").val() == "") {
		$("#vyska").val(vyska_fake_value);
	}

	if ($("#hmotnost").val() == "") {
		$("#hmotnost").val(vaha_fake_value);
	}
	
	if ($("#formmenu").val() == "") {
		$("#formmenu").val(formcenamenuskoly_fake_value);
	}
	
	if ($(".formleft").val() == "") {
		$(".formleft").val(formpokrmyskoly_fake_value);
	}
	
	if ($(".formright").val() == "") {
		$(".formright").val(formpokrmyskoly_fake_value);
	}
		
	manageFakeValue("#search-field", fake_search_value);
	manageFakeValue("#vyska", vyska_fake_value);
	manageFakeValue("#hmotnost", vaha_fake_value);
	manageFakeValue("#author", autor_fake_value);
	manageFakeValue("#comment", koment_fake_value);
	manageFakeValue("#vyska-box", vyska_fake_value);
	manageFakeValue("#hmotnost-box", vaha_fake_value);
	manageFakeValue("#formmenu", formcenamenuskoly_fake_value);
	manageFakeValue(".formleft", formpokrmyskoly_fake_value);
	manageFakeValue(".formright", formpokrmyskoly_fake_value);
	
	$("#vyska,#hmotnost").keypress(function(e) {
		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
	    $("#errmsg-bmi").html("Zadávejte pouze čísla.").show().fadeOut(4000);
	    return false;
	  }
	});
	
	$("#vyska-box,#hmotnost-box").keypress(function(e) {
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
	    $("#errmsg-box").html("Zadávejte pouze čísla.").show().fadeOut(4000);
	    return false;
	  }
	});
	
	$("#odeslat_bf").hover(function() { $(this).attr("src", "/images/page/btn_odeslat_bf_on.gif"); }, function() { $(this).attr("src", "/images/page/btn_odeslat_bf.gif"); });
	
//	$("a[rel^='PrettyPhoto']").prettyPhoto({
//		showTitle: false,
//		theme: 'facebook',
//		opacity: .8
//	});
	
	scroller.init();
});

function manageFakeValue(obj, val) {
	$(obj)
	.focus(function() {
		if ($(this).val() == val) {
			$(this).val("");
			this.style.color='#000';
		}
	})
	.blur(function() {
		if ($(this).val() == "") {
			$(this).val(val);
			this.style.color='#999';
		}
	});
	var elms = $(obj).get();
	for(i in elms){elms[i].style.color='#999';}
}

Cufon.replace('.menu-box ul li a, h2, .hp-box h3, .hp-box span a, .anketa span, .content-left-right-cont h1', { fontFamily: 'AnselmSans', hover: true });

function videoPlayer(index){
	var html='';
	var disabled=false;
	html+='<div id="videoPlayer"><div class="videoPlayer-top"></div><div class="videoPlayer-bg">';
		html+='<div class="cross" onclick="closePopup()"></div>';
		html+='<h3>'+videa[index].comment+'</h3>';
		html+='<iframe title="YouTube video player" width="522" height="331" src="http://www.youtube.com/embed/'+videa[index].code+'?rel=0" frameborder="0" allowfullscreen></iframe>';
		// nasledujici
		html+='<a ';
		if(index<videa.length-1){
			html+='href="javascript:openPopup('+(index+1)+')" ';
		} else disabled=true;
		html+='class="arrowRight'+(disabled?' disabled':'')+' innerText">Následující video</a>';
		// predchozi
		disabled=false;
		html+='<a ';
		if(index>0){
			html+='href="javascript:openPopup('+(index-1)+')" ';
		} else disabled=true;
		html+='class="arrowLeft'+(disabled?' disabled':'')+' innerText">Předchozí video</a>';
		html+='<div class="clear"></div>';
	html+='</div><div class="videoPlayer-bottom"></div></div>';
	return html;
}

popupOpen=false;
function openPopup(index){
	if(!popupOpen){
		var overlay = document.createElement('div');
		overlay.id='shadowOverlay';
		overlay.className='shadowOverlay';
		document.body.appendChild(overlay);
		var popup = document.createElement('table');
		popup.id='popup';
		document.body.appendChild(popup);
		$(popup).empty();
		$(popup).append('<tr><td>'+videoPlayer(index)+'</td></tr>');
	}
	else {
		document.getElementById('popup').innerHTML='<tr><td>'+videoPlayer(index)+'</td></tr>';
	}
	popupOpen=true;
}
function closePopup(){
	if(popupOpen){
		document.body.removeChild(document.getElementById('popup'));
		document.body.removeChild(document.getElementById('shadowOverlay'));
	}
	popupOpen=false;
}

function getRecipes(id){
	$.ajax({
		url: "/ajax/recepty",
		data: "getCategory="+id,
		success: function(response) {
			eval('var recipes='+response);
			var select = $('#menuDishId');
			select.options.length=recipes.length;
			var i=0;for(var id in recipes){
				select.options[i].text=recipes[id];
				select.options[i].value=id;
				i++;
			}
		}
	});
}

function checkFormPrihlaska(form){
	f=form;
	return false;
}

