/* --- Press --- */
/*$(document).ready(function()
{
	$('.cli').mousedown(function() { $(this).addClass('press');	});
	$('.cli').mouseout(function() { $(this).removeClass('press'); });
	$('.cli').click(function() { $(this).removeClass('press'); });
});*/

var menuChecker;

/* --- Přihlašovací formulář --- */
$(document).ready(function()
{
	$('#loginShow').click(function ()
	{
		$('#loginForm').fadeIn(300);
		$('#loginName').focus();
		return false;
	});
	
	var loginChecker = false;	
	$('#loginForm').hover(function() { menuChecker = true; }, function() { menuChecker = false; });	

	$('body').click(function ()
	{
		if (!$('#loginForm').is(':hidden') && !menuChecker)
		{
			$('#loginForm').fadeOut(200);
			$('#loginForm span').hide();
		}
	});

	$('#inputLogin').click(function ()
	{
		$('#loginLoading').css('visibility', 'visible');
	});
});	
	

/* --- Vyhledávací pole --- */
$(document).ready(function()
{		
	$('#searchText').focus(function ()
	{
		$('#headSearch').addClass('focus');
	});

	$('#searchText').blur(function ()
	{
		$('#headSearch').removeClass('focus');
	});
});





	/* --- Notify --- */
$(document).ready(function()
{	
	$('.delete').live('click', function () {
		$("#head-background").append('<div id="notify"></div>')
		$("#head-background").append('<div id="notifyField"><div id="fieldBoxTop"></div><div id="fieldBoxMiddle"><div id="fieldBoxBg"><strong>Upozornění</strong><p>' + $(this).attr('rel')  + '</p><a href="#" id="notifyCancel">Storno</a><a href="' + $(this).attr('href') +'" id="notifyOK">OK</a><div class="clear"></div></div></div><div id="fieldBoxBottom"></div></div>')		
	
		$('#notify').show();	
		$('#notifyField').fadeIn(300);
		return false;
	});
	
	$('#notifyCancel').live('click', function () {
		$('#notifyField').fadeOut(300);
		$('#notify').hide();		
		return false;
	});

	$('#notifyOK').live('click', function () {
		$('#notifyField').fadeOut(300);
		$('#notify').hide();	
		return true;
	});
	
	$('#notifyField a').live('mousedown', function () {
		$(this).addClass('press');
	});
	
	$('#notifyField a').live('mouseout', function () {
		$(this).removeClass('press');
	});
	
	 	/* --- Pravidla --- */
 	if ($('#siteRules'))
 	{
		$('#rules').show();	
 		$('#siteRules').fadeIn(300);
 	}
 	
	$('#siteRules a').mousedown(function ()
	{
		$(this).addClass('press');
	});
	
	$('#siteRules a').mouseout(function ()
	{
		$(this).removeClass('press');
	});
	
	$('#rulesCancel').click(function ()
	{
		$(this).removeClass('press');	
		return false;
	});
});


/* ----------------------------------------------------------------------------------------------------- */
/* Checkboxy */
$(document).ready(function()
{
	$('dd.checkbox a').click(function ()
	{
		$(this).removeClass('press');
		var checkInput = $(this).closest('dd').children('input');
	
		// Unchecking
		if ($(checkInput).is(':checked'))
		{
			$(this).animate({"left": "-=36px"}, 300);
			$(this).children('span').animate({"left": "+=36px"}, 300);			
			$(this).removeClass('on');
			$(checkInput).removeAttr('checked');			
		}
		
		// Checking
		else
		{
			$(this).animate({"left": "+=36px"}, 300);
			$(this).children('span').animate({"left": "-=36px"}, 300);
			$(this).addClass('on');
			$(checkInput).attr('checked', 'checked');
		}
		return false;
	});
	
	$('dd.checkbox a').mousedown(function () {
		$(this).addClass('press');
	});

	$('dd.checkbox a').mouseout(function () {
		$(this).removeClass('press');
	});		
});


/**
 * Funkce pro rozbalování a zabalování položek
 *
 * @param string			param
 */
function outliner(param) 
{
	var obj = $('#' + param);

	if ($(obj).hasClass('collapsed'))
	{
		$(obj).addClass('expanded').removeClass('collapsed');
	}
	
	else if ($(obj).hasClass('expanded'))
	{
		$(obj).addClass('collapsed').removeClass('expanded');	
	}
}


/* Funkce zajišťující kompatibilitu práce s objekty v různých browserech */
function getObj(name) 
{
	if (document.getElementById) return document.getElementById(name);
	else if (document.all) return document.all[name];
	else if (document.layers) return document.layers[name];
}






