//Cookies manipulation functions
function readCookie(name)
{
	var nameEQ = name + '=';
	var arr = document.cookie.split(';');
	for(var i = 0; i < arr.length; i++) {
		var c = arr[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));
	}
	return null;
}

function createCookie(name, value, exp)
{
	if (exp) {
		var date = new Date();
		date.setTime(date.getTime() + (exp * 60 * 1000));
		var expires = '; expires=' + date.toGMTString();
	}
	else var expires = '';
	document.cookie = name + '=' + escape(value) + expires + '; path=/';
}

function eraseCookie(name)
{
	createCookie(name, '', -1);
} 
//

var inLoad = false;

function loading(el)
{
  if (inLoad) return false;
  inLoad = true;
  el.firstDescendant().style.visibility = 'hidden';
  el.style.overflow = 'hidden';
	insertLoadingAnim(el);
  return true;
}

function insertLoadingAnim(el)
{
  el.insert('<span id="loading" style="display:none;">&nbsp;</span>');
  var anim = $('loading');
  anim.style.top = (Math.round(el.getHeight() - anim.getHeight()) / 2)+'px';
  anim.style.left = (Math.round(el.getWidth() - anim.getWidth()) / 2)+'px';
  Effect.Appear('loading');
} 

function loaded(el, cont)
{
  inLoad = false;
  Element.remove('loading');
  var elHeight = el.getHeight();
  el.style.height = elHeight+'px';
	el.firstDescendant().update(cont);
	
	var finHeight = el.firstDescendant().getHeight();
	globalEl = el;
  new Effect.Morph(el, {
    style: 'height:'+finHeight+'px;',
    afterFinish: function() {
      var vpPos = document.viewport.getScrollOffsets();
	    var scrollPos = Element.cumulativeOffset(globalEl);
	    if (vpPos['top'] > scrollPos['top'])
	    {
        scrollPos = Element.cumulativeOffset(globalEl.up().previous()); 	
        window.scrollTo(scrollPos['left'], scrollPos['top']);
      }
		  el.firstDescendant().style.visibility = 'visible';
			el.firstDescendant().style.display = 'none';
			el.style.overflow = 'visible';
			Effect.Appear(el.firstDescendant(), {duration: 0.3});
		}
  });
}

function ajaxLoad(path, el, offset, category)
{
  if (!loading($(el))) return;
  new Ajax.Request(ajaxUrl+path,
    {
      method:'get',
      parameters: { offset: offset, category: category },
      onSuccess: function(response) {
        loaded($(el), response.responseText);
      }
    }
	);
	return true;
}

function loadNews(offset)
{
  ajaxLoad('news/list/', 'news', offset);
}

function loadArticles(offset, category)
{
  if (ajaxLoad('article/list/', 'articles', offset, category))
	{
	  var openedNews = $('openedNews');
	  if (openedNews) openedNews.removeClassName('opened');
	  
	  var tabsCont = $('articleTabs');
	  var tabs = tabsCont.childElements();
	  
	  for (var i = tabs.length-1; i >= 0; i--) {
			var childs = tabs[i].childElements();
			if (childs[childs.length-1].nodeName.toUpperCase() == 'SPAN') childs[childs.length-1].remove();
			
			tabs[i].removeClassName('opened');
			
			if (category == i)
			{
			  tabs[i].addClassName('opened');
			  tabs[i].insert('<span>&nbsp;</span>');
	  	}
		}
	}
}

function popupArticle(id)
{
  if (inLoad) return false;
  inLoad = true;
  Element.insert(document.body, '<div id="popup"><div id="popupBody" style="visibility: hidden;height: 215px;"><div></div></div><a href="#" class="closeLink" onClick="Element.remove(\'popup\');">Zavřít</a></div>');
  var popup = $('popup');
  popup.style.left = (Math.round(Element.getWidth(document.body) - popup.getWidth()) / 2)+'px';
  insertLoadingAnim(popup);
  
  new Ajax.Request(ajaxUrl+'current_copy/view',
    {
      method:'get',
      parameters: { article: id },
      onSuccess: function(response) {
        loaded($('popupBody'), response.responseText);
      }
    }
	); 
}