//
// Function:    dynamicPopup
// Created:     2008-12-04 by Alx Reuterskiöld
// Description: Launches page in Popup window, takes arguments from link: size and features
// Arguments:   intProductID - Product id for media file, program or series to open
//              strFolderName - The name of the media player folder to open
// Usage: javascript:dynamicPopup('http://www.ur.se/', 784, 671, 'no', 'no')

function dynamicPopup(strURL, w, h, scrollbar, resize)
{
    newWindow = window.open(strURL,'newWin',"fullscreen=no,scrollbars=" + scrollbar + ",width="+w+",height="+h+",resizable=" + resize);
    //newWindow.resizeTo(w,h);
    newWindow.focus();
}


//
// Function:    launchMediaPlayer
// Created:     2007-02-12 by Fredrik
// Modified:    2010-02-17 by Fredrik - Changed to work with new UR Play
// Description: Launches the UR media player in a popup window
// Arguments:   intProductID - Product id for media file, program or series to open
//
// Links to UR Play should go directly to the URL
// this function should not be used anymore
function launchMediaPlayer(intProductID)
{
    var strHref = "http://www.ur.se/play/"
    strHref += (intProductID == null) ? "" : intProductID;
    window.open(strHref,"_top");
}

function mouseover(strImageID, strSrc){
    if (document.getElementById(strImageID))
        document.getElementById(strImageID).src = strSrc;
}

function mouseout(strImageID, strSrc){
    if (document.getElementById(strImageID))
        document.getElementById(strImageID).src = strSrc;
}

/* Used by urtopbar search field */

function doSearch(bButton){

    var objElement = document.getElementById("seachfield");

    if (objElement != null){
        if ((escape(objElement.value) == "S%F6k%20p%E5%20UR%3As%20webbplatser") || (objElement.value == "")){
            return;
        }
        else{
            document.location = 'http://www.ur.se/Sok/?q=' + encodeURIComponentNew(objElement.value);
            return false;
        }
    }
}

function handleKeyPress(e,form){
    var key=e.keyCode || e.which;
    if (key==13){
        doSearch();
    }
}

// There were problems with encoding, therefore the functions below that does the encoding
// encodeURIComponentNew does what built-in function encodeURIComponent does, but for older browsers...

function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}            

/* END Used by urtopbar search field */

//From MB. Only needed as long as the embedded MB pages does

function pdfFonster(url,name,width,height,menubar,location,toolbar,scrollbars,resizable,directories) 
{
 win = window.open(url,'PDF','resizable=yes,menubar=no,scrollbars=yes,location=no,toolbar=no,width=700,height=500,directories=no');
 win.window.focus();
}