$(document).ready(function()
{
	/* --- Focus formulářových prvků --- */
	$("dl.formInputs textarea").each(function ()
	{
		$(this).focus(function ()
		{
			$(this).parent().addClass('focus');
		});
		
		$(this).blur(function ()
		{
			$(this).parent().removeClass('focus');
		});		
	});

	$("dl.formInputs input").each(function ()
	{
		$(this).focus(function ()
		{
			$(this).parent().addClass('focus');
		});
		
		$(this).blur(function ()
		{
			$(this).parent().removeClass('focus');
		});		
	});
	
	$("dl.formInputs select").each(function ()
	{
		$(this).focus(function ()
		{
			$(this).parent().addClass('focus');
		});
		
		$(this).blur(function ()
		{
			$(this).parent().removeClass('focus');
		});		
	});
});
















/* ----------------------------------------------------------------------------------------------------- */
/* Funkce pro zašrktnutí všech řádků */
function checkAll(child, type) {
	var object = $('.' + child);
	
	for (i = 0; i < object.length; i++) {
		if (type == 'over') {
			object[i].style.backgroundColor = '#fdf8e1';		
		} else {
			object[i].style.backgroundColor = 'transparent';				
		}
	}
}

function checkAlli(child) {
	var object = $('.' + child);

	for (i = 0; i < object.length; i++) {	
			object[i].checked = true;
	}
}

/* ----------------------------------------------------------------------------------------------------- */
/* Zašrktnutí všech checkboxů */
$(document).ready(function() {
	$("#postid_all").click(function()				
		{
			$("input[name=postid[]]").each(function()
			{
			this.checked = this.checked == true ? false : true;
		});
		this.innerHTML = this.innerHTML == 'Označit vše' ? 'Zrušit vše' : 'Označit vše';
		return false;
	});					
});
$(document).ready(function() {
	$("#postus_all").click(function()				
		{
			$("input[name=postus[]]").each(function()
			{
			this.checked = this.checked == true ? false : true;
		});
		this.innerHTML = this.innerHTML == 'Označit vše' ? 'Zrušit vše' : 'Označit vše';
		return false;
	});					
});


function checkItem(child)
{
	var ident = getObj(child);

	ident.checked = ident.checked == true ? false : true;
	return false;	
}




/* ----------------------------------------------------------------------------------------------------- */
/* Ošetření odkazů */
function urlencode(str) 
{
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
  
	return str;
}









/* ----------------------------------------------------------------------------------------------------- */
/* Selectboxy */
(function($)
{
	$.fn.extend(
	{
		customStyle : function(options)
		{
			if (!$.browser.msie || ($.browser.msie&&$.browser.version>6))
			{
				return this.each(function()
				{
	  				var currentSelected = $(this).find(':selected');
					$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0});
					var selectBoxSpan = $(this).next();
					var selectBoxSpanInner = selectBoxSpan.find(':first-child');
					selectBoxSpan.css({display:'block'});
					selectBoxSpanInner.css({display:'block'});
					var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
					$(this).height(selectBoxHeight).change(function()
					{
						// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
						selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
						// Thanks to Juarez Filho & PaddyMurphy
					});
				});
			}
		}
	});
})(jQuery);
$(document).ready(function()
{
	$('.mySelectBoxClass').customStyle();
});





/*
 * jQuery selectbox plugin
 *
 * Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
 * Licensed under the GPL license and MIT:
 *   http://www.opensource.org/licenses/GPL-license.php
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * The code is inspired from Autocomplete plugin (http://www.dyve.net/jquery/?autocomplete)
 *
 * Revision: $Id$
 * Version: 0.5
 * 
 * Changelog :
 *  Version 0.5 
 *  - separate css style for current selected element and hover element which solve the highlight issue 
 *  Version 0.4
 *  - Fix width when the select is in a hidden div   @Pawel Maziarz
 *  - Add a unique id for generated li to avoid conflict with other selects and empty values @Pawel Maziarz
 */
jQuery.fn.extend({
	selectbox: function(options) {
		return this.each(function() {
			new jQuery.SelectBox(this, options);
		});
	}
});


/* pawel maziarz: work around for ie logging */
if (!window.console) {
	var console = {
		log: function(msg) { 
	 }
	}
}
/* */

