var tmp = 0;
var pole = window.location.search.substr(1).split("&");
var addr = new Array();
for (i = 0; i < pole.length; i++) {
	addr[pole[i].split("=")[0]] = unescape(pole[i].split("=")[1]);
}
function nactiFormular(form) {
	var data = [];
	var id = $(this).attr('rel');
	$('input[type=text]', form).each(function() {
		data.push( {
			name : $(this).attr('name'),
			value : encoduj($(this).attr('value'))
		});
	});
	$('input[type=password]', form).each(function() {
		data.push( {
			name : $(this).attr('name'),
			value : encoduj($(this).attr('value'))
		});
	});
	$('input[type=checkbox]', form).each(function() {
		if ($(this).attr('checked'))
			data.push( {
				name : $(this).attr('name'),
				value : $(this).val()
			});
		else
			data.push( {
				name : $(this).attr('name'),
				value : 0
			});
	});
	$('input[type=radio]', form).each(function() {
		if ($(this).attr('checked'))
			data.push( {
				name : $(this).attr('name'),
				value : $(this).val()
			});
	});
	$('textarea', form).each(function() {

			data.push( {
				name : $(this).attr('name'),
				value : encoduj($(this).val())
			});
		

	});
	$('select', form).each(function() {
		data.push( {
			name : $(this).attr('name'),
			value : encoduj($(this).attr('value'))
		});
	});
	return data;
}
var zpracujForm = function() {
	var url = $(this).attr('action');
	// $(this).attr('action','#top');

	var data = nactiFormular(this);
	$.ajax( {
		dataType : 'json',
		url : url,
		data : data,
		beforeSend : preda,
		success : function(json) {
			succa(json);
			$('form.udaje select').attr('disabled', false);
		},
		error : erra,
		complete : coma
	});
	$('form.udaje select').attr('disabled', false);
	return false;
};

$(document).ready(function() {
	$.jheartbeat.set( {
		url : "json.php", // SPECIFIES WHERE TO UPDATE FROM
		delay : 60000, // SPECIFIES THE DELAY
		div_id : "test_div" // SPECIFIES WHERE TO UPDATE
	});

	$('.container').tabs( {
		cache : false,
		select : function(event, ui) {
			// location.href = $.data(ui.tab, 'load.tabs');
		var str = ui.tab.toString().split("#");
		if (str.length == 2) {
			location.href = "#" + str[1];
		}
	}
	});

	$('form.udaje').submit(zpracujForm);

	/** ********* */

	$.ajaxSetup( {
		type : "POST"
	});

	$('abbr').cluetip( {
		splitTitle : '|'
	});
});
/** * */
function encoduj(data) {
	if (!data)
		data = "";
	// data = encodeURIComponent(data);
	return data;
}
function formatItem(row) {
	return row[0] + "<br><i style='font-size:10px'>" + row[1] + "</i>";
}
function zprava(json) {
	if (json.stav == 'ok') {
		$('.success p').empty();
		$('.success p').html(json.zprava);
		$('.success').show();

	} else {
		$('.error p').empty();
		$('.error p').html(json.zprava);
		$('.error').show();
	}
	$('.loading').hide();
}
succa = function(json) {
	zprava(json)
};
preda = function() {
	$('.loading').show();
	$('.error').hide();
	$('.success').hide();
};
erra = function(json) {
	$('.loading').hide();
	$('.error p').empty();
	if (!json.zprava || json.zprava == '') {
		$('.error p')
				.html(
						'Při odesílání dat došlo k chybě. Zkontrolujte prosím, zda jste stále přihlášení, připojeni k internetu a zda zadáváte všechny údaje v platném fomrátu.<br />V případě dalších potíží kontaktujte administrátora.');
	} else {
		$('.error p').html(json.zprava);
	}
	$('.error').show();
	$('.loading').show();
	$('.jqmWindow').jqmHide();
};
coma = function() {
	$('.loading').hide();
};

