var moonmemory = new Class({

		initialize: function(eid, pictures, thumbs, options)
		{
			this.setOptions({
				renderDelay: 	1000,
				resetDelay: 	3000,
				fxDuration: 	900,
				thumbModeSize:  [100, 100],
				picModeSize: 	[400, 150],
				toggleMode: 	true
			}, options);
			
			this.pictures = pictures;
			this.thumbs = thumbs;
			
			this.mode = '';
			
			this.timerObject = null;
			
			this.element = $(eid);
			this.element.setStyle('overflow', 'hidden');
			
			this.childs = [];
			
			this.curBackPic = [];
			this.curMemoryPic = [];
			this.curThumbs = [];

			this.breakRender = false;
			
			boxself = this;		

			this.element.addEvents({
				'mouseenter' : function(){
					//boxself.pauseRender();
				},
				'mouseout' : function(){
					//boxself.continueRender();
				}
			})
			
			if(this.pictures.length > 0){
				this.reset();		
				this.render.periodical( this.options.renderDelay, this );
			}
		},
		
		setMemoryThumbs: function(){
			/* Get new pics  **/
			var thumbs = $A(this.thumbs);
			if(thumbs.length>0){
				var i = 0;
				while(this.maxCount>=i){
					if(thumbs.length==0){
						thumbs = $A(this.thumbs);
					}
					ct = thumbs.getRandom();
					this.curThumbs.include(ct);
					thumbs.erase(ct);
					i++;
				}
			}
		},
		
		getMemoryThumb: function(){
			/* Get new pic  **/
			ct = this.curThumbs.getRandom();
			this.curThumbs.erase(ct);
			return ct;		
		},
		
		addThumb: function(){
		
			var thumbsize = this.getThumbSize();
			switch(this.mode){
				case 'picture':
								if(this.curCol == this.colCount+1){
									this.curRow++;
									this.curCol = 1;
								}

								var backV = thumbsize[0] * (this.curCol-1) * -1;			
								var backH = thumbsize[1] * (this.curRow-1) * -1;			
								var thumb = new Element('div').setStyles({ display: 'block', 
																		   float: 'left', 
																		   height: thumbsize[1], 
																		   width:   thumbsize[0], 
																		   background: 'url(' + this.curMemoryPic + ') ' + backV + 'px ' + backH + 'px no-repeat #000',
																		   opacity: 0 });
								break;
								
				case 'thumb':
								var thumbS = this.getMemoryThumb();
								var thumbA = thumbS.split('|');
								thumb = new Element('a', {'href' : thumbA[1]}).setStyles({   display: 'block', 
																							 float: 'left', 
																		  					 height: thumbsize[1], 
																		   					 width:   thumbsize[0], 
																							 background: 'url(' + thumbA[0] + ') 0px  0px no-repeat #000',
																							 opacity: 0 });
								break;				
			}
			this.curCol++;
			this.element.adopt(thumb);
			this.childs.include(thumb);

			thumb.set('tween', {duration: 'short'});										   
			thumb.tween('opacity', 1);
		},

		toggleMode: function(){
			if(this.mode == 'picture'){
				this.mode = 'thumb';
			}else{
				this.mode = 'picture';
			}
		},
		
		getThumbSize: function(){
			if(this.mode == 'picture'){
				return this.options.picModeSize;
			}else{
				return this.options.thumbModeSize;
			}
		},

		reset: function(){

			if(this.mode==''){
				if (this.thumbs.length > 0) {
					this.mode = 'thumb';
				}else{
					this.mode = 'picture';
				}				
			}else{
				if(this.options.toggleMode)
					this.toggleMode();	
			}
			
			var thumbSize = this.getThumbSize();
			this.colCount = this.element.getStyle('width').toInt() / thumbSize[0];
			this.rowCount = this.element.getStyle('height').toInt() / thumbSize[1];
			this.maxCount = this.rowCount*this.colCount;
			
			/* Get new pics  **/
			var pics = $A(this.pictures);
			
			if(this.curBackPic)
				pics.erase(this.curBackPic);
				
			if(this.curMemoryPic)
				pics.erase(this.curMemoryPic);
				
			this.curBackPic = pics.getRandom();
			pics.erase(this.curBackPic);
			this.curMemoryPic = pics.getRandom();
			this.curThumbs = [];
			
			this.setMemoryThumbs();

			/* Set new Background  **/
			this.element.setStyle('background', 'url(' + this.curBackPic + ') no-repeat #000');

			/* Delete elements **/
			if(this.childs){		
				this.emptyBox();
			}
			this.curCol = 1;
			this.curRow = 1;
		},
				
		emptyBox: function(){	
			
			this.element.getChildren().fade(0);
			this.element.empty.delay(600, this.element);
			this.childs = [];	
		},
				
	    render: function(mode)
		{
			if(this.breakRender == false){

				if(this.maxCount >= this.childs.length){
					this.addThumb();		
				}else{
					this.pauseRender();			
					this.reset();
					this.continueRender();
				}				
			}
		},
		
		pauseRender: function(){
			this.breakRender = true;	
		},
		
		continueRender: function(){
			this.breakRender = false;
		}
		
})
moonmemory.implement(new Options,new Events);
