/* 
 * funkce k jQuery
 *
 * verze 0.1
 */

 /**
  * String @selector eg #myCheckbox or .allCheckboxes
  * boolean @check
  */
function jqSetCheckbox(selector, check) {
	jQuery("input:checkbox" + selector).attr('checked', check);
}

function jqToggleCheckbox(selector) {
	// inspirovano http://www.texotela.co.uk

	var checked = jQuery("input:checkbox" + selector).each(
				function()
				{
					this.checked = !this.checked;
				}
			)
}

function checkboxAllByClass(className) {
	jQuery("input:checkbox." + className).attr('checked', true);
}

function checkboxNoneByClass(className) {
	jQuery("input:checkbox." + className).attr('checked', false);
}

function setGlobalMessagesToHide()
{
	if (jQuery("#globalMessageRemove").length>0) {
		jQuery("#globalMessageRemove").click(function () {
			jQuery("#globalMessages").hide(1000);
			return true;
		});

		setTimeout(function(){
			jQuery("#globalMessages").hide(1000);
		}, 10000);

	}
}

function setGlobalMessages(htmlInsert) {
	jQuery("#globalMessages").hide();
	jQuery("#globalMessages").html(htmlInsert);
	jQuery("#globalMessages").show(1000);

	setGlobalMessagesToHide();
}

// zavola php a naparsuje vracena data
/*
	jQuery("#ajaxtextClick").click(function() {
				myAjax('AJAX_TYP_TEST', 'pole1=ahoj&pole2=vole');
			})

			toto je stara verze pro novy typ lib.php
*/
function myAjaxAA(typ, myData, file) {
	if (file == '' || file == null) file = 'ajax.php';
	jQuery.ajax({
		url : file + '?typ=' + typ,
		type : "POST",
		//data : "employee=" + jQuery("#employee_id").attr("id"),
		data : myData,
		dataType : "json",
		beforeSend: function(){
			jQuery("body").addClass('loading');
		},
		error : function (xhr, desc, exception) {
			jQuery("body").removeClass('loading');
			alert("Chyba: " + xhr.responseText + desc + exception);
		},
		success : function (data) {
			jQuery("body").removeClass('loading');
			//assuming an Array of objects was retreived
			jQuery.each(data, function(i, item){
				// pokud je to message, pak se vola jina funkce
				if (item.element == "#globalMessages") setGlobalMessages(item.output);
				else if (item.element == "sessionError") top.location = item.output;
				else if (jQuery(item.element).length) {
					jQuery(item.element).fadeOut(200, function () {
						jQuery(item.element).html(item.output).fadeIn(500)
					});
				}
			});
		}
	});
}

function loadingOn() {
	jQuery("body").addClass('loading');
	$("#loading").css('opacity', 0);
	$("#loading").animate({
		opacity: 0.75,
		height: 'toggle'
	}, 100, function() {
		// Animation complete.
		});
}

function loadingOff() {
	jQuery("body").removeClass('loading');
	$("#loading").animate({
		opacity: 0,
		height: 'toggle'
	}, 250, function() {
		// Animation complete.
		});
}



// zavola php a naparsuje vracena data
/*
	jQuery("#ajaxtextClick").click(function() {
				myAjax('AJAX_TYP_TEST', 'pole1=ahoj&pole2=vole');
			})
*/
function myAjax(url) {
	myData = "''";
	
	jQuery.ajax({
		url : url,
		type : "POST",
		//data : "employee=" + jQuery("#employee_id").attr("id"),
		data : myData,
		dataType : "json",
		beforeSend: function(){
			loadingOn();
		},
		error : function (xhr, desc, exception) {
			loadingOff();
			maskOff();
			alert("Chyba: " + xhr.responseText + desc + exception);
		},
		success : function (data) {
			loadingOff();
			maskOff();
			//assuming an Array of objects was retreived
			myProcessJson(data);
		}
	});
}

