var changeImg = Class.create({
	initialize:function(arrA,arrB,desc,interval,className){
				this.arrA = arrA;
				this.arrB = arrB;
				this.desc = desc;
				this.interval = interval || 6;
				this.className = className || 'here';
				this.thisAdd = 0;
				this.thisAdd01 = null;
				this.scrollId,othis = this;
				this.arrA.each(function(n,j){
					n.setStyle({zIndex:60 - j});
					});
				this.arrB.each(function(m){
						m.writeAttribute({href:'javascript:void(0);'});
						//m.setOpacity(0.7);
					});
				this.render();
				this.start();
	},
	
	render:function(){
		  if(this.desc){
		    this.desc[0].setStyle({left:600 + 'px',top:440 + 'px',opacity:0});
		    this._updateCaption(this.arrA[othis.thisAdd], this.desc[0]);
		    new Effect.Parallel( 
		         [ 
		         new Effect.Move(this.desc[0], {x:-590, y:-430,sync: true}), 
		         new Effect.Opacity(this.desc[0],{from:0,to:1, sync: true }) 
		         ], 
		         {duration:1} 
		         );	
			};
			this.observe();
	},
	
	observe:function(){
			this.arrB.each(function(o,k){
				o.observe('click',function(){
					othis.stop();
					othis.thisAdd01 = k;
					if(othis.thisAdd != k){
						othis.arrB.invoke('hide');
						othis.click();
						othis.start();						
					}else{
						if(othis.desc){othis.desc[0].setStyle({left:10 + 'px',top:10 + 'px'});};
						othis.start();						
					}
				});
			})
	},
	
	start:function() {
		if(!this.scrollId){	
				this.scrollId = new PeriodicalExecuter(othis.goOn.bind(othis), othis.interval);
				}
	},

	stop:function() {
		if(this.scrollId){
			this.scrollId.stop();
			this.scrollId = 0;
		}
	},

	goOn:function() {
		new Effect.Fade(this.arrA[this.thisAdd], {duration:1, from:1.0, to:0.0,beforeFinish:function(){
			othis.arrB.invoke('removeClassName',othis.className);
		}});
		
		this.thisAdd++;
		if(this.thisAdd==this.arrA.length)this.thisAdd=0;
		new Effect.Appear(this.arrA[this.thisAdd], {duration:1, from:0.0, to:1.0,beforeFinish:function(){othis.arrB[othis.thisAdd].addClassName(othis.className);},afterFinish:function(){			
		    if(othis.desc){
			    othis.desc[0].setStyle({left:600 + 'px',top:440 + 'px',opacity:0});
			    othis._updateCaption(othis.arrA[othis.thisAdd], othis.desc[0]);
			};
		    new Effect.Parallel( 
		         [ 
		         new Effect.Move(othis.desc[0], {x:-590, y:-430,sync: true}), 
		         new Effect.Opacity(othis.desc[0],{from:0,to:1, sync: true}) 
		         ], 
		         {duration:1} 
		    );
		    othis.arrB.invoke('show');
		}});
	},
	
	click:function(){
		this.arrA.invoke('hide');
		this.arrA[this.thisAdd].show();
		new Effect.Fade(this.arrA[this.thisAdd], {duration:1, from:1.0, to:0.0,beforeFinish:function(){
					othis.arrB.invoke('removeClassName',othis.className);
		}});
		new Effect.Appear(this.arrA[this.thisAdd01], {duration:1, from:0.0, to:1.0,beforeFinish:function(){othis.arrB[othis.thisAdd01].addClassName(othis.className);},afterFinish:function(){			
		    if(othis.desc){
			    othis.desc[0].setStyle({left:600 + 'px',top:440 + 'px',opacity:0});
		    	othis.desc.invoke('setStyle','color:#fff');
			    if(othis.thisAdd01 == 1){othis.desc[0].setStyle({color:'#f00'});}
			    if(othis.thisAdd01 == 3){othis.desc[0].setStyle({color:'#ff0'});}

					othis._updateCaption(othis.arrA[othis.thisAdd01], othis.desc[0]);
			};
		    new Effect.Parallel( 
		         [ 
		         new Effect.Move(othis.desc[0], {x:-590, y:-430,sync: true}), 
		         new Effect.Opacity(othis.desc[0],{from:0,to:1, sync: true}) 
		         ], 
		         {duration:1} 
		    );
		    othis.thisAdd = othis.thisAdd01;
		    othis.arrB.invoke('show');
		}});
		
	},
	
	_updateCaption:function(srcElem, targetElem) {
	    var tmp = srcElem.alt.split("|");
	    var tmp1 = srcElem.readAttribute("rel");
	    
	    $w(targetElem.className).each(function(clsName) {
	    	targetElem.removeClassName(clsName);
	    });
	    targetElem.addClassName("indexDesc");
	    if(tmp1 != null) {
	    	targetElem.addClassName(tmp1);
	    }

	    targetElem.down(".indexDescTitle").update(tmp[0]);
	    if(tmp.length > 1) 
	    	targetElem.down(".indexDescContent").update(tmp[1]);		
	}
	
})

