/*
 * (c) 2009,2010,2011 Alphons van der Heijden
 * Voor: www.Natuurkanaal.nl
 */
var CurrentUrl;
var timeID,auxID;
var refreshRate= 2000;
var pageLength = 100;
var auxKill = 10000;
var SilverPlayer;

function ObjectLoad()
{
   if (objectSource)
   {
       document.body.innerHTML = "<embed width=100% height=100% fullscreen=yes src=\"" + objectSource + "\" />";
   }
}

function SetSize() 
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement 
		&& ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	var panel1Height = myHeight -255;
	var panel2Height = myHeight -280 -235;
	if(panel2Height>0)
	{
		$get("PanelUsers").style.height=panel2Height+"px";
		if(panel1Height>0)
			$get("PanelMessages").style.height=panel1Height+"px";
	}
	if(!$get("CheckBox2").checked)
		$get("PanelMessages").scrollTop=$get("PanelMessages").scrollHeight;
}

function MediaPlayerHtml()
{
    var htmlBuilder = [];

	htmlBuilder.push('<object id="MediaPlayer" width="320" height="290" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"');
	htmlBuilder.push('	standby="Beelden worden geladen..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112">');
	
	htmlBuilder.push('<param value="" name="filename" />');
	htmlBuilder.push('<param value="" name="URL" />');
	htmlBuilder.push('<param value="True" name="AutoStart" />');
	htmlBuilder.push('<param value="True" name="ShowControls" />');
	htmlBuilder.push('<param value="True" name="ShowStatusBar" />');
	htmlBuilder.push('<param value="False" name="ShowTracker" />');
	htmlBuilder.push('<param value="False" name="ShowDisplay" />');
	htmlBuilder.push('<param value="False" name="ShowPositionControls" />');
	htmlBuilder.push('<param value="full" name="uimode" />');
	htmlBuilder.push('<param value="0" name="stretchtofit" />');
	htmlBuilder.push('<param value="1" name="enablecontextmenu" />');
	htmlBuilder.push('<param value="1" name="rate" />');
	htmlBuilder.push('<param value="0" name="balance" />');
	htmlBuilder.push('<param value="0" name="currentPosition" />');
	htmlBuilder.push('<param value="1" name="playCount" />');
	htmlBuilder.push('<param value="0" name="currentMarker" />');
	htmlBuilder.push('<param value="-1" name="invokeURLs" />');
	htmlBuilder.push('<param value="" name="baseURL" />');
	htmlBuilder.push('<param value="75" name="volume" />');
	htmlBuilder.push('<param value="0" name="mute" />');
	htmlBuilder.push('<param value="0" name="windowlessVideo" />');
	htmlBuilder.push('<param value="-1" name="enabled" />');
	htmlBuilder.push('<param value="1" name="fullScreen" />');
	htmlBuilder.push('<param value="0" name="enableErrorDialogs" />');

	htmlBuilder.push('	<embed id="MyEmbed" width="320" height="290" align="middle" showstatusbar="false" showcontrols="false"');
	htmlBuilder.push('	pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" type="application/x-mplayer2"');
	htmlBuilder.push('  autostart="true" src="" />');

    htmlBuilder.push('<\/object>');
    return htmlBuilder.join('');
}

function SilverLigthHtml(strUrl)
{
	var htmlBuilder = [];

	htmlBuilder.push('<div id="silverlightControlHost">');
    htmlBuilder.push('    <object id="silverlightControl" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">');
    htmlBuilder.push('	  <param name="enableGPUAcceleration" value="true" />');
    htmlBuilder.push('	  <param name="source" value="http://www.natuurkanaal.nl/silver/ClientBin/SilverCam.xap"/>');
	htmlBuilder.push('	  <param name="onError" value="onSilverlightError" />');
	htmlBuilder.push('	  <param name="background" value="white" />');
	htmlBuilder.push('	  <param name="Windowless" value="false" />');
	htmlBuilder.push('	  <param name="InitParams" value="url='+strUrl+'" />');
	htmlBuilder.push('	  <param name="minRuntimeVersion" value="3.0.40624.0" />');
	htmlBuilder.push('	  <param name="autoUpgrade" value="true" />');
	htmlBuilder.push('	  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">');
 	htmlBuilder.push('		  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>');
	htmlBuilder.push('	  </a>');
	htmlBuilder.push('    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>');
	return htmlBuilder.join('');
}

function SetTimer()
{
	if($get("PanelChat").style.display=="block")
		refreshRate = 2000;
	else
		refreshRate = 4000;
	timeID = window.setTimeout( "GetNewMessages()", refreshRate );
}

function GetNewMessages() 
{
   PageMethods.GetNewMessages($get("HiddenField1").value, SucMess, FailMess); 
}

function FillMessages(result)
{
	var intI,div;
	var panel = $get("PanelMessages");
	for(intI=0;intI<result.length;intI++)
	{
		if(result[intI] == "")
			continue;
		div = document.createElement("div");
		div.innerHTML = result[intI];
		panel.appendChild(div);
	}
	if(!$get("CheckBox2").checked)
	{
		while(panel.childNodes.length>pageLength)
			panel.removeChild ( panel.firstChild );
		panel.scrollTop=panel.scrollHeight;
	}		
}


