// JavaScript Document


Listing = function( numOfItemsPerPage , name ){
	
	this.numOfItemsPerPage = numOfItemsPerPage;
	
	this.name = name;
	
	this.pageOffset = 0;
	
	this.Items = [];
	
	
	
	this.addItem = function( Item ){
		this.Items.push( Item );
	}
	
	this.switchToPage = function( pageOffset ){
		this.pageOffset = pageOffset;
		
		for( var i=0 ; i<this.Items.length ; i++ )
		{
			var Item  =  this.Items[ i ];
			if(   i  >=  this.numOfItemsPerPage * this.pageOffset   &&   i  <  this.numOfItemsPerPage * (this.pageOffset+1)   ){
				Item.style.display = "block";
			} else {
				Item.style.display = "none";
			}
			
		}
		
		this._broadcastEvent( "onSwitchPage" );
		
		if( this.onSwitchPage )  this.onSwitchPage();
		
		
		
	}
	
	
	this.switchToNextPage = function(){
		
		if(  (this.pageOffset+1) * this.numOfItemsPerPage  >=  this.Items.length  )     return;
		this.switchToPage(  this.pageOffset + 1  );
	}
	this.switchToPrevPage = function(){
		if(   this.pageOffset <= 0   )    return;
		this.switchToPage(  this.pageOffset - 1  );
	}
	
	this.initiate = function(){
		this.switchToPage( this.pageOffset );
	}
	
	this._eventListeners = new Array;
	this.addListenerFunc = function( funcName , eventName ){
		var Listener = new Object;
		Listener.funcName = funcName;
		Listener.eventName = eventName;
		this._eventListeners.push( Listener );
	}
	this._broadcastEvent = function ( eventName ){
		for(  var i  in  this._eventListeners  ){
			var Listener  =  this._eventListeners[ i ];
			if( Listener.eventName == eventName ){
				eval(  Listener.funcName + "()"  );
			   }
		}
	}
	
}

function outputListingNavigationPageNums( listingId ){
	var Listing = eval(listingId);
	
	Listing.onSwitchPage = function(){
		var PageBtn;
		for(   var i=1   ;   PageBtn  =  findObj( listingId + "PageBtn" + i )   ;   i++   ){
			if( i-1 == Listing.pageOffset ) {
				PageBtn.className = "pageNumBtn selected";
				PageBtn.isActive = false;
			} else {
				PageBtn.className = "pageNumBtn mouseOut";
				PageBtn.isActive = true;
			}

		}
	}
	
	var output="";
	for(  var i=1  ;  (i-1) * Listing.numOfItemsPerPage  <  Listing.Items.length  ;  i++  ){
		if( !output )  output += "|";
		output  +=  ' <span id="'+listingId+'PageBtn'+i+'" onClick="if( this.isActive ){  '+listingId+'.switchToPage('+(i-1)+');  } " onMouseOver="if( this.isActive ){  this.className = \'pageNumBtn mouseOver\';  } " onMouseOut="if( this.isActive )  this.className = \'pageNumBtn mouseOut\';">'+i+'</span> ';

		output  +=  "|";
	}
	document.write( output );
}


function makeListingPreservePageOffset( Listing , identifierValue ){
	
	Listing.addListenerFunc(  Listing.name + ".offsetPreserverFunc" , "onSwitchPage" );
	
	Listing.identifierValue = identifierValue;
	
	Listing.offsetPreserverFunc = function(){
		setCookie(  this.name + "PageOffset"  ,  this.pageOffset  );
		setCookie(  this.name + "IdentifierValue"  ,  this.identifierValue  );
	}
	
	var listingPageOffsetCookie    =    parseInt(   getCookie(  Listing.name + "PageOffset"  )   );
	if(   listingPageOffsetCookie    &&    parseInt( getCookie( Listing.name + "IdentifierValue" ) )   ==   parseInt( Listing.identifierValue )     ){
		Listing.pageOffset = listingPageOffsetCookie;
	}

}
