/* Attaching event handlers to elements */
var clickToggle = true; 
tii_addEventHandlerOnElementLoad ('bubbleLink', 'mouseover', function (event){displayBubble(event);}, false);
tii_addEventHandlerOnElementLoad ('bubbleLink', 'mouseout', function (event){hideBubble(event);}, false);
tii_addEventHandlerOnElementLoad ('bubbleLink', 'keypress' , function (event){displayBubble(event);}, false); 
tii_addEventHandlerOnElementLoad ('bubbleLink', 'blur', function (event){hideBubble(event);}, false);

/* Thought Bubble for The Ag */
function displayBubble(event){
	//alert(event);
	bubLink = document.getElementById('bubbleLink'); 
	bub = document.getElementById('bubble'); 
	var bubPos = new Array(2);
	bubPos = findPos(bubLink); 
 
	/*  Check for IE */
	if (tii_isie) 
	{
		bub.style.left = (parseInt(bubPos[0]) - 162).toString() + 'px';
		bub.style.top = (parseInt(bubPos[1]) +  25).toString() + 'px';	
	}
	else
	{
		bub.style.left = (parseInt(bubPos[0]) - 176).toString() + 'px';
		bub.style.top = (parseInt(bubPos[1]) +  26).toString() + 'px';	
	}
	
	/* Display the bubble */
	bub.style.display = 'block';
	clickToggle = false;
}

/* Hides the bubble */
function hideBubble(event){
/*	alert ('test');*/
	var bub = document.getElementById('bubble');	
	bub.style.display = 'none';
	clickToggle = true;
}

/* Finds the position of an element on the page */
function findPos(obj) {
	//alert(obj);
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/* Begin TimeStamp function */
function renderTimestamp(year,month,day,hour,minutes,seconds) 
{
    var aYear = new String(year);
    var aMonth = new String(month);
    var aDay = new String(day);
    var aHour = new String(hour); 
    var aMinutes = new String(minutes);
    var aSeconds = new String(seconds); 
    articleUTC = Date.UTC(aYear, aMonth, aDay, aHour, aMinutes, aSeconds);

    /* this is EST timezone offset of 5 hours = 300 minutes
    all Time articles are published in EST */
    articleTZOffsetMS = 300 * 60 * 1000 ; 
    articleCorrectedMS = articleUTC + articleTZOffsetMS;

    localDate = new Date();
    localYear = localDate.getFullYear();
    localMonth = localDate.getMonth(); 
    localDay = localDate.getDate(); 
    localHour = localDate.getHours();
    localMinutes = localDate.getMinutes();
    localSeconds = localDate.getSeconds();
    localTZOffset = localDate.getTimezoneOffset();
    //localTZOffsetMS = localTZOffset * 1000* 60; 
    localTZOffsetMS = 300 * 1000* 60;    localUTC = Date.UTC(localYear, localMonth, localDay, localHour, localMinutes, localSeconds); 

    localCorrectedMS = localUTC + localTZOffsetMS;

    // calculate how many minutes between article utc and user utc 
    minuteDifferential = (localCorrectedMS - articleCorrectedMS)/(1000 * 60);
    ageInMinutes = Math.round(minuteDifferential);
 
 if (ageInMinutes < 0) { ageInMinutes = 0;} 

    if ( ageInMinutes < 15 ) {
        document.write("<span>UPDATED:</span> "+ ageInMinutes +" minutes ago"); 
    } else {
        if (hour >= 12) {
            if (hour != 12) {aHour = aHour - 12;} 
            ampm = "PM";
        } else {
            ampm = "AM";
        }
        if (aMinutes < 10) {aMinutes = "0" + aMinutes} 
        document.write("<span>UPDATED:</span> "+ aHour+":" + aMinutes + ampm + " ET"); 
    }
}
/* End TimeStamp function */
/* Begin Main story module */
var currPic=0;
var timer1, timer2;
var paused = false;
var opacity = 100;
var msDivs = new Array(5);
var msButtons = new Array(5); 
var msWrap;
var button;
var position;
var currPosition;



 //   Added By Madhulatha for HomeTop Slideshow [Backend depends]
function testpause(val)
{
	
	if(val==1)
	{ 
		paused=false;     // Autoplay On
	}
	else
	{
		paused=true;      // Autoplay Off
	}
	
}

function play_next1()
{  
	if(currPic<4)
    next_val=currPic+1;
	else
	next_val=0;
	paused=true;
	//doImageSwap();
	change(next_val,1);
}

function play_prev1()
{  
	if(currPic>0)
    prev_val=currPic-1;
	else
	prev_val=4;
	paused=true;
	//doImageSwap();
	change(prev_val,1);
}
 
 function stop_onmouse()
 {    
		paused=true;
		doImageSwap(); 
		 //alert(currPic); 
		change(currPic,3);
		 clearTimeout(timer1);
		
 } 

function initPageComponents() {
	
 /*  Used to load all components on the page */
 msDivs[0] = document.getElementById('s1');
 msDivs[1] = document.getElementById('s2');
 msDivs[2] = document.getElementById('s3');
 msDivs[3] = document.getElementById('s4');
 msDivs[4] = document.getElementById('s5');
 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('mainStoryWrap');
 mstopstories=document.getElementById('topStories');
 initPausePlayEvents();
 //alert("test");
 //var breakingNews = document.getElementById('breakingNewsImg'); 
 //if (breakingNews == null)
 {
		/*	  
				// Commented By Madhulatha (Auto Play Flag)
				
				paused = false; 
			  timer1=setTimeout('timedStory()',7000);
		*/ 
			
			if(paused == false)
			 {	
			  timer1=setTimeout('timedStory()',7000);
			 }
			 
}
  // doImageSwap(); 
}

function initPausePlayEvents() {
 /* add Event Handlers for the Photo Module */
 if (!document.getElementById || !document.getElementsByTagName) {
  return true;
 }
 /* checks for Javascript operability  */ 

 /*  get all the links in the photo module  */
 var topStories = document.getElementById('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 beginnig with 'a'
   //add the doNumber event handler for the number links
   links.item(i).href='javascript:{}';
   tii_addEventHandler (links [i], 'click', function (event)
   {
    doNumber (event);
   }, false);
  }
 }
 
 var playLink = document.getElementById('a1');
  
 //add the doButton event handler for the play pause button 
 tii_addEventHandler ( playLink , 'click', function (event)
 {
  doButton (event);
 }, false);
 
 
}

