var isDOM = document.getElementById ? true : false;
var isIE4 = document.all ? true : false;
var isNS4 = document.layers ? true : false;

function getRef(name)
{
  if (isDOM) return document.getElementById(name);
  if (isIE4) return document.all[name];
  if (isNS4) return document.layers[name];
}
function getStyRef(ref) { return ((isNS4 || ref == null) ? ref : ref.style); }
function getSty(id) { return getStyRef(getRef(id)); };

function hyperlink(url) { window.location=url; }

pics = new Array();
var pocet = 0;
function preload(obrName) {
  pics[pocet] = new Array(1);
  pics[pocet][0] = new Image();
  pics[pocet][0].src = obrName;
  pocet++;
}

function check_pass()
{
	if(getRef('heslo1').value != getRef('heslo2').value)
	{
		alert('Hesla se neshodují!');
		return false;
	}
	else
		return true;
}

function EssoxUrl(url)
{
  var x = 660;
  var y = 490;

  var href = (typeof(url) == 'string') ? url : url.href;
 	var l = Math.floor( (window.screen.availWidth - 10 - x) / 2 );
	var t = Math.floor( (window.screen.availHeight - 20 - y) / 2 );
	return !window.open(href, '_blank', "toolbar=0,location=0,scrollbars=1,status=1,width="+x+",height="+y+",resizable=1,left="+l+",top="+t);
}

function targetBlank(url)
{
  var width = 800;
  var height = 600;
  var left = Math.floor( (window.screen.availWidth - 10 - width) / 2);
  var top = Math.floor( (window.screen.availHeight - 20 - height) / 2);
  var href = (typeof(url) == 'string') ? url : url.href;

	if (href != "")
	{
    var winParms = "screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
		return !window.open(href, '_blank', 'menubar=yes,toolbar=yes,location=yes,directories=no,fullscreen=no,titlebar=yes,hotkeys=yes,alwayslowered=no,status=yes,scrollbars=yes,resizable=yes,dependent=no,channelmode=NO,alwaysraised=yes,zlock=yes,'+winParms);
	}
}

function sw(id)
{
  menuItem = getSty(id);
  if (menuItem.display == 'none' || menuItem.display == '')
    menuItem.display = 'block';
  else
    menuItem.display = 'none';
  return false;
}

var zobr;
function sw1(target)
{
	if (zobr != target)
	{ //jinak se jen skryje
	  if (zobr) getSty(zobr).display = "none";
	  zobr = target;
	  getSty(target).display = "block";
	}
  return false;
}

function printPage()
{
  window.print();
  return false;
}

function sendLink(odkaz)
{
 if (odkaz == '') odkaz = window.location.href;
 odkaz_string = new String(odkaz);

 odkaz_string = odkaz_string.replace('&', '%26');
 document.location = "mailto:?subject=Odkaz%20na%20bikegallery.cz&body=Odkaz%20na%20server%20bikegallery.cz%20:%0D%0A"+odkaz_string+"%0D%0Abikegallery.cz";
 return false;
}

function swVarianty(variantyStr)
{
  varianty = variantyStr.split(',');
  for (varianta in varianty)
  {
    sw(varianty[varianta]);
  }
}

function addEvent(obj, event, funct)
{
  if (obj.attachEvent) { obj.attachEvent('on'  + event, funct); }
  else if (obj.addEventListener) { obj.addEventListener(event, funct, false); }
  else eval('obj.' + event + ' = ' + funct);
}

function hledat() {
  hledani1 = getRef('slovo');
  if (hledani1.value != '-- hledat --' && hledani1.value != '') {
    window.location = "/hledat/"+hledani1.value;
  }
  return false;
}

function newletter() {
  newslett = getRef('newsletterEmail');
  if (newslett.value != '-- E-mail --' && newslett.value != '') {
    return true;
  } else {
    return false;
  }
}

