var AXjAva_isopera = typeof window.opera != 'undefined';
var AXjAva_isie = typeof document.all != 'undefined'
&& !AXjAva_isopera && navigator.vendor != 'KDE';
var AXjAva_issafari = navigator.vendor == 'Apple Computer, Inc.';


function AXjAva_callFunctionOnWindowLoad (functionToCall){
if (typeof window.addEventListener != 'undefined'){
window.addEventListener ('load', functionToCall, false);
}else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener ('load', functionToCall, false);
}else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent ('onload', functionToCall);
}else
{
var oldFunctionToCall = window.onload;
if (typeof window.onload != 'function'){
window.onload = functionToCall;
}else
{
window.onload = function ()
{
oldFunctionToCall ();
functionToCall ();
};
}
}
}

function AXjAva_callFunctionOnElementLoad (targetId, functionToCall){
var myArguments = arguments;
AXjAva_callFunctionOnWindowLoad (function (){
window.loaded = true;
});
var targetElement = document.getElementById (targetId);
if (targetElement == null && !window.loaded){
var pollingInterval = setInterval (function (){
if (window.loaded)
{
clearInterval (pollingInterval);
}
targetElement = document.getElementById (targetId);
if (targetElement != null)
{
clearInterval (pollingInterval);
var argumentsTemp = new Array ();
var argumentsTempLength = myArguments.length - 2;
for (var i = 0; i < argumentsTempLength; i++)
{
argumentsTemp [i] = myArguments [i + 2];
}		
functionToCall.apply (this, argumentsTemp);
}
}, 10);
}
}


function AXjAva_addEventHandler (targetElement, eventType, functionToCall, bubbleEventUpDOMTree)
{
if (!targetElement)
{
window.status = 'Warning: Tried to attach event to null object';
return false;
}
if (typeof targetElement.addEventListener != 'undefined')
{
targetElement.addEventListener (eventType, functionToCall, bubbleEventUpDOMTree);
}
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;
}

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;

function initPageComponents() {
/*  Used to load all components on the page */
msDivs[0] = document.getElementById('mainStory1');
msDivs[1] = document.getElementById('mainStory2');
msDivs[2] = document.getElementById('mainStory3');
msDivs[3] = document.getElementById('mainStory4');
msDivs[4] = document.getElementById('mainStory5');

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 = document.getElementById('TopStoriesWrap');
initPausePlayEvents();
var breakingNews = document.getElementById('breakingNewsImg'); 
if (breakingNews == null)
{
paused = false; 
timer1=setTimeout('timedStory()',3000);
}
doImageSwap();
}

function initPausePlayEvents() {
if (!document.getElementById || !document.getElementsByTagName) {
return true;
}
var topStories = document.getElementById('TopStoriesTitles');
var links = topStories.getElementsByTagName('a');
for (i=0;i < links.length; i++) {
if (links.item(i).id.substring(0,1) == 'a'){  
links.item(i).href='javascript:{}';
AXjAva_addEventHandler (links [i], 'click', function (event)
{
doNumber (event);
}, false);
}
}

var playLink = document.getElementById('playLink');
AXjAva_addEventHandler( playLink,'click',function (event)
{
doButton (event);
}, false);
}

function fader(opac) {
if (msWrap.style.MozOpacity!=null) {  
msWrap.style.MozOpacity = (opac/100) - .001;
} else if (msWrap.style.opac!=null) {
msWrap.style.opacity = (opac/100) - .001;
} else if (msWrap.style.filter!=null) {
if (opac==100){
msWrap.style.filter = "none;";
} else {
msWrap.style.filter = "alpha(opacity="+opac+");";
}
}
}

function change(num,step){
if (step==1) {
opacity -= 10;
if (opacity>0) {
fader(opacity);
timer2=setTimeout('change('+num+', 1)',30);
}else{ 
change(num, 2);
}
}
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);
}
else if (step==3) { 
opacity += 10;
if (opacity <= 100) {
fader(opacity);
timer2=setTimeout('change(' + num + ', 3)',30);
}
}
}

function timedStory(){
if(currPic<4)
currPic++;
else
currPic=0;
change(currPic,1);
timer1=setTimeout('timedStory()',3000); 
}

function doButton(event) {
paused = !paused;
doImageSwap();
if (paused) {
clearTimeout(timer1);
}
else { 
timedStory();
}
}  

function doNumber (event) {
var eventSource = typeof event.target != 'undefined' ? event.target : window.event.srcElement;
currPic = eventSource.id.substring(1,2) - 1;
paused = true;
doImageSwap();
clearTimeout(timer1);
clearTimeout(timer2);
change(currPic, 1);

} 

function doImageSwap() {
var button = document.getElementById('playLink'); 
if (!AXjAva_isie){
var imageFile = paused ? "" : "";
button.style.background= "url("+imageFile+") 0px 0px no-repeat"; 	
}else{
button.style.backgroundImage= "url()";
position = paused ? "-16 px" : "0 px";  /* change the image source */
try {
document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

if (paused == true && currPosition != "-16 px"){
button.style.backgroundPositionY=position;
}
if (paused != true && currPosition != "0 px"){
button.style.backgroundPositionY=position;			  
}  
currPosition = position;
}
}  
AXjAva_callFunctionOnElementLoad('playLink', initPageComponents);
