// auxiliary functions

// attach CSS style to document
function addStyle(s)
{
  document.write('<style>'+s+'</style>');
}

// return list of elements with specified classname
function getElementsByClassName(tagName, className) 
{
  var result = []; 
  if (typeof document.getElementsByTagName != 'undefined')
  {
    var all = document.getElementsByTagName(tagName);
    for (var i = 0; i < all.length; i++) if (all[i].className == className) result[result.length] = all[i];
  }
  return result;
}

// returns boolean value determining whether specified
// element exists within the document
function elementExists(id)
{
  var t = document.getElementById(id);
  return (t ? true : false); 
}

// event attacher
function addEvent(object, event, func) 
{
  if (typeof object[0] != 'undefined') for (var i = 0; i < object.length; i++) addEvent(object[i], event, func);
	if (window.opera && (object == window)) object = document;	
	if (typeof object.addEventListener != 'undefined') object.addEventListener(event, func, true);
  else if (typeof object.attachEvent != 'undefined') object.attachEvent("on"+event, func);
  else return false;
}

// returns element which triggered given event 
function sourceElement(event)
{
  var source; 
  if (typeof event == 'undefined') var event = window.event;
  if (typeof event.target != 'undefined') source = event.target;
  else if (typeof event.srcElement != 'undefined') source = event.srcElement;
  else return false;
  if (source.nodeType == 3) source = targ.parentNode;
  return source;
}

// tells the browser whether or not to continue
// in processing current event 
function stopDefault(event) { if (event && event.preventDefault) event.preventDefault(); return false; }
function doDefault() { return true; }

// opens external links in new window 
// (in standards compliant way)
function external(event)
{
  var source = sourceElement(event);
  if (source) 
  { 
    if (window.open(source.href)) 
      return stopDefault(event); 
    else
      return doDefault(); 
  } 
}

// opens popup window
function miniOpener(href)
{
  return window.open(href, "_blank", "width=780, height=585, left=40, top=80, scrollbars=yes, resizable=yes, toolbar=no, location=no, directories=no, menubar=no");
}

// opens link in popup window
function mini(event)
{
  var source = sourceElement(event);
  if (source) 
  { 
    if (miniOpener(source.href))
      return stopDefault(event); 
    else
      return doDefault(); 
  } 
}

// opens photo in popup window
function photo(href)
{
  var h = window.open(href, "_blank", "width=320, height=240, left=40, top=80, scrollbars=yes, resizable=yes, toolbar=no, location=no, directories=no, menubar=no, status=yes"), html = '';
  html += '<html><head>'; 
  html += '<meta http-equiv="Content-Type" content="text/html; charset=windows-1250" />';
  html += '<style>body { margin: 0; margin-top: 3px; _margin-top: 0; padding: 0; text-align: center; } img { border: 0; cursor: hand; cursor: pointer; } p { margin: 0; text-align: center; font-family: sans-serif; } a { color: #D60000; }</style>';
  html += '<title>Plná velikost</title>'; 
  html += '<script>'; 
  html += 'function resize() { var img = document.getElementById("photo"); window.resizeTo(Math.min(img.width+32, 900), Math.min(img.height+80, 700)); }';
  html += '</script>';
  html += '</head><body>'; 
  html += '<img onclick="window.close()" onload="resize()" id="photo" src="' + href + '" alt="Plná velikost fotografie" />';
  html += '<p><a href="javascript:window.close()">(kliknutím na obrázek uzavřete okno)</a></p>';
  html += '</body></html>';
  h.document.write(html);
  return !h; 
}

// opens text in popup window
function info(text)
{
  var h = window.open('', "_blank", "width=320, height=240, left=40, top=80, scrollbars=yes, resizable=yes, toolbar=no, location=no, directories=no, menubar=no, status=yes"), html = '';
  html += '<html><head>'; 
  html += '<meta http-equiv="Content-Type" content="text/html; charset=windows-1250" />';
  html += '<style>body { font: 80% Tahoma, "Lucida Grande CE", lucida, sans-serif; margin: 10px; padding: 0; text-align: left; background-color: #E5E5E5; } a { color: #D60000; }</style>';
  html += '<title>Popis</title>'; 
  html += '</head><body>';
  html += '<p>' + text + '</p>'; 
  html += '<p><a href="javascript:window.close()">(kliknutím uzavřete okno)</a></p>';
  html += '</body></html>';
  h.document.write(html);
}

function gI(name)
{
  return document.getElementById(name);
}

/*
 *
 * Main code
 * --------- 
 *
 */
 
