
// +/- Open/Close element
function openClose(element) {
  e = $(element);
  if (e.className == "" || e.className == "opcls-closed") {
    e.className = "opcls-open";
  } else {
    e.className = "opcls-closed";
  }
  return false;
}

// Popup window helper
function popNewWindow(page,name,w,h,scroll) {
  var settings = 'height='+h+',width='+w+',resizable,scrollbars='+scroll;
  var win = window.open(page,name,settings);
  if (win.window.focus) win.window.focus();
  return false;
}

// Print out date for header & EPG
function headerDate(date) {
  var days = ["Sunnuntai", "Maanantai", "Tiistai", "Keskiviikko",
              "Torstai", "Perjantai", "Lauantai"];
  document.write(
    days[date.getDay()] + " " + date.getDate() + "." + (date.getMonth()+1) + ". <span class='divd'>|</span> "
    + (date.getHours() < 10 ? "0" + date.getHours() : date.getHours())
    + "." + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
  );
}

// Print out date revamped EPG
function epgHeaderDate(date) {
  document.write(
    "<strong>"
    + (date.getHours() < 10 ? "0" + date.getHours() : date.getHours())
    + ":" + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
    + "</strong> | "
    + date.getDate() + "." + (date.getMonth()+1) + "." + (date.getYear())
  );
}

////////////////////////////////////////////////////////////////////
// Resize an image to fit within / fill a bounding box element.
//  - Assumes the image is always larger than the parent.
//  - Attempts to center the image in the bounding box according to
//    the verti- and horizCenter parameters. DOES NOT properly
//    check for bounds in all scenarios.
////////////////////////////////////////////////////////////////////
function resizeImage(image, vertiCenter, horizCenter) {
  
  if (typeof vertiCenter == "undefined") vertiCenter = 0.3;
  if (typeof horizCenter == "undefined") horizCenter = 0.5;
  
  var factor = null;
  var imgWidth = image.clientWidth;
  var imgHeight = image.clientHeight;
  var parentWidth = image.parentNode.clientWidth;
  var parentHeight = image.parentNode.clientHeight;
  
  if ( imgWidth < imgHeight ) {
    factor = parentWidth / imgWidth;
    if ( imgHeight * factor < parentHeight ) {
      factor = parentHeight / imgHeight;
    }
  } else {
    factor = parentHeight / imgHeight;
    if ( imgWidth * factor < parentWidth ) {
      factor = parentWidth / imgWidth;
    }
  }
  
  image.width = imgWidth * factor;
  image.height = imgHeight * factor;
  
  if (image.height != parentHeight) {
    var upperHalf = image.height * vertiCenter;
    var topOffset= 0;
    topOffset = (parentHeight/2 - upperHalf);
    if (topOffset < 0) {
      image.style.top = (topOffset) + "px";
    }
  }
  
  if (image.width != parentWidth) {
    var leftHalf = image.width * horizCenter;
    image.style.left = (parentWidth/2 - leftHalf) + "px";
  }
  
  image.className = "loaded";
  
}

/////////////////////////////////////////////////////
// Favorite and highlight stuff
/////////////////////////////////////////////////////

function getElement(eid, from)
{
  if (typeof(from) == 'undefined')
    from = document;

  if (from.all)
    return from.all[eid];
  else if (from.getElementById)
  
    return from.getElementById(eid);
}

function user_highlight()
{
  return typeof(hlmap) != 'undefined' && hlmap.length > 0;
}

// The actual highlighting function
function h(eid, text)
{
  var match = hlmap[text];
  if (match != null) {
    getElement(eid).className += " pghl-" + match;
  }
}

// Checks is this is a fav or not
function isFav(text) {
  
  // Un- and re-escape the chars as escaped in
  // the actual fav data structure (/js/highlight.js),
  // in accordance with JSTL escapeXml.
  //text = encodeURI(text);
  text = decodeURIComponent(text);
  if (text.indexOf(";") == -1) {
  text = text.replace(/&/g,  "&amp;");
  }
  text = text.replace(/</g,  "&lt;");
  text = text.replace(/>/g,  "&gt;");
  text = text.replace(/"/g, "&#034;");
  text = text.replace(/'/g,  "&#039;");

  if (hlmap != null) {
    var match = hlmap[text];
  }
  if (match != null) {
    // DEBUG alert("Hit for:" + text + " (re-escaped)");
    return true;
  } else {
    // DEBUG alert("Miss for:" + text + " (re-escaped)");
    return false;
  }
}

function  addRemFavtwo(text,id) {

	  if (! isFav(text)) {
	    popNewWindow("/settings/highlight/?ac=edit&text=" + text, "newFav", 500, 250, 0);
	  } else {
	    popNewWindow("/settings/highlight/?ac=remove&fromlink=true&okey=" + text, "remFav", 500, 250, 0);
	  }
  }
// Opens the addition / removal popup
function  addRemFav(text,id) {
  // DEBUG alert(text); alert(unescape(text));
var xmlhttp;

if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
	  if (! isFav(text)) {
	    popNewWindow("/settings/highlight/?ac=edit&text=" + text, "newFav", 500, 250, 0);
	  } else {
	    popNewWindow("/settings/highlight/?ac=remove&fromlink=true&okey=" + text, "remFav", 500, 250, 0);
	  }
  }
if (loggedInUser != null) {
if (! isFav(text)) {

    xmlhttp.open("GET","/settings/highlight/?ac=save&include=true&nkey=" + text + "&value=1&okey=&targetelement=tooltip-content" + id,true);
} else {
    xmlhttp.open("GET","/settings/highlight/?ac=remove&include=true&fromlink=true&okey=" + text + "&targetelement=tooltip-content" + id,true);
    
}
} else { 
	if (navigator.appName.indexOf('Explorer') != -1)     
	{
		xmlhttp.open("GET","/login.jsp?returnpage=/settings/highlight/&ac=save&include=true&nkey=" + decodeURIComponent(text) + "&okey=&targetelement=tooltip-content" + id,true);
	} else {
		xmlhttp.open("GET","/login.jsp?returnpage=/settings/highlight/&ac=save&include=true&nkey=" + text + "&okey=&targetelement=tooltip-content" + id,true);		
	}

	
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  document.getElementById('tooltip-content' + id).innerHTML=xmlhttp.responseText;
  if (xmlhttp.responseText.match("tallennettu") != null) {
  	setTimeout('window.location.reload();', 500);
  	}
  }
}

xmlhttp.send(null);
  // DEBUG alert(text); alert(unescape(text));
  //lightbox.prototype.deactivate();
  return false;
}
 
