function RegBoxImpl() {
	
	// statics
	var initalLoadSize = 25, initalPosition = 13, prefetchLoadSize = 6;

	// dynamic
	var prefetchCounter = 0;
	var RegBox;
	var regionId;
		
	this.setup = function setup() {
		
		regionId = $('#regbox').attr('rel');
		
		RegBox = new CRegBox({
	        listener: function(name, p1, p2, p3, p4){
						
	            // Notable Variables
	            // this.Objs    objects queue
	            // this.pos     index of the object being shown
	            // this.photow  photo display width
	            // this.photoh  photo display height
	            
	            switch(name){
	                case 'remove':      // object removal. p1 = object being removed
	                    break; 
	                case 'change':      // when the listing being shown changes. p1: left-most object being show. p2: current index.
	                	
	                	
	                	var queuePosition = p1.position;
	                	var currentIndex = p2;
	                	var previousIndex = p3;
	                	var moveForward = p4;
	                	
	                	moveForward ? prefetchCounter++ : prefetchCounter--;
	                		                	
	                	if ( prefetchLoadSize == Math.abs(prefetchCounter) ) {
	                		
	                		var reqPosStart; // Internal position - retrieved from objs
	                		var reqIndex;
	                			                		
		                	if ( moveForward ) {
		                		reqPosStart = ( this.Objs.length > 0 ? this.Objs[this.Objs.length-1].position : 0 );
		                		readNext( reqPosStart );
		                	} else {
		                		reqPosStart = this.Objs[0].position;
		                		readPrevious( reqPosStart );
		                	}	
		                	// reset prefetch counter
	                		prefetchCounter = 0;
	                	}
	                    break;
	                case 'photoerror':  // photo downloading error
	                    break;
	                case 'photoloaded': // object being added, photo downloaded. p1: success. p2: object being loaded. p3: image jQuery object. 
	                    break;
	                case 'isloaded':    // when show() gets called.  Initiation.
	                    break;
	                }
	            },
	        defaultsrc: "/resources/eda49c056d/images/vrfresh/featbox-nophoto.jpg",
	        coverdelay: -1,
	        pos: initalPosition,
	        max: initalLoadSize,
	        debug: false
	        });
		
		readInitial();
	}
	
	var readInitial = function readInitial() {
		handleAjaxRequest('initial.htm?regionNavId=' + regionId + '&numResults=' + initalLoadSize, handleAjaxInitialSuccess);
	}
		
	var readNext = function readNext(position) {
		handleAjaxRequest('next.htm?regionNavId=' + regionId + '&numResults=' + prefetchLoadSize + '&lastPosition='+position, handleNextAjaxSuccess);
	}
	
	var readPrevious = function readPrevious(position) {
		handleAjaxRequest('previous.htm?regionNavId=' + regionId + '&numResults=' + prefetchLoadSize + '&lastPosition='+position, handlePreviousAjaxSuccess);
	}
	
	function handleAjaxRequest(url, callback) {
		
		$.ajax({
			type: 'GET',
			cache: false,
			url: '/queue/' + url,
			success: callback,
			dataType: 'json',
			error : handleAjaxError 
		});
		return false;
	}
	
	function handleAjaxInitialSuccess(data) {

		RegBox.show(convertToArray(data));
	}
	
	function handleNextAjaxSuccess(data) {
		
		RegBox.add(convertToArray(data), false);
	}
	
	function handlePreviousAjaxSuccess(data) {
		
		RegBox.add(convertToArray(data).reverse(), true);
	}
		
	function convertToArray(spotlights) {
		var featObjs = new Array;
		$(spotlights).each(function(){
			featObjs.push(convert(this));
		});
		return featObjs;
	}
	
	function convert(spotlight) {
		return {
			position: spotlight.position,
		    id: spotlight.listingId,
		    name: spotlight.propertyName,
		    url: spotlight.propertyLink + "?promoType=spotlight",
		    summary: spotlight.summary,
		    photosrc: spotlight.imageSrc,
		    photoalt: spotlight.imageAlt,
		    hasfreebie: spotlight.hasFreebie,
		    hassavings: spotlight.hasSaving,
		    toString: function() {
		    	return "Spotlight["+spotlight.position+"]";
		    }
		}
	}
	
	function handleAjaxError(XMLHttpRequest, textStatus, errorThrown) {
		if (textStatus == 'timeout') {
			if(window.console) {
				console.debug('The request took too long. Please try again.');
			}
		} else {
			// console.debug('An error occured. Please try again or contact customer support.');
			alert('An error occured. Please try again or contact customer support.');
		}
	}
	
}

function sendContainerTracking() {
	sendContainerClickedOmniture('RE', 'R1', 'DealsHover');
	return true;
}

function sendContainerTrackingSpecific(i) {
	var columnId = 'C'+(i+1);
	sendContainerClickedOmniture('RE', 'R1', columnId);
	return true;
}

