$(document).ready(function() {
	
	$('.gal').fancybox();

	$('.items img[src*=small], .items img[src*=big]').hover(function(){
		var $this = $(this);
		$this.attr('src', $this.attr('src').replace('.jpg', '-hover.jpg'));
	}, function(){
		var $this = $(this);
		$this.attr('src', $this.attr('src').replace('-hover.jpg', '.jpg'));
	});

	////////////////////////////////////////////////////////////////////////////
	// Registration
	
	$('#reg').submit(function(){
		if(!$('#agree:checked').length) {
			alert('Potvrďte prosím, že souhlasíte s pravidly soutěže');
			return false;
		}
	});
	
	//////////////////////////////////////////////////////////////////////////////
  	// Voting - bude predelano, napsano v casove nouzi :)

	function voting(obj, type) {
	
		// Get the parent element
		var parent = $(obj).parent();
		
		// Get the siblings and remove class active
		var siblings = $(obj).parent().parent().find("li");
		siblings.removeClass("active");
		
		// Hover type = 1, set to default !=1
		if(type==1){
		
			var firstchild = $('.voting li').index();
			var childnumber = ($('.voting li').index(parent)%5)+1;
		
		} else {
		
			var activesibling = $(obj).parent().parent().find(".default");
			var childnumber = ($('.voting li').index(activesibling)%5)+1;
		
		}
		
		// And set the default value which was before hover
		for(var i = 0; i < childnumber; i++){
		
			$(siblings[i]).addClass("active");
		
		}
	
		return childnumber; // We need it when we bind click
	}
	
	// JS is "on", we can turn off the js-less CSS alternative
	$(".panel").removeClass("jsless");
	
	// Show values of previous voting of other users
	jQuery.each( $(".voting a") , function() {
		voting(this);
	});
	
	// And bind hover on voting with function
	$(".voting a").hover(function () {
	
		voting(this,1);
	
	}, function () {
	
		voting(this);
	
	});

	////////////////////////////////////////////////////////////////////////////
	// Bookmarks
	
	var activeclass = 'active';
	var primaryhiding = 'n';
	var secondaryhiding = 'ac';
	
	//------------------------------------------------------------------------//
	
	var $bookmarks = $('.bookmarks');
	
	$.each($bookmarks, function() {
	
		$parent = $(this);
		var $links = $parent.find('li a');
		
		// If parent has class "hover", then we bind hover event
		if($parent.hasClass('hover'))
			eventtype = 'mouseenter mouseleave';
		else
			eventtype = 'click';
		
		$.each($links, function() {
		
			$link = $(this);
			$target = $($link.attr('href'));
			
			// If the target exists
			if($target.length) {
			
				// Eliminating problem with selectbox and display="none"
				if($target.find('select').length == 0)
					hidingclass = primaryhiding;
				else
					hidingclass = secondaryhiding;
				
				// Hide all bookmarks except first
				
				if(!$link.closest('li').is(':first-child'))
					$target.addClass(hidingclass);
				else
					$link.addClass(activeclass);
				
				// Binding with click event
				$link.bind(eventtype, function() {
				
					$link = $(this);
					
					// Eliminating problem with selectbox and display="none"
					if($target.find('select').length == 0)
						hidingclass = primaryhiding;
					else
						hidingclass = secondaryhiding;
					
					$sibling = $link.closest('ul').find('.'+activeclass);
					
					// Hide bookmark from active sibling and remove active class
					$($sibling.attr('href')).addClass(hidingclass);
					$sibling.removeClass(activeclass);
					
					// Display target bookmark and add active class
					$($(this).attr('href')).removeClass(hidingclass);
					$link.addClass(activeclass);
				
					return false;
				});
			
			} else { // The target doesn't exist
			
				alert('cil neexistuje');
				$link.bind(eventtype, function() { return false; });
			
			};
		
		});
	
	});

	////////////////////////////////////////////////////////////////////////////
	// Autosending forms

	var sendclass = "autosend";
	var senditems = "input, textarea, select";

	//------------------------------------------------------------------------//

	// Input change binding
	$("."+sendclass).find(senditems).click(function() {
		alert('Automatické odeslání formuláře, zpráva pro testovací účely.');
		this.form.submit();
	});

	////////////////////////////////////////////////////////////////////////////
	// Anchors

	var anchorSelector = 'a';
	var externalTitle = 'Odkaz vede do nového okna';

	//------------------------------------------------------------------------//

	function external() {
		
		var anchors = $(anchorSelector);
		
		jQuery.each( anchors , function() {
			
			$link = $(this);
			
			if($link.attr('rel').search(/external/) == 0) {
				
				$link.attr('target','_blank');
				
				if($link.attr('title') == '')
					$link.attr('title', externalTitle);
				else
					$link.attr('title', $link.attr('title') + ' | ' + externalTitle);
				
			}
				
		});
		
	}
	external();

	////////////////////////////////////////////////////////////////////////////
	// Titleboxes
	
	var offsetX = -200;
	var offsetY = 15;
	var paddingX = paddingY = 15;
	var effectSpeed = 'slow';
	var titleSelector = '.help[title]';

	//------------------------------------------------------------------------//
	// Nice titleboxes
	
	titleMove = function(e) {
	
		var totalX = e.pageX + offsetX;
		var totalY = e.pageY + offsetY;
	
		if(totalX < paddingX)
			totalX = 0 + paddingX;
		else if (totalX > $('body').width())
			totalX = $('body').width() - paddingX;
		
	
		if(totalY < paddingY)
			totalY = 0 + paddingY;
		else if (totalY > $('body').height())
			totalY = $('body').height() - paddingY;
	
		$('#titlebox').css({ left: totalX, top: totalY });
	};
	
	$(document).mousemove(titleMove);
	
	$(titleSelector).mouseover(function(e){
	
		$('body').append('<div id="titlebox"><div id="titlebox-wrap"><div id="titlebox-in"><strong>Nápověda</strong><em></em></div></div></div>');
	
		var origText = $(this).attr('title');
		$(this).attr('title','');
	
		$('#titlebox em').text(origText).parent('#titlebox').fadeIn('normal');
	
	});
	
	
	$(titleSelector).mouseout(function(e){      
	
		$(this).attr('title',$('#titlebox em').text());
		$('#titlebox').css('background-image','none').fadeOut(effectSpeed).remove();
	
	});

});
