





function $(x){return document.getElementById(x)};
var curFontSize = 1; 	
/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) 
		date.setTime (date.getTime() - skew);
}

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while (i < clen) {	
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
		}
	return null;
}

function SetCookie(name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "; path=/") +
	((domain) ? "; domain=.townhall.com" : "; domain=.townhall.com") +
	((secure) ? "; secure" : "");
}

function fontSize(act) {
    if (document.getElementById) {
        column = document.getElementById("columnBody");
        printColumn = document.getElementById("printDiv");
        if (column != null)
        {
            var p = column.getElementsByTagName('p');
            var p2 = printColumn.getElementsByTagName('p');
            
            var style = '';
            if (act == 2) {
                curFontSize = 2;
                style = 'largeFontSize';
            }
            else if (act == 1) {
                curFontSize = 1;
                style = 'mediumFontSize';
            }
             else if (act == 0) {
                curFontSize = 0;
                style = 'smallFontSize';
            }
            for (var i=0;i<p.length;i++)
            {
                p[i].className=style;
            }
            for (var i=0;i<p2.length;i++)
            {
                p2[i].className=style;
            }
        }
    }
	// set cookie with font size
	var expdate = new Date();
	FixCookieDate (expdate);
	expdate.setTime (expdate.getTime() + (672*60*60*1000*100));
	SetCookie("userfontc",curFontSize,expdate);		
	return false;
}

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;

YAHOO.util.Event.onDOMReady(init);
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;
    
    // Get data from web service
    TOWNHALL.ajax.calls.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]);
        }
    }
}


function FlagThis(guid) {
    TOWNHALL.ajax.calls.Flag(guid,null,null);
    document.getElementById('aFlag' + guid).innerHTML = 'Flagged. Thank you.';
    document.getElementById('aFlag' + guid).href = 'javascript:;';
};

	
/* AjaxVote Functions */
var contentGuid;var checkedImg="";var uncheckedImg="";
var voteButtons = new Array(); var voteVal=0;

function VoteButton(id,value)
{
    this.img = $(id);
    this.img.voteButton = this;
    this.img.onclick = onclickFunc;
    this.value = value;
    
    function onclickFunc()
    {
        resetUserVote(this.voteButton.value);        
        TOWNHALL.ajax.calls.SubmitVote(contentGuid,this.voteButton.value,voteCallback,voteCallback);
    }
    
};
function initVote() {
    var qs = new Querystring();
    resetUserVote(qs.get("voted","0"));
}
VoteButton.prototype.check = function(){
    this.img.src = checkedImg;
};
VoteButton.prototype.uncheck = function(){
    this.img.src = uncheckedImg;
};
    
function resetAvgVote(val)
{
    for (i=0;i<avgArr.length;i++){
        avgArr[i].src = uncheckedImg;
    };
    for (i=0;i<val;i++){
        avgArr[i].src = checkedImg;
    };
};
function resetUserVote(val)
{
    for (i=0;i<val;i++){
        voteButtons[i].check();
    };
    for (i=val;i<voteButtons.length;i++){
        voteButtons[i].uncheck();
    };
    voteVal = val;
};




function voteCallback(obj)
{
    if (obj)
        resetAvgVote(obj);
    
    var loc = window.location.href;
    if (loc.indexOf('?') > 0)
        window.location = loc + '&voted=' + voteVal;
    else
        window.location = loc + '?voted=' + voteVal;
}


// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling)
{
    if (element == null)
        return false;
    bubbling = bubbling || false;
    if(window.addEventListener) { // Standard
        element.addEventListener(type, expression, bubbling);
        return true;
    } else if(window.attachEvent) { // IE
        element.attachEvent('on' + type, expression);
        return true;
    } else return false;
}
function gotoUrl(o) {
    if (o.options[o.selectedIndex].value != '')
    window.location.href = o.options[o.selectedIndex].value;
};
