/**
 * OffSite Links
 *
 * Creates new windows for links which are not relative to the
 * document / server root.
 *
 * For example:
 *   http://www.google.com/ will popup in a new window
 *   /index.html will not
 */

// Once the content has loaded, initialize the script.
window.onload = init;

// Create a shorthand for the base element of the Document Object Model
var d = document;

function init()
{
	AttachToOffsiteHyperlinks();
}

/**
 *	Scans all of the (a) elements in the document and determines
 *	wether they are offsitelinks. If they are it appends the
 *	function to create a new window.
 */
function AttachToOffsiteHyperlinks()
{
	// bail out if this is an older browser
	if (!d.getElementById)
	{
		return;
	}
	
	// Fetch a list of hyperlink elements
	var links = d.getElementsByTagName("a");
	
	var i = 0;
	while(links[i])
	{
      if( links[i].href.indexOf('.asx') != -1 )
      {
      links[i].onclick = function ()
         {
            var x = screen.width / 3;
            var y = 10;
            var now = new Date();
            var features = "width=" + x + ",height=" + y + ",location=no,menubar=yes,scrollbars=yes,status=yes,toolbar=no,resizable=yes,screenX=0,screenY=0,top=0,left=0";
            var offsite = window.open(this, "Player_"+now.getTime(), features);
            window.focus();
            window.setTimeout( function() { offsite.close(); }, 2000);
            //offsite.close();
            return false;
         }
      }
      else if(links[i].href.substring(7, location.host.length + 7) != location.host && links[i].href.substring(0, 6) != 'mailto')
		{
         // Secure Site
         if (links[i].href.match( /secure\.zradio\.net/ ))
         {
         links[i].onclick = function ()
			{
				var x = screen.width;
				var y = screen.height;
            var now = new Date();
				var features = "width=" + x + ",height=" + y + ",location=no,menubar=yes,scrollbars=yes,status=yes,toolbar=no,resizable=yes,screenX=0,screenY=0,top=0,left=0";
				var offsite = window.open(this, "Offsite_"+now.getTime(), features);
				offsite.focus();
				return false;
			}
         }
         else if (links[i].href.match( /gallery\.zradio\.org/ ))
            {
            }
         // General Link
			else if (links[i].onclick == undefined)
			{
			links[i].onclick = function ()
			{
				var x = (screen.width / 2) - 353;
				var y = (screen.height / 2) - 250;
            var now = new Date();
				var features = "width=706,height=500,location=yes,menubar=yes,scrollbars=yes,status=yes,toolbar=yes,resizable=yes,screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x;
				var offsite = window.open(this, "Offsite_"+now.getTime(), features);
				offsite.focus();
				return false;
			}
			}
		}
		i += 1;
	}
}

function addLoadEvent(addFunc)
   {var preFunc=window.onload;if(typeof window.onload!='function')
   {window.onload=addFunc;}
   else
   {window.onload=function()
   {preFunc();addFunc();};}}