
function CFeatBox(element_or_id, attrs){
	if(typeof(attrs)!='object') attrs = {};
	
	// configurable
	this.max = Math.max(attrs.max || 10, 4); // max must be 4 or larger, default is 10
	this.timer = attrs.timer || 5000;
	this.autoplay = (typeof(attrs.autoplay)=='undefined') ? true : attrs.autoplay;
	this.listener = attrs.listener || function(){};
	this.defaultsrc = attrs.defaultsrc || "/resources/eda49c056d/images/vrfresh/featbox-nophoto.jpg";
	this.pos = attrs.pos || 0;
	this.debug = attrs.debug || false;
	this.disableSlide = attrs.disableSlide || false;
	this.coverdelay = attrs.coverdelay || 2000;

	// not configurable
	this.Objs = new Array();
	this.photow = 282;
	this.photoh = 210;
	
	// create
	var ThisObj = this;
	this.Element = (typeof(element_or_id)=='string') ? $('#' + element_or_id) : element_or_id;
	if(!this.Element[0]) return; // error
	
	this.Element.append(this.Prev = $('<a href="#" class="featbox-prev"><img src="/resources/eda49c056d/images/vrfresh/t.gif" />previous</a>'));
	this.Element.append(this.Next = $('<a href="#" class="featbox-next"><img src="/resources/eda49c056d/images/vrfresh/t.gif" />next</a>'));
	this.Element.append(this.Pause = $('<a href="#" class="featbox-pause"><img src="/resources/eda49c056d/images/vrfresh/t.gif" />pause</a>'));
	this.Element.append(this.Play = $('<a href="#" class="featbox-play"><img src="/resources/eda49c056d/images/vrfresh/t.gif" />play</a>'));
	this.Element.append(this.Info = $('<div class="featbox-info"></div>'));
	this.Info.append(this.LeftInfo = $('<div class="featbox-leftinfo"></div>'));
	this.Info.append(this.RightInfo = $('<div class="featbox-rightinfo"></div>'));
	
	this.LeftInfo.append(this.Link = $('<a href="#" onclick="sendContainerTracking()"></a>'));
	this.LeftInfo.append(this.Bullet = $('<div class="featbox-bullet"></div>'));
	
	this.RightInfo.append(this.Freebie = $('<img src="/resources/eda49c056d/images/vrfresh/t.gif" class="featbox-freebie" />').hide());
	this.RightInfo.append(this.Savings = $('<img src="/resources/eda49c056d/images/vrfresh/t.gif" class="featbox-savings" />').hide());
	
	this.Element.append(this.PhotoBox = $('<div class="featbox-photo"></div>'));
	this.Element.append(this.OverPhotoBox = $('<div class="featbox-overphoto"></div>'));
	this.Element.append(this.PhotoLink = $('<a href="#" onclick="sendContainerTracking()" class="featbox-photolink"></a>'));
	this.Element.append(this.LeftBox = $('<div class="featbox-left-box"></div>'));
	this.Element.append(this.RightBox = $('<div class="featbox-right-box"></div>'));
	this.Element.append(this.Left = $('<div class="featbox-left"></div>'));
	this.Element.append(this.Right = $('<div class="featbox-right"></div>'));
	this.Element.append(this.Cover = $('<div class="featbox-cover"></div>'));
	
	// top number
	var Bold = this.Element.find('p b');
	if(Bold[0]){
		var arr = Bold.html().split("");
		str = '';
		for(var i=0; i<arr.length; i++) str += '<span>' + arr[i] + '</span>';
		Bold.html(str);
	}
	
	// handlers
	var ThisObj = this;
	this.Prev.click(function(){ThisObj.offset(-1); if (ThisObj.play) { ThisObj.play(false); } return false;});
	this.Next.click(function(){ThisObj.offset(1); if (ThisObj.play) { ThisObj.play(false); } return false;});
	this.Play.click(function(){ThisObj.play(true); return false;});
	this.Pause.click(function(){ThisObj.play(false); return false;});
}

