var SLASH = "/";

function styleTable(table) {
   table.border = 0;
   var rows = table.getElementsByTagName("tr");
   if (!rows)
      return;
   var cols = rows[0].getElementsByTagName("td");
   if (rows.length > 2 || cols.length > 1) {   
      for (var i=0; i < rows.length; i++) {
         table.className = "zebra";
         if (i % 2 == 0)
            rows[i].className = "even";
         else
            rows[i].className = "odd";
      }
   } 
   return;
}

function styleAllTables() {
   var contentDiv = document.getElementById ("story");
   if (!contentDiv)
      return;
   var tables = contentDiv.getElementsByTagName("table");
   for (var i=0; i < tables.length; i++) {
      styleTable(tables[i]);
   }
   return;
}

function zapTo() {
   var now = new Date();
   var dateStr = "";
   var currYear = now.getYear() < 1000 ? (now.getYear() + 1900) : now.getYear();
   var currMonth = (now.getMonth() + 1) < 10 ? ("0" + (now.getMonth() + 1)) : (now.getMonth() + 1);
   var currDay = now.getDate() < 10 ? ("0" + now.getDate()) : now.getDate();
   dateStr = currYear + currMonth + currDay;
   parent.location.href = "http://tv.orf.at/all/" + dateStr + "/filter?cat=Sport";
}

function openPopup(page,pixelheight,scroll) {
   var popup = window.open(page,'Popup','toolbar=no,scrollbars=' + (scroll != null && scroll != 'yes' ? 'no' : 'yes') + ',location=no,directories=no,status=no,copyhistory=no,resizable=yes,width=515,height=' + pixelheight);
   if (popup.opener == null) popup.opener = window;
}

function popupWindow(url, width, height) {
   var win = window.open(url, 'Popup', "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no");
   return;
}

function popupBox(url, width, height) {
   var win = window.open(url, 'Popup', "width=" + width + ",height=" + height + ",resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no");
   return;
}

function openWin(url,name,w,h) {
   var popup = window.open(url,name,'toolbar=no,location=no,directories=no,status=no,copyhistory=no,scrollbars=no,resizable=no,width=' + w + ',height=' + h);
   if (popup.opener == null) popup.opener = window;
}

function livePopup(id) {
	if (id == "cl")
		var popup = window.open('http://sport.orf.at/liveplayer_cl.html','livepopup','toolbar=no,location=no,directories=no,status=no,copyhistory=no,scrollbars=no,resizable=no,width=460,height=410');
	else
   	var popup = window.open('http://sport.orf.at/liveplayer_bundesliga.html','livepopup','toolbar=no,location=no,directories=no,status=no,copyhistory=no,scrollbars=no,resizable=no,width=460,height=410');
   if (popup.opener == null) popup.opener = window;
}

function isDeframed(href) {
   if (!href)
      href = location.href;
   var topHref = top.location.href;
   if (topHref.substring(0, topHref.indexOf(SLASH, 8)) != 
       href.substring(0, href.indexOf(SLASH, 8))) {
      return true;
   }
   return false;
}

if (isDeframed())
   top.location.replace(location.href);

function reframe() {
   var href = top.location.href;
   var pos, prefix = "?href=";
   if ((pos = href.lastIndexOf(prefix)) > -1) {
      href = decodeURIComponent(href.substring(pos + prefix.length));
      if (isDeframed(href)) {
         top.location.replace(top.location.href.substring(0, pos));
      }
      if (location.href != href && !top.location.ready) {
         location.replace(href);
      }
      top.location.ready = true;
   } else if (/(abs|txt|smallstory|ticker).*\.html$/.test(href)) {
      var baseuri = href;
      if (href.lastIndexOf("ticker") > -1 || 
          href.lastIndexOf("smallstory") > -1) {
         baseuri = baseuri.substring(0, baseuri.lastIndexOf(SLASH));
      }
      baseuri = baseuri.substring(0, baseuri.lastIndexOf(SLASH));
      location.replace(baseuri + SLASH + prefix + encodeURIComponent(href));
   }

   return;
}

function zoom(img, w, h) {
   window.open(img, 'zoom'+w+h, 'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width='+w+',height='+h);
}

function ivwTag(suffix, tailing) {
   var server = 'http://orf.oewabox.at/cgi-bin/ivw/CP/';
   var OEWA = server + suffix;
   var pos = document.location.href.indexOf("-");
   if (pos > -1) {
      var idstrg = document.location.href.substring(pos+1);
      var storytype = document.location.href;
      storytype = storytype.substring(storytype.length - 8);
      pos = idstrg.indexOf("/");
      idstrg = idstrg.substring(0,pos);
      OEWA += idstrg;
      if (tailing) {
         OEWA += tailing;
      } else if (storytype == 'txt.html') {
         OEWA += '/sub';
      }
   } else {
      OEWA += 'unspecified';
      if (tailing) OEWA+=tailing;
   }
   var ref = escape(document.referrer);
   if (ref.length > 1) {
      OEWA += '?r=' + ref;
   }
   document.write('<img src="' + OEWA + '" height="1" width="1" border="0">');
}

function renderDate() {
   var now = new Date();
   var date = now.getDate();
   if (date < 10) {
      document.write("0");
   }
   document.write(date);
   document.write(".");
   var month = now.getMonth() + 1;
   if (month < 10) {
      document.write ("0");
   }
   document.write(month);
   document.write(".");
   document.write(now.getFullYear());
   return;
}

