var inLoad = false;

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)
{
  globalEl = el;
  inLoad = false;
  Element.remove('loading');
  var elHeight = el.getHeight();
  el.style.height = elHeight+'px';
  
	el.firstDescendant().update(cont+'<span class="clearFix">&nbsp;</span>');
	
	setTimeout(function() {
	  el.firstDescendant().update(cont+'<span class="clearFix">&nbsp;</span>');
	  el.firstDescendant().style.display = 'none';
    el.firstDescendant().style.visibility = 'visible';
	  var finHeight = el.firstDescendant().getHeight();
	
	  new Effect.Morph(el, {
      style: 'height:'+finHeight+'px;',
      afterFinish: function() {
        var vpPos = document.viewport.getScrollOffsets();
	      var scrollPos = Element.cumulativeOffset(el);
	      if (vpPos['top'] > scrollPos['top'])
	      {
          scrollPos = Element.cumulativeOffset(el.up().previous()); 	
          window.scrollTo(scrollPos['left'], scrollPos['top']);
        } 
		  	Effect.Appear(el.firstDescendant(), {duration: 0.3});
		  }
    });
	}, 200);
}

function popup(url, params)
{
  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\'); return false;">Zavřít</a></div>');
  var popup = $('popup');
  popup.style.left = (Math.round(Element.getWidth(document.body) - popup.getWidth()) / 2)+'px';
  popup.style.top = (250+document.viewport.getScrollOffsets().top)+'px';
  insertLoadingAnim(popup);
  
  new Ajax.Request(url,
    {
      method:'get',
      parameters: params,
      onSuccess: function(response) {
        loaded($('popupBody'), response.responseText);
      }
    }
	); 
}

var changeInterval, changePos;
var defChangeCount = 5;
var currentHoverImage = 0;
var defaultSrcs = [];

Event.observe(window, 'load', function()
{
  var els = $$('img.changeable');
  for(var i = 0; i < els.length; i++) {
	  els[i].onmouseover = function()
	  {
	    currentHoverImage = this.getAttribute('rel');
	    defaultSrcs[currentHoverImage] = this.getAttribute('src');
	    changePreview(this, 1);
		}
		
		els[i].onmouseout = function()
	  {
		  window.clearTimeout(changeInterval);
		  currentHoverImage = 0;
		  this.setAttribute('src', defaultSrcs[this.getAttribute('rel')]);
		}
	}
	
	var writing = $$('p.writing');
	if (writing.length)
	{
	  writing = writing[0];
	  writing.style.height = writing.getHeight()+'px';
	  
	  var writingTextsPos = -1;		
		var writingPos = 0;
		var openedTag = false;
		var openedTagLength = 0;
		
		function writingInterval()
		{
		  if (writingTextsPos == -1 || (typeof(writingText) != 'undefined' && writingPos == writingText.length))
			{
			  writingPos = 0;
			  writingTextsPos++;
			  if (writingTextsPos == writingTexts.length) writingTextsPos = 0;
	      writingText = writingTexts[writingTextsPos];
	      writing.innerHTML = '<strong>'+writingTextTitles[writingTextsPos]+'</strong> - ';
			}
			
		  writing.innerHTML += writingText.substring(writingPos,writingPos+1);
			writingPos++;
			
			window.setTimeout(writingInterval, writingPos == writingText.length ? 5000 : 100);
		}
		
		writingInterval();
	}
});

function changePreview(el, changePos)
{
  var img = document.createElement('img');
	img.setAttribute('src', '/uploads/image/video/'+el.getAttribute('rel')+'/'+changePos+'.jpg');
	img.onload = function()
	{
	  if (currentHoverImage != el.getAttribute('rel')) return;
	  el.setAttribute('src', img.getAttribute('src'));
	  if (changePos == videoPrevs[el.getAttribute('rel')]) changePos = 0;
	  changeInterval = window.setTimeout(function()
		{
			changePreview(el, changePos+1);
		}, 750);
	}
}
