$("document").ready(function() {

    $("a").click(function() {
      $(this).blur();
    });
  
    /* kontakty a reference*/
  	$(".akordeon").accordion({ autoHeight: false, active: false, collapsible: true, clearStyle: true });
    /*if(location.hash.length > 1)
      $(".akordeon").accordion('activate', parseInt(location.hash.substr(1)));*/

    /* reference */
    $("div#referencesContainer div div.akordeonItemName").click(function(e) {
      if($(this).next().is(":hidden")) {
        $("div#referencesContainer div div.akordeonItemContent").hide('fast');
        $(this).next().show('fast');
      }
      else
        $("div#referencesContainer div div.akordeonItemContent").hide('fast');
    });


    /* kontakty */
    $("div#map div.poi").hover(function() {
        $(this).children().children().stop(true, true).animate({opacity: "show", left: "0"}, "slow");
      },
      function() {
        $(this).children().children().animate({opacity: "hide", left: "50"}, "fast");
      }
    );

    $("table.adminTable div#map").click(function(e) {
      var map_offset = $(this).offset();
      var left = e.pageX - map_offset.left - 7;
      var top = e.pageY - map_offset.top - 7;
      $("table.adminTable div#map > div").css("left", left);
      $("table.adminTable div#map > div").css("top", top);
      $('table.adminTable input[name="left"]').val(left);
      $('table.adminTable input[name="top"]').val(top);
    });

    /* fotografický pás */
    function checkArrows() {
      if(pageClipPos <= 0)
        $("a#pageClipArrowL").hide();
      else
        $("a#pageClipArrowL").show();

      if(pageClipPos >= pageClipItems.length - 5)
        $("a#pageClipArrowR").hide();
      else
        $("a#pageClipArrowR").show();
    }

    $("div#pageClip").click(function() {
      deselectAllText();
    });

    $("a#pageClipArrowL").click(function() {
      if(pageClipPos > 0) {

        $("div#pageClipItem_0 a img").attr('src', '');

        --pageClipPos;
        for(var i = 0; i <= 4; i++) {
          $("div#pageClipItem_" + i + " a").attr('href', pageClipItems[pageClipPos + i].link);
          $("div#pageClipItem_" + i + " a").colorbox({ href: pageClipItems[pageClipPos + i].link});
          $("div#pageClipItem_" + i + " a img").attr('src', pageClipItems[pageClipPos + i].src);
        }

        checkArrows();
      }
    });
    $("a#pageClipArrowR").click(function() {
      if(pageClipPos < pageClipItems.length - 5) {

        $("div#pageClipItem_4 a img").attr('src', '');

        ++pageClipPos;
        for(var i = 0; i <= 4; i++) {
          $("div#pageClipItem_" + i + " a").attr('href', pageClipItems[pageClipPos + i].link);
          $("div#pageClipItem_" + i + " a").colorbox({ href: pageClipItems[pageClipPos + i].link});
          $("div#pageClipItem_" + i + " a img").attr('src', pageClipItems[pageClipPos + i].src);
        }

        checkArrows();
      }
    });


    // srovnání výšky obsahu dle navigace, pokud je nižší
    if(parseInt($("div#pageContent").css('height')) < parseInt($("div#pageNavi").css('height')))
      $("div#pageContent").css('min-height', $("div#pageNavi").css('height'));

}); //konec document ready

/* ----------------------------------------------- */

function createXhr () {
  var xhr = null;

  if (window.XMLHttpRequest)
    xhr = new XMLHttpRequest ();
  else if (window.ActiveXObject)
    try { xhr = new ActiveXObject ("Msxml2.XMLHTTP"); }
    catch (e) {
      try { xhr = new ActiveXObject ("Microsoft.XMLHTTP"); }
      catch (e) {}
    }
  return xhr;
}

function gElm(element) {
  return document.getElementById(element);
}

function zeroFill( number, width )
{
  width -= number.toString().length;
  if ( width > 0 )
  {
    return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
  }
  return number;
}

// ********************************************* //

function checkFieldsUser() {
  if(gElm('typ').value == '3') {
    gElm('row_perm').className = 'displayNone';
  }
  else {
    gElm('row_perm').className = '';
  }
}

function checkFieldsPages() {
  if(gElm('typ1').checked) {
    gElm('row_script').className = 'displayNone';
    gElm('row_text').className = '';
  }
  if(gElm('typ2').checked) {
    gElm('row_script').className = '';
    gElm('row_text').className = 'displayNone';
  }
  if(gElm('typ4').checked) {
    gElm('row_script').className = '';
    gElm('row_text').className = '';
  }
}