// //////////////////
function strom(options) {

	var rozbal = function() {
		var obj = this;
		;
		var li = $(this).parent('li');
		var id = $(this).attr('rel');
		$.ajax( {
			dataType : 'json',
			url : 'json.php?akce=strom',
			data : 'id=' + id,
			success : function(json) {
				li.removeClass('rozbalit');
				li.addClass('zabalit');
				$('.plus', li).unbind('click');
				$('.plus', li).removeClass('minus');
				$('.plus', li).addClass('minus');
				$('.minus', li).click(zabal);
				if (!json.zbozi && json.kategorie.length > 0) {
					li.append($.UL( {}));
				} else {
					if (json.zbozi) {
						if (json.kategorie.length + json.zbozi.length > 0) {
							li.append($.UL( {}));
						}
					}
				}
				// li.append($.UL({}));

			for ( var i = 0; i < json.kategorie.length; i++) {
				if (options.akcekategorie) {
					$('ul', li).append($.LI( {
						Class : 'rozbalit'
					}, $.A( {
						href : '#',
						Class : 'plus',
						rel : json.kategorie[i].id
					}), $.IMG( {
						src : 'design/img/strom/folder.gif'
					}), $.A( {
						href : '#',
						Class : 'akcekategorie',
						rel : json.kategorie[i].id
					}, json.kategorie[i].nazev)));
				} else {
					$('ul', li).append($.LI( {
						Class : 'rozbalit'
					}, $.A( {
						href : '#',
						Class : 'plus',
						rel : json.kategorie[i].id
					}), $.IMG( {
						src : 'design/img/strom/folder.gif'
					}), json.kategorie[i].nazev));
				}
			}
			if (options.akcekategorie)
				$('ul a.akcekategorie', li).click(options.akcekategorie);

			// /////

			$('ul a.plus', li).click(rozbal);
			if (options.rozbalzbozi) {
				if (json.zbozi) {
					for ( var i = 0; i < json.zbozi.length; i++) {
						$('ul', li).append($.LI( {
							Class : 'rozbalit'
						}, $.IMG( {
							src : 'design/img/strom/file.gif'
						}), $.A( {
							href : '#',
							Class : 'akcezbozi stromzbozi',
							rel : json.zbozi[i].id
						}, json.zbozi[i].nazev)));
					}
					if (options.akcezbozi)
						$('ul a.akcezbozi', li).click(options.akcezbozi);
				}
			}
		}
		});
		return false;
	}
	var zabal = function() {
		var obj = this;
		;
		// alert(obj);
		var li = $(this).parent('li');
		var id = $(this).attr('rel');

		li.removeClass('zabalit');
		li.addClass('rozbalit');
		$('.minus', li).unbind('click');
		$('.minus', li).removeClass('plus');
		$('.minus', li).addClass('plus');
		$('.plus', li).click(rozbal);

		$('ul', li).remove();
		return false;
	}
	$('.plus', options.obj).click(rozbal);
	$('.plus', options.obj).click();

}

var nahraniObrazku = function(parametry) {
	if ($('.jqmWindow').size() == 0) {
		$('body').append($.DIV( {
			Class : 'jqmWindow'
		}, $.DIV( {
			Class : 'jqmTitulek'
		}, $.A( {
			href : '#',
			Class : 'jqmClose'
		}, 'zavřít')), $.DIV( {
			Class : 'jqmText'
		})));
	}
	$('.jqmWindow').jqm( {
		modal : false
	});
	$('.jqmText').empty();
	$('.jqmText').append($.FORM( {
		enctype : 'multipart/form-data'
	}, $.INPUT( {
		type : 'file',
		name : 'fileToUpload',
		id : 'fileToUploadx',
		enctype : 'multipart/form-data'
	}), $.INPUT( {
		type : 'submit',
		Class : 'submit',
		value : 'Nahrát'
	})));
	$('.jqmText .submit').click(
			function() {
				$.ajaxFileUpload( {
					url : parametry.url,
					secureuri : false,
					fileElementId : 'fileToUploadx',
					dataType : 'json',
					success : function(data, status) {
					   $("#"+parametry.id).tinymce().execCommand(
								'mceInsertContent',
								false,
								'<img src="' + data.srcfull + '" width="'
										+ data.fullwidth + '" height="'
										+ data.fullheight + '" >');
						$('.jqmWindow').jqmHide();
					},
					error : function(data, status, e) {
					}
				});
				return false;
			});
	$('.jqmWindow').jqmShow();
	return false;
};

