/* massaudience.js -- General utility functions for all Mass Audience sites 

requires scriptaculous to work. you know it.*/

/*Which browser are we using? */
var mass_opera = typeof window.opera != 'undefined';

var mass_ie = typeof document.all != 'undefined' 
				&& !mass_opera && navigator.vendor != 'KDE';

var mass_safari = navigator.vendor == 'Apple Computer, Inc.';


/* Javascript Event Handling Functions */

/* Appends a function to the window load event */
function mass_callFunctionOnWindowLoad (designatedFunctionToCall)
{
  Event.observe(window, 'load', designatedFunctionToCall)
}

/* Appends a function to element load */
function mass_callFunctionOnElementLoad (nodeId, designatedFunctionToCall)
{
	var functArgs = arguments;
	mass_callFunctionOnWindowLoad (function ()
		{
			window.loaded = true;
		});
	var targetElement = document.getElementById (nodeId);
	if (targetElement == null && !window.loaded)
	{
		var pollingInterval = setInterval (function ()
			{
				if (window.loaded)
				{
					clearInterval (pollingInterval);
				}
				targetElement = document.getElementById (nodeId);
				if (targetElement != null)
				{
					clearInterval (pollingInterval);
					var argumentsTemp = new Array ();
					var argumentsTempLength = functArgs.length - 2;
					for (var i = 0; i < argumentsTempLength; i++)
					{
						argumentsTemp [i] = functArgs [i + 2];
					}		
					designatedFunctionToCall.apply (this, argumentsTemp);
				}
			}, 10);
	}
}

/* Attaches an event handling function to the targetElement.*/
function mass_addEventHandler (targetElement, eventType, functionToCall, bubbleUp)
{
  if (!targetElement)
  {
	  window.status = 'Hang on now: Don\'t attach an event to a null object. Jackass.';
	  return false;
  }
  if (typeof targetElement.addEventListener != 'undefined')
  {
    targetElement.addEventListener (eventType, functionToCall, bubbleUp);
  }
  else if (typeof targetElement.attachEvent != 'undefined')
  {
    targetElement.attachEvent ('on' + eventType, functionToCall);
  }
  else
  {
    eventType = 'on' + eventType;
    if (typeof targetElement [eventType] == 'function')
    {
      var oldListener = targetElement [eventType];
      targetElement [eventType] = function ()
      {
        oldListener ();
        return functionToCall ();
      }
    }
    else
    {
      targetElement [eventType] = functionToCall;
    }
  }

  return true;
}


/* Begin story ticker */

/*Story ticker variables */
var currPic=0;
var timer1, timer2;
var paused = true;
var opacity = 100;
var msDivs = new Array(5);
var msButtons = new Array(5); 
var msWrap;
var button;
var position;
var currPosition;

/* Loads story ticker components*/
function initPageComponents() {
 /*  Used to load all components on the page */
 msDivs[0] = document.getElementById('hoofdpunt1');
 msDivs[1] = document.getElementById('hoofdpunt2');
 msDivs[2] = document.getElementById('hoofdpunt3');
 msDivs[3] = document.getElementById('hoofdpunt4');
 msDivs[4] = document.getElementById('hoofdpunt5');
 msButtons[0] = document.getElementById('a1'); 
 msButtons[1] = document.getElementById('a2');
 msButtons[2] = document.getElementById('a3');
 msButtons[3] = document.getElementById('a4'); 
 msButtons[4] = document.getElementById('a5');
 msWrap = $('hoofdpuntWrap');
 initPausePlayEvents();
 paused = false; 
 timer1=setTimeout('timedStory()',7000);
 doImageSwap();
}

/*Sets up the story ticker play and pause puttons*/
function initPausePlayEvents() {
 /* checks for Javascript operability  */ 
 if (!document.getElementById || !document.getElementsByTagName) {
  return true;
 }
 /*  Grab the story ticker <a> tags */
 var topStories = $('topStories');
 var links = topStories.getElementsByTagName('a');

 for (i=0;i < links.length; i++) {
  if (links.item(i).id.substring(0,1) == 'a'){  
   //filter the links for those that have a class name beginning with 'a'
   //add the doNumber event handler for the number links
   links.item(i).href='javascript:{}';
   Element.observe(links [i], 'click', function (event)
   {
    doNumber (event);
   });
  }
 }

 var playLink = $('playLink');

 //add the doButton event handler for the play pause button 
 Event.observe ( playLink , 'click', function (event)
 {
  doButton (event);
 });
}