function addRemFavLink(text, id, lbox) {
  var link = "";

  if (! isFav(text)) {  
    link = "<a href=\"javascript:void(0);\" class=\"icn-suosikit\" ";
  } else {
	    link = "<a href=\"javascript:void(0);\" class=\"icn-suosikit-rem\" ";	  
  }
    link += "onclick=\"addRemFav(&quot;" + text + "&quot;,&quot;" + id + "&quot;);\"><span>";
    if (! isFav(text)) {
      link += "LIS&Auml;&Auml; SUOSIKKEIHIN";
    } else {
      link += "POISTA SUOSIKEISTA";
    }
    link += "</span></a>";
   
  return link;
}

function addRemFavLinkpop(text, id, lbox) {
  var link = "";
  if (loggedInUser != null) {
    link = "<a href=\"javascript:void(0);\" class=\"icn-suosikit\" ";
    link += "onclick=\"addRemFavtwo(&quot;" + text + "&quot;,&quot;" + id + "&quot;);\"><span>";
    if (! isFav(text)) {
      link += "LIS&Auml;&Auml; SUOSIKKEIHIN";
    } else {
      link += "POISTA SUOSIKEISTA";
    }
    link += "</span></a>";
   }
  return link;
}

// Prints the addition / removal link
function addRemFavLinkTwo(text, id, lbox) {
  var link = "";

if (loggedInUser != null) {    
    if (! isFav(text)) {
    	link = "<a href=\"javascript:void(0);\" title=\"Lis&auml;&auml; &quot;" + text + "&quot; suosikeihin\" ";
//    	link += "onclick=\"addRemFav(&quot;" + encodeURI(text) + "&quot;);\"><img src=\"/images/icons/icon_favourite.gif\" border=\"0\">";
    	link += "onclick=\"showFavbox(event);ajaxFunction(&quot;/settings/highlight/?ac=save&include=true&nkey=" + text.replace(/&amp;/g,  '%26') + "&value=1&okey=&targetelement=tooltip-contentgeneral&quot;,&quot;tooltip-contentgeneral&quot;,&quot;GET&quot;,&quot;&quot;,&quot;&quot;);\"><img src=\"/images/icons/icon_favourite.gif\" border=\"0\">";
    } else {
    	link = "<a href=\"javascript:void(0);\" title=\"Poista &quot;" + text + "&quot; suosikeista\" ";
    	link += "onclick=\"showFavbox(event);ajaxFunction(&quot;/settings/highlight/?ac=remove&include=true&fromlink=true&okey=" + text.replace(/&amp;/g,  '%26') + "&targetelement=tooltip-contentgeneral&quot;,&quot;tooltip-contentgeneral&quot;,&quot;GET&quot;,&quot;&quot;,&quot;&quot;);\"><img src=\"/images/tvopas/icon_remfav.gif\" border=\"0\">";
    }
    link += "</a>";
 } else {
 }
  return link;
}
function showFavbox(event)

{

if (!event) var event = window.event;
var posx = 0;
var posy = 0;
var bottomedge = document.documentElement.scrollTop + screen.availHeight - 330;
if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    bottomedge = window.innerHeight + document.documentElement.scrollTop - 230;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    bottomedge = document.documentElement.scrollTop + document.documentElement.clientHeight - 230;
  } 
if (event.pageX || event.pageY)
{
	posx = event.pageX - 430;
	posy = event.pageY;


}
else if (event.clientX || event.clientY)
{

posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - 430;
posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;


}
// posx and posy contain
// the mouse position relative to the document
// Do something with this information

	if( bottomedge < posy) {
	document.getElementById('addfav').style.top = posy - 210 + 'px';
	} else {
	document.getElementById('addfav').style.top = posy + 'px';
	}
document.getElementById('addfav').style.left = posx + 'px';
document.getElementById('addfav').style.visibility = 'visible';

return;
}

// Escape the program name for transport, decodes possible entities present
function favUrlEscape(text) { return (escape(text.unescapeHTML())).replace("+", "%2b"); }

// Shows an alert if you're adding favs without a login
function toggleFavRegInfo(showHide) {
  var nfoBox = $("lbox-favregnfo");
  var ad = $("ad-suurtaulu-lbox");
  if (showHide == "show") {
    ad.style.display = "none";
    nfoBox.style.display = "block";
  } else {
    nfoBox.style.display = "none";
    ad.style.display = "block";
  }
}

/////////////////////////////////////////////////////
// Favorite and highlight stuff ends
/////////////////////////////////////////////////////