﻿// ajax headlines
    var req;
    var responseContent;
    var start;
    var timeStart;
    var currentHeadlineId;
    var nextHeadlineId;
    var originalHeadlineId = 0;
    var prevHeadlineId;
    var tempNum = 0;
    var headlinesTimer;
    var enableHeadlinesSlideShow = true;
    var headlineSlideShowSpeed = 6000;
    var headlinesStack = new Array(); // used to get around passing param for up one or down as headline selected
    
    var loadingImage = new Image();
    loadingImage.src = "headlines/images/LoadingSmall.gif";

    function loadNewContainer(recordId) {
        currentHeadlineId = recordId;  
        
        //alert(recordId);
        
        // capture orignal headline id for resets
        if (originalHeadlineId == 0) {
        	originalHeadlineId = currentHeadlineId;
        }
        
        //if the recordId is 0, reset to start
        if (currentHeadlineId == 0) {
        	currentHeadlineId = originalHeadlineId;
        }
        
        
         
	    var sURL = "headlines/GetHeadline.aspx?h=" + recordId;
	    //alert("fetching " + sURL);
        
        
        //# 2/3/2006 [sef] set our start time
        start = new Date();
        timeStart = start.getTime(); 

	    //# 2/3/2006 [sef] setup xml http request
	    req = GetXMLHttpObject();
   	    req.onreadystatechange = handleRequest;	
        req.open("GET", sURL, true);
        req.send(null);  

    }
    
    function loadNextHeadlineViaTimer() {
        // ignore request from time if player state is now stopped
        if (enableHeadlinesSlideShow == true) {
              // specify the original headline id for the "replay" feature to start things over again
              if (originalHeadlineId == 0) {
                    originalHeadlineId = currentHeadlineId;
              }
              // mimic loadNextHeadline
              headlinesStack.push(currentHeadlineId);
              loadNewContainer(nextHeadlineId);
              tempNum = 1;
        }
    }
    
    function loadNextHeadline() {
        //alert('current headline id = ' + currentHeadlineId + ', loading next headline id ' + nextHeadlineId);
        headlinesSlideShowStop();
              
        headlinesStack.push(currentHeadlineId);
        
        loadNewContainer(nextHeadlineId);
        
        tempNum = 1;

        /* error fix
        var totalHeadlines = parseInt(document.getElementById("headlineTotal").innerHTML);
            var selectedHeadline = parseInt(document.getElementById("headlineCurrent").innerHTML);
            //alert("loadNextHeadline\(\) - totalHeadlines = " + totalHeadlines + ", selectedHeadline = " + selectedHeadline);
            
            // at this point the selectHeadline is one less than actual, decrement to checks
            if (totalHeadlines == selectedHeadline) {
                // set next headline id to be original to restart the player
                nextHeadlineId = originalHeadlineId;
                document.getElementById("headlineCurrent").innerHTML = 0;
                setHeadlineButtons();
                enableHeadlinesSlideShow = true;
                headlinesTimer = window.setTimeout("loadNextHeadlineViaTimer();", 0); 
                document.getElementById("headlinesPlay").innerHTML = "Stop";
                
            }
		/* end error fix */
        
    }
    
    function loadPrevHeadline() {
        headlinesSlideShowStop();
        
        var old = currentHeadlineId;
        currentHeadlineId = headlinesStack.pop();
        //alert('current headline id = ' + old + ', loading prev headline id ' + currentHeadlineId);
        
        loadNewContainer(currentHeadlineId);       
        tempNum = -1;
    }
    
    function handleRequest() {
	     //# 2/3/2006 [sef] if the response has made it's way back from the server then validate it and take action
	 //alert(req.responseText);
         if (req.readyState == 4) {
              // only if "OK"
               if (req.status == 200) {
                   // ...processing statements go here...
                   //alert(req.responseText);
		           responseContent = req.responseText;
               } 
               else {
                    responseContent = "We apologize but there was an error processing your request.  Please <a href='javascript:window.location.reload()'>Try Again<\/a>.<p>Error Detail: There was a problem retrieving the XML data:\n" + req.statusText;
                    fillContainer(responseContent);
                    
               }
            //# 2/3/2006 [sef] calculate how long the xml http operation took, make the response take 2 seconds so you get to see the gears ;)
            var end = new Date();
    	    var timeEnd = end.getTime();  
    	    var diff = timeEnd - timeStart;
    	    var waitTime = 0 - diff;
    			 //alert("record id " + currentHeadlineId + " loaded in " + diff + "ms");
        	
    		    if (waitTime > 0) {
    			    //document.getElementById("ajaxStat").innerHTML = "Execution Time: " + diff + "ms";
			        window.setTimeout("processResponse();", waitTime);
    		    }
    		    else {
    			    processResponse();
    		    }
          }
          else
          {
      	    setLoadingStatus();
          }
    }
    
    function processResponse() {
          var responseString = responseContent;
          var contentResults;
          var content;
    
          //alert(responseString);
          contentResults = responseString.split("$$$;$$$");
          //alert(contentResults.length);
          
         // alert('next id length = ' + contentResults[0].length);
         // check for no more headlines
         if (contentResults[1].length == 0) {
            //document.getElementById("headlinesNext").className = "headlinesButton disabled";
            clearLoadingStatus();
            //alert("empty headline");
         }
         else {
            nextHeadlineId = contentResults[0];
            prevHeadlineId = currentHeadlineId;         
            content = contentResults[1];
            
            fillContainer(content);                                                           
                    
            updateHeadlineCurrentCount(tempNum);
            setHeadlineButtons();
            
            rotateHeadlines();

         }
    }
    
    function fillContainer(txt) {
	    var container = document.getElementById("headlineDetail");
	    container.innerHTML = txt;
	    clearLoadingStatus();
    }
    
    function setLoadingStatus() {
        document.getElementById("headlineDetail").style.background = "url(/thecomusinn.com/headlines/images/LoadingSmall.gif) no-repeat 100% 0%";
    }
    
    function clearLoadingStatus() {
        //alert(document.getElementById("headlineDetail").style.background);
        document.getElementById("headlineDetail").style.background = "";
    }
    
    function updateHeadlineCurrentCount(n) {
        var ele = document.getElementById("headlineCurrent");
        var num = parseInt(ele.innerHTML);
        num = num + n;
        if (num > 0) {
            ele.innerHTML = num;
        }
    }
    
    function setHeadlineButtons() {
        var totalHeadlines = parseInt(document.getElementById("headlineTotal").innerHTML);
        var selectedHeadline = parseInt(document.getElementById("headlineCurrent").innerHTML);
       // alert(selectedHeadline);
        // check prev btn
        if (selectedHeadline <= 1) {
            // disable
            document.getElementById("headlinesPrev").className = "headlinesButton disabled";
            document.getElementById("headlinesPrev").onclick = "";
        }
        else {
            document.getElementById("headlinesPrev").className = "headlinesButton";  
            document.getElementById("headlinesPrev").onclick = function() {loadPrevHeadline();};
        }
        
                
        // check next btn
        if (selectedHeadline >= totalHeadlines) {
            // disable
            document.getElementById("headlinesNext").className = "headlinesButton disabled";
            headlinesSlideShowStop();
            document.getElementById("headlinesNext").onclick = "";
        }
        else {
            document.getElementById("headlinesNext").className = "headlinesButton";  
            document.getElementById("headlinesNext").onclick = function() {loadNextHeadline();};
                  
        }
    }   
    
    function rotateHeadlines() { 
        var totalHeadlines = parseInt(document.getElementById("headlineTotal").innerHTML);
        var selectedHeadline = parseInt(document.getElementById("headlineCurrent").innerHTML);

        if (selectedHeadline < totalHeadlines) {
            if (enableHeadlinesSlideShow == true) {
                  headlinesSlideShowStart(headlineSlideShowSpeed);
            }
        }
        else {
            // stop
            //  headlinesSlideShowStop();
            
                window.setTimeout("headlinesSlideShowStart(" + headlineSlideShowSpeed + ");", headlineSlideShowSpeed);
           
        }
        
    }
    
    function toggleHeadlinesSlideShow() {
            //alert("enableHeadlinesSlideShow = " + enableHeadlinesSlideShow);
            if (enableHeadlinesSlideShow == true) {
                // request to disable
                headlinesSlideShowStop();
            }
            else {
                // request to turn on
                headlinesSlideShowStart(0);  
            }
        

    }
    
    function headlinesSlideShowStop() {
                enableHeadlinesSlideShow = false; 
                clearTimeout(headlinesTimer);
                document.getElementById("headlinesPlay").innerHTML = "Play";       

    }
    
    function headlinesSlideShowStart(slideShowSpeed) {
            var totalHeadlines = parseInt(document.getElementById("headlineTotal").innerHTML);
            var selectedHeadline = parseInt(document.getElementById("headlineCurrent").innerHTML);
            //alert("headlinesSlideShowStart\(\) - totalHeadlines = " + totalHeadlines + ", selectedHeadline = " + selectedHeadline);
            
            if (totalHeadlines == selectedHeadline) {
                // set next headline id to be original to restart the player
                nextHeadlineId = originalHeadlineId;
                document.getElementById("headlineCurrent").innerHTML = 0;
                setHeadlineButtons();
                enableHeadlinesSlideShow = true;
                headlinesTimer = window.setTimeout("loadNextHeadlineViaTimer();", 0); 
                document.getElementById("headlinesPlay").innerHTML = "Stop";
            }
            else {
                enableHeadlinesSlideShow = true;
                headlinesTimer = window.setTimeout("loadNextHeadlineViaTimer();", slideShowSpeed);  
                document.getElementById("headlinesPlay").innerHTML = "Stop";   
            }
    

    }

function GetXMLHttpObject()
{
    var oXMLHttp = null;

    try
    {
        oXMLHttp = new ActiveXObject("MSXML2.XMLHTTP");
    }
    catch (E)
    {
        try
        {
            oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
          oXMLHttp = null;
        }
    }

    if ((oXMLHttp == null) && (typeof(XMLHttpRequest) != 'undefined'))
    {
        oXMLHttp = new XMLHttpRequest();
    }

    return oXMLHttp;
}

function LoadPage(sURL)
{
    var oXMLHttp = GetXMLHttpObject();

    oXMLHttp.open("GET", sURL, true);
    oXMLHttp.send(null);
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "";
}

