//Cross platform addEventListener
function addEvent(object, eventType, eventHandlerMethod){
  //DOM
	if(object.addEventListener){
	  object.addEventListener(eventType, eventHandlerMethod, false);
	}
	//IE6 creat an "onEvent" type and attach it
	else if(object.attachEvent){
	   object.attachEvent("on" + eventType, eventHandlerMethod);
	}
	//IE 5
	else
	   object["on" + eventType] = eventHandlerMethod;
}

//function to slowly fade the opacity of an image over specified duration.
//Requires the id of the object, the startOpacity, endOpacity and the duration 
//of the transition
function fadeOpacity(id, startOpacity, endOpacity, duration){
   var speed = Math.round(duration / 100);
   var timer = 0;
   if(endOpacity < startOpacity){
      for(var j=startOpacity; j > endOpacity; j--){
          setTimeout("changeOpacity(" + j + ",'" + id + "')",(timer * speed));
          timer++;
		  }
   }
   if(endOpacity > startOpacity){
      for(var j=startOpacity; j < endOpacity; j++){
          setTimeout("changeOpacity(" + j + ",'" + id + "')",(timer * speed));
          timer++;
      }
   }
}


//Blends one image into another.  Requires the image be in an div the exact same 
//size of the two images being blended.  Blends based on the id's of both the image
//and the div
function blendImage(divId, imageId, imageFile, duration){
   //document.getElementById(divId).style.width = document.getElementById(imageId).style.naturalWidth;
   document.getElementById(divId).style.backgroundImage = 'url(' + document.getElementById(imageId).src + ')';
   changeOpacity(0, imageId);
	 //setTimeout("pauseScript()",1000);
   setTimeout('document.getElementById("' + imageId + '").src = "' + imageFile + '"', 5);
   setTimeout('fadeOpacity("' + imageId + '", 0, 100, ' + duration +')', 100);
	 setTimeout('document.getElementById("' + divId + '").style.backgroundImage = "url()"', duration + 200);
}

//Changes the opacity of an image based on its ID and the supplied opacity argument
function changeOpacity(opacity, id){
   var image = document.getElementById(id).style;
   image.opacity = (opacity / 100);
   image.filter = 'alpha(opacity = ' + opacity + ')';
   image.MozOpacity = (opacity / 100);
   image.KhtmlOpacity = (opacity / 100); 
}

//cross platform get current Node
function getCurrentNode(event){
   if (event.target == null)
	    return window.event.srcElement;  //IE
	 else
	    return event.target; //DOM
}

function getRelatedTarget(e){
   if(e.relatedTarget)
	    return e.relatedTarget;
	 else
	    return e.toElement;
}

function getPreviousSibling(e){
   var previousSibling = e.previousSibling
	 while(previousSibling.nodeName == "#text" || previousSibling.nodeName == "BR")
	    previousSibling = previousSibling.previousSibling;
	 return previousSibling;
}

addEvent(window, "load", initializePage);

function changeColor(id, color){
  document.getElementById("liveOnAirHeadingText").style.color = "#" + color;
}

function decToHex(num){
   if(!isNaN(num)){
	    hex = (num-0).toString(16);
			return hex;
	 }
	 else 
	    return NaN;
}

function leadingZeros(number, numberOfZeros){
   for(var i=0; i < numberOfZeros; i++)
	    number = "0" + number;
	 return number;
}

function fadeColor(id, beginColor, endColor, duration){
   var speed = Math.round(duration / 100);
   var beginRedRGB = parseInt("0x" + beginColor.substr(0,2));
	 var beginGreenRGB = parseInt("0x" + beginColor.substr(2,2));
	 var beginBlueRGB = parseInt("0x" + beginColor.substr(4,2));
   var endRedRGB = parseInt("0x" + endColor.substr(0,2));
	 var endGreenRGB = parseInt("0x" + endColor.substr(2,2));
	 var endBlueRGB = parseInt("0x" + endColor.substr(4,2));
	 var step = 100;
   var timer = 0;
	 
	 //add in to string function to handle leading zeros
	 for(var i=0; i < step; i++){
	    var red = decToHex(Math.floor(beginRedRGB * ((step-i)/step) + endRedRGB * (i/step))); 
			var green = decToHex(Math.floor(beginGreenRGB * ((step-i)/step) + endGreenRGB * (i/step)));
			var blue = decToHex(Math.floor(beginBlueRGB * ((step-i)/step) + endBlueRGB * (i/step)));
			red = red.toString();
			green = green.toString();
			blue = blue.toString();
			
			if(red.length == 1){
			   red = leadingZeros(red, 1);
			}
			if(green.length == 1)
			   green = leadingZeros(green, 1);
			if(blue.length == 1)
			   blue = leadingZeros(blue, 1);
			color = red + green + blue;
			
			setTimeout("changeColor('" + id + "','" + color + "')", (timer*speed));
			timer++;
	 }
}


function onAirHeadingFadeOut(e){
	 fadeColor("liveOnAirHeadingText", "ff0000", "770000", 500);
	 setTimeout("onAirHeadingFadeIn()", 500);
}

function onAirHeadingFadeIn(){
   fadeColor("liveOnAirHeadingText", "770000", "ff0000", 500);
	 setTimeout("onAirHeadingFadeOut()", 500);
}

function buttonMouseOver(e){
   currentNode = getCurrentNode(e);
	 currentNode.style.color = "#ff0000";
}

function buttonMouseOut(e){
   currentNode = getCurrentNode(e);
	 currentNode.style.color = "#ffffff";
}