function checkFieldsMenu() {
  if(gElm('typ1').checked) {
    gElm('row_page').className = '';
    gElm('row_link').className = 'displayNone';
  }
  if(gElm('typ2').checked) {
    gElm('row_page').className = 'displayNone';
    gElm('row_link').className = '';
  }
}

function checkFieldsMultimedia() {
  if(gElm('typ1').checked) {
    gElm('row_picture').className = '';
    gElm('row_flash').className = 'displayNone';
    gElm('row_code').className = 'displayNone';
    gElm('row_size').className = 'displayNone';
  }
  if(gElm('typ2').checked) {
    gElm('row_picture').className = 'displayNone';
    gElm('row_flash').className = '';
    gElm('row_code').className = 'displayNone';
    gElm('row_size').className = '';
  }
  if(gElm('typ4').checked) {
    gElm('row_picture').className = 'displayNone';
    gElm('row_flash').className = 'displayNone';
    gElm('row_code').className = '';
    gElm('row_size').className = 'displayNone';
  }
}


// ****************** Galerie ****************** //

function gallerySelectPicture(pict, urlId, pictId, buttonId) {
  //gElm(buttonId).innerHTML = 'Změnit obrázek';
  
  if(urlId && gElm(urlId))
    gElm(urlId).value = galleryGetRelativePath(pict);
  
  if(pictId && gElm(pictId))
    gElm(pictId).src = wwwroot + galleryGetThumbPath(galleryGetRelativePath(pict));
}

function galleryGetRelativePath(fullPath) {
  return fullPath.substr(fullPath.search(path_a) + path_a.length);
}

function galleryGetThumbPath(fullPath) {
  var folderPath = fullPath.substr(0, fullPath.lastIndexOf('/') + 1);
  var imagePath = fullPath.substr(fullPath.lastIndexOf('/') + 1);
  return folderPath + 'mcith/mcith_' + imagePath;
}

// ****************** Menu ****************** //

var animating = false;

function toggleSubmenu(submenuId, itemCount) {
  if(!animating) {
    if(gElm('menuParrent_' + submenuId).className == 'mCollapsed') {
      gElm('subMenuId_' + submenuId).style.height = '1px';
      gElm('menuParrent_' + submenuId).className = 'mExpanded';
      animation = setInterval('slideDownMenu(' + submenuId + ', ' + itemCount + ');', animation_interval);
    }
    else {
      animation = setInterval('slideUpMenu(' + submenuId + ');', animation_interval);
    }
  }
}

function slideDownMenu(submenuId, itemCount) {
  
  var itemHeight = itemCount * 29;
  
  if(gElm('subMenuId_' + submenuId).clientHeight <= (itemHeight - animation_step)) {
    animating = true;
    gElm('subMenuId_' + submenuId).style.height = (gElm('subMenuId_' + submenuId).clientHeight + animation_step) + 'px';
  }
  else {
    animating = false;
    clearInterval(animation);
    gElm('subMenuId_' + submenuId).style.height = 'auto';
  }
}

function slideUpMenu(submenuId) {
  if(gElm('subMenuId_' + submenuId).clientHeight >= animation_step) {
    animating = true;
    gElm('subMenuId_' + submenuId).style.height = (gElm('subMenuId_' + submenuId).clientHeight - animation_step) + 'px';
  }
  else {
    animating = false;
    clearInterval(animation);
    gElm('menuParrent_' + submenuId).className = 'mCollapsed';
    gElm('subMenuId_' + submenuId).style.height = 'auto';
  }
}

// ************************************ //

function downloadItemToggle(itemId) {
  if(gElm('downloadItemDesc_' + itemId).style.display == 'block') {
    gElm('downloadItemDesc_' + itemId).style.display = 'none';
    gElm('downloadItemToggle_' + itemId).innerHTML = 'Zobrazit popis';
  }
  else {
    gElm('downloadItemDesc_' + itemId).style.display = 'block';
    gElm('downloadItemToggle_' + itemId).innerHTML = 'Skrýt popis';
  }
}

/* helping */

function getValBetween(value, min, max) {
  if(value < min)
    return min;
  else if(value > max)
    return max;
  else
    return value;
}

function deselectAllText() {
  if(window.getSelection)
    window.getSelection().removeAllRanges();
  else if(document.selection)
    document.selection.empty();
}
