var _googleInterval;
/**
 * Audified Website functions
 * www.audified.com 
 * by Martin Slovacek
*/  

/* 
 * Dolarova funkce pro zkraceni document.getElementById();
 */
function $m() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}



/*
 * Na zaklade jmena sekce leveho menu vrati jeji index,
 * aby bylo mozne danou sekci rozbalit
*/
function getMenuSectionIndexByName(name)
{    
    var theNumberIwant = -1;
    var liCounter = -1;
    var objList=document.getElementById("leftmenuul")
       
    for (i=0; i<objList.childNodes.length; i++){        
        if (objList.childNodes[i].nodeName=="LI")
        {
            theNumberIwant++;
            var objLi = objList.childNodes[i];
            for (j=0; j<objLi.childNodes.length; j++){
                if (objLi.childNodes[j].nodeName == "DIV")
                {
                    var objDiv = objLi.childNodes[j];
                    for (k=0; k<objDiv.childNodes.length; k++){
                        if (objDiv.childNodes[k].nodeName == "A")
                            if (objDiv.childNodes[k].innerHTML == name)                    
                                return theNumberIwant;
                        }
                }
            }
        }
    }
}


/*
 * Funkce volana z inline skriptu na strance
 * Podle predaneho nazvu sekce provede jeji rozbaleni,
 * zaroven prida objektu myAccordion osetreni udalosti mouseleave,
 * aby pri opusteni menu zustala odpovidajici sekce rozbalena   */
function prepareUnwrap(_index, sectionName)
{    
    $(document).ready(function() {
        if (_index != 1)
            index = getMenuSectionIndexByName(sectionName)
        else 
            index = 0;
        
        $('#leftmenuul').accordion('activate', index);
        $('#leftmenuul').mouseleave(function(){
          setTimeout("$('#leftmenuul').accordion('activate', index);", 1000);  
        });                        
        
    });
}

/**
 *   Funkce pro zpozdene nacteni kodu Google Analytics
 **/ 
function addGoogleTracking()
{
	var sc = document.createElement('script');
	sc.type = 'text/javascript';
	sc.src = 'http://www.google-analytics.com/ga.js';
	document.getElementsByTagName("head").item(0).appendChild(sc);
	_googleInterval = setInterval(activateGoogle,250);
}
function activateGoogle()
{
	if(typeof _gat != 'undefined') 
	{
		clearInterval(_googleInterval);
		var pageTracker = _gat._getTracker("UA-15464505-1");
		pageTracker._initData();
		pageTracker._trackPageview();
	}
}
/***/

/*
 * Operace provadene po nacteni DOM stromu
*/  
$(document).ready(function() {
    // vlozim sledovaci kod Google Analytics
   addGoogleTracking();
    
    // nastaveni quickLinks podle webid - pouze pokud jsem na webu prvni urovne, jinak zbytecne
    try {
        if (webid < 10)
            quickLinks(webid);
    } catch(err){};
    
    // leve menu - akordeon
    $("#leftmenuul").accordion({header: 'div.leftmenu-category', autoheight: false, event: 'mouseover'});

    // ikona lupy na popup oknech
    var loadGlass = 1;
    $('a.shutter img').hover(function(){
    	if(loadGlass == 1) {
    		$('a.shutter img').parent().prepend('<img id="magnify" style="border: none; position: absolute;" src="/design/zoom20.png" alt="" width="20" height="20" />');    	
    	}
    	var p = $(this);
    	var position = p.position();
        $('img#magnify').css({'top' : position.top+5, 'left' : position.left+5});
    	$('img#magnify').show();
    },function(){
	   $('img#magnify').hide();
	   loadGlass = 2;
    });
    
    // image popup - Shutter
    $('a.shutterset img').hover(function(){
    	if(loadGlass == 1) {
    		$('a.shutterset img').parent().prepend('<img id="magnify" style="border: none; position: absolute;" src="/design/zoom20.png" alt="" width="20" height="20" />');    	
    	}
    	var p = $(this);
    	var position = p.position();
        $('img#magnify').css({'top' : position.top+5, 'left' : position.left+5});
    	$('img#magnify').show();
      //alert($('img#magnify').length);
    },function(){
	   $('img#magnify').hide();
	   loadGlass = 2;
    });
    
    // scrollable
    if ( $("div#scrollable").length != 0 ) { 
        var api = $("div#scrollable").scrollable({ 
            vertical:true,
            keyboard:false,
            speed:250,
            clickable:false,
            easing:'linear',                                         
            size: 11                                          
        }).mousewheel().autoscroll({interval:250, steps:1, autoplay: false, api:true});
        
        $('#arrow-up').mouseover(function(){
            api.setSteps(-1);
            api.play();
        });
        $('#arrow-up').mouseout(function(){
            api.stop();
        });                         
        $('#arrow-down').mouseover(function(){
            api.setSteps(1);
            api.play();                        
        });
        $('#arrow-down').mouseout(function(){
            api.stop();
        });
    }                                                
    
});  



// ----------------------------------------------------------------------- Popup
/* from 
 * http://rndnext.blogspot.com/2009/02/jquery-ajax-tooltip.html
 */
$(function()
{
  var hideDelay = 500;  
  var hideTimer = null;

  var container = $('<div id="popupContainer">'
      + '<div id="popupContent"></div>'
      + '</div>');

  $('body').append(container);

  $('.popupTrigger').live('mouseover', function(event)
  {      
      event.preventDefault();
      var text = $(this).attr('rel');

      if (hideTimer)
          clearTimeout(hideTimer);

      var pos = $(this).offset();
      var width = $(this).width();
      container.css({
          left: (pos.left + width + 20) + 'px',
          top: pos.top - 5 + 'px'
      });

      $('#popupContent').html(text);
      container.delay(0).fadeIn(700);

      //$('#popupContent').html($(data));
  });

  $('.popupTrigger').live('mouseout', function()
  {

      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.fadeOut(1000);
      }, hideDelay);
      $().clearQueue();      
  });

  // Allow mouse over of details without hiding details
  $('#popupContainer').mouseover(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      $().clearQueue();          
  });

  // Hide after mouseout
  $('#popupContainer').mouseout(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.fadeOut(1000);
      }, hideDelay);
  });
});
