var cB = {
	takeStep: function() {
		var from = cB.container.scrollLeft;
		var to = from + cB.size;
		new Effect.Tween(cB.container, from, to, { duration: 0.3, transition: Effect.Transitions.linear, queue: cB.queue, afterFinish: cB.requeue }, 'scrollLeft');
	},
	requeue: function() {
		if ((cB.container.scrollLeft % cB.itemWidth) == 0) {
			cB.listing.appendChild(cB.listing.firstChild);
			cB.container.scrollLeft = 0;
		};
		if (cB.on) { cB.takeStep() };
	},
	shouldContinue: function(e) {
		var to = (e.type == 'mouseover') ? e.element() : $(e.relatedTarget||e.fromElement);
		var from = (e.type == 'mouseover') ? $(e.relatedTarget||e.fromElement) : e.element();
		if (!to.descendantOf(cB.container) && from.descendantOf(cB.container)) {
			cB.on = true;
			cB.takeStep();
		} else {
			cB.on = false;
		}
	},
	init: function() {
		cB.listing = jsonFeedParser.feedContainer;
		cB.container = jsonFeedParser.feedContainer.up();
		cB.leftness = 0;
		cB.size = 29;
		cB.container.scrollLeft = 0;
		cB.itemWidth = cB.listing.down('li').getWidth();
		cB.queue = { scope: 'cB', position: 'end', limit: 1 };
		cB.on = true;
		cB.takeStep();		
		cB.container.observe('mouseover', cB.shouldContinue).observe('mouseout', cB.shouldContinue);
	}};

var conveyorBeltOptions = {
	feedID: 'jobsearch_conveyor_belt',
	feedContainer: 'cbListing',
	templateHTML: '<li class="item"><span class="image image_#{image_index}"></span><div class="job"><ul><li><a href="#{job_url}">#{title}</a></li><li>#{location_text}</li><li>#{salary}</li></ul></div></li>',
	afterInit: function() {
		this.onResultsPage = window.location.href.indexOf('/search.cgi')>-1;
		if ((noRunOfSite == '1') && !(this.onResultsPage)) { return };
		new Insertion.Bottom('conveyorBelt', '<ul id="cbListing"></ul>');
		this.setUp();
	},
	afterSetup: function() {
		var d = new Date();
		d = '&d='+d.getTime();
		if (!this.onResultsPage) {
			this.dataURL += '&default=1';
		}
		this.loadData();
	},
	afterLoad: function() {
		
		// if no results, stop
		if (!this.feedData.length) {
			return;
		}
		
		// if not enough and not already default
		if (this.feedData.length<4 && (this.dataURL.indexOf('&default=1') == -1)) {
			this.dataURL += '&default=1';
			this.loadData();
			return;
		}

		// add randomization lookup
		var origData = this.feedData.clone();
		this.feedData = [];
		var plainCount = $A($R(0,origData.length-1));
		while (plainCount.length) {
			var index = plainCount.splice(Math.floor(Math.random()*plainCount.length), 1).first();
			this.feedData.push(origData[index]);
		}
		
		// add image index loop & change 'none specified' in locations
		var j=0;
		for (var i=0;i<this.feedData.length;i++) {
			if (j<7) { j++; } else { j=0 };
			this.feedData[i].image_index = j;
			this.feedData.salary = this.feedData[i].salary.replace('None specified','&nbsp;')
		}
		
		// get on with it
		this.parseData();
	},
	afterParse: function() {
		this.finish();
		cB.init();
	}
}

if (Event.observe) {
	document.observe('dom:loaded', function() {
		jsonFeedParser.initialize(conveyorBeltOptions)
	});
}

