/* 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){
	
    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 Main story module */
var currPic=0;
var timer1, timer2;
var paused = true;
var opacity = 100;
var msDivs = new Array();
var msButtons = new Array(); 
var msTotalImage = 0;
var msWrap;
var button;
var position;
var currPosition;
function initPageComponents() {
    /*  Used to load all components on the page */
    //////////Assign total image added by pravin
    msTotalImage = totalImage();
    //alert(msTotalImage);
    var i=0;
    for(var j=1; j<=msTotalImage; j++){
        ///////for image list
        msDivs[i] = document.getElementById('mainStory'+j);
        /////////for button list
        msButtons[i] = document.getElementById('a'+j);
        if(i != msTotalImage) i++;
 
    }
    //////////end by pravin
 
    msWrap = document.getElementById('mainStoryWrap');
    initPausePlayEvents();
    var breakingNews = document.getElementById('breakingNewsImg');
    if (breakingNews == null)
    {
        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('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) {
    ///end hide tag
    var totalImg = msTotalImage;

    /*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;

  
    
    
        ///end hide tag
        var totalImg = msTotalImage;
        for(var i=0; i<totalImg; i++){
            ////////////////////////
            //alert(num);
            ///////for image list
            msDivs[i].style.display = (num == i ? "block" : "none");
            /////////for button list
            msButtons[i].className = (num == i ? "on" : "off");
        }
        change(num, 3);
    }
    /*fadeIn*/
    else if (step == 3) {
        opacity += 10;
        if (opacity <= 100) {
            fader(opacity);
            timer2=setTimeout('change(' + num + ', 3)',50);
        }
    }

    if(num >= 1 && msTotalImage > num){
        //alert(num);
        var hideSID2 = 0;
        var hideEID2 = 0;  
        if(msTotalImage > 5){
            var showSID = num-1;
            var showEID = num+4;
            var hideSID1 = 0;
            var hideEID1 = showSID-1;
            if(showEID <= msTotalImage){
                var hideSID2 = showEID+1;
                var hideEID2 = msTotalImage;
            }
        } else {
            var showSID = 1;
            var showEID = msTotalImage;
            var hideSID1 = 0;
            var hideEID1 = 0;
            var hideSID2 = 0;
            var hideEID2 = 0;
        }
      
        ///for click 2 number
        if(num==1){
            var showSID = 1;
            var showEID = num+4;
        }
           
        //alert(showEID);
        ////loop for show/hode
        for(var s=showSID; s<=showEID; s++){
       
            if(document.getElementById('DID'+s) != null)
                document.getElementById('DID'+s).style.display = "inline";
        }
       
        for(var h=hideSID1; h<=hideEID1; h++){
            if(document.getElementById('DID'+h) != null)
                document.getElementById('DID'+h).style.display = "none";
        }
       
        for(var h=hideSID2; h<=hideEID2; h++){
            if(document.getElementById('DID'+h) != null)
                document.getElementById('DID'+h).style.display = "none";
        }
          
    }

}

/* change picture, wait 5 seconds, repeat */
function timedStory() {
    var totalImg = msTotalImage;
    totalImg = totalImg - 1;
    /// add by pravin
    if (currPic<totalImg){
        currPic++;
        change(currPic, 1);
        timer1=setTimeout('timedStory()',4000);
    }else{
        currPic=0;
        change(currPic, 1);
        timer1=setTimeout('timedStory()',4000);
        currPic++;
    }

/* hide by pravin
 if (currPic<totalImg){
 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 = Number(eventSource.id.substring(1,3));
    paused = true;
    doImageSwap();
    clearTimeout(timer1);
    clearTimeout(timer2);
    change((currPic-1), 1);
} 

/* swap the play pause button image */
function doImageSwap() {
    var button = document.getElementById('playLink');
    if (!tii_isie){
        var imageFile = paused ? "/img/btn_play.gif" : "/img/btn_pause.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(/img/btn_playpause.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;
    }
}  

function showPrev()
{
    paused = true;
    doImageSwap();
    clearTimeout(timer1);
    clearTimeout(timer2);
    if(currPic > 0)
        change((currPic-1), 1);
    else
        change(0, 1);
}

function showNext()
{
    //////////added by pravin
    var totalImg = msTotalImage;
    totalImg = totalImg - 1;
    //alert(totalImg);
    paused = true;
    doImageSwap();
    clearTimeout(timer1);
    clearTimeout(timer2);
    if(currPic < totalImg)
        change((currPic+1), 1);
    else
        change(totalImg, 1);
}

/* swap the play pause button image */
function doImageSwapOld() {
    var button = document.getElementById('playLink');
    var hostw = location.hostname;
    var hostUrl = 'http://'+hostw+'/';
    var pauseImage = hostUrl+"images/pause.gif";
    var playImage = hostUrl+"images/play.gif";
    var imageFile = paused ? pauseImage : playImage;
    button.style.background= "url("+imageFile+") 0px 0px no-repeat";
    if (tii_isie){
        var imageFile = paused ? pauseImage : playImage;
        button.style.background= "url("+imageFile+") 0px 0px no-repeat";
    }else{
        /*  Use an image sprite to deplete the image flickering in IE */
        var ListImage = hostUrl+"images/img_smallcircle.gif";
        button.style.backgroundImage= "url("+ListImage+")";
        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;
    }
}  
/* End Main story module */
