
	//an array to handle the global variables
    var globals = new Array();

	

    function startHeadlines()
	{		
		//row 1
		var url = "../common/handlefeeds.php?numresults=10&row=1";
		headlines(url);

		//row 2
		url = "../common/handlefeeds.php?numresults=10&row=2";
		headlines(url);		
	}


/**
 *  makes the url call and loads the dynamic script
 *  think of it as an alternative to ajax 
 */
    function headlines(url)
	{ 
		
        //ie6 caches the source url so a random number is needed to make the address unique
		var randomnumber=Math.floor(Math.random()*1000);

        var se = document.createElement("script");
        se.type = "text/javascript";
        se.src = url;

        document.getElementsByTagName("head")[0].appendChild(se);
        return false;
    }

	var MAX_DUMP_DEPTH = 10;

      

       function dumpObj(obj, name, indent, depth) {

              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + name + "\n";

                     indent += "\t";

                     for (var item in obj)

                     {

                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "\n";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }

/**
 *  the callback: handles the function returned from the server
 */
    function getJson(hl_one, hl_two, row) 	   
	{
		if(row == 1)
		{
		    parseHeadlines(hl_one, 'yahoo');
		    parseHeadlines(hl_two, 'google');
		}
		else if(row == 2)
		{
			parseHeadlines(hl_one, 'reuters');
		    parseHeadlines(hl_two, 'ap');
		}
	}


/**
 *  the workhorse: parses the twitter feed and fills the dynamic divs
 */
    function parseHeadlines(obj, div_id) 	   
	{	     
		
		 var line = "";	
      

        //this is the div I'm writing the content to	
        var tDiv = document.getElementById(div_id);
	    tDiv.style.display = "block";

		//make the logo appear too
	    var cDiv = document.getElementById(div_id + "_logo");
	    cDiv.style.display = "block";
	
        var title,  pubDate, source, description, link, bgcolor, timeline, tagline, line;
		
        var footer = document.getElementById('footer');
		
		// if the last headline box is done turn on the footer		
        if((obj.results.length) > 0 && (footer.style.display == 'none') && div_id == 'ap')
		{			
			  footer.style.display = "block";
		}
		
			
		
        for (var i=0;i<obj.results.length;i++) {	
			
    	    //banding for the rows
      	    if(i % 2) {
        	    bgcolor="rgb(248, 248, 248)"
            } else {
       		    bgcolor="rgb(255, 255, 255)"	
            }

            //get stuff from the feed
            title = obj.results[i].title;
            pubDate = obj.results[i].pubDate;
		    source = div_id;           
            description = obj.results[i].description;
			link = obj.results[i].link;

		   

            //get the time difference and build a tag line based on it
		    var headlinetime = new Date(pubDate);
            var currenttime = new Date();
		    timeline = getTimeDiff(currenttime, headlinetime);
		    tagline = timeline + " from " + source;
		
          
	 
	        //put together the innerHtml
		    line += "<div class='headline' id='" + div_id + '' + i + "' style='background-color:" + bgcolor +
			        "'><div class='headline_content'><strong><a href='" + link + "'>"+title+"</a></strong><br> " + description + 
					"</div><div style='clear:both;'></div><div class='headline_source'>" + tagline +
					"</div></div>";
	    }	
	
	    //some decoding that needs to be done on the tag brackets
        line = line.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"');
	    line = line.replace(/&gt;/g, ">");
        
		//set the innerHTML and fade it in 
	    tDiv.innerHTML = line;
	   
    }


/**
 *  feed this function the currenttime and a headlinetime and get a colloquial expression of the difference
 *  it's used to build the bottom line of every headline  
 */
    function getTimeDiff(currenttime, headlinetime)
    {
        var difference = currenttime.getTime() - headlinetime.getTime();
 
        var days = Math.floor(difference/1000/60/60/24);
        difference -= days*1000*60*60*24
 
        var hours = Math.floor(difference/1000/60/60);
        difference -= hours*1000*60*60
 
        var minutes = Math.floor(difference/1000/60);
        difference -= minutes*1000*60
 
        var seconds = Math.floor(difference/1000);

        var line = '';
        if (days >= 1)
	    {
            line = "more than one day ago";
        }  
	    else if (days < 0)
	    {
	        line = '';
	    }
	    else if (hours >=1)
	    {
	        if (hours==1)
		    {
		        line = "1 hour ago";
		    }
		    else
		    {
		        line = hours + " hours ago";
		    }
	    }
	    else if (minutes >=1)
	    {
	        if (minutes==1)
		    {
		        line = "1 minute ago";
		    }
		    else
		    {
		        line = minutes + " minutes ago";
	 	    }
        }
	    else if (seconds >=1)
	    {
	        if (seconds==1)
		    {
		        line = "1 second ago";
		    }
		    else
		    {
		        line = seconds + " seconds ago";
		    }
	    }
		else if (seconds < 1)
		{
			line = "1 second ago";
		}

		return line;	 
    }




