var listNum = 1;

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

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

function submitOnChange() {
  document.getElementById('hledej_dotace').submit();
}

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 switchPanel(num) {
  listNum = num;
  $('product-list-' + num).style.left = 0 + 'px';
  if ($$('#slider #product-list-' + num + ' .product-item').length > 3) {
    $('navig-next').show();
    $('navig-prev').hide();
  } else {
    $('navig-next').hide();
    $('navig-prev').hide();
  }
  $$('#slider .product-list').each(function(s){
    if (s.id == 'product-list-' + num) {
      s.appear();
      $$('#slider #product-tabs a').each(function(a){
        if (a.id == 'product-tab-' + num) {
          a.className = 'active';
        } else {
          a.className = 'disabled';
        }
      });
    } else {
      s.fade();
    }
  });
}

function moveList(dir, list) {
  var oldPosition = $('product-list-' + list).style.left.split('px')[0];
  var newPosition = (dir == 1) ? oldPosition - 957 : parseInt(oldPosition) + 957;
  var count = $$('#slider #product-list-' + list + ' .product-item').length;
  var page = newPosition/957 - 1;
  if (dir == 1) {
    var isLast = (Math.ceil(count/3) + page == 0) ? true : false;
    $('navig-next').onclick = function(event) {return false};
    new Effect.Move('product-list-' + list, {x: -957, y: 0});
    setOnClick.delay(1, 'navig-next', dir);
    if (isLast) {
      $('navig-next').hide();
    }
    if (!$('navig-prev').visible()) {
      $('navig-prev').show();
    }
  } else {
    var fromLast = (Math.ceil(count/3) + page == 1) ? true : false;
    $('navig-prev').onclick = function(event) {return false};
    new Effect.Move('product-list-' + list, {x: 957, y: 0});
    setOnClick.delay(1, 'navig-prev', dir);
    if (newPosition == 0) {
      $('navig-prev').hide();
    }
    if (fromLast) {
      $('navig-next').show();
    }
  }
}

function setOnClick(el, dir) {
  $(el).onclick = function(event) {moveList(dir, listNum)};
}

function switchVis(el) {
  if ($(el).visible()) {
    $(el).hide();
  } else {
    $(el).show();
  }
}

function switchGubiTreeClass(el){
    if ($(el).hasClassName("minus")) {
      $(el).addClassName("plus");
      $(el).removeClassName("minus");

    } else {
      $(el).addClassName("minus");
      $(el).removeClassName("plus");
    }
  }

