﻿var HOMEPAGEVIDEO_TIMER_LENGTH = 5000;
var HOMEPAGEVIDEO_TIMER_CLICKED_LENGTH = 30000;
var ENABLE_HP_VID = true;
function HomepageVideo() {
    this.vidTimer = null;
    this.vidContent = null;
    this.frozen = false;
    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()
    {
        if ($('aVidLocal') != null)
            new VideoTab('aVidLocal','Local',this);
        if ($('aVidFox') != null)
            new VideoTab('aVidFox','Fox',this);
        if ($('aNews') != null)
            new VideoTab('aNews','News',this);
        if ($('aVidHam') != null)
            new VideoTab('aVidHam','Ham',this);
        if ($('atheFive') != null)
            new VideoTab('atheFive','Five',this);
        
        
        this.vidContent = document.getElementById('vidContent');
        VideoTab.prototype.current = VideoTab.prototype.tabs[0];
        VideoTab.prototype.tabs.current = 0;
        if (ENABLE_HP_VID)
	        this.vidTimer = window.setTimeout(function(){me.vidTimerStart();}, HOMEPAGEVIDEO_TIMER_LENGTH);
        this.activateVideo(true);  
    };
    
    this.freezeTimer = function()
    {
        if (me.vidTimer)
        {
            window.clearTimeout(me.vidTimer);
            if (ENABLE_HP_VID)
                me.vidTimer = window.setTimeout(function(){me.vidTimerStart();}, HOMEPAGEVIDEO_TIMER_CLICKED_LENGTH);
        }
        me.frozen = true;
        me.vidContent.onmouseover = null;
        me.vidContent.onmouseout = me.unfreezeTimer;
    };
    this.unfreezeTimer = function()
    {
        me.frozen = false;
        if (me.vidTimer)
        {
            window.clearTimeout(me.vidTimer);
        }
        me.vidTimerStart();
        me.vidContent.onmouseover = me.freezeTimer;
    };
    
    this.activateVideo = function(auto)
    {
        if (VideoTab.prototype.current)
        {
            VideoTab.prototype.current.activate(auto);
        };
    };
    HomepageVideo.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);
        if (ENABLE_HP_VID)
	        this.vidTimer = window.setTimeout(function(){me.vidTimerStart();}, HOMEPAGEVIDEO_TIMER_LENGTH);
	    this.activateVideo(true);
    };
    HomepageVideo.prototype.vidTimerStart = this.vidTimerStart;

    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);
        this.error = false;
        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;
        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;
        };
        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)
            {
                window.setTimeout(function(){me.parent.activateVideo(auto);},1000);
            }
            else
            {
                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 (ENABLE_HP_VID & storyTimerCallback)
            storyTimerCallback();
        if (!auto)
        {  
            if (this.parent.vidTimer)
                window.clearTimeout(this.parent.vidTimer);
            if (ENABLE_HP_VID)
                this.parent.vidTimer = window.setTimeout(function(){me.parent.vidTimerStart();}, HOMEPAGEVIDEO_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.GetVideoData(this.name,this.loadComplete,this.loadError);
        this.loading = true;
    };
    
    this.preVideoInit();
};
new HomepageVideo();
