function searchClear(target, searchText)
{
  if (target.value == searchText)
  {
    target.value = '';
  }
}

function searchRestore(target, searchText)
{
  if (target.value == '')
  {
    target.value = searchText;
  }
}

function bar(url, title) {
	wasOpen = false;
	win     = window.open(url, title);
	return (typeof(win) == 'object') ? true : false;
}

function myPopImage(imageURL,imageTitle,imageWidth,imageHeight) {

  PositionX = 30;
  PositionY = 30;

  ratio = imageWidth/imageHeight;
  maxHeight = screen.height - 105;
  maxWidth = maxHeight*ratio;
  if (maxWidth > screen.width-30) {
    maxWidth = screen.width - 80;
    maxHeight = maxWidth/ratio;
  }

  var opt='width=320,height=240,left='+PositionX+',top='+PositionY+',status=0,toolbar=0,menubar=0,location=0';
  imgWin=window.open('about:blank','null',opt);
  with (imgWin.document) {
    writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
    writeln('<sc'+'ript>');
    writeln('function reSizeToImage(){');
    writeln('if (document.images[0].height > '+maxHeight+' || document.images[0].width > '+maxWidth+'){');
    writeln('height = '+maxHeight+';');
    writeln('width = '+maxWidth+';');
    writeln('document.images[0].width = '+maxWidth+';');
    writeln('document.images[0].height = '+maxHeight+';');
    writeln('window.resizeTo(width+40,height+100);}');
    writeln('else {');
    writeln('height = document.images[0].height;');
    writeln('width = document.images[0].width;');
    writeln('window.resizeTo(width+40,height+100);}}');
    writeln('function doTitle(){document.title="'+imageTitle+'";}');
    writeln('</sc'+'ript>');
    writeln('</head><body bgcolor="FFFFFF" onload="reSizeToImage();doTitle();self.focus()">');
    writeln('<img alt="'+imageTitle+'" title="'+imageTitle+'" src="'+imageURL+'" style="display:block; margin:0 auto;" /></body></html>');
    close();
  }
}

function myPopWindow(windowURL, windowName, windowFeatures) {
  var defaultFeatures = {"status": 0, "toolbar": 0, "location": 0, "menubar": 0,
                         "directories": 0, "resizeable": 1, "scrollbars": 1,
                         "width": 640, "height": 480};
  var features = '';

  for (i in windowFeatures) { defaultFeatures[i] = windowFeatures[i]; }
  for (i in defaultFeatures) { features += i + '=' + defaultFeatures[i] + ','; }

  window.open(windowURL, windowName, features);
  return false;
}

function switchBmk(n, css) {
  if (document.getElementById('bmk_header')) {
    var num = document.getElementById('bmk_header').getElementsByTagName('div').length;
    for (var i=1;i<=num;i++) {
      var elt = document.getElementById('bmk_title_' + i);
      var elc = document.getElementById('bmk_content_' + i);
      if (i == n) {
        elt.className = css + '_title active';
        elc.className = css + '_content visible';
      }
      else {
        elt.className = css + '_title';
        elc.className = css + '_content invisible';
      }
    }
  }
}

function highlightWords(elCls, hCls, words) {
  if (typeof words == 'object') {
    words = Object.keys(words);
  }
  if ($$(elCls).length > 0 && words.length > 0) {
    words.each(
      function(c, i){
        words[i] = c.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-])/g, '\\$1');
        words[i] = words[i].replace(/ /g, '\\s');
      }
    );
    var re = new RegExp('([^a-zA-Z0-9_\\-]|^)('+words.join('|')+')([^a-zA-Z0-9_\\-]|$)', 'gim');
    $$(elCls).each(function(el) {
      highlightWordsElem(el, re, hCls);
    });
  }
}

