var timestamp = 0;
var dt = new Date();

//***********************************************************
// OnImageLoad
//***********************************************************

function _tester(t)
{
  if (t.img.complete)
  {
    t.on_load_method();
  } else {
    setTimeout(_tester, 200, t);
  }
}

function load(src, method)
{
  this.img.src = src;
  this.on_load_handler = method;
  _tester(this);
}

function OnImageLoad()
{
  this.img = new Image();
  this.on_load_method = null;

  this.load = load;
  this._tester = _tester;
}

//***********************************************************

function set_clock()
{
  dt.setTime(timestamp * 1000);
  h = numberFormatter("00")(dt.getHours());
  m = numberFormatter("00")(dt.getMinutes());
  getElement("time").innerHTML = h + ":" + m;
  timestamp += 1;
}

function run_clock(ts)
{
  timestamp = ts;
  set_clock();
  window.setInterval(set_clock, 1000);
}

//***********************************************************
// photos slideshow
//***********************************************************

var PHOTO_SWAP_PHASES = 10;

var g_photos_count = 0;
var g_photos_idx = 0;
var g_photo_ch = 0;

//***********************************************************

function swap_photos(new_photo)
{
  if (g_photo_ch == 0)
  {
    getElement("photo_img2").src = new_photo;
    setOpacity("photo_img2", 0)
  } else if (g_photo_ch == PHOTO_SWAP_PHASES) {
    getElement("photo_img1").src = new_photo;
  }

  if (g_photo_ch < PHOTO_SWAP_PHASES)
  {
    g_photo_ch += 1;
    setOpacity("photo_img2", g_photo_ch / PHOTO_SWAP_PHASES);
    setTimeout("swap_photos(null)", 100);
  }
}

function setup_photos(photos)
{
  g_photos_count = photos.length;
  getElement("photo_img1").src = photos[0];
  //getElement("photo_img2").src = photos[0];

  //swap_photos(photos[1]);
}

//***********************************************************
// other
//***********************************************************

function eid(id)
{
  if (document.all) return document.all[id];
  else return document.getElementById(id);
}

/********************************************************/

function new_window(url, width, height)
{
  window.open(url,'_blank','width='+width+',height='+height+',resizable,scrollbars=yes'); 
  return false;
}

//********************************************************

function food_menu_select(id)
{
  eid("food_menu_body").innerHTML = eid("food_menu_item_"+id).innerHTML;
}

//********************************************************

function check_reserv_form(rform)
{
  must_fill = new Array(
      rform.arrival,
      rform.departure,
      rform.countPersons,
      rform.countKids,
      rform.name,
      rform.surname,
      rform.phone,
      rform.email,
      rform.creditCardId,
      rform.creditCard
    );

  for (i = 0; i < must_fill.length; i++)
  {
    if (must_fill[i].value == "")
    {
      alert("Je nutné vyplnit všechna pole označené hvězdičkou.\nIt is neccessary to fill all fields marked with star.");
      must_fill[i].focus();
      return false;
    }
  }
    
  return true;
}

//********************************************************
// fake scrollbar handling
//********************************************************

SCROLL_COUNT = 20;
SCROLL_SPEED = 150; // ms

//***************

blue_sbar = new Object();
blue_sbar.id = "blue_fold_info";

//***************

// f***ing IE
actual_sbar = -1;

//***************

/// convert e.g. "30px" to 30
function margin2num(marg)
{
  idx = marg.indexOf("px");
  if (idx < 0) return 0;
  marg = marg.substring(0, idx);
  return parseInt(marg);
}

function scroll_up(sbar)
{
  if (actual_sbar != -1) sbar = actual_sbar;
  
  blk = eid(sbar.id);
  pos = margin2num(blk.style.marginTop);
  if (pos < 0) pos += SCROLL_COUNT;
  if (pos > 0) pos = 0;
  blk.style.marginTop = pos.toString() + "px";
}

function scroll_down(sbar)
{
  if (actual_sbar != -1) sbar = actual_sbar;
  
  blk = eid(sbar.id);
  pos = margin2num(blk.style.marginTop);
  pos -= SCROLL_COUNT;
  blk.style.marginTop = pos.toString() + "px";
}

//***************

function mouse_scroll_up(sbar)
{
  actual_sbar = sbar;
  scroll_up(sbar);
  sbar.timer = window.setInterval(scroll_up, SCROLL_SPEED, sbar);
}
function mouse_off_scroll_up(sbar)
{
  actual_sbar = -1;
  window.clearInterval(sbar.timer);
}

function mouse_scroll_down(sbar)
{
  actual_sbar = sbar;
  scroll_down(sbar);
  sbar.timer = window.setInterval(scroll_down, SCROLL_SPEED, sbar);
}
function mouse_off_scroll_down(sbar)
{
  actual_sbar = -1;
  window.clearInterval(sbar.timer);
}

//********************************************************
// "div windows" handling
//********************************************************

function dw_open(div_id, add_class)
{
  div = eid(div_id);
  div.className = "dw_visible " + add_class;
}
function dw_close(div_id)
{
  div = eid(div_id);
  div.className = "dw_hidden";
}

/********************************************************/
// cookies
/********************************************************/

function nameDefined(ckie,nme)
{
   var splitValues
   var i
   for (i=0;i<ckie.length;++i)
   {
      splitValues=ckie[i].split("=")
      if (splitValues[0]==nme) return true
   }
   return false
}
function delBlanks(strng)
{
   var result=""
   var i
   var chrn
   for (i=0;i<strng.length;++i) {
      chrn=strng.charAt(i)
      if (chrn!=" ") result += chrn
   }
   return result
}
function getCookieValue(ckie,nme)
{
   var splitValues
   var i
   for(i=0;i<ckie.length;++i) {
      splitValues=ckie[i].split("=")
      if(splitValues[0]==nme) return splitValues[1]
   }
   return ""
}
function testCookie(cname, cvalue) {  //Tests to see if the cookie 
   var cookie=document.cookie;           //with the name and value 
   var chkdCookie=delBlanks(cookie);  //are on the client computer
   var nvpair=chkdCookie.split(";");
   if(nameDefined(nvpair,cname))       //See if the name is in any pair
   {   
      tvalue=getCookieValue(nvpair,cname);  //Gets the value of the cookie
      if (tvalue == cvalue) return true;
	   else return false;
   }
   else return false;
}

