$(document).ready(function() {
});

var TSubMenu = function(id)
{
	$('.topSubMenu').hide();
	$('#smenu-'+id).show();
}

var TSubMenuCurr = function(id)
{
	$('#smenu-'+id).attr("rel", 'activeMenu');
	$('#m'+id+' img').attr("src", '/public/img/topmenu/main-active_'+id+'.gif');
	$('#m'+id+' img').attr("rel", 'active');
}

var goodbye = function() {
	answer = confirm("Are you sure to leave this post?");
	if (answer !=0)
	{
		var pid = $('#hiddenID').val();
		$.get("/admin/ajax/unlockpost", {pid: pid});
	}
}

var chCounter = function(countable,printer,max)
{
	$('#'+countable).each(function(){
		var length = $(this).val().length;
		$('#'+printer).html( max - length + ' characters left');
		$(this).keyup(function(){
			var new_length = $(this).val().length;
			$('#'+printer).html( max - new_length + ' characters left');
		});
	});
}

var gotoAdmin = function(where)
{
	//var cl = $(this).attr('class');
	var where = where.toLowerCase();
	var id = $('.trSelected td:first-child div').text();
	
	if(where.search(/delete/) == 0) {
		answer = confirm("Are you sure to delete this element?");
		if (answer !=0)
		{
			if(id != '')
			{
				document.location.href = url+'/admin/'+where+'/'+id;
			} else {
				document.location.href = url+'/admin/'+where;
			}
		}	
	} else {
		if(id != '')
		{
			document.location.href = url+'/admin/'+where+'/'+id;
		} else {
			document.location.href = url+'/admin/'+where;
		}
	}
}

var jumpTo = function(id)
{
	var selObj = document.getElementById(id);
	var selIndex = selObj.selectedIndex;
	var jump = (selObj.options[selIndex].value);
	if(jump != '')
	{
	 document.location.href = url+jump;
	}
}

function lookup(inputString,where,id) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#'+id+' .suggestionsBox').hide();
	} else {
		$.post(url+"/admin/ajax/"+where, {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#'+id+'> .suggestionsBox').show();
				$('#'+id+'> div .suggestionsList').html(data);
				$(".draggable").draggable({helper: 'clone'}); 
			}
		});
	}
}

function quicksearch(inputString,where,id) {
	if(inputString.length == 0) {
		$('#'+id+' .suggestionsBox').hide();
	} else {
		$.post(url+"/ajax/"+where, {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#'+id+'> .suggestionsBox').show();
				$('#'+id+'> div .suggestionsList').html(data);
				$(".draggable").draggable({helper: 'clone'}); 
			}
		});
	}
} // lookup

function overEffect(id)
{
	$('#'+id).css({background:'#000'});
	$('#'+id).css({color:'#fff'});
	setTimeout("$('.suggestionsBox').hide();", 5100);
}

function outEffect(id)
{
	$('#'+id).css({background:'#fff'});
	$('#'+id).css({color:'#000'});
}

function doAjax(what,pid,where)
{
  $.ajax({
	type: "POST",
	url: url+"/admin/ajax/"+where,
	data: what,
	dataType: 'html',
	success: function(html)
	{
	 $('#'+pid+' .saveBlock').css({background:'green'});
	}
   });	
}

function fill(thisValue) {
	//$('#inputString').val(thisValue);
	//$('#adminQuickSearch').append(thisValue);
	//
}

function gotUrl(title,type) {
	if(type == 1)
	{
		document.location.href = url+'/post/'+title+'.html';
	} else {
		document.location.href = url+'/page/'+title+'.html';
	}
}

var showEl = function(tp,id) {
		$(tp+id).show();
}

var setAllTree = function() {
	
	//alert($('div.rw ul.tree li ul li .checkbox').hasClass('checked').toString());
	
	if(!$.browser.msie) {
	
		if($('div.rw ul.tree li ul li .checkbox').hasClass('checked').toString() == 'true')
		{
			$('div.rw ul.tree li .checkbox').removeClass('checked');
			$('div.rw ul.tree li ul li .checkbox').removeClass('checked');
		} else {
			$('div.rw ul.tree li .checkbox').addClass('checked');
			$('div.rw ul.tree li ul li .checkbox').addClass('checked');
		}
	
	}
}

/* HACKS */

var spanChange = function(cl,params,tx) {
//for flexigrid ajax <span> replace to <a>
//usage: spanChange('spanclass','#div','title'); OR spanChange('spanclass','#TB_inline?height=550&width=700&inlineId=addForm','title');
	$('span.'+cl).html("<a class='thickbox' rel='facebox' href='"+params+"'>"+tx+"</a>");
}

function tb_reinit(domChunk) {
//for thickbox ajax
//usage: tb_reinit('a.thickbox, area.thickbox, input.thickbox');
	$(domChunk).unbind("click");
	tb_init(domChunk);
}

