(function() {
	if(!this.slideWidgetParams || this.slideWidgetParams.length != 1) return;

	var bb_widget_base = this.bb_widget_base === undefined ? "http://widgets.breitbart.com" : this.bb_widget_base,
		json = [{"author":"Carl Kozlowski","title":"Book Excerpt: &#8216;Seize the Day Job&#8217; &#8212; Part 1","link":"http://bighollywood.breitbart.com/ckozlowski/2009/11/21/book-excerpt-seize-the-day-job-part-1/","image":"http://bighollywood.breitbart.com/files/2009/11/book1.jpg"},{"author":"Alfonzo Rachel","title":"ZoNation: Defending Palin, Calling Out Liberals","link":"http://bighollywood.breitbart.com/arachel/2009/11/21/zonation-defending-palin-calling-out-liberals/","image":"http://img.youtube.com/vi/xbUkpKUg2uw/default.jpg"},{"author":"Leo Grin","title":"For Conservative Movie Lovers: John Ford, John Wayne, and &#8216;They Were Expendable&#8217; Part 6","link":"http://bighollywood.breitbart.com/lgrin/2009/11/21/for-conservative-movie-lovers-john-ford-john-wayne-and-they-were-expendable-part-6/","image":"http://bighollywood.breitbart.com/files/2009/11/blake_edwards_they_were_expendable_2.jpg"},{"author":"Batton Lash","title":"Steve Ditko&#8217;s &#8216;The Ever Unreachable&#8217;","link":"http://bighollywood.breitbart.com/blash/2009/11/21/steve-ditkos-the-ever-unreachable/","image":"http://bighollywood.breitbart.com/files/2009/11/ditko-inkwell.gif"},{"author":"Big Hollywood","title":"Open Thread Saturday","link":"http://bighollywood.breitbart.com/bighollywood/2009/11/21/open-thread-saturday-23/","image":"http://bighollywood.breitbart.com/files/2009/11/karl_malden1.jpg"},{"author":"NewsBusters","title":"NewsBusted: What Makes Sammy White?","link":"http://bighollywood.breitbart.com/newsbusters/2009/11/20/newsbusted-what-makes-sammy-white/","image":"http://img.youtube.com/vi/wd7-WC0-4zo/default.jpg"},{"author":"Andrew Klavan","title":"Conservatives, It&#8217;s Time to Listen to Our Friends in the Mainstream Media","link":"http://bighollywood.breitbart.com/aklavan/2009/11/20/conservatives-its-time-to-listen-to-our-friends-in-the-mainstream-media/","image":"http://img.youtube.com/vi/gqgYpfsrcJw/default.jpg"},{"author":"Greg Gutfeld","title":"Daily Gut: Sarah Palin&#8217;s Books Sales Prove We&#8217;re All Racists (Again)","link":"http://bighollywood.breitbart.com/ggutfeld/2009/11/20/daily-gut-sarah-palins-books-sales-prove-were-all-racists-again/","image":"http://bighollywood.breitbart.com/files/2009/11/500x_custom_1254326184382_palin_01.jpg"},{"author":"Adam Baldwin","title":"Pledge of Allegiance to Dissent: An Intolerant \u2018Excess of Liberty\u2019?","link":"http://bighollywood.breitbart.com/abaldwin/2009/11/20/pledge-of-allegiance-to-dissent-an-intolerant-excess-of-liberty/","image":"http://img.youtube.com/vi/TZBTyTWOZCM/default.jpg"},{"author":"Gary Graham","title":"It&#8217;s Morning-After In America","link":"http://bighollywood.breitbart.com/ggraham/2009/11/20/its-morning-after-in-america/","image":"http://bighollywood.breitbart.com/files/2009/11/9-11-attacks.jpg"}],
		head = document.getElementsByTagName("head")[0],
		widget;

	if(!this.jQuery) {
		var script = document.createElement("script");
		script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js";
		script.onreadystatechange = function() {
			if(!(window.event && script.readyState) || script.readyState == "loaded"){
				if($) $(scriptLoaded);
			}
		}
		if(!script.readyState) script.onload = script.onreadystatechange; // Opera w/o.
		head.appendChild(script);
	} else {
		$(scriptLoaded);
	}

	var link = document.createElement("link");
	link.rel = "stylesheet";
	link.href = bb_widget_base + "/bighollywood/css/slidewidget.css";
	head.appendChild(link);
	head = link = null;

	function scriptLoaded() {
		var cont = $('#' + slideWidgetParams[0].id);
		widget = new SlideWidget(cont, json);
	}

	var template =
		'<div class="slideNewsDiv">' +
			'<div class="slideNewsLogoDiv"></div>' +
			'<span class="slideNewsLogoDivArrow"/>' +
			'<div class="slideNewsButtonsDiv">' +
				'<span class="slideNewsLeftArrowBut"></span><span class="slideNewsRightArrowBut"></span>' +
			'</div>' +
			'<div class="slideNewsScrollDivFix">' +
					'<div class="slideNewsScrollDivOuter">' +
					'<div class="slideNewsScrollDiv"></div>' +
				'</div>' +
			'</div>' +
		'</div>';

	function SlideWidget(outerCont, json) {
		var cont = $(template).appendTo(outerCont);
		this._scrollDivOuter = cont.find("div.slideNewsScrollDivOuter");
		this._scrollDiv = cont.find("div.slideNewsScrollDiv");
		var self = this;
		cont.find(".slideNewsRightArrowBut").bind("click dblclick", function(ev) { self._rightArrowClick(ev); cancel(ev); }).bind("selectstart", cancel);
		cont.find(".slideNewsLeftArrowBut").bind("click dblclick", function(ev) { self._leftArrowClick(ev); cancel(ev); }).bind("selectstart", cancel);
		this._renderItems(json);
		this._autoScrollInterval = setInterval(function(){ self._autoScroll(); }, 10000);
		cont.bind("mouseenter", function(){ clearInterval(self._autoScrollInterval); });
	}
	function cancel(ev) {
		ev.preventDefault();
	}
	function itemClick(ev) {
		var links = $(this).find("a:first");
		if(links.length) {
			document.location = links.attr("href");
		}
	}
	SlideWidget.prototype = {
		_renderItems: function(json) {
			this._items = [];
			for(var i = 0; i < json.length; i++) {
				var jsonItem = json[i];
				var item = $('<span class="slideNewsItem"></span>').click(itemClick);
				var inner = $('<div class="slideNewsItemInner"></div>').appendTo(item);
				if(jsonItem.image) inner.append($('<img/>').attr("src", jsonItem.image));
				else item.addClass("slideNewsNoImage");
				$('<a/>').html(jsonItem.title).attr("href", jsonItem.link).appendTo(inner);
				$('<div class="slideNewsNumber">' + (i + 1) + ' of ' + json.length + '</div>').appendTo(inner);
				if(jsonItem.author) $('<div class="slideNewsAuthor"/>').text(jsonItem.author).appendTo(inner);
				this._scrollDiv.append(item);
				this._items.push(item);
			}
			if(this._items.length) {
				this._lastItem = this._items[this._items.length - 1];
			}
		},
		_firstVisibleItemIx: function() {
			var x = this._scrollDiv.position().left;
			//console.log(x);
			for(var i = 0; i < this._items.length; i++) {
				var item = this._items[i];
				x += item.width();
				if(x > 0) return i;
			}
			return -1;
		},
		_rightArrowClick: function() {
			if(this._scrollDiv.is(":animated")) return;
			var ix = this._firstVisibleItemIx();
			if(ix < 0 || ix >= this._items.length - 1) return;

			var lastRightX = this._lastItem.position().left + this._lastItem.width();
			var offset = 0;
			if(lastRightX > this._scrollDivOuter.width()) {
				var offset = -this._items[ix + 1].position().left;
				var screenLastRightX = lastRightX - this._items[ix + 1].position().left - this._scrollDivOuter.width();
				if(screenLastRightX < 0) offset -= screenLastRightX;
			}
			var diff = this._scrollDiv.position().left - offset;
			this._scrollDiv.animate({left: offset + "px"});
			return diff;
		},
		_leftArrowClick: function() {
			if(this._scrollDiv.is(":animated")) return;
			var ix = this._firstVisibleItemIx();
			if(ix < 0 || ix >= this._items.length - 1) return;
			var lastRightX = this._lastItem.position().left + this._lastItem.width();
			var offset = 0;
			if(lastRightX > this._scrollDivOuter.width()) {
				if(-this._scrollDiv.position().left != this._items[ix].position().left) ix++;
				if(ix > 0) {
					var offset = -this._items[ix - 1].position().left;
				}
			}
			var diff = this._scrollDiv.position().left - offset;
			this._scrollDiv.animate({left: offset + "px"});
			return diff;
		},
		_autoScroll: function() {
			if(this._rightArrowClick() == 0) {
			    this._scrollDiv.animate({left: 0});
			}
		}
	}
})();