/***************************************************************
*  Copyright notice
*  
*  (c) 2006 kaliber5 GmbH, Simon Ihmig (ihmig@kaliber5.de)
*  All rights reserved
*
***************************************************************/


	
function update(force) {
  window.clearInterval(updInt);
	callFunction("update",update.arguments);
	}
	

	
function updateFE(data) {
	if (!data["error"]) {
    if (data["benchmark"])
	    document.getElementById("debug").innerHTML = "Debug: " + data["benchmark"];

    if (data["reset"]) {
		//window.location.reload();
		clearEvents();
		e = new Array();
		//return;
		}					
	
  	attributes = data["attributes"];
  	if (attributes && typeof(attributes) == 'object') {
    	for( var i in attributes )
    	  setAttribute(i,attributes[i]);
  		}	
		
		if (window.inoutHook) { 
  		inoutArray = data["inout"];
  		if (inoutArray && typeof(inoutArray) == 'object') {
      	for( var i in inoutArray )
      	  inoutHook(i,inoutArray[i]);
    		}	
  		}
		
		evts = data["events"];
		if (evts) {
		  clearEvents();
			e = e.concat(evts);
		  e.sort(eventSort);
			for( var i = 0 ; i < e.length ; i++ ) 
  			insertEvent(e[i]);
			}
  	
  	playtime = parseInt(data["playtime"]);
		timerRunning = data["timerRunning"];
		tstamp = data["tstamp"];

		showTimer();
		controlTimer();
		
		window.clearInterval(updInt);
	  if (data["finished"]) {
			if (window.finishHook)
			  finishHook();
			}
		else 		
	    updInt = window.setInterval("update()",(isNaN(updateInterval)) ? 5000 : updateInterval);
		}
	else alert(data["errorMsg"]);
	}

function eventSort(a, b) {
  return b["id"]-a["id"];
	
	}

function clearEvents() {
  if (body = document.getElementById("events.body")) {
		while (body.hasChildNodes()) {
		  body.removeChild(body.firstChild);
			
			}
		}
	}
	
function insertEvent(evt) {
  if (document.getElementById("event."+evt["id"]))
	  return;

  if (body = document.getElementById("events.body")) {
		
		if (window.insertEventHook)
		  obj = insertEventHook(evt);
		else {
		  cnt = body.childNodes.length;
			
  		obj = document.createElement("tr");
  		obj.setAttribute("id","event."+evt["id"]);
  		obj.className = cnt%2 ? "even" : "odd";
  		
  		td1 = document.createElement("td");
  		if (evt["playtime"])
  		  td1.appendChild(document.createTextNode(Math.ceil(evt["playtime"]/60) + "'"));
  		td1.className = "timer";
  		
  		td2 = document.createElement("td");
      td2.appendChild(document.createTextNode(evt["text"]));
  		td2.className = "text";
  		
  		obj.appendChild(td1);
  		obj.appendChild(td2);
  		}
		if (obj)	
		  body.appendChild(obj);
		}
	}
	
function controlTimer() {
  if (timerRunning) { 
	  window.clearInterval(interval);
	  interval = window.setInterval("updateTimer()",1000); }
	else {
	  window.clearInterval(interval);
		}
	}
	
function updateTimer() {
  playtime = playtime + 1;
	showTimer();
	}	
	
function showTimer() {
  time = Math.floor(playtime/60) + "'" + (playtime%60) + '"';
  if (t = document.getElementsByName("ctl.timer")) {
	  t[0].firstChild.nodeValue = time;
		t[0].className = timerRunning ? "run" : "stop";
		}
	}
	
function init() {
  if (window.initHook) 
	  initHook(); 
  update(true);
  controlTimer();
	
	if (window.inoutHook) {
  	for (var i=0 ; i<inout.length ; i++ )
	    inoutHook(i,inout[i]);
		}
	}	
	
function setAttribute(attr,val) {
  tags = document.getElementsByName(attr);
	for( var i = 0 ; i < tags.length ; i++ ) 
	  tags[i].firstChild.nodeValue = val;
	}	
	
function getXmlHttpRequestObject()
{
  var xmlHttpRequest = false;

  if ( window.XMLHttpRequest )
  {
     try
     {
        xmlHttpRequest = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlHttpRequest = false;
     }
  }
  else if ( window.ActiveXObject )
  {
     try
     {
        xmlHttpRequest = new ActiveXObject( "Msxml2.XMLHTTP" );
     }
     catch (e)
     {
        try
        {
           xmlHttpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
        }
        catch (e)
        {
           xmlHttpRequest = false;
        }
     }
  }

  // Override mime type if browser supports it (like Mozilla)
  if ( xmlHttpRequest.overrideMimeType )
  {
     xmlHttpRequest.overrideMimeType( "text/xml" );
  }

  return xmlHttpRequest;
}

function prepareXmlHttpRequest( xmlHttpRequest, func, params ) {
	
	data = Array();
	data.push(tstamp);
  for( var i = 0; i < params.length ; i++ )
    data.push( params[ i ] );
		
  req = handler + "?ticker=" + ticker + "&func=" + func + "&lang=" + lang + "&ajax=" + escape( JSON.stringify( data ) );

  xmlHttpRequest.open( "GET", req, true );

  xmlHttpRequest.onreadystatechange = function()
  {
     if ( xmlHttpRequest.readyState == 4 )
     {
        try
        {
           var response = JSON.parse( xmlHttpRequest.responseText );
        }
        catch (e)
        {
           //alert( "AJAX error:\nUnknown response from server!\n\n" + e["message"] + ": " + xmlHttpRequest.responseText);
           return;
        }

        updateFE( response );

     }
  }

}	 

function callFunction( func, params )
{
  if (xmlHttpRequest = getXmlHttpRequestObject()) {
	  prepareXmlHttpRequest( xmlHttpRequest, func, params );
    xmlHttpRequest.send( null );
		}
  else {
	  alert("AJAX error:\nNo xmlHttpRequest Object available!");
	}
	
}	 	
