// 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; }</style>';
  html += '<title>Plná velikost</title>'; 
  html += '<script>'; 
  html += 'function resize() { var img = document.getElementById("photo"); window.resizeTo(Math.min(img.width+32, 800), Math.min(img.height+75, 600)); }';
  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; 
}

/*
 *
 * Main code
 * --------- 
 *
 */
 
function checkSeznamka(e)
{
  t = document.getElementById('email');
  if (t && (t.value.indexOf('@') == -1)) 
  {
    alert('Email je nutné vyplnit, bez něho nemůžeme váš inzerát odvysílat.');
  	return stopDefault(e);
  }
}

function checkDiskuze(e)
{
  t = document.getElementsByName('message')[0];
  if (!t.value)
  {
    alert('Ups, text zprávy je nutné vyplnit.');
  	return stopDefault(e);
  }
}

var news_rotation = 0, http, body;

function addEvents()
{
  addEvent(getElementsByClassName("a", "external"), "click", external); 
  addEvent(getElementsByClassName("a", "mini"), "click", mini); 
  
  body = document.getElementById('www-radio-relax-cz');
  
  if (body)
  {
    if (body.className.indexOf('seznamka') != -1)
    {
      f = document.getElementById('seznamka');
      if (f) addEvent(f, 'submit', checkSeznamka);
    }

    if (body.className.indexOf('diskuze') != -1)
    {
      f = document.getElementById('boardform');
      if (f) addEvent(f, 'submit', checkDiskuze);
    }
  }
}

function getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
    }
	}
	return xmlhttp;
}

function load() 
{
  addEvents(); 
  
  http = getHTTPObject();
  
  setTimeout("document.getElementById('flashad').style.display = 'none';", 10000); 
  
  if (elementExists('news-2'))
  {
    window.setInterval("fading()", 7000);
    news_rotation = 1;
  }
  
  if (elementExists('feature-body-1'))
  {
    window.setInterval('newsrot()', 600);
  }
  
  if (elementExists('news-1')) fadein('news-1');
      
  if (elementExists('visuals')) 
  {
    if (!document.all && elementExists('news-1')) fadein('visuals');
    addEvent(document.getElementById('visuals'), 'click', turnVizOff);
  }
  
  //window.setInterval('changesong()', 5000);
  
  if (body.className.indexOf('program') != -1)
  {
    datum = new Date();
    datum = datum.getDay() - 1; 
    if (datum < 0) datum = 6;
    
    programSelect(datum);
  }
}

function handleResponse()
{
  if (http.readyState == 4)
  {
  	response = http.responseText;
  	e = document.getElementById('just-playing');
  	if (e) e.innerHTML = response;
  }
}

function changesong()
{
  http.open("GET", 'http://www.radio-relax.cz/song.php', true);
  http.onreadystatechange = handleResponse;
  http.send(null); 
}

function turnVizOff()
{
  fadeout('visuals');
}

function visual(i, height)
{
  t = document.getElementById('v' + i);
  h = height * 4 - 2;
//  t.style.height = h + 'px';
  t.style.top = (42 - h) + 'px';
}

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 fadeout(id, alpha)
{
  if (typeof alpha == 'undefined') alpha = 100;
  alpha -= 10; 
  setAlpha(id, alpha);
  if (alpha > 0) return window.setTimeout("fadeout('" + id + "', " + alpha + ")", 70);
}

function fadein(id, alpha)
{
  if (typeof alpha == 'undefined') alpha = 0;
  alpha += 8; 
  setAlpha(id, alpha);
  if (alpha < 100) return window.setTimeout("fadein('" + id + "', " + alpha + ")", 70);
}

function fading()
{
  out = news_rotation; 
  fadeout('news-' + news_rotation++); 
  if (!elementExists('news-' + news_rotation)) news_rotation = 1;
  window.setTimeout("fadein('news-" + news_rotation + "'); document.getElementById('news-" + out + "').style.visibility = 'hidden';", 1250); 
}

var newsTime = 0;

function mbSelectTab(which)
{
  t1 = document.getElementById('tab_mobil_seg_Hry');
  t2 = document.getElementById('tab_mobil_seg_Tapety');
  t3 = document.getElementById('tab_mobil_seg_Melodie');
  t1.style.display = 'none'; 
  t2.style.display = 'none'; 
  t3.style.display = 'none'; 
  t = document.getElementById('tab_' + which);
  t.style.display = 'block';
}

function programSelect(id)
{
  p = document.getElementById('program');
  p = p.getElementsByTagName('ul'); 
  for (i=0;i<p.length;i++)
    p[i].style.display = 'none'; 
  
  p = document.getElementById('prog' + id);
  p.style.display = 'block';
}

function newsrot()
{
  i = newsTime % 25 + 1;
  if (i <= 6)
  {
    fadeout('feature-body-' + i);
  }
  else if (i >= 12 && i <= 17)
  {
    fadein('feature-body-' + (i - 11));
  }

  newsTime++; 
}

window.onload = load;
