﻿Vehix$Web$Consumer$Portal$UserControls$Headline = function(cnfg) {
	this._stories = new Array();
	this._count = 0;
	this._currentPosition = 0;
	this._intervalSeconds = cnfg.IntervalSeconds;
	this._headlineImageID = cnfg.HeadlineImageID;
	this._headlineTitleID = cnfg.HeadlineTitleID;
	this._headlineAuthorID = cnfg.HeadlineAuthorID;
	this._headlineBlurbID = cnfg.HeadlineBlurbID;
	this._headlineHyperLinkID = cnfg.HeadlineHyperLinkID;
	this._headlineParentTitleID = cnfg.HeadlineParentTitleID;
	this._headlineImageLinkID = cnfg.HeadlineImageLinkID;
}

Vehix$Web$Consumer$Portal$UserControls$Headline.prototype =
{
	_bind: function(fnc) {
		var obj = this;
		temp = function() {
			return fnc.apply(obj, arguments);
		};
		return temp;
	},

	AddStory: function(story) {
		this._count = this._stories.push(story);
	},

	ReplaceStory: function(pos, story) {
		if (pos >= 0 && pos <= this._count) {
			this._stories[pos] = story;
		}
	},

	StartRotation: function() {
		if (this._count > 0) {
			this._currentPosition = 1;
			var rotate = this._bind(this.Rotate);
			this._interval = setInterval(rotate, this._intervalSeconds);
		}
	},

	Rotate: function() {
		var pos = this._currentPosition < this._count ? this._currentPosition : 0;
		this._currentPosition = pos + 1;
		this.SetPosition(pos);
	},

	StopRotation: function() {
		clearInterval(this._interval);
		if (typeof (arguments[1]) != "undefined")
			s_objectID = arguments[1];
		if (!isNaN(arguments[0]))
			this.SetPosition(arguments[0]);
	},

	NavigateToStory: function(navurl, soid) {
		s_objectID = soid;
		window.location.href = navurl;
	},

	SetPosition: function(pos) {
		var story = this._stories[pos];
		var nav = this._bind(this.NavigateToStory);
		$("#" + this._headlineImageID).attr("src", story.ImageUrl);
		$("#" + this._headlineImageID).attr("alt", story.Title);
		$("#" + this._headlineTitleID).text(story.Title);
		$("#" + this._headlineTitleID).attr("href", "javascript:void(0)");
		$("#" + this._headlineTitleID)[0].onclick = function() { nav(story.ReadMoreUrl, "Headline".concat(pos, "TitleLink")) };
		$("#" + this._headlineParentTitleID).text(story.ParentTitle);
		$("#" + this._headlineAuthorID).text(story.Author);
		$("#" + this._headlineBlurbID).text(story.Blurb);
		$("#" + this._headlineHyperLinkID).text(story.ReadMoreText);
		$("#" + this._headlineHyperLinkID)[0].onclick = function() { nav(story.ReadMoreUrl, "Headline".concat(pos, "MoreLink")) };
		$("#" + this._headlineHyperLinkID).attr("href", "javascript:void(0)");
		$("#" + this._headlineImageLinkID)[0].onclick = function() { nav(story.ReadMoreUrl, "Headline".concat(pos, "ImageLink")) };
		$("#" + this._headlineImageLinkID).attr("href", "javascript:void(0)");

		for (var i = 0; i < this._count; i++) {
			$("#" + this._stories[i].ButtonClientID).attr("class", "image headlineButtonImage");
		}
		$("#" + this._stories[pos].ButtonClientID).attr("class", "image headlineButtonImageSelected");
	}

}