var vyberKategorie = function(parametry) {
	var callback = parametry.callback;
	if ($('.jqmWindow').size() == 0) {
		$('body').append($.DIV( {
			Class : 'jqmWindow'
		}, $.DIV( {
			Class : 'jqmTitulek'
		}, $.A( {
			href : '#',
			Class : 'jqmClose'
		}, 'zavřít')), $.DIV( {
			Class : 'jqmText'
		})));
	}
	$('.jqmWindow').jqm( {
		modal : true
	});
	$('.jqmText').empty();
	$('.jqmText')
			.html(
					"<div style='display:block;height:350px;overflow:scroll'><ul id='strom' class='strom' ><li class='rozbalit'><a href='#'class='plus' rel='0'>&nbsp;</a><img src='design/img/strom/folder.gif' /> Katalog</li></ul></div>");
	strom( {
		obj : $('ul#strom'),
		akcekategorie : function() {
			$('.jqmWindow').jqmHide();
			callback(this.rel, $(this).text());
		},
		rozbalzbozi : false
	});
	$('.jqmWindow').jqmShow();
	return false;
}
var vyberZbozi = function(parametry) {
	var callback = parametry.callback;
	if ($('.jqmWindow').size() == 0) {
		$('body').append($.DIV( {
			Class : 'jqmWindow'
		}, $.DIV( {
			Class : 'jqmTitulek'
		}, $.A( {
			href : '#',
			Class : 'jqmClose'
		}, 'zavřít')), $.DIV( {
			Class : 'jqmText'
		})));
	}
	$('.jqmWindow').jqm( {
		modal : true
	});
	$('.jqmText').empty();
	$('.jqmText')
			.html(
					"<div style='display:block;height:350px;overflow:scroll'><ul id='strom' class='strom' ><li class='rozbalit'><a href='#'class='plus' rel='0'>&nbsp;</a><img src='design/img/strom/folder.gif' /> Katalog</li></ul></div>");
	strom( {
		obj : $('ul#strom'),
		akcezbozi : function() {
			$('.jqmWindow').jqmHide();
			callback(this.rel, $(this).text());
		},
		rozbalzbozi : true
	});
	$('.jqmWindow').jqmShow();
	return false;
}
function obarvyRadky() {
	var i = 0;
	$('#list tr, .list tr').each(function() {
		$(this).removeClass();
		if (i++ % 2 == 0) {
			$(this).addClass('odd');
		} else {
			$(this).addClass('even');
		}
	});
}

function dump(arr, level) {
	var dumped_text = "";
	if (!level)
		level = 0;

	// The padding given at the beginning of the line.
	var level_padding = "";
	for ( var j = 0; j < level + 1; j++)
		level_padding += "    ";

	if (typeof (arr) == 'object') { // Array/Hashes/Objects
		for ( var item in arr) {
			var value = arr[item];

			if (typeof (value) == 'object') { // If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value, level + 1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value
						+ "\"\n";
			}
		}
	} else { // Stings/Chars/Numbers etc.
		dumped_text = "===>" + arr + "<===(" + typeof (arr) + ")";
	}
	return dumped_text;
}

jQuery.fn.extend({
    fdialog: function (par){
        $('input[type=text]',this).addClass("text");
        if(par){
            par.modal = true;
            par.bgiframe = true;
			par.autoOpen = false;
            $.extend(par.buttons, {'zavřít': function() {
                    $('input',this).val('');
					$(this).dialog('close');
			}});
            if(par.buttons.ok && $(this).attr('action').length<3){
                $(this).submit(par.buttons.ok);
            }
            if(!par.close){
                par.close = function(){
                    $('input',this).val('');
                }
            }
        }
       $(this).dialog(par);
    }
});



jQuery(function($){
	$.datepicker.regional['cs'] = {
		closeText: 'Zavřít',
		prevText: '&#x3c;Dříve',
		nextText: 'Později&#x3e;',
		currentText: 'Nyní',
		monthNames: ['leden','únor','březen','duben','květen','červen',
        'červenec','srpen','září','říjen','listopad','prosinec'],
		monthNamesShort: ['led','úno','bře','dub','kvě','čer',
		'čvc','srp','zář','říj','lis','pro'],
		dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
		dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
		dayNamesMin: ['ne','po','út','st','čt','pá','so'],
		dateFormat: 'dd.mm.yy', firstDay: 1,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['cs']);
});
$.datepicker.setDefaults($.extend({showMonthAfterYear: true, dateFormat: 'dd. mm. yy' }, $.datepicker.regional['cs']));



