/* 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) {
	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 lang;
var language;
var currPic = 0;
var currPic_ads = 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;
var total_img;
var gallery;
var gallery2;

var timer1_ads, timer2_ads;
var paused_ads = true;
var opacity_ads = 100;
var msDivs_ads = new Array(5);
var msButtons_ads = new Array(5); 
var msWrap_ads;
var button_ads;
var position_ads;
var currPosition_ads;
var total_ads;
var total_categ;
/*function ForLang(language)
{
 var lang = language;
 //return lang;
}*/
function testpause(val,total,gallname)
{
	if(val==1)
	{ 
		paused=false;     // Autoplay On
	}
	else
	{
		paused=true;      // Autoplay Off
	}
	
	total_img=total;
	gallery=gallname;
}
function testpause_ads(val,total2,gallname2)
{
	if(val==1)
	{ 
		paused_ads=false;     // Autoplay On
	}
	else
	{
		paused_ads=true;      // Autoplay Off
	}
	total_ads=total2;
	gallery2=gallname2;
}
function play_next()
{  
    var currPic = eval(document.getElementById('curr').value);
	lastbut1 = total_img-1;
	if(currPic<lastbut1)
    next_val=currPic+1;
	else
	next_val=0;
	paused=true;
	if(next_val==0)
	{
	galtab(0,lastbut1);
	}
	document.getElementById('curr').value = next_val;
	doImageSwap();
	change(next_val,1);
}

function play_prev()
{  
	lastbut1=total_img-1;
	if(currPic>0)
    prev_val=currPic-1;
	else
	prev_val=lastbut1;
	if(prev_val==0)
	{
	galtab(0,lastbut1);
	}
	paused=true;
	doImageSwap();
	change(prev_val,1);
}
function play_next_ads()
{  
	lastbut2=total_ads-1;
	if(currPic_ads<lastbut2)
    next_val2=currPic_ads+1;
	else
	next_val2=0;
	paused_ads=true;
	doImageSwap_ads();
	change_ads(next_val2,1);
	
}

function play_prev_ads()
{  
	lastbut2=total_ads-1;
	if(currPic_ads>0)
    prev_val2=currPic_ads-1;
	else
	prev_val2=lastbut2;
	paused_ads=true;
	doImageSwap_ads();
	change_ads(prev_val2,1);
}
 
 function stop_onmouse()
 {      
		paused=true;
		doImageSwap(); 
		change(currPic,3);
		clearTimeout(timer1);
 } 
function stop_onmouse_ads(id)
 {      
		paused_ads=true;
		change_ads(currPic_ads,3);
		clearTimeout(timer1_ads);
 } 
 function initPageComponentsads() 
 {

	 /*  Used to load all components on the page */
	for($a=0;$a<total_ads;$a++)
	{
		$a2=eval($a)+1;
		msDivs_ads[$a] = document.getElementById('mainStoryads'+$a2);
		msButtons_ads[$a] = document.getElementById('ads'+$a2)
	}
	
 msWrap_ads = document.getElementById('mainStoryWrapads');

 initPausePlayEvents_ads();
 var breakingNews = document.getElementById('breakingNewsImg'); 
 if (breakingNews == null)
 {	
			if(paused_ads == false)
			 {	
			  timer1_ads=setTimeout('timedStory_ads()',2000);
			 }
			 
}
   doImageSwap_ads();
}
function initPageComponents() 
{
	 /*  Used to load all components on the page */
	for($n=0;$n<total_img;$n++)
	{
		$n2=eval($n);
		msDivs[$n] = document.getElementById('mainStory'+$n2);
		msButtons[$n] = document.getElementById('a'+$n2)
	}
	    msWrap = document.getElementById('mainStoryWrap');
 	    initPausePlayEvents();

 var breakingNews = document.getElementById('breakingNewsImg'); 
 if (breakingNews == null)
 {
			if(paused == false)
			 {	
			  timer1=setTimeout('timedStory()',4000);
			 }
						 
}
   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('playLink');
 //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+");";
      }
 }
}

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

  if (opacity <= 100) {
   fader(opacity);
   timer2=setTimeout('change(' + num + ', 3)',100);
  }
  
 }
}
}