function highlightWordsElem(el, re, hCls) {
  for (var i=0;i<el.childNodes.length;i++) {
    if (el.childNodes[i].nodeName == '#text' && !$(el).hasClassName('h_word')) {
      var text = el.childNodes[i].data || el.childNodes[i].textContent;
      if (text.match(re)) {
        var html = text.replace(re, '$1<span class="' + hCls + '"><span class="h_word">$2</span></span>$3');
        if (el.childNodes.length == 1) {
          el.innerHTML = html;
        } else {
          var hSpan = new Element('span');
          hSpan.innerHTML = html;
          if (typeof el.childNodes[i].nextSibling != 'undefined') {
            el.insertBefore(hSpan, el.childNodes[i].nextSibling);
            if (typeof el.childNodes[i].data != 'undefined') {
              el.childNodes[i].data = '';
            } else {
              el.childNodes[i].textContent = '';
            }
          } else if (typeof el.childNodes[i].previousSibling != 'undefined') {
            el.appendChild(hSpan);
            if (typeof el.childNodes[i].data != 'undefined') {
              el.childNodes[i].data = '';
            } else {
              el.childNodes[i].textContent = '';
            }
          }
        }
      }
    } else {
      highlightWordsElem(el.childNodes[i], re, hCls);
    }
  }
}

var wideContent = {
  collapsed: true,

  init: function() {
    $$('.wide_content').each(function(el) {
      var par = el.parentNode;
      $(el).setStyle({'overflow': 'visible', 'width': 'auto'});
      var wideWrapper = $(el).wrap('div', {'class': 'wide_content_wrapper', 'style': 'border: 1px solid #D9D9D9; width: '+($(par).getWidth()-2)+'px'});
      var wideBlock = wideWrapper.wrap('div', {'class': 'wide_content_block', 'style': 'height: ' + wideWrapper.getHeight() + 'px'});
      var expandLinkTop = new Element('a', {'href': 'javascript:void(0)', 'class': 'expand_link'}).update('&laquo; celý obsah &raquo;');
      var expandLinkBtm = new Element('a', {'href': 'javascript:void(0)', 'class': 'expand_link bottom_link'}).update('&laquo; celý obsah &raquo;');
      var expandLinkIn = new Element('a', {'href': 'javascript:void(0)', 'class': 'expand_link_in', 'style': 'height: '+wideBlock.getHeight()+'px; line-height: '+wideBlock.getHeight()+'px;'}).update('&raquo;').setOpacity(0.7);
      wideBlock.insert({'before': expandLinkTop, 'after': expandLinkBtm, 'bottom': expandLinkIn});
      expandLinkTop.observe('click', wideContent.expand.curry(el, wideBlock, wideWrapper, expandLinkTop, expandLinkBtm, expandLinkIn, par));
      expandLinkBtm.observe('click', wideContent.expand.curry(el, wideBlock, wideWrapper, expandLinkTop, expandLinkBtm, expandLinkIn, par));
      expandLinkIn.observe('click', wideContent.expand.curry(el, wideBlock, wideWrapper, expandLinkTop, expandLinkBtm, expandLinkIn, par));
    });
  },

  expand: function(el, b, w, lt, lb, li, p, e) {
    var elw = $(el).down('table').getWidth();
    var parw = $(p).getWidth();
    var left = ((elw - parw) / 2) > $(p).viewportOffset().left ? $(p).viewportOffset().left : (elw - parw) / 2;
    if (wideContent.collapsed) {
      $(b).setStyle({'overflow': 'visible'});
      $(li).hide();
      $(w).setStyle({'width': parw+'px', 'border': '3px solid #D9D9D9'});
      $(w).morph('width: '+elw+'px; left: -'+left+'px;', {duration: 0.5});
      Element.update.delay(0.5, lt, '&raquo; zmenšit &laquo;');
      Element.update.delay(0.5, lb, '&raquo; zmenšit &laquo;');
      wideContent.collapsed = false;
    } else {
      $(w).morph('width: '+(parw-2)+'px; left: 0px;', {duration: 0.5});
      Element.setStyle.delay(0.5, b, {'overflow': 'hidden'});
      Element.setStyle.delay(0.5, w, {'border': '1px solid #D9D9D9'});
      Element.update.delay(0.5, lt, '&laquo; celý obsah &raquo;');
      Element.update.delay(0.5, lb, '&laquo; celý obsah &raquo;');
      Element.show.delay(0.5, li);
      wideContent.collapsed = true;
    }
  }
}

document.observe("dom:loaded", function() {
  if (window.location.hash && !window.location.hash.split('#')[1].empty()) {
    var hashValue = window.location.hash.split('#')[1];
    if (hashValue.indexOf('bmk') != -1) {
      var bmkVars = hashValue.split('-');
      switchBmk(bmkVars[1], bmkVars[0]);
    }
  }

  wideContent.init();
});