function initializePage(){
   onAirHeadingFadeIn();
   addEvent(document.getElementById("buttonHighQuality"), "mouseover", buttonMouseOver);
   addEvent(document.getElementById("buttonLowQuality"), "mouseout", buttonMouseOut);
   addEvent(document.getElementById("buttonLowQuality"), "mouseover", buttonMouseOver);
   addEvent(document.getElementById("buttonHighQuality"), "mouseout", buttonMouseOut);
	 //addEvent(document.getElementById("donateButton"), "mouseover", buttonMouseOver);
   //addEvent(document.getElementById("donateButton"), "mouseout", buttonMouseOut);
   scrollUp(document.getElementById("weatherViewer").parentNode.clientHeight, "weatherViewer");
	 //scrollUp(document.getElementById("passReportViewer").parentNode.clientHeight, "passReportViewer");
	 scrollUp(document.getElementById("localNewsViewer").parentNode.clientHeight, "localNewsViewer");
	 scrollUp(document.getElementById("localSportsViewer").parentNode.clientHeight, "localSportsViewer");
	 document.getElementById("scrollAirText").style.marginLeft = document.getElementById("scrollAirText").parentNode.clientWidth + "px";
	 scrollLeft(240, "scrollAirText", document.getElementById("scrollAirText").parentNode.scrollWidth);
	 addEvent(document.getElementById("weatherViewer").parentNode, "mouseover", defaultCursor);
	 addEvent(document.getElementById("weatherViewer").parentNode, "mouseout", resetCursor);
	 addEvent(document.getElementById("localNewsViewer").parentNode, "mouseover", defaultCursor);
	 addEvent(document.getElementById("localNewsViewer").parentNode, "mouseout", resetCursor);
	 addEvent(document.getElementById("localSportsViewer").parentNode, "mouseover", defaultCursor);
	 addEvent(document.getElementById("localSportsViewer").parentNode, "mouseout", resetCursor);
	 //addEvent(document.getElementById("passReportViewer").parentNode, "mouseover", defaultCursor);
	 //addEvent(document.getElementById("passReportViewer").parentNode, "mouseout", resetCursor);
	 
}

function scrollUp($margin, $objectName){
   var marg = $margin;
	 var object = document.getElementById($objectName);
	 //alert (object.parentNode.style.cursor);
	 if(object.parentNode.style.cursor != "default"){
      object.style.marginTop = marg + "px";
	    marg--;
	 }
	 if(object.style.marginTop == 0 - object.scrollHeight -30 + "px"){
			marg = object.parentNode.clientHeight;
	 }
	 setTimeout("scrollUp(" + marg + ",\"" + $objectName + "\")", 30);	 
}


//changes the style on the cursor of the event handler object to default.  This
//function is not reusuable with out an over parent object with the class barViewer.
//this is because IE does not support the "this" object in reference to the object
//the event handler is tied to.
function defaultCursor(e){
   currentNode = getCurrentNode(e);
	 
	 //checks if the currentnode is the object the event handler is tied to.
	 if(currentNode != this && currentNode.className != "barViewer"){
	    parentNode = currentNode;
			
			//retrieves the parent nodes until it reaches the object the event handler is 
			//tied to
	    while(parentNode != this && parentNode.className != "barViewer")
			   parentNode = parentNode.parentNode;

			parentNode.style.cursor = "default";
	 }
	 
	 //the currentNode is the object the event handler is tied to
	 else
	    currentNode.style.cursor = "default";
}

//changes tye sytle on the cursor of the event handler object to an empty string.
//This function is not reusuable with out an over parent object with the class barViewer.
//this is because IE does not support the "this" object in reference to the object
//the event handler is tied to.
function resetCursor(e){
   currentNode = getCurrentNode(e);
	 relTarg = getRelatedTarget(e);
	 parentNode = relTarg;
	 
	 //retrieves the parentNode until it reaches the body tag or the object the 
	 //event handler is tied to.  checking to see if the related target is a child
	 //of the object the event handler is tied to
	 while(parentNode.nodeName != "BODY" && (parentNode != this || parentNode.className != "barViewer"))
			parentNode = parentNode.parentNode;
			
   //If the related target is not a child of the object the event handler is tied to
	 //changes the .style.cursor attribute of the event handler object back to an 
	 //empty string.  checks to make sure an mouseOut event was not generated by 
	 //entering a child object
	 if(parentNode != this || parentNode.className != "barViewer"){
	 
	    //if the browser is IE, we need to retrieve the object the event handler 
			//is tied to, and it should be an ancestor of the object geneating the event
			if(window.event){
				 while(currentNode.className != "barViewer")
						currentNode = currentNode.parentNode;
						
				 currentNode.style.cursor = "";
		  }
			
			//if not IE, we can directly reference the object of the event handler with
			//"this"
			else
			   this.style.cursor = "";
   }
}

function scrollLeft($margin, $objectName, $scrollWidth){
	 var object = document.getElementById($objectName);
	 object.style.marginLeft = $margin + "px";
	 $margin = $margin - 2;
	 if(parseInt(object.style.marginLeft) <= 0 - $scrollWidth){
			$margin = object.parentNode.scrollWidth;
	 }
	 setTimeout("scrollLeft(" + $margin + ",\"" + $objectName + "\"," + $scrollWidth + ")", 45);	 
}



function poptastic(url)
{
newwindow=window.open(url,'name','location=no, menubar=no, resizable=yes,scrollbars=no, status=no, titlebar=no, toolbar=no');	if (window.focus) {newwindow.focus()}
}

