AIM = {

	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}

		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}

}


function startCallback()
{
  ajaxon();
	return true;
}

function completeCallback(response)
{
  if(response != 'fail')
  {
  	document.getElementById("description").value="";
  	document.getElementById("description").focus();
    var element = document.getElementById("photoContainer");
    var li = document.createElement('li');
    li.innerHTML = response;
    element.appendChild(li);
	}
	else
	{
		NoPermission('add');
	}
	ajaxoff();
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ajax_todo_done( id )
{
  ajax_get('/ajax/todo_done.php?id='+id, ajax_todo_done_handle );
}
function ajax_todo_add( el_id )
{
	ajax_post( '/ajax/todo_add.php', ajax_todo_add_handle, createQueryId(el_id));
}

function ajax_todo_add_handle( xmlhttp )
{
  if (xmlhttp.readyState == 4)
  {
    var resp = xmlhttp.responseXML.getElementsByTagName('result')[0];
    var result = resp.getAttribute('id');
		var edit = ge('todolist');
		
    if(result == 'ok')
    {
    	var data = resp.firstChild.nodeValue;
    	var d=new Date();
			var ul = ge('todo-container');
  		edit.style.color="#000000";
  		
			ul.innerHTML += data;
			edit.value = (d.getDate()*1 +1 ) +'/' + ((d.getMonth()*1)+1);
			
		}
		else
		{
			edit.style.color="#cc0000";
		}
		ajaxoff();
  }
}
function ajax_todo_done_handle( xmlhttp )
{
  if (xmlhttp.readyState == 4)
  {
    var resp = xmlhttp.responseXML.getElementsByTagName('result')[0];
		var result = resp.firstChild.nodeValue;
    var id = resp.getAttribute('id');

    if(result == '1')
    {
    	var tr = document.getElementById(id);
    	tr.parentNode.removeChild(tr);
		}
		ajaxoff();
  }
}

/*
nsp = inp[i].getAttributeNode('class');
    if(nsp && nsp.value == 'datetime-date' )

*/

function check(pole, form_id, state, classname)
{

  elm = document.getElementById(form_id).elements;

  for(i = 0; i < elm.length; i++)
  {
    if(elm[i].name == pole)
    {
			if(classname != 0)
			{
				nsp = elm[i].getAttributeNode('class');
				if(nsp && nsp.value == classname )
					elm[i].checked = state;
				if(nsp && nsp.value != classname )
				  elm[i].checked = !state;
			}
			else
			{
      	elm[i].checked = state;
			}
    }
  }
  return false;
}

function focusTo(id)
{
	document.location = '#'+id;
	document.getElementById(id).focus();
	return true;
}

function getPos(obj)
{
	var left = 0;
	var top = 0;
	if (obj.offsetParent)
	{
		do
		{
			left += obj.offsetLeft;
			top  += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
	}
	return [left,top];
}

function ajax_rating( id, rating )
{
	ajax_get('/ajax/rating.php?id='+id+'&rating='+rating+'&t=board', ajax_rating_handle );
}

function ajax_rate_news( id, rating )
{
	ajax_get('/ajax/rating.php?id='+id+'&rating='+rating+'&t=hub_comments&tc=hub', ajax_rate_news_handle );
}

function ajax_rate_news_handle( xmlhttp )
{
  if (xmlhttp.readyState == 4)
  {
    var resp = xmlhttp.responseXML.getElementsByTagName('result')[0];
    var postID = resp.getAttribute('id');
    var voteRating = resp.getAttribute('rating');
		var rSrc = resp.getAttribute('src');
		var rAlt = resp.getAttribute('alt');
		var rTitle = resp.getAttribute('title');

    if( postID != 0 )
    {
    	var Image = '<img src="'+rSrc+'" title="'+rTitle+'" alt="'+rAlt+'" />';
    	
			var myResult = ge('rating-date-' + postID );
			if( myResult )
			{
				myResult.innerHTML = Image;
			}
			
			myResult = ge('rating-rate-' + postID );
			if( myResult )
			{
				myResult.innerHTML = Image;
			}

		}
		ajaxoff();
  }
}


// novy celkovy rating
function ajax_rating_handle( xmlhttp )
{
  if (xmlhttp.readyState == 4)
  {
    var resp = xmlhttp.responseXML.getElementsByTagName('result')[0];
    var postID = resp.getAttribute('id');
    var voteRating = resp.getAttribute('rating');
		var rSrc = resp.getAttribute('src');
		var rAlt = resp.getAttribute('alt');
		var rTitle = resp.getAttribute('title');
    
    if( postID != 0 )
    {
    
			var myVote = ge('voteRating-' + postID );
			if( myVote && voteRating )
			{
				myVote.innerHTML = '<b>('+ voteRating +')</b>';
			}

			var myResult = ge('resultRating-' + postID );
			if( myResult )
			{
				myResult.innerHTML = '<img src="'+rSrc+'" title="'+rTitle+'" alt="'+rAlt+'" />';
			}
		}
		ajaxoff();
  }
}

function loadmessage( id )
{
	if(id)
		window.location = '/messages/read/'+id+'/';
}

function hpNavig( id )
{
  ge('ov1').style.display='none';
  ge('ov2').style.display='none';
  ge('ov3').style.display='none';
  ge('ov4').style.display='none';
  ge(id).style.display='block';
}

/*
function getFontSize(x)
{
	if (x.currentStyle)
		var y = x.currentStyle['fontSize'];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue('font-size');
	return y;
}
*/

function sizeArea(t)
{
	t.rows = countLines(t.value, 145);
}

function countLines(strtocount, cols)
{
    var hard_lines = 1;
    var last = 0;
    while ( true ) {
        last = strtocount.indexOf("\n", last+1);
        hard_lines ++;
        if ( last == -1 ) break;
    }
    var soft_lines = Math.round(strtocount.length / (cols-1));
    var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
    if ( hard ) soft_lines = hard_lines;
    return soft_lines+3;
}

