// This file contains functions used by both the player and the Display page
// or the display page only

var splitDomain = document.domain.split(".");
if (splitDomain.length > 1) {
    document.domain = splitDomain[splitDomain.length - 2] + "." + splitDomain[splitDomain.length - 1]
}
//alert(document.domain);

// Vars on both player and display
var islocal=true;
// have we got a child, to prevent some weird ie errors trying to look for children straight away
// also doubles as a islocal=false flag for the display pages
var hasRegistered=false;


// CODE FOR DISPLAY PAGES ONLY

// Finding player vars
var trys=0;

// Entry point for display page
// USER
function playerTrigger(station, stations, starthd){
        trys=0;
        islocal=false;
	//alert(starthd);
        findPlayer(station, stations, starthd);
}

var playerlink='Player not present<br/><a href="/?player=true">Create player</a><br/><br/>';

function findPlayer(station, stations, starthd) {
	var playerrlink='Player not loaded<br/><a href="/modules/mod_tunein/mod_tunein_hid.php" target="playerframe" onclick="playerTrigger(\'' + station + '\',\'' + stations + '\');">Reload player</a><br/><br/>';
			
    if ((parent==null) || (parent.playerframe==null))
    {
            //alert("No parent - click to open");
            //alert("No player frame - click to open");
            updateElement("playerstate",playerlink);
            return false;
    }
    else
    if (parent.playerframe.playerEntry==null)
    {
            trys++;
            //alert("No player function - wait for it to load?");
            updateElement("playerstate",playerrlink);
            if (trys<10)
            {
                    setTimeout("findPlayer('" + station + "','" + stations + "'," + starthd + ")",2000);
            }
            return false;
    }
    else
    {
		if (parent.document.title!=null)
		{
			parent.document.title=document.title;
		}
            //alert("Its there");
            updateElement("playerstate","Player is ready");
            if (document.getElementById("playerstate")!=null)
            {
                    document.getElementById("playerstate").style.display="none";
            }
            if (parent.playerframe.playerEntry!=null)
            {
                    parent.playerframe.playerEntry(station, false, stations, starthd);
            }
            var elem = document.getElementById( "playercontrols" );
            if (elem != null)
            {
                    if (elem.style != null)
                    {
                            elem.style.display="block";
                    }
            }
            return true;
    }
}

function unloadFunc()
{
	alert("Unload");
	passItOn('ReAddTopBit');
}

// SHARED FUNCTIONS
// These can be used from a display page or the player

// passItOn allows a player function to be called from either location
// USER
function passItOn(whattocall, param1, param2, param3) {
	var callOn=this;
	if (!islocal)
	{
        if ((parent==null) || (parent.playerframe==null))
        {
                return false;
        }
        else
        if (parent.playerframe[whattocall]==null)
        {
                return false;
        }
        else
        {
	        callOn=parent.playerframe;
		}
	}
	
    if (param1==null)
    {
        callOn[whattocall]();
        return true;
    }
    else
    {
        if (param2==null)
        {
            callOn[whattocall](param1);
            return true;
        }
        else
        if (param3==null)
        {
            callOn[whattocall](param1,param2);
            return true;
        }
        else
        {
            callOn[whattocall](param1,param2,param3);
            return true;
        }
    }
}

//USER
function SwitchLocal()
{
	var chosen = "";

	if(!document.f1)
		return;
	//	alert("Can't find it 1");
	if(!document.f1.r1)
		return;
	//	alert("Can't find it 2");

	var radioLength = document.f1.r1.length;
	if(radioLength == undefined)
	{
		radioLength=0;

		if(document.f1.r1.checked)
			chosen=radioObj.value;
	}

	for (i = 0; i <radioLength; i++) {
		if (document.f1.r1[i].checked) {
			chosen = document.f1.r1[i].value;
		}
	}
	
	if (chosen == "") {
		//alert("No Location Chosen");
	}
	else {
		if(chosen=="Stop")
		{
			passItOn('stopSched');
		}
		else
		{
			SplitName = chosen.split(",")
			//alert("Station: " + SplitName[0] + " JB: " + SplitName[1]);
			var isjb=false;
			var ishd=false;
			if (SplitName[1]=="true")
				isjb=true;
			if (SplitName[2]=="true")
				ishd=true;
			passItOn("switchStation",SplitName[0],isjb,ishd);
		}
	}
}