/* change picture, wait 5 seconds, repeat */
function timedStory() {
	lastbut1=total_img-1;
 if (currPic<lastbut1){
 currPic++;
 change(currPic, 1);
 timer1=setTimeout('timedStory()',4000);
 }else{
	currPic=0;
	clearTimeout(timer1);
	change(currPic,1);
	galtab(currPic,lastbut1);
	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,3) - 1;
 paused = true;
 doImageSwap();
 clearTimeout(timer1);
 clearTimeout(timer2); 
 change(currPic, 1);
 
} 
/* swap the play pause button image */
function doImageSwap()
{
		 var button = document.getElementById('playLink'); 
		 var imageFile = paused ? "/includes/"+lang+"/images/play_icon.gif" : "/includes/"+lang+"/images/pause_btn.gif";
		 document.getElementById('pausePlay').src=imageFile ;
}  
/* End Main story module */ 
function initPausePlayEvents_ads()
{
 /* 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 topStoriesads = document.getElementById('topStoriesads');
 var links = topStoriesads.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_ads (event);
   }, false);
  }
 }
 var playLink_ads = document.getElementById('playLinkads');
  
 //add the doButton event handler for the play pause button_ads 
 tii_addEventHandler ( playLink_ads , 'click', function (event)
 {
  doButton_ads (event);
 }, false);
}
function fader_ads(opac)
{
 if (msWrap_ads.style.MozOpacity!=null) {  
  /* Mozilla's pre-CSS3 proprietary rule */ 
  msWrap_ads.style.MozOpacity = (opac/100) - .001;
 } else if (msWrap_ads.style.opac!=null) {
  /* CSS3 compatible */
  msWrap_ads.style.opacity_ads = (opac/100) - .001;
 } else if (msWrap_ads.style.filter!=null) {
  /* IE's proprietary filter */ 
 if (opac==100){
  msWrap_ads.style.filter = "none;";
 } else {
  msWrap_ads.style.filter = "alpha(opacity="+opac+");";
      }
 }
}
function change_ads(num, step)
{
 /*fadeOut*/
//alert(num);
 if (step == 1) {
  opacity_ads -= 10;
  
  if (opacity_ads > 0) {
   fader_ads(opacity_ads);
   timer2_ads=setTimeout('change_ads(' + num + ', 1)',80);
  }
  else {  
   change_ads(num, 2);
  }
 }
 /*change picture*/
 else if (step == 2) {
//	 alert(num);
  currPic_ads = num;
  //alert(currPic_ads);
	 for($k=0;$k<total_ads;$k++)
		 {
			
			 msDivs_ads[$k].style.display = (num == $k ? "block" : "none");
			 msButtons_ads[$k].className = (num == $k ? "currentpage" : "off");
		 }
  change_ads(num, 3);
 }
 /*fadeIn*/
 else if (step == 3) {  

  opacity_ads += 10;
// alert(opacity_ads);
if (opacity_ads <= 100) {
  fader_ads(opacity_ads);
   timer2_ads=setTimeout('change_ads(' + num + ', 3)',80);
  }
 }
}
function timedStory_ads()
{
	
	lastbut1=total_ads-1;
 if (currPic_ads<lastbut1){
 currPic_ads++;
 change_ads(currPic_ads, 1);
 timer1_ads=setTimeout('timedStory_ads()',2000);
 }else{
	 currPic_ads=0;
	clearTimeout(timer1_ads);
	change_ads(currPic_ads,1);
	paused_ads = true;
	doImageSwap_ads();
 }
}

/* executed when the play pause button is selected */
function doButton_ads(event) 
{
 paused_ads = !paused_ads;
 doImageSwap_ads();
 if (paused_ads) {
  /* stop the image loop */
  clearTimeout(timer1_ads);
 }
 else { 
  /* restart the image loop */
  timedStory_ads();
 }
}  

/*executed when a number link is selected */
function doNumber_ads (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_ads = eventSource.id.substring(3,4) - 1;

 paused_ads = true;
 doImageSwap_ads();
 clearTimeout(timer1_ads);
 clearTimeout(timer2_ads); 
 
 change_ads(currPic_ads, 1);
 
} 
function doImageSwap_ads() 
{
	 
		 var button_ads = document.getElementById('playLinkads'); 
		 
		// alert("ads"+paused_ads);
		 var imageFile = paused_ads ? "/includes/language1/images/p_play.jpg" : "/includes/language1/images/p_pause.jpg";
		  //button_ads.style.background= "url("+imageFile+") 0px 0px no-repeat";
		//  alert( button_ads.style.background);
		  
		  
		  document.getElementById('pausePlayads').src=imageFile ;
		
}  