CFeatBox.prototype.status = function(name, bool, callListener){
	var cur = this.Element.hasClass('featbox-' + name);
	if(typeof(bool)=='undefined') return cur;
	if((bool && cur) || (!bool && !cur)) return; // do nothing
	if(bool) this.Element.addClass('featbox-' + name);
	else this.Element.removeClass('featbox-' + name);
	if(callListener) this.listener.call(this, name, bool);
}

CFeatBox.prototype.playing = function(){return this.playid ? true : false;},
CFeatBox.prototype.play = function(bool){
	if(typeof(bool)=='undefined') bool = true;
	if((bool && this.playid) || (!bool && !this.playid)) return; // do nothing
	if(bool){
		var ThisObj = this;
		if(this.status('isloaded')) this.offset(1);
		this.playid = setInterval(function(){ThisObj.offset(1);}, ThisObj.timer);
		this.Element.removeClass('featbox-ispausing');
		}
	else{
		clearInterval(this.playid);
		this.playid = 0;
		}
	this.status('ispausing', bool?false:true);
	this.status('isplaying', bool?true:false, true);
}

CFeatBox.prototype.update = function(Obj){
	this.status('isone', (this.Objs.length==1));
	this.status('istwo', (this.Objs.length==2));
	this.status('ismulti', (this.Objs.length>1));
}

CFeatBox.prototype.cleanup = function(num, isReverse){
		
	if(this.Objs.length<=this.max) return;
	
	var remObjs = new Array();
	var holdPos = this.pos;
	if (isReverse) {
		// Delete from end
		remObjs = this.Objs.splice(this.Objs.length-num, num);
		this.pos = holdPos + num;
	} else {
		// Delete from start
		remObjs = this.Objs.splice(0, num);
		this.pos = holdPos - num;
	}
	
	if( this.debug && window.console ) {
		console.debug('Removed ' + remObjs.toString() + ' removed from the ' + (isReverse ? 'end' : 'beginning'));
		console.debug("Queue State: " + this.Objs.toString());
	}
}

CFeatBox.prototype.add = function(mixed, isReverse){
		
	var thisObj = this;
	
	if (mixed instanceof Array) {
		
		if (isReverse) {
			this.Objs = mixed.concat(this.Objs);
		} else {
			this.Objs = this.Objs.concat(mixed);
		}
		
		$(mixed).each(function(){
			thisObj.listener.call(thisObj, 'add', this);
			thisObj.loadImage(this);
		});
		
		this.cleanup(mixed.length, isReverse);
		
	} else {
		
		if (isReverse) {
			this.Objs.unshift(mixed);
		} else {
			this.Objs.push(mixed);
		}
		this.listener.call(this, 'add', mixed);
		this.loadImage(mixed);
		
		this.cleanup(1, isReverse);
	}
		
	if( this.debug && window.console ) {
		console.debug('Added ' + mixed.toString());
		console.debug("Queue State: " + this.Objs.toString());
	}	
}


CFeatBox.prototype.show = function(arr){
	var ThisObj = this;
	if(this.coverdelay)setTimeout(function(){ThisObj.show2(arr); ThisObj.Cover.fadeOut(); }, this.coverdelay);
	else this.show2(arr);
	}

CFeatBox.prototype.show2 = function(arr){
	if(arr instanceof Array){
		this.add(arr);
	}
	if(!this.Objs.length) {
		return; // error
	}
	if(this.status('isloaded')) return;
	if(this.autoplay)this.play();
	else this.status('ispausing', true);
	this.status('isloaded', true, true);
	this.update();
	this.go(this.pos, true);
}

