var NewsTabber = new JS.Class({
    initialize: function(options) {
        this.options = options || {};
    },
    
    setup: function() {
        this.stories = [];
        $('.' + this.options.className).forEach(function(el, i) {
            this.stories.push(new this.klass.Story(this, el, i));
        }, this);
        this.stories.forEach('hide');
        this.state = this.getInitialState();
        this.setCurrentStory(this.stories[this.state.story]);
        this.insertTabs();
        return this;
    },
    
    getInitialState: function() { return {
        story: 0
    }},
    
    changeState: function(state) {
        if (state.story !== undefined)
            this.setCurrentStory(this.stories[state.story]);
    },
    
    setCurrentStory: function(story) {
        this.state.story = story.id;
        if (this.currentStory) this.currentStory.hide();
        this.currentStory = story;
        story.show();
    },
    
    insertTabs: function() {
        this.tabHolder = $( $.HTML.div({className: 'newsTabs'}) );
		if (this.options.insertTabs) {
			loop: for (var key in this.options.insertTabs) {
				Ojay(this.options.insertTabs[key]).insert(this.tabHolder.node, key);
				break loop;
			}
		} else {
			this.stories[this.stories.length - 1].element.insert(this.tabHolder.node, 'after');
		}
        this.stories.map('trigger.getElement.node').forEach(this.tabHolder.method('insert'));
    },
    
    extend: {
        Story: new JS.Class({
		    initialize: function(owner, element, id) {
		        this.owner = owner;
		        this.element = element;
		        this.id = id;
		        this.trigger = new this.klass.Trigger(this);
		    },
		    
		    show: function() {
		        this.element.show();
		        this.trigger.select();
		    },
		    
		    hide: function() {
		        this.element.hide();
		        this.trigger.deselect();
		    },
		    
		    extend: {
		        Trigger: new JS.Class({
				    initialize: function(story) {
				        this.story = story;
				        var find = this.story.element.method('descendants');
				        this.headline = find('h2').node.innerHTML.stripTags();
				        this.imageSource = find('.thumbnail img').node.src;
				        this.getElement();
				        this.element.on('click', function(el, ev) {
				            this.story.owner.changeState({story: this.story.id});
				        }, this);
				    },
				    
				    getElement: function() {
				        if (this.element) return this.element;
				        return this.element = $( $.HTML.div({className: 'newsTrigger'}, function(h) {
				            h.img({src: this.imageSource});
				            h.h5().innerHTML = this.headline;
				        }.bind(this)) );
				    },
				    
				    select: function() { this.getElement().addClass('selected'); },
				    
				    deselect: function() { this.getElement().removeClass('selected'); }
				})
		    }
		})
    }
});

function imageCaptionMouseOver(t) {
	var em = t.getElementsByTagName('em');
	var a = t.getElementsByTagName('a');
	if (!a[0]) return; // only change caption color if image is a link
	if (!em[0]) return;
	em[0].style.color = '#C33C30';
}

function imageCaptionMouseOut(t) {
	var em = t.getElementsByTagName('em');
	var a = t.getElementsByTagName('a');
	if (!a[0]) return; // only change caption color if image is a link
	if (!em[0]) return;
	em[0].style.color = '#888';
}

function checkPoll()
{
	var a1 = getEl('PollAnswer1') && getEl('PollAnswer1').checked;
	var a2 = getEl('PollAnswer2') && getEl('PollAnswer2').checked;
	var a3 = getEl('PollAnswer3') && getEl('PollAnswer3').checked;
	var a4 = getEl('PollAnswer4') && getEl('PollAnswer4').checked;
	var isComp = getEl('email') && 1;
	alert("HERE");
	if(!a1 && !a2 && !a3 && !a4)
	{
	    if (isComp)
	    {
	        var email = getEl('email').value;
	        if (email == null || email.length() < 1)
	        {
	            alert("You must enter you email address");
	        }
	        else
	        {
	            alert("You must select an answer.");
	        }
	    }
		alert("You must select an answer.");
		return false;
	}
}

// Live scores
Ojay.onDOMReady(function() {
	Ojay('a.livescores-link').on('click', function(link, evnt) {
	    evnt.stopEvent();
	    openLiveScores(link.node.href);
	});
});

openLiveScores = function(url) {
    window.open(url,'livescores','width=700,height=500,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
};