function fader(opac) {
 if (msWrap.style.MozOpacity!=null) {  
  // Mozilla's pre-CSS3 proprietary rule 
  msWrap.style.MozOpacity = (opac/100) - .001;
 } else if (msWrap.style.opac!=null) {
  // CSS3 compatible
  msWrap.style.opacity = (opac/100) - .001;
 } else if (msWrap.style.filter!=null) {
  // IE's proprietary filter
 if (opac==100){
  msWrap.style.filter = "none;";
 } else {
  msWrap.style.filter = "alpha(opacity="+opac+");";
      }
 }
}

function change(num, step) {
 /*fadeOut*/
 if (step == 1) {
  opacity -= 10;
  if (opacity > 0) {
   fader(opacity);
   timer2=setTimeout('change(' + num + ', 1)',50);
  }
  else {
   change(num, 2);
  }
 }
 /*change picture*/
 else if (step == 2) {
  currPic = num;
  msDivs[0].style.display = (num == 0 ? "block" : "none"); 
  msDivs[1].style.display = (num == 1 ? "block" : "none"); 
  msDivs[2].style.display = (num == 2 ? "block" : "none");
  msDivs[3].style.display = (num == 3 ? "block" : "none");
  msDivs[4].style.display = (num == 4 ? "block" : "none");
  msButtons[0].className = (num == 0 ? "on" : "off");
  msButtons[1].className = (num == 1 ? "on" : "off");
  msButtons[2].className = (num == 2 ? "on" : "off");
  msButtons[3].className = (num == 3 ? "on" : "off");
  msButtons[4].className = (num == 4 ? "on" : "off");
  change(num, 3);
 }
 /*fadeIn*/
 else if (step == 3) { 
  opacity += 10;
  if (opacity <= 100) {
   fader(opacity);
   timer2=setTimeout('change(' + num + ', 3)',50);
  }
 }
}

/* Rotate story every five seconds */
function timedStory() {
 if (currPic<4){
 currPic++;
 change(currPic, 1);
 timer1=setTimeout('timedStory()',7000);
 }else{
	 currPic=0;
	clearTimeout(timer1);
	change(currPic,1);
	paused = true;
	doImageSwap();
 }
}

/* Runs when play/pause button is pushed */
function doButton(event) {
 paused = !paused;
 doImageSwap();
 if (paused) {
  /* stop the image loop */
  clearTimeout(timer1);
 }
 else { 
  /* restart the image loop */
  timedStory();
 }
}  

/*executed when a number button is pushed */
function doNumber (event) {
 var eventSource = typeof event.target != 'undefined' ? event.target : window.event.srcElement;
 /*  get the number from the class name of the button clicked */ 
 currPic = eventSource.id.substring(1,2) - 1;
 paused = true;
 doImageSwap();
 clearTimeout(timer1);
 clearTimeout(timer2);
 change(currPic, 1);

}

/*filp from pay to pause or pause to play*/
function doImageSwap() {
		 var button = document.getElementById('playLink'); 
		 if (!mass_ie){
		  var imageFile = paused ? "http://www.theomega.ca/images/temps/start.gif" : "http://www.theomega.ca/images/temps/stop.gif";
		  button.style.background= "url("+imageFile+") 0px 0px no-repeat"; 	
		 }else{
		  /*  Use an image sprite to deplete the image flickering in IE */
		  button.style.backgroundImage= "url(http://www.theomega.ca/images/temps/startstop.gif)";
		  position = paused ? "-16 px" : "0 px";  /* change the image source */
		  try {
			  document.execCommand('BackgroundImageCache', false, true);
			} catch(e) {}
				
			  /* if paused and play is not displayed */
			  if (paused == true && currPosition != "-16 px"){
				  button.style.backgroundPositionY=position;
			  }
			  /* if playing and paused is not displayed */
			  if (paused != true && currPosition != "0 px"){
				  button.style.backgroundPositionY=position;			  
			 }  
		 currPosition = position;
		 }
}

/*load the damn thing*/
mass_callFunctionOnElementLoad('playLink', initPageComponents);