function FillMessagesOld(result)
{
	var intI,div;
	var panel = $get("PanelMessages");
	for(intI=0;intI<result.length;intI++)
	{
		if(result[intI] == "")
			continue;
		div = document.createElement("div");
		div.innerHTML = result[intI];
		panel.appendChild(div);
		
		if(!$get("CheckBox2").checked)
		{
			while(panel.childNodes.length>pageLength)
				panel.removeChild ( panel.firstChild );
			panel.scrollTop=panel.scrollHeight;
		}		
	}
}


function FillAux(result)
{
	var intI,div;
	var panel = $get("PanelAux");
	window.clearTimeout(auxID);
	for(intI=0;intI<result.length;intI++)
	{
	    if (result[intI].indexOf("wav/") == 0 || result[intI].indexOf(".mp3")> 0)
		{
			PlaySound(result[intI]);
			continue;
		}
		div = document.createElement("div");
		div.innerHTML = result[intI];
		panel.appendChild(div);
	}
	auxID = window.setTimeout( "ClearAux()", auxKill );
}

function ClearAux()
{
	var panel = $get("PanelAux");
	panel.innerHTML=""; 
}

function ClearMessages()
{
	var panel = $get("PanelMessages");
	panel.innerHTML = "";
}

function FillNames(result)
{
	var intI,div;
	var panel = $get("PanelUsers");
	panel.innerHTML="";
	for(intI=0;intI<result.length;intI++)
	{
		div = document.createElement("div");
		div.innerHTML = result[intI];
		panel.appendChild(div);
	}
}
 
function SucMess(result, userContext, methodName) 
{
	SetTimer();
	if(result[0])
		FillMessages(result[0]);
	if(result[1])
		FillNames(result[1]);
	if(result[2])
		if(!$get("CheckBox3").checked)
			FillAux(result[2]);
}
 
function FailMess(error, userContext, methodName) 
{
	SetTimer();
	FillMessages([ "Er gaat iets fout." ]);
}

function PlaySound(url) {
    try {
        var control = document.getElementById("silverlightControl");
        if (control != null)
            control.Content.SilverCam.PlaySound(url);
    }
    catch (e) {
    }
}

function doColors()
{
	try
	{
		if(document.getElementById("OldScool").checked)
			document.getElementById("MyStyle").href= "css/chat1.css";
		else
			document.getElementById("MyStyle").href= "css/chat.css";
	}
	catch(e)
	{
	}
}

function Cam(intCam)
{
	try
	{
		switch(intCam)
		{
			case 0:
				CurrentUrl = url0;
				break;
			case 1:
				CurrentUrl = url1;
				break;
			case 2:
				CurrentUrl = url2;
				break;
			case 3:
				CurrentUrl = url3;
				break;
			case 4:
				CurrentUrl = url4;
				break;
			case 5:
				CurrentUrl = url5;
				break;
			case 6:
				CurrentUrl = url6;
				break;
			case 7:
				CurrentUrl = url7;
				break;
			case 8:
				CurrentUrl = url8;
				break;
            case 9:
                CurrentUrl = url9;
                break;
            default:
				break;
		}
		try
		{
			var control = document.getElementById("silverlightControl");
			if(control != null)
				control.Content.SilverCam.Url = CurrentUrl;
		}
		catch(e)
		{
		}
		try
		{
			document.getElementById("MyEmbed").src = CurrentUrl;
			document.getElementById("MyEmbed").src = "leeg.txt";
		}
		catch(e)
		{
		}
		document.getElementById("MediaPlayer").URL = CurrentUrl;
		document.getElementById("MediaPlayer").fileName = CurrentUrl;
		document.getElementById("MediaPlayer").autostart = "1";
	}
	catch(e)
	{
		//alert(""+e);
	}
}

function MediaPlayer()
{
	var cnt = document.getElementById("mediaspace");
	cnt.innerHTML = MediaPlayerHtml();
	Cam(-1);
}

function SilverLight()
{
	var cnt = document.getElementById("mediaspace");
	cnt.innerHTML = SilverLigthHtml(CurrentUrl);
}

function CamSwitch()
{
	try
	{
		if(document.getElementById("SilverOn").checked)
			MediaPlayer();
		else
			SilverLight();
	}
	catch(e)
	{
		//alert(e+"");
	}
}

function OpenSmileys()
{
	window.open('Smileys.aspx','','scrollbars=1,resizable=1,location=0,toolbar=0,status=0,width=520,height=700').focus();
}

function OpenWaarnemingen()
{
	window.open('./Waarneming/','','scrollbars=1,resizable=1,location=0,toolbar=0,status=0,width=700,height=700').focus();
}

function OpenFotos() {
    window.open('/FotoArchief/', '', 'scrollbars=1,resizable=1,location=0,toolbar=0,status=0,width=700,height=700').focus();
}

function SnapShot() {
    intI = CurrentUrl.lastIndexOf("/");
    if (intI > 0)
        strName = CurrentUrl.substring(intI);
    var left = (screen.width / 2) - 250;
    var top = (screen.height / 2) - 200;
    window.open('http://www.natuurkanaal.nl/SnapShot/Default.aspx?q=' + strName + '&u=' + $get("HiddenField1").value, '', 'scrollbars=0,resizable=1,location=0,toolbar=0,status=0,width=600,height=575, top=' + top + ', left=' + left).focus();
}

function DoInit()
{
	SetSize();
	window.onresize = SetSize;
	Cam(8);
	SilverLight();
	SetTimer();
}

