function rewriteSearch() {
  var str;

  // ziskat retezec k vyhledani
  str = document.getElementById('keyword').value;

  // osetrit nebezpecne znaky
  str = str.replace(/(\s+)/g, "_");
  str = encodeURIComponent(str);

  // zmenit adresu v browseru
  window.location = '/gallery/' + str + '/All/page1.html';
  return false; // vratit false aby se neodeslal formular
}

function showPicture(obj, width, height) {
  var picture, tags, name, url, win;

  // ziskat adresu obrazku
  picture = firstChild(obj).href;
  // zmenit script z pictures na show
  picture = picture.replace("/pictures/", "/show/");

  name = picture.substr(picture.lastIndexOf("/") + 1, picture.length - picture.lastIndexOf(".") + 1);

  tags = firstChild(firstChild(firstChild(obj))).alt;
  tags = tags.replace(/(.)[\s,()]+(.)/g, "$1-$2");
  tags = tags.replace(/^[\s,()]+(.)/g, "$1");
  tags = tags.replace(/(.)[\s,()]+$/g, "$1");

  url = picture + "/" + name + "-" + tags + ".html";

  if(document.cookie.indexOf("resize_new_win=1") >= 0) {
    var params = "resizable=yes,scrollbars=yes,location=yes";

    width = width < 887 ? 887 : width;
    if(width >  screen.availWidth) {
      width = screen.availWidth;
      params += ",left=0";
    }
    params += ",width=" + width;

    if((height += 402) > screen.availHeight) {
      height = screen.availHeight;
      params += ",top=0";
    }
    params += ",height=" + height;

    // otecrit v novem okne aby se pouzil html odkaz
    win = window.open(url, "_blank", params);
  }
  else {
    win = window.open(url, "_blank");
  }

  if(navigator.appName == 'Microsoft Internet Explorer' || navigator.appVersion.indexOf('Chrome/')) {
    makeViewed(obj);
  }

  return win;
}


/*
 * Oznacit obrazek jako zobrazeny (pri IE a Chrome)
 * @param obj (object) bunka tabukly s thumbnailama
 */
function makeViewed(obj) {
  var picId, viewed;
  picId = dechex(obj.firstChild.firstChild.firstChild.id.substring(2));

  if(isViewed(picId) == false) {

    viewed = getCookie('viewed');
    if(viewed) { viewed += '|'; }
    else { viewed = ''; }

    document.cookie = 'viewed=' + viewed + picId + ';';
    obj.firstChild.className += ' visited';
  }
}


/*
 * Zjistit jestli je obrazek nastaveny v cookies jako zobrazeny
 * @param picId (string) hexadecimalni cislo obrazku
 */
function isViewed(picId) {
  var viewed;
  viewed = getCookie('viewed')

  if(viewed) {
    viewed = viewed.split('|');

    for(var i = 0; i < viewed.length; i++) {
      if(picId == viewed[i]) {
        return true;
      }
    }
  }
  
  return false;
}


/*
 * Vrati hodnotu cookie
 * @param  val (string) nazev cookie
 */
function getCookie(val) {
  var cookies, cookie;
  cookies = document.cookie.split(';');

  for(var i = 0; i < cookies.length; i++) {
    cookie = cookies[i].split('=');
    if(trim(cookie[0]) == val) {
      return cookie[1];
    }
  }

  return null;
}


if(navigator.appName == 'Microsoft Internet Explorer' || navigator.appVersion.indexOf('Chrome/') != -1) {
  addEvent(window, 'load', function() { highlightViewed() });
}


function highlightViewed() {
  var thumbs, picId;
  thumbs = document.getElementById('thumbnails').getElementsByTagName('td');

  for(var i = 0; i < thumbs.length; i++) {
    if(thumbs[i].className == 'thumbnail') {
      picId = dechex(thumbs[i].firstChild.firstChild.firstChild.id.substring(2));
      if(isViewed(picId)) {
        thumbs[i].firstChild.className += ' visited';
      }
    }
  }
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function dechex(num) {return parseInt(num).toString(16);}
function hexdec(num) {return parseInt(num,16);}
