var changeCatType = 'article';


var pole = window.location.search.substr(1).split("&"); 
	// location.search je ta zaj�mavava cast adresy za otaznikem, 
	// substr(1) odebere nulty znak (otaznik), split("&") to rozsek� podle &

var httpGetVars = new Array(); 
for(i = 0; i < pole.length; i++){
	httpGetVars[pole[i].split("=")[0]] = unescape(pole[i].split("=")[1]); 
}

function gGetElementById(s) {
  var o = (document.getElementById ? document.getElementById(s) : document.all[s]);
  return o == null ? false : o;
}

function zipUnzip(blockID, imageID) {
var obj = gGetElementById(blockID);
var img = gGetElementById(imageID)
if (obj != null) {
if (obj.style.display=='block'){
obj.style.display='none';
img.src='img/arrow-off.gif';
}
else {
obj.style.display='block';
img.src='img/arrow-on.gif';
}
}
}


$('document').ready(function(){
	$('.editKat').click(function(){
		var id = this.rel;
		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('<img src="/img/loading.gif" />');
		$('.jqmWindow').jqmShow();
		$.ajax({
		   type: "POST",
		   url: "/json.php?akce=kategorie&id="+id,
		   dataType:'json',
		   success: function(json){
		   		$('.jqmText').empty();
		   		$('.jqmText').append($.TABLE({}));
		   		for(var i=0;i<json.length;i++){
		   			addRadek(json[i].id,json[i].nazev,id);
		   		}
		   }});
		  return false;
	});
	
	
	function formatItem(row) {
		
	return row[0] + "<br><i style='font-size:10px'>" + row[1] + "</i>";
}
function selectItem(li) {
	if (li.extra) {
		alert("That's '" + li.extra[1] + "' you picked.")
	}
}

$("#hledejkat").autocomplete("/searchkat.php?id="+httpGetVars['id']+"&type="+changeCatType, 
			{ minChars:3, matchSubset:1, matchContains:1, cacheLength:10,
			 onItemSelect:function(e){
			 	if (e.extra) {
			 		switch(changeCatType){
			 			case 'kategorie':
			 				var url = 'upravy.php?akce=kategorie_changecat&id='+httpGetVars['id']+'&kategorie='+e.extra[1];
			 				break;
			 			case 'kategorie-search':
			 				var url = 'sprava-kategorii.php?id='+e.extra[1];
			 				break;
			 			case 'public-search':
			 				var url = e.extra[2];
			 				break;
			 			default:
			 				var url = 'upravy.php?akce=addkat&clanek='+httpGetVars['id']+"&kategorie="+e.extra[1];
			 		}
					window.location.href = url;
				}
			}, formatItem:formatItem, selectOnly:1 }); 
});

function addRadek(id,nazev,idc){
	$('.jqmText table').append($.TR({id:'tr-'+id},
		   				$.TD({},nazev),
		   				$.TD({},$.A({href:'#',Class:'del-'+id},'odebrat'))
		   				));
	$('#del-'+id).click(function(){
		if(!confirm('Opravdu smazat?'))return false;
		$.ajax({
		   type: "POST",
		   url: "/json.php?akce=del-kat&id="+id+"&clanek="+idc,
		   dataType:'json',
		   success: function(json){
		   		$('.jqmText table #tr-'+id).remove();
		   }});
		return false;
	});
	
	$("#hledejkat").autocomplete("/searchkat.php", 
			{ minChars:3, matchSubset:1, matchContains:1, cacheLength:10,
			 onItemSelect:function(e){
			 	alert(e);
			 },
			  formatItem:formatItem, selectOnly:1 }); 

	
	/***********/
	
	
}