function radio_button_checker()
{
  if (gI('user_name').value.length <= 2) 
  { 
    alert('Musíte správně vyplnit jméno.');
    return false;
  }

  m = gI('email').value;
  if (m.indexOf('@') <= 0) 
  { 
    alert('Musíte správně vyplnit email.');
    return false;
  }

  if (gI('phone').value.length < 9) 
  { 
    alert('Musíte vyplnit telefonní číslo.');
    return false;
  }
  
  if (!(
    gI('opt_delivery_2').checked ||
    gI('opt_delivery_6').checked ||
    gI('opt_delivery_7').checked ||
    gI('opt_delivery_3').checked ||
    gI('opt_delivery_5').checked
  ))
  {
    alert('Je nutné vyplnit způsob dodání.'); 
    return false; 
  }
  
  if (!( 
    (gI('opt_payment_3').checked && (!gI('opt_payment_3').disabled)) ||
    (gI('opt_payment_6').checked && (!gI('opt_payment_6').disabled)) ||
    (gI('opt_payment_4').checked && (!gI('opt_payment_4').disabled)) ||
    (gI('opt_payment_1').checked && (!gI('opt_payment_1').disabled))
  ))
  {
    alert('Je nutné vyplnit způsob platby.');
    return false;
  }

  if (gI('opt_delivery_2').checked || gI('opt_delivery_6').checked || gI('opt_delivery_7').checked)
  {
    if (gI('street').value.length < 2) 
    { 
      alert('Musíte vyplnit celou adresu doručení.');
      return false;
    }

    if (gI('city').value.length < 2) 
    { 
      alert('Musíte vyplnit celou adresu doručení.');
      return false;
    }

    if (gI('zip').value.length < 2) 
    { 
      alert('Musíte vyplnit celou adresu doručení.');
      return false;
    }
  }
  
  return (true);
}

var loaded = false;

function addEvents()
{
  //addEvent(getElementsByClassName("a", "external"), "click", external); 
}

function setAlpha(id, alpha)
{
  if (alpha < 0)   alpha = 0;
  if (alpha > 100) alpha = 100; 
  var t = document.getElementById(id); 
  if (!t) return; 
  t.style.visibility = (alpha > 0) ? 'visible' : 'hidden';
  if (typeof t.style.filter != 'undefined') t.style.filter = "alpha(opacity=" + alpha + ")";
  if (typeof t.style.MozOpacity != 'undefined') t.style.MozOpacity = alpha / 100;
}

function setY(id, y)
{
  var t = document.getElementById(id); 
  if (!t) return; 
  t.style.top = y + 'px';
}

function fadeout(id, alpha)
{
  if (typeof alpha == 'undefined') alpha = 100;
  alpha -= 5; 
  setAlpha(id, alpha);
  if (alpha > 0) return window.setTimeout("fadeout('" + id + "', " + alpha + ")", 100);
}

function fadein(id, alpha)
{
  if (typeof alpha == 'undefined') alpha = 0;
  alpha += 5; 
  setAlpha(id, alpha);
  if (alpha < 95) return window.setTimeout("fadein('" + id + "', " + alpha + ")", 100);
}

function movedown(id, currentY, finalY)
{
  currentY += 1; 
  setY(id, currentY);
  if (currentY < finalY) return window.setTimeout("movedown('" + id + "', " + currentY + ", " + finalY + ")", 50);
}

function show(id)
{
  var t = document.getElementById('segment' + id);
  t.className = 'around enabled';
}

function hide(id)
{
  var t = document.getElementById('segment' + id);
  t.className = 'around disabled';
}

var hide_call, hide_elem = false;

function oh_rly_hide_segment_kthx_LOL()
{
  hide_elem.className = 'segment around';
}

function show_segment(elem) 
{
  if (hide_elem == elem) 
  { 
    hide_elem = false;
    clearTimeout(hide_call);
  }

  elem.className = 'segment around segment_on';
}

function hide_segment(elem)
{
  hide_elem = elem;
  hide_call = setTimeout(oh_rly_hide_segment_kthx_LOL, 150); 
}

var category_to_move = -1;

function start_category_moving(cat) 
{
  category_to_move = cat; 
  document.getElementById('content').className = 'category_moving_on';  
}

function finish_category_moving(cat) 
{
  window.location.href = "move_kat.php?id=" + category_to_move + "&above=" + cat;
}

/* execute */ 

var ad_total = 6;
var ad = ad_total;

function rotate_ads()
{
  if (ad % 2 == 0)
  {
    fadein('ad_col_2');
  }
  else
  {
    fadeout('ad_col_2');
  }

  if (ad > 1)
  {
    fadeout('ad' + ad);
    ad--; 
  }
  else
  {
    var i;
    for (i = 2; i <= ad_total; i++)
    {
      fadein('ad' + i);
    }
    ad = ad_total;
  }
}

function load() 
{
  loaded = true;

  addEvents(); 
  if (window.__utmSetTrans) __utmSetTrans(); // Google Analytics
  
  // window.setInterval("rotate_ads()", 4000);
  
/*  if (elementExists('logo'))
  {
    //setAlpha('logo', );
    movedown('logo', 0, 34);
    fadein('logo', -10);
    fadein('informations');
  } */
}

window.onload = load;
addStyle('#categories #segment85 { width: 170px; height: 23px; overflow: hidden; margin: 0 6px 7px 6px; -moz-opacity: 0.9; }');
addStyle('#wrapper #categories #segment85 ul li { display: block; }');
addStyle('#categories #segment85 ul a { display: block; }');


