﻿var sectionTimer;var stat;var feature;var headlines;var vidhref;var vidlist;var f_circleTimer;var imgTimer;
var FOOTER_TIMER_LENGTH = 8000;
var FOOTER_TIMER_CLICKED_LENGTH = 30000;
var FOOTER_TIMER_IMG = "http://media.townhall.com/Townhall/CircleTimer.gif";
var FOOTER_TIMER_IMG_STATIC = "http://media.townhall.com/Townhall/CircleTimer.gif";
var imgFooterTimer;

function preInit() {
	if (!document.getElementById) return;

	if (document.getElementById('statusmsg') == null) {
		window.setTimeout("preInit();", 100);
		return;
	}
	// Document body is loaded. Proceed with the rest of the code
    init();
}
preInit();
function init()
{
    if (!document.getElementById('footernav'))
        return;
    //var so = new SWFObject("/circleTimer.swf", "CircleTimer", "18", "18", "8", "#FFFFFF");
    //so.addParam("allowScriptAccess", "always");
    //so.write("footernav_timer");
    //var ie = navigator.appName.indexOf("Microsoft") != -1;
    //f_circleTimer = (ie) ? window['CircleTimer'] : document['CircleTimer'];
	
    new Section('aCol','Columnists');
    new Section('aPol','Politics');
    new Section('aUS','US');
    new Section('aWor','World');
    new Section('aBus','Business');
    new Section('aSpo','Sports');
    new Section('aEnt','Entertainment');
    new Section('aTec','Technology');
    imgFooterTimer = document.getElementById('imgFooterTimer');
    stat = document.getElementById('statusmsg');
    //stat.style.display = 'inline';
    
    feature = document.getElementById('footermainstory');
    headlines = document.getElementById('headlinesleft');
    vidhref = document.getElementById('videohref');
    vidlist = document.getElementById('videolist');
    Section.prototype.current = Section.prototype.sections[0];
    Section.prototype.sections.current = 0;
    sectionTimer = window.setTimeout("timerStart();", FOOTER_TIMER_LENGTH);
    
    activateCurrent(true);    
}
function Section(id,name)
{
    this.id = id;
    this.name = name;
    this.a = document.getElementById(id);
    if (!this.a)
        return;
    this.a.onclick = this.onclick;
    this.a.section = this;
    this.index = this.sections.length;
    this.sections.push(this);
    this.error = false;
    
    if (!Section.prototype.current)
        Section.prototype.current = this;
    
    this.loadComplete = loadComplete;
    this.loadError = loadError;
    function loadComplete(obj, xml, txt)
    {
        status('Load Complete: ' + loadComplete.section);
        //status(txt);
        loadComplete.section.data = obj;
        loadComplete.section.loading = false;
        loadComplete.section.activated = false;
    }
    loadComplete.section = this;
    function loadError(args)
    {
        status('There was an error loading ' + this.name + '\r\n' + args.ErrorText + '\r\n' + args.Text);
        loadError.section.loading = false;
        loadError.section.error = true;
    }
    loadError.section = this;
}

Section.prototype.sections = new Array();
Section.prototype.sections.current = 0;

Section.prototype.onclick = function()
{
    if (sectionTimer)
        window.clearTimeout(sectionTimer);
    status('Old section: ' + Section.prototype.current.name);    
    Section.prototype.current = this.section;
    status('New section: ' + Section.prototype.current.name);       
    Section.prototype.current.sections.current = this.section.index;
    Section.prototype.current.activate(false);
}
function status(m)
{
    return;
    //if (stat)
        //stat.innerHTML = m;
        //stat.innerHTML = m + "<br/>" + stat.innerHTML;
}

Section.prototype.activate = function(auto)
{
    if (this.error)
        return;
	//f_circleTimer.stopCircle();

    // Verify that data exists, render, and display

    // Don't do anything if this isn't the current section
    if (this.current != this | this.activated)
    {
        status('Exiting: ' + this.current.name + ' - ' + this.name + ' - ' + this.activated);
        return;
    }
        
    if (!this.data)
    {
        if (this.loading)
        {
            status("Waiting on load of " + this.name);
            window.setTimeout("activateCurrent(" + auto + ");",1000);
        }
        else
        {
            status("Forcing load of " + this.name);
            window.setTimeout("activateCurrent(" + auto + ");",1000);
            this.load(); 
        }
        return;
    }
    var i;
    for (i=0;i<this.sections.length;i++)
    {
        this.sections[i].a.className = '';
        this.sections[i].activated = false;
    }
    
    this.a.className = 'navon';
    purge(feature);    
    feature.innerHTML = this.data.FeaturedStory;
    purge(headlines);
    headlines.innerHTML = this.data.StoryList;
    purge(vidhref);
    if(this.id != 'aPol'){
    vidhref.innerHTML = this.data.VideoHref;
    }
    purge(vidlist);
     if(this.id != 'aPol'){
    vidlist.innerHTML = this.data.VideoList;    
    }
    if (!auto)
    {  
        if (sectionTimer)
            window.clearTimeout(sectionTimer);
        sectionTimer = window.setTimeout("timerStart();", FOOTER_TIMER_CLICKED_LENGTH);
        if (imgTimer)
            window.clearTimeout(imgTimer);
        imgTimer = window.setTimeout("doImage();", FOOTER_TIMER_CLICKED_LENGTH - FOOTER_TIMER_LENGTH);
    }
    else
    	doImage();
        
    this.activated = true;
    status('Activated ' + this.name);
}
function doImage()
{
    status('doImage');
//    imgFooterTimer.src = FOOTER_TIMER_IMG;
};
function activateCurrent(auto)
{
    if (Section.prototype.current)
    {
        status('Activating ' + Section.prototype.current.name);
        Section.prototype.current.activate(auto);
    }
}
function flashActivate()
{
    activateCurrent();
}

Section.prototype.load = function()
{
    if (this.loading)
        return;
    if (AjaxCalls.ServicePath == '')
        AjaxCalls.ServicePath = 'http://www.townhall.com/Services/AjaxCalls.asmx';
    // Get data from web service
    AjaxCalls.GetFooterData(this.name,this.loadComplete,this.loadError);
    this.loading = true;
}



function timerStart(first)
{
    //if (!Section.prototype.current.activated)
    if (Section.prototype.sections.last != null & !Section.prototype.current.activated)
    {        
        //Section.prototype.current.activate(true);
    }
    if (!Section.prototype.sections.last)
    {
        Section.prototype.sections.last = Section.prototype.sections[0];
    }
    Section.prototype.current = Section.prototype.sections.next();
    if (!Section.prototype.current.data)    
        Section.prototype.current.load();
    if (sectionTimer)
        window.clearTimeout(sectionTimer);
	sectionTimer = window.setTimeout("timerStart();", FOOTER_TIMER_LENGTH);
	
	activateCurrent(true);
	    
	//f_circleTimer.startCircle();
	status('timerStart(): ' + Section.prototype.current.name);
}


Array.prototype.next = function()
{
    if (this.length == 0)
        return null;
    if (this.current != null)
        this.last = this[this.current];
    if (this.current == null | this.current >= this.length - 1)
        this.current = 0;
    else
        this.current++;
    return this[this.current];
}
Array.prototype.previous = function()
{
    if (this.length == 0)
        return null;
    if (this.current != null)
        this.last = this[this.current];
    if (this.current == null | this.current == 0)
        this.current = this.length - 1;
    else
        this.current--;
    return this[this.current];
}

function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}




	
