/* ********************************************** 
** BuyItForHalf.com class files
** Copyright © 2008 OmniSpear, Inc.
** Website: http://www.omnispear.com
** Created By: Jared Armstrong - OmniSpear, Inc.
** ******************************************* */
var bifh  = {
  Version: '0.0.1',
  prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
};
/**
 * Image rotation function handles lazy loading of images
 *
 */
bifh.SngRot = Class.create();
bifh.SngRot.prototype = {
		initialize: function(container,options){
			this.pos=-1;
			this.items=-1;
			this.maxDisp=4;
			this.container            = $(container);
			this.setOptions(options);

			if(!container) return;	
			
			if(this.options.lclVar==null && this.options.usePath!=null){
				this._loadVars();
			}else if(this.options.lclVar==null){
				this.options.lclVar.items=array();
			}else{
				this._attachBehaviors();
			}
			
			

		},
		setOptions: function(o){
			this.options= {
					fadeDelay:			3000,
					loadDelay:          500,
					lclVar:				null,
					usePath:			null
			};	
			Object.extend(this.options, o || {});
		},
		_loadVars: function(){
			new Ajax.Request(this.options.usePath,   
			{     
				method:'get',
				requestHeaders: {Accept: 'application/json'},
			    onSuccess: this._successload.bind(this),
               	onFailure: function(){ /*alert('Something went wrong...');*/ }
             }); 
		},
		_successload: function(transport,json){

	    	var response = transport.responseText || "no response text";       

	    	this.options.lclVar=transport.responseText.evalJSON();

           
           this._attachBehaviors();			
		},
		_attachBehaviors: function(){

		
			if(this.pos < 0){
			
				for(var i=0;i<this.maxDisp;i++){
					this._displayNext(true);
				}
				
				tmp=setTimeout(this._displayNext.bind(this,false),this.options.fadeDelay);
			}
		},
		_destroyStale: function(){
		    if(this.items < (this.maxDisp-2)) return;
		    var list = $$('#'+this.container.id+' li');
			
		    list.slice(0,1).each(function(li){ 
					
					li.id='tmp-remv';
					
					if(typeof(li.childNodes[0])!='undefined'){
						li.childNodes[0].setStyle='';
						li.childNodes[0].id='tmp-remv-div';
					}

					new Effect.Event({ afterFinish:function(){ Element.remove($(li.id)); }, position: 'end' }); 
					});
			this.items--;
		},
		_displayNext: function(){
			this.pos++;
			
			if(this.pos >= this.options.lclVar.items.length){
				this.pos=0;
			}
			
			var f=false;
			if(arguments.length > 0){
				f=arguments[0];
			}
			
			var cur = this.options.lclVar.items[this.pos];
			
			if( (cur.pic_load==false) && f==false){
				var tmpA=new Image;
				// for some reason this can seriously blow up IE
				// addEventListener supposedly works but is a pain
				if(!Prototype.Browser.IE) tmpA.onload=this._imgLoaded(this.pos,'pic');
				else cur.pic_load=true;
				
				tmpA.src=cur.pic;
				
				tmp=setTimeout(this._displayNext.bind(this,false),this.options.loadDelay);
				
				return;
			}
			
			this._destroyStale();
			
			
			var renderedString = '<DIV align="center"><A HREF="'+cur.link+'"><IMG  SRC="'+cur.pic+'" WIDTH="150" HEIGHT="114" BORDER="0" ALT="'+cur.pictxt+'"/></A></DIV><DIV align="center"><A HREF="'+cur.link+'"><IMG  SRC="'+cur.pic_btn+'" WIDTH="120" HEIGHT="35" BORDER="0" ALT="'+cur.pictxt+'"/></A></div><hr/>';
		    
		    var element = new Element('li',{ 
		      className: '', style:'height:auto;display:none'
		    }).update(renderedString);
		    
		    $('rotPanel').insert(element,{'position':'top'});
			this.items++;
			

			if(f==false){
				Effect.BlindDown(element);
			}else{
				element.setStyle({display:''});
			}
			

			element.setStyle({height:'auto'});
			
			
			if(f==false){
				tmp=setTimeout(this._displayNext.bind(this,false),this.options.fadeDelay);
			}
		},
		_imgLoaded: function(cp,lr){
			this.options.lclVar.items[cp][lr+'_load']=true;
		}
		
};