window.onload =  function() {

  if (getRef('znackyMove') != null) {
    VSScroller('znackyMove', 60, 2000, 4);
  }
  if (getRef('galleryOdkaz') != null) {
    startSlideShow();
  }

  hledani1 = getRef('slovo');
  if (hledani1 != null)
  {
    hledani1.value = '-- hledat --';
    addEvent(hledani1, 'click', function() { if (hledani1.value == '-- hledat --') hledani1.value=''; });
  }
  newslett = getRef('newsletterEmail');
  if (newslett != null)
  {
    newslett.value = '-- E-mail --';
    addEvent(newslett, 'click', function() { if (newslett.value == '-- E-mail --') newslett.value=''; });
  }

  if (typeof(DD_belatedPNG)!= 'undefined') {
    DD_belatedPNG.fix('#bublina');
    DD_belatedPNG.fix('div.image.novsklad a');
    DD_belatedPNG.fix('div.image.novinka a');
    DD_belatedPNG.fix('div.image.vyprsklad a');
    DD_belatedPNG.fix('div.image.akcesklad a');
    DD_belatedPNG.fix('div.image.akce a');
    DD_belatedPNG.fix('div.image.testovaci a');
    DD_belatedPNG.fix('img.logo');
    DD_belatedPNG.fix('.top');
    DD_belatedPNG.fix('.middle');
    DD_belatedPNG.fix('.bottom');
  }
}

function show(url)
{
  pic = getRef('showPic');
  if (pic == null)
    return true;
  else
  {
    pic.src = url;
  }
  return false;
}

var slideShow = true;
var timeToShow = 7;
function uvodTema(obrazek, url, nazev)
{
  temaUvod1 = getRef('galleryOdkaz');
  temaUvod1.href = url;
  temaUvod1Style = getStyRef(temaUvod1);
  temaUvod1Style.backgroundImage = 'url(' + obrazek + ')';
  temaUvod1.innerHTML = nazev + ' ///';
}

var iCountPicture = 0;
function startSlideShow()
{
  if (slideShow) {
    iCountPicture++;
    if (iCountPicture == inputPicture.length) iCountPicture = 0

    uvodTema('/img/tema_uvod/' + inputPicture[iCountPicture], inputUrl[iCountPicture], inputNazev[iCountPicture]);
  }
  setTimeout(startSlideShow, timeToShow * 1000);
}
function VSScroller(ulId, speed, pause, pauseafter)
{
  this.container = getRef(ulId);
  this.container.Scroller = this;
  this.speed = speed;
  this.count = 0;
  this.pause = pause;
  this.pauseafter = pauseafter;
  this.scroll = function() {
    var c = this.container.firstChild;
    var first = null;
    var blnPause = false;
    while (c) {
      if (c.tagName == 'LI') {
              first = c;
              break;
      }
      c = c.nextSibling;
    }
    var px = 0;
    var nodeSize = 0;
    nodeSize = first.clientWidth;
    if (first.style.marginLeft != '') {
            px = parseInt(first.style.marginLeft);
    }
    first.style.marginLeft = ( px - 2 ) + 'px';
    if ( parseInt(first.style.marginLeft) <= -(nodeSize) ) {
      first.style.marginLeft = '0px';
      this.container.removeChild(first);
      this.container.appendChild(first);
      this.count++;
      if (this.count == this.pauseafter)
      {
        var blnPause = true;
        this.count = 0;
      }
    }
    if (!blnPause)
    {
      setTimeout('getRef(\'' + this.container.id + '\').Scroller.scroll()', this.speed);
    }
    else
    {
      setTimeout('getRef(\'' + this.container.id + '\').Scroller.scroll()', (this.speed + this.pause));
    }
  }
  setTimeout('getRef(\'' + ulId + '\').Scroller.scroll()', this.speed);
}

function rezervace() {
  if (getRef('splatkyEssox').checked) {
    return confirm('Opravdu si přejete využít splátkový program Essox?');
  }
  return true;
}

function redirectToURL(url) {
  if (targetBlank(url)) {
    hyperlink(url);
  }
}