﻿var changeTimer;
var clipId, townhallId;
function checkForChange() {
    RooCalls.ClipId(townhallId,changeCallback,changeError);
};
function changeCallback(o) {
var newId = o;
    if (newId != clipId && newId != '' && newId != null)
    {
        clipId = newId;
        resetControls();
        
    }
    if (changeTimer)
            window.clearTimeout(changeTimer);
    if (!killTimer)
        changeTimer = window.setTimeout('checkForChange()',5000);
};
function changeError() {
    if (changeTimer)
        window.clearTimeout(changeTimer);
    if (!killTimer)
        changeTimer = window.setTimeout('checkForChange()',5000);
};
var killTimer = false;
function killTimer() {
    killTimer = true;
    if (changeTimer)
        window.clearTimeout(changeTimer);
};
window.setTimeout('killTimer()',900000);

function resetControls() {
    AjaxCalls.GetVideoGuid(clipId,videoGuidCallback,null);
};
lblTitleId = 'ctl00_cphMain_lblTitle'; lblDescId = 'ctl00_cphMain_lblDescription';
function videoGuidCallback(o) {
    o = eval("(" + o + ")");
    $(lblTitleId).innerHTML = o.Title;
    $(lblDescId).innerHTML = o.Desc;
    contentGuid = o.Guid;
    resetSendToFriend();

    try
    {
        ResetVote(o.Guid,o.User, o.Avg)
    }
    catch(err){}
    
    resetComments();
   
};
function vxClientInitialize()
{
 var vxFP = vxGetPlayer();

 vxFP.addEventListener("eVideoClipBegin", "onVideoClipBegin");
}

function onVideoClipBegin(oEvent)
{
 
    clipId = oEvent.clip.clipId;
    AjaxCalls.GetVideoGuid(clipId,videoGuidCallback,null);
}

//changeTimer = window.setTimeout('checkForChange()',5000);



window._rooCalls = function()
{
	this.ServicePath = '/session/roo.asmx';
	RadAjaxServiceNamespace.CreateProxyMethod(this, "ClipId", "townhallId");
};
window.RooCalls = new window._rooCalls();






var SIDEBARVIDEO_TIMER_LENGTH = 6000;
var SIDEBARVIDEO_TIMER_CLICKED_LENGTH = 30000;

function SidebarVideo() {
    this.vidTimer = null;
    this.vidContent = null;
    var me = this;
    

    this.preVideoInit = function() {
	    if (!document.getElementById) return;

	    if (document.getElementById('vidContent') == null) {
		    window.setTimeout(function(){me.preVideoInit();}, 100);
		    return;
	    };
	    // Document body is loaded. Proceed with the rest of the code
        this.initVideo();
    };
    
    this.initVideo = function()
    {
        new VideoTab('aVidRecent',1,this);
        new VideoTab('aVidVotes',2,this);
        new VideoTab('aVidViews',3,this);
        
        this.vidContent = document.getElementById('vidContent');
        VideoTab.prototype.current = VideoTab.prototype.tabs[0];
        VideoTab.prototype.tabs.current = 0;
	    this.vidTimer = window.setTimeout(function(){me.vidTimerStart();}, SIDEBARVIDEO_TIMER_LENGTH);
        this.activateVideo(true);  
    };
    
    this.activateVideo = function(auto)
    {
        if (VideoTab.prototype.current)
        {
            VideoTab.prototype.current.activate(auto);
        };
    };
    SidebarVideo.prototype.activateVideo = this.activateVideo;
    
    this.vidTimerStart = function()
    {

        if (!VideoTab.prototype.tabs.last)
        {
            VideoTab.prototype.tabs.last = VideoTab.prototype.tabs[0];
        };
        
 
        VideoTab.prototype.current = VideoTab.prototype.tabs.next();
     
        
        if (!VideoTab.prototype.current.data)    
            VideoTab.prototype.current.load();
        if (me.vidTimer)
            window.clearTimeout(me.vidTimer);
	  //  this.vidTimer = window.setTimeout(function(){me.vidTimerStart();}, SIDEBARVIDEO_TIMER_LENGTH);
	  //  this.activateVideo(true);
	    

    };
    SidebarVideo.prototype.vidTimerStart = this.vidTimerStart;
var videoTabActivateTimer;
    function VideoTab(id,name,parent)
    {
        this.parent = parent;
        this.id = id;
        this.name = name;
        this.a = document.getElementById(id);
        this.a.onclick = this.onclick;
        this.a.tab = this;
        this.index = this.tabs.length;
        this.tabs.push(this);
        
        if (!VideoTab.prototype.current)
            VideoTab.prototype.current = this;
        
        this.loadComplete = loadComplete;
        this.loadError = loadError;
        function loadComplete(obj, xml, txt)
        {
            status('Load Complete: ' + loadComplete.tab);
            //status(txt);
            loadComplete.tab.data = obj;
            loadComplete.tab.loading = false;
            loadComplete.tab.activated = false;
        };
        loadComplete.tab = this;
        this.error = false;
        function loadError(args)
        {
            status('There was an error loading ' + this.name + '\r\n' + args.ErrorText + '\r\n' + args.Text);
            loadError.tab.loading = false;
            this.error = true;
            window.clearTimeout(videoTabActivateTimer);
        };
        loadError.tab = this;
    };
    VideoTab.prototype.tabs = new Array();
    VideoTab.prototype.tabs.current = 0;

    VideoTab.prototype.onclick = function()
    {
        if (this.tab.parent.vidTimer)
            window.clearTimeout(this.tab.parent.vidTimer);
        VideoTab.prototype.current = this.tab;
        VideoTab.prototype.current.tabs.current = this.tab.index;
        VideoTab.prototype.current.activate(false);
    };

    VideoTab.prototype.activate = function(auto)
    {
        if (this.error)
            return;
        var me = this;
        // Verify that data exists, render, and display
        // Don't do anything if this isn't the current section
        if (this.current != this | this.activated)
        {
            return;
        };
            
        if (!this.data)
        {
            if (this.loading)
            {
                
                videoTabActivateTimer = window.setTimeout(function(){me.parent.activateVideo(auto);},1000);
            }
            else
            {
                videoTabActivateTimer = window.setTimeout(function(){me.parent.activateVideo(auto);},1000);
                this.load(); 
            };
            return;
        };
        var i;
        for (i=0;i<this.tabs.length;i++)
        {
            this.tabs[i].a.className = '';
            this.tabs[i].activated = false;
        };
        
        this.a.className = 'roll_on';
        
        this.parent.vidContent.innerHTML = this.data;
        if (!auto)
        {  
            if (this.parent.vidTimer)
                window.clearTimeout(this.parent.vidTimer);
            this.parent.vidTimer = window.setTimeout(function(){me.parent.vidTimerStart();}, SIDEBARVIDEO_TIMER_CLICKED_LENGTH);
        };
        this.activated = true;
    };
    
    VideoTab.prototype.load = function()
    {
        if (this.loading)
            return;
        if (AjaxCalls.ServicePath == '')
            AjaxCalls.ServicePath = '/Services/AjaxCalls.asmx';
        // Get data from web service
        AjaxCalls.GetSidebarVideoData(this.name,this.loadComplete,this.loadError);
        this.loading = true;
    };
    
    this.preVideoInit();
};
new SidebarVideo();




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()
{
  
}
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]);
        }
    }
}




	
