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);
    }
  }
}

function moveTextLoop() {
  var el = $('scroll');
  var elInnerWidth = 0;
  if (el) {
    el.select('li').each(function(s){
      elInnerWidth += $(s).getWidth();
    });
    if (elInnerWidth > 950) {
      moveText.delay(7, el);
    }
  }
}

function moveText(el) {
  var titles = el.select('li');
  var moveX = - titles.first().getWidth();
  var toEnd = new Element('li', { 'class': 'last' }).update(titles.first().innerHTML);
  titles.last().removeClassName('last');
  el.insert(toEnd);
  new Effect.Move(el, { x: moveX, y: 0 });
  (function() {titles.first().remove();el.style.left = '0'}).delay(1.5);
  moveText.delay(7, el);
}

function switchPanel(num, stopLoop) {
  $$('#doporucujeme .item').each(function(s){
    if (s.id == 'stick-item-' + num) {
      s.show();
      $$('#doporucujeme .art-switch a').each(function(a){
        if (a.id == 'item-link-' + num) {
          a.className = 'active';
        } else {
          a.className = 'disabled';
        }
      });
    } else {
      s.hide();
    }
  });
  if (stopLoop == true) {
    window.clearTimeout(timerId);
  } else {
    if (num == $$('#doporucujeme .item').length) {
      timerId = switchPanel.delay(8, 1);
    } else {
      timerId = switchPanel.delay(8, num+1);
    }
  }
}

function switchImg(num) {
  $$('#doporucujeme img').each(function(s, i){
    if (i+1 == num) {
      s.appear();
    } else {
      s.hide();
    }
  });
  if (num == $$('#doporucujeme img').length) {
    timerId = switchImg.delay(8, 1);
  } else {
    timerId = switchImg.delay(8, num+1);
  }
}

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]);
    }
  }

  moveTextLoop();
});

function switchTab(active, tabsElement) {
  $(tabsElement).previous('div.tabs_wrp').select('ul li a').each(function (a, i){
    a = $(a);
    a.removeClassName('active');
    if (active == i) {
      a.addClassName('active');
    }
  });
  $(tabsElement).select('.tab').each(function(tab, i){
    tab = $(tab);
    tab.hide();
    if (active == i) {
      tab.show();
    }
  })
}

function attrFnc(url, id) {
  var key = 0;
  var imgObj = new Element('img');
  var imgObjLink = new Element('a');
  var timer = null;
  var imgData = [];
  var imgTexts = [];
  new Ajax.Request(url, {
    method: 'post',
    onSuccess: function(transport) {
      var imgs = transport.responseXML.getElementsByTagName('image');
      if (imgs.length > 1) {
        timer = loop.delay(7, 1);
      }
      for (i=0;i<imgs.length;i++) {
        var texts = imgs[i].getElementsByTagName('text');
        var txtValue = '';
        for (j=0;j<texts.length;j++) {
          var textCnt = texts[j].text || texts[j].textContent;
          if (j == 0) {
            txtValue += textCnt;
          } else {
            txtValue += '<br />' + textCnt;
          }
        }
        imgTexts[i] = txtValue;
        imgData[i] = [];
        imgData[i]['src'] = imgs[i].getAttribute('src');
        imgData[i]['target'] = imgs[i].getAttribute('target');
      }
      imgObj.src = imgData[key]['src'];
      imgObjLink.href = imgData[key]['target'];
      imgObjLink.addClassName('img');
      $('img_cnt_text_'+id).update(imgTexts[key]);
      $('img_cnt_text_bg_'+id).setOpacity(0.7).setStyle({'height': $('img_cnt_text_'+id).getHeight() + 'px'});
      $('img_cnt_'+id).insert(imgObjLink);
      imgObj.onload = function(){
        $('img_cnt_'+id).setStyle({'height': imgObj.getHeight() + 'px'});
      }
      imgObjLink.insert(imgObj);
    }
  });

  function changeImg(k) {
    imgObj.fade({duration: 0.5});
    Element.update.delay(0.25, 'img_cnt_text_'+id, imgTexts[k]);
    Element.writeAttribute.delay(0.5, imgObj, 'src', imgData[k]['src']);
    Element.writeAttribute.delay(0.5, imgObjLink, 'href', imgData[k]['target']);
    Element.appear.delay(0.6, imgObj, {duration: 0.5, queue: 'end'});
  }

  function loop(k) {
    changeImg(k);
    if (imgData[k+1]) {
      timer = loop.delay(7, k+1);
    } else {
      timer = loop.delay(7, 0);
    }
  }
}

// adding method getPageSize to prototype's Position method
// returns [width, height] and {width, height}
//
// usage:
// Position.getPageSize().width/height OR
// var pageSize = Position.getPageSize()
Position.getPageSize = function() {
  var array = [];
  var pageW, pageH;

  if (window.innerHeight && window.scrollMaxY) {
		pageW = document.body.scrollWidth;
		pageH = window.innerHeight + window.scrollMaxY;
  } else {
		pageW = document.body.scrollWidth || document.body.offsetWidth;
		pageH = document.body.scrollHeight || document.body.offsetHeight;
  }

	if(pageH < document.viewport.getHeight()) {
		pageH = document.viewport.getHeight();
	}

	if(pageW < document.viewport.getWidth()) {
		pageW = document.viewport.getWidth();
	}

  array.width = array[0] = pageW;
  array.height = array[1] = pageH;

  return array;
}  

function showWarnBox() {
    document.cookie = 'popup=1;path=/';
    $('warn-box-close').observe('click', function(event){
      $('warn-box-bg').hide();
      $('warn-box').hide();
    });
    $('warn-box-bg').show().setOpacity(0.7).setStyle({'height': Position.getPageSize().height + 'px'});
    $('warn-box').show();
  }