jQuery.SelectBox = function(selectobj, options) {
	
	var opt = options || {};
	opt.inputClass = opt.inputClass || "selectbox";
	opt.containerClass = opt.containerClass || "selectbox-wrapper";
	opt.hoverClass = opt.hoverClass || "current";
	opt.currentClass = opt.selectedClass || "selected"
	opt.debug = opt.debug || false;
	
	var elm_id = selectobj.id;
	var active = -1;
	var inFocus = false;
	var hasfocus = 0;
	//jquery object for select element
	var $select = $(selectobj);
	// jquery container object
	var $container = setupContainer(opt);
	//jquery input object 
	var $input = setupInput(opt);
	// hide select and append newly created elements
	$select.hide().before($input).before($container);
	
	
	init();
	
	$input
	.click(function(){
    if (!inFocus) {
		  $container.toggle();
		}
	})
	.focus(function(){
	   if ($container.not(':visible')) {
	       inFocus = true;
	       $container.show();
	   }
	})
	.keydown(function(event) {	   
		switch(event.keyCode) {
			case 38: // up
				event.preventDefault();
				moveSelect(-1);
				break;
			case 40: // down
				event.preventDefault();
				moveSelect(1);
				break;
			//case 9:  // tab 
			case 13: // return
				event.preventDefault(); // seems not working in mac !
				$('li.'+opt.hoverClass).trigger('click');
				break;
			case 27: //escape
			  hideMe();
			  break;
		}
	})
	.blur(function() {
		if ($container.is(':visible') && hasfocus > 0 ) {
			if(opt.debug) console.log('container visible and has focus')
		} else {
			hideMe();	
		}
	});


	function hideMe() { 
		hasfocus = 0;
		$container.hide(); 
	}
	
	function init() {
		$container.append(getSelectOptions($input.attr('id'))).hide();
//		var width = $input.css('width');
//		$container.width(width);
    }
	
	function setupContainer(options) {
		var container = document.createElement("div");
		$container = $(container);
		$container.attr('id', elm_id+'_container');
		$container.addClass(options.containerClass);
		
		return $container;
	}
	
	function setupInput(options) {
		var input = document.createElement("input");
		var $input = $(input);
		$input.attr("id", elm_id+"_input");
		$input.attr("type", "text");
		$input.addClass(options.inputClass);
		$input.attr("autocomplete", "off");
		$input.attr("readonly", "readonly");
		$input.attr("tabIndex", $select.attr("tabindex")); // "I" capital is important for ie
		
		return $input;	
	}
	
	function moveSelect(step) {
		var lis = $("li", $container);
		if (!lis) return;

		active += step;

		if (active < 0) {
			active = 0;
		} else if (active >= lis.size()) {
			active = lis.size() - 1;
		}

		lis.removeClass(opt.hoverClass);

		$(lis[active]).addClass(opt.hoverClass);
	}
	
	function setCurrent() {	
		var li = $("li."+opt.currentClass, $container).get(0);
		var ar = (''+li.id).split('_');
		var el = ar[ar.length-1];
		$select.val(el);
		$input.val($(li).html());
		return true;
	}
	
	// select value
	function getCurrentSelected() {
		return $select.val();
	}
	
	// input value
	function getCurrentValue() {
		return $input.val();
	}
	
	function getSelectOptions(parentid) {
		var select_options = new Array();
		var ul = document.createElement('ul');
		$select.children('option').each(function() {
			var li = document.createElement('li');
			li.setAttribute('id', parentid + '_' + $(this).val());
			li.innerHTML = $(this).html();
			if ($(this).is(':selected')) {
				$input.val($(this).html());
				$(li).addClass(opt.currentClass);
			}
			ul.appendChild(li);
			$(li)
			.mouseover(function(event) {
				hasfocus = 1;
				if (opt.debug) console.log('over on : '+this.id);
				jQuery(event.target, $container).addClass(opt.hoverClass);
			})
			.mouseout(function(event) {
				hasfocus = -1;
				if (opt.debug) console.log('out on : '+this.id);
				jQuery(event.target, $container).removeClass(opt.hoverClass);
			})
			.click(function(event) {
			  var fl = $('li.'+opt.hoverClass, $container).get(0);
				if (opt.debug) console.log('click on :'+this.id);
				$('li.'+opt.currentClass).removeClass(opt.currentClass); 
				$(this).addClass(opt.currentClass);
				setCurrent();
				hideMe();
			});
		});
		return ul;
	}	
};













/* ----------------------------------------------------------------------------------------------------- */
/* Textarea */
$(document).ready(function()
{
	$("textarea").each(function ()
	{
		if ($(this).val().length == 0)
		{
			$(this).height('45px');
		}
		else
		{
			$(this).height('81px');
		}	
	});	

	// Focus
	$('textarea').focus(function()
	{
		if ($(this).val().length == 0)
		{	
			$(this).height('81px');
		}
	});

	// Blur
	$('textarea').blur(function()
	{
		if ($(this).val().length == 0)
		{
			$(this).height('45px');
		}
	});	
});