/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
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+");";
      }
 }
// alert(document.getElementById("a"+(currPic+1)).style.filter);
 if (document.getElementById("a"+(currPic+1)).style.MozOpacity!=null) {  
  /* Mozilla's pre-CSS3 proprietary rule */ 
 document.getElementById("a"+(currPic+1)).style.MozOpacity = (opac/100) - .001;
 } else if (document.getElementById("a"+(currPic+1)).style.opac!=null) {
  /* CSS3 compatible */
  document.getElementById("a"+(currPic+1)).style.opacity = (opac/100) - .001;
 } else if (document.getElementById("a"+(currPic+1)).style.filter!=null) {
  /* IE's proprietary filter */ 
 if (opac==100){
  document.getElementById("a"+(currPic+1)).style.filter = "none;";
 } else {
  document.getElementById("a"+(currPic+1)).style.filter = "alpha(opacity="+opac+");";
      }
 }
 
 
}

function change(num, step) {
	//alert(num);
	//alert(opacity);
 /*fadeOut*/
 if (step == 1) {
  opacity -= 10;
  if (opacity > 0) {
   fader(opacity);
   timer2=setTimeout('change(' + num + ', 1)',80);
  }
  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 ? "active" : "off");
  msButtons[1].className = (num == 1 ? "active" : "off");
  msButtons[2].className = (num == 2 ? "active" : "off"); 
  msButtons[3].className = (num == 3 ? "active" : "off");
  msButtons[4].className = (num == 4 ? "active" : "off");
  change(num, 3);
 }
 /*fadeIn*/
 else if (step == 3) {  
  opacity += 10;
  if (opacity <= 100) {
   fader(opacity);
   timer2=setTimeout('change(' + num + ', 3)',80);
  }
 }
}

/* change picture, wait 5 seconds, repeat */
function timedStory() {
	
 if (currPic<4){
 currPic++;
 change(currPic, 1);
 timer1=setTimeout('timedStory()',7000);
 }else{
	 currPic=0;
	clearTimeout(timer1);
	change(currPic,1);
	paused = true;
	//doImageSwap();
 }
}

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


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

/* swap the play pause button image */
function doImageSwap() {
	 
		/* var button = document.getElementById('playLink'); 
		 
		// alert(paused);
		 var imageFile = paused ? "/images/hometop_play.jpg" : "/images/hometop_stop.jpg";
		  button.style.background= "url("+imageFile+") 0px 0px no-repeat";
		//  alert( button.style.background);
		  
		  
		  document.getElementById('pausePlay').src=imageFile ;*/
		
}  
/* End Main story module */ 