CFeatBox.prototype.center = function(Obj){
	if(Obj.iserror)return Obj.Image.width(this.photow).height(this.photoh);
	if(Obj.photow) return; // already centered
	
	// scale
	Obj.photow = Obj.Image.width();
	if(!Obj.photow) return; // not visible yet
	Obj.photoh = Obj.Image.height();
	if((Obj.photow>this.photow) || (Obj.photoh>this.photoh)){
		var ratio = Math.min(this.photow/Obj.photow, this.photoh/Obj.photoh);
		Obj.Image.width(Obj.photow = Math.round(ratio*Obj.photow));
		Obj.Image.height(Obj.photoh = Math.round(ratio*Obj.photoh));
		}
	
	// center
	if((Obj.photoh>50) && (Obj.photoh<this.photoh))Obj.Image.css('top', Math.round((this.photoh/2) - (Obj.photoh/2))); // 40 is the height of missing image icons for IE6
}

CFeatBox.prototype.loadImage = function(Obj){
	
	if(Obj.Image) return; // already done
	var ThisObj = this;
	Obj.ImageDiv = $('<div></div>').append(Obj.Image = $(new Image()));
	if(Obj.photoalt) Obj.Image.attr('alt', Obj.photoalt);
	Obj.SmallImage = $('<img />').attr({src: Obj.photosrc, alt: Obj.photoalt});
	Obj.iserror = false;

	var onerror = function(){
		Obj.ImageDiv.addClass('featbox-photo-iserror');
		Obj.iserror = true;
		if(Obj.Image.attr('src') !== ThisObj.defaultsrc){
			Obj.Image.attr('src', ThisObj.defaultsrc);
			Obj.SmallImage.attr('src', ThisObj.defaultsrc);
		}
		ThisObj.listener.call(ThisObj, 'photoerror', Obj, Obj.Image);
	}
	
	var onload = function(response, status, xhr){
		Obj.ImageDiv.addClass('featbox-photo-isloaded');
		ThisObj.center(Obj);
		ThisObj.listener.call(ThisObj, 'photoloaded', !Obj.iserror, Obj, Obj.Image);
	}
	
	Obj.Image.load(onload).error(onerror).attr('src', Obj.photosrc || this.defaultsrc);// *finally*, set the src attribute of the new image to our image
}

CFeatBox.prototype.offset = function(offset){this.go(this.pos + offset);}
CFeatBox.prototype.go = function(n, isinit){
	if(!this.Objs.length || this.islocked) return;
	var isforward = isinit ? true : (n>this.pos);
	var prevpos = this.pos;
	if(n<0) n = this.Objs.length + n;
	n = n % this.Objs.length;
	if(!isinit && (n==this.pos)) return; // no changes
	this.islocked = true;
	var ThisObj = this;
	var Obj = this.Objs[this.pos = n];
	
	// append
	this.OverPhotoBox.html(Obj.ImageDiv);
	this.center(Obj);
	
	// animate main image
	if (this.disableSlide) {
		ThisObj.PhotoBox.children().remove();
		ThisObj.PhotoBox.append(Obj.ImageDiv);
		ThisObj.islocked = false;
	} else {
		Obj.ImageDiv.css({left: isforward?200:-200}).animate({left:'0'},'slow', function(){
			ThisObj.PhotoBox.children().remove();
			ThisObj.PhotoBox.append(Obj.ImageDiv);
			ThisObj.islocked = false;
		});
	}
	
	// set variables
	this.PhotoLink.attr('href', Obj.url||'');
	this.Link.html(Obj.name||'').attr('href', Obj.url||'');
	this.Bullet.html(Obj.summary||'');
	Obj.hasfreebie ? this.Freebie.show() : this.Freebie.hide();
	Obj.hassavings ? this.Savings.show() : this.Savings.hide();
	this.status('isdeal', (Obj.hasfreebie||Obj.hassavings));
	
	// side photos
	if(this.Objs.length>1){
		n = this.pos ? (this.pos-1) : (this.Objs.length-1);
		this.LeftBox.html(this.Objs[n].SmallImage);
		n = (this.pos+1) % this.Objs.length;
		this.RightBox.html(this.Objs[n].SmallImage);
	}
	
	// reset timer
	if(this.playid){ 
		clearInterval(this.playid);
		this.playid = setInterval(function(){ThisObj.offset(1);}, ThisObj.timer);
	}
	this.listener.call(this, 'change', this.Objs[this.pos], this.pos, prevpos, isforward);
}

