$(window).load(function() {
	
	var baseurl = $('#baseurl').text();
	var siteurl = $('#siteurl').text();
	
	
		$( ".sortable" ).sortable({
			items: 'tr'
		});
		
		$('#slider').animate({
			opacity: 1
		}, 1000);
		
		
		$('#slider')
		.cycle({
			fx: 'scrollLeft',
			speed: 1000, 
	    	timeout: 5000,
			pause: 1,
		});
		
		var lok=$('#postit li.select');
		
		lok.live('click', function() {
			$.ajax({
				url: siteurl+'postit/select/'+$(this).attr('rel'),
				success: function(html) {
					$('#contact').html(html);
				}
			});
		});
		
		$('#postit .change').live('click', function() {
			$.ajax({
				url: siteurl+'postit/change',
				success: function(html) {
					$('#contact').html(html);
				}
			});			
		});

		$('.viac').live('click', function() {
			var vel=$(this).parents('.control').attr('rel');
			var id=$(this).parents('.jedlo').attr('rel');
			var qty=parseInt($(this).siblings('.cislo').html());
			if (qty < 5) {
				qty=qty+1;
				$(this).siblings('.cislo').html(qty);
				$(this).siblings('.cislo').removeClass('zero');
				$.ajax({
					url: siteurl+'ponuka/addToCart/'+id+'/'+vel+'/'+qty,
					success: function(html) {
						$('#cart').html(html);
					}
				})
			}
			return false; // prevent scrolling to top after ajax call
		});

		$('.menej').live('click', function() {
			var vel=$(this).parents('.control').attr('rel');
			var id=$(this).parents('.jedlo').attr('rel');
			var qty=parseInt($(this).siblings('.cislo').html());
			if (qty > 0) {
				qty=qty-1;
				if (qty == 0) {
					$(this).siblings('.cislo').addClass('zero');
				}
				$(this).siblings('.cislo').html(qty);
				$.ajax({
					url: siteurl+'ponuka/addToCart/'+id+'/'+vel+'/'+qty,
					success: function(html) {
						$('#cart').html(html);
					}
				})
			}
			return false; // prevent scrolling to top after ajax call
		});
		
		$('#filter a').click(function() {
			var id=$(this).attr('id');
			$('tr.jedlo').hide();
			$('tr.'+id).show();
			$('#filter a').removeClass('active');
			$(this).addClass('active');
		});
		
		var blink = $('#blink').attr('rel');
		if (blink == 1) {
			blinkni();
		}
		
		function blinkni() {
			$('#postit .select').fadeOut('100', function() {
				$(this).fadeIn('100');
			})
		}
		
		var blikanie = window.setInterval(function() {
			blinkni();
		}, 3000);
		

	$('.slide-out-div').tabSlideOut({
	  tabHandle: '.handle',                     //class of the element that will become your tab
	  pathToTabImage: baseurl+'/images/napiste_nam.png', //path to the image for the tab //Optionally can be set using css
	  imageHeight: '202px',                     //height of tab image           //Optionally can be set using css
	  imageWidth: '43px',                       //width of tab image            //Optionally can be set using css
	  tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
	  speed: 300,                               //speed of animation
	  action: 'click',                          //options: 'click' or 'hover', action to trigger animation
	  topPos: '100px',                          //position from the top/ use if tabLocation is left or right
	  leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
	  fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
  });

	/** kniha **/
	$('.button').click(function() {
		id = $(this).attr('id');
		$('.button').removeClass('active');
		$(this).addClass('active');
		$('#what-value').val(id);
	});

	$('.odoslat a').click(function() {
		if ($('.kniha [name="odkaz"]').val()) {
			$.ajax({
				type: 'POST',
				data: {
					www: $('.kniha input[name="www"]').val(),
					lokalita: $('.kniha [name="lokalita"]').val(),
					what: $('.kniha [name="what"]').val(),
					odkaz: $('.kniha [name="odkaz"]').val(),
					kontakt: $('.kniha [name="kontakt"]').val(),
					submit: 1
				},
				url: siteurl+'kniha/odoslat/',
				success: function(html) {
					$('.kniha .messages').html(html);
					$('.odoslat').remove();
					$('.button').removeClass('active');
					$('.kniha [name="odkaz"]').val('');
					$('.kniha [name="kontakt"]').val('');
				},
				error: function() {
					alert ('Ups, prepáčte, čosi je zle...');
				}
			});
		}
		else {
			$('.kniha [name="odkaz"]').fadeOut('50', function() {
				$(this).fadeIn('50');
			});
		}
	});

});