function myAjax(url, data) {
	myData = data;
	
	jQuery.ajax({
		url : url,
		type : "POST",
		//data : "employee=" + jQuery("#employee_id").attr("id"),
		data : myData,
		dataType : "json",
		beforeSend: function(){
			loadingOn();
		},
		error : function (xhr, desc, exception) {
			loadingOff();
			maskOff();
			alert("Chyba: " + xhr.responseText + desc + exception);
		},
		success : function (data) {
			loadingOff();
		 	maskOff();
			//assuming an Array of objects was retreived
			myProcessJson(data);
		}
	});
}

//assuming an Array of objects was retreived
function myProcessJson(data) { 
   jQuery.each(data, function(i, item){
				// pokud je to message, pak se vola jina funkce
				if (item.element == "#globalMessages") setGlobalMessages(item.output);
				else if (item.element == "sessionError") top.location = item.output;
				else if (item.element == "eval") eval(item.output);
				else if (jQuery(item.element).length) {
					jQuery(item.element).fadeOut(200, function () {
						jQuery(item.element).html(item.output).fadeIn(500)
					});
				}
			});
}


// jquery.form
function myAjaxForm(formSelector, url) {
	$(formSelector).ajaxForm({
		// dataType identifies the expected content type of the server response 
		dataType:  'json', 
        url: url,
	
		beforeSend: function(){
			loadingOn();
		},
		error : function (xhr, desc, exception) {
			loadingOff();
            maskOff();
			//overlayElem.overlay().close();
				
			alert("Chyba: " + xhr.responseText + desc + exception);
		},
		success : function (data) {
			loadingOff();
            maskOff();
			//overlayElem.overlay().close();
	
			myProcessJson(data);
		}
	}); 
}



// jQuery tools

// save the overlay element in a global variable
var overlayElem;
function myOverlay(aSelector) {
    
        // register the click event for ajax load and page load
        $(aSelector).live('click', function (e) {
            e.preventDefault();

            // save overlay
            overlayElem = $(this); 

            // load overlay
            $(this).overlay({
                mask: '#3B5872',
                effect: 'apple',
                api: true,
                load: true,
                onBeforeLoad: function () {
                    var wrap = this.getOverlay().find(".contentWrap");
                    wrap.load(this.getTrigger().attr("href"));
                },
					 
				onLoad: function() {
				    // nejaka divna chyba ve firefoxu, mozna se tyka jen pfNetu
				    $('img[src$="overlay.png"]').css("top", $('#overlay').css('top')).css("left", $('#overlay').css('left'));
				}
			});
            
		});
}

// close the overlay
function closeOverlay() {
	overlayElem.overlay().close();
}


// save the overlay element in a global variable
function maskOn() {
	$(document).mask({ 
		color: '#3B5872',
		closeOnEsc: false, 
		closeOnClick: false 
	});
}


function maskOff() {
jQuery.mask.close();
}

 
 
/*
http://jquery.open2space.com/node/56

jQuery.ajax({
url : "employee_sales_json.php",
type : "POST",
data : "employee=" + jQuery("#employee_id").attr("id"),
dataType : "json",
error : function (xhr, desc, exception) { alert(xhr.responseText); },
success : function (data) {
//assuming an Array of objects was retreived
var output = "<ul>";
for (var x=0; x < data.length; x++) {
var sale = data[x];
output += "<li>";
output += "Type: " + sale.type + ", ";
output += "QTY : " + sale.qty + ", ";
output += "Total: " + sale.total;
output += "</li>";
}
output += "</ul>";
jQuery("#employee_sales").html(output);
}
});


 */


/*--------------------------------------------------------------------
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article:
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)
 * Usage Example: jQuery(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: jQuery(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

jQuery.fn.equalHeights = function(px) {
	jQuery(this).each(function(){
		var currentTallest = 0;
		jQuery(this).children().each(function(i){
			if (jQuery(this).height() > currentTallest) {currentTallest = jQuery(this).height();}
		});
		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if (jQuery.browser.msie && jQuery.browser.version == 6.0) {jQuery(this).children().css({'height': currentTallest});}
		jQuery(this).children().css({'min-height': currentTallest});
	});
	return this;
};