//--------------------------------------
// Visual helpers
//--------------------------------------

var windowSize=0;

function hideShowWindow(size) {
        if (parent==null)
        {
                return false;
        }
        else
        if (parent.document.getElementById("playerframeset")==null)
        {
                return false;
        }
        else
        {
	        if (size==null)
	        {
		        if (windowSize==0)
			        windowSize=60;
		        else
		        	windowSize=0;
	        }
	        else
	        	windowSize=size;
        	parent.document.getElementById("playerframeset").rows=windowSize + ',*';
	        if (windowSize==0)
	        	updateElement("showlink","Show");
	        else
	        	updateElement("showlink","Hide");
	        if (windowSize==0)
	        	updateElement("toplink","Show Player");
	        else
	        	updateElement("toplink","Hide Player");
	}	
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,status=no,menubar=0,resizable=0,width=500,height=330');");
}

//--------------------------------------
// Element helpers
//--------------------------------------

function updateElement(htmlelement, theval, where)
{
	if (where==null)
		where=document;

	if (where.getElementById(htmlelement)!=null)	
	{
		if (where.getElementById(htmlelement).innerHTML!=null)
		{
			//alert("ele " + htmlelement + " val " + theval);
			where.getElementById(htmlelement).innerHTML=theval;
		}
		if (where.getElementById(htmlelement+"con")!=null)
		if (theval=="")
		{
			where.getElementById(htmlelement+"con").style.display="none";
		}
		else
		{
			where.getElementById(htmlelement+"con").style.display="inline";
		}
	}

	if (hasRegistered)
	if (parent!=null)
        if (parent.mainframe!=null)
	{
        	if (false)//(parent.mainframe.updateElement!=null)
		{
			if (parent.mainframe.updateElement!=updateElement)
			{
				parent.mainframe.updateElement(htmlelement, theval);
			}
		}
		else
		{
			try {
			if (parent.mainframe.document!=null)
				if (parent.mainframe.document!=where)
					updateElement(htmlelement, theval, parent.mainframe.document);
			}
			catch (err)
			{
				//nothing
			}
		}
	}
}

function updateLink(htmlelement, theval, where)
{
	if (where==null)
		where=document;

        if (theval!="")
        {
                if (where.getElementById(htmlelement)!=null)
                {
	                if (where.getElementById(htmlelement).href!=null)
    	            {
                        where.getElementById(htmlelement).href=theval;
                    }
                }
		if (hasRegistered)
		if (parent!=null)
	        if (parent.mainframe!=null)
		{
		        if (false)//(parent.mainframe.updateLink!=null)
			{
				if (parent.mainframe.updateLink!=updateLink)
				{
					parent.mainframe.updateLink(htmlelement, theval);
				}
			}
			else
			{
				try {
				if (parent.mainframe.document!=null)
					if (parent.mainframe.document!=where)
						updateLink(htmlelement, theval, parent.mainframe.document);
				}
				catch (err)
				{
					//nothing
				}
			}
		}
        }
}

function updatePic(htmlelement, theval)
{
        if (theval!="")
        {
                if (document.getElementById(htmlelement)!=null)
                {
	                if (document.getElementById(htmlelement).src!=theval)
	                {
                        document.getElementById(htmlelement).src=theval;
                    }
                }
		if (hasRegistered)
		if (parent!=null)
	        if (parent.mainframe!=null)
	        if (parent.mainframe.updatePic!=null)
		{
			if (parent.mainframe.updatePic!=updatePic)
			{
				parent.mainframe.updatePic(htmlelement, theval);
			}
		}
        }
}


