/* slide*/
var Slide = new Class({
	Implements: [Options],
	options:{ speed: 2000,fx:{transition: Fx.Transitions.Cubic.easeOut,duration: 500,link:'cancel'}},
	initialize:function(options,id){
		this.setOptions(options);	
		this.wrapper = $(id);
		this.wrapper.setStyle('overflow','hidden');
		this.content = new Element('div',{'styles':{'position':'absolute'}});		
		this.content.adopt(this.wrapper.getChildren(),'top');
		this.wrapper.adopt(this.content,'top');
		this.myFx =new Fx.Tween(this.content,this.options.fx);
		this.slides = this.content.getElements('img');
		this.n = this.slides.length;	
		this.pos = 0;	
		this.clock = 2;
		this.wrapper.addEvent('mouseenter',this.stop.bindWithEvent(this));
		this.wrapper.addEvent('mouseleave',this.start.bindWithEvent(this));
		this.generateBar();
		this.start();
	},	
	numbers:[],
	generateBar:function(){
		var bar = new Element('ul',{styles:{'position':'absolute','right':'5px','bottom':'5px','font-family':'verdana','font-size':'10px','z-index':'20'}});
		this.slides.each(function(item,index){
			var number = new Element('li',{'html':index+1,'styles':{'cursor':'pointer'}});
			this.numbers[index] = number.inject(bar);
			this.numbers[index].set('title',item.get('alt'));
		},this);			
		this.numbers.each(function(number,index){
			number.addEvent('click',this.showThis.bindWithEvent(this));
			number.addEvent('mouseover',this.showThis.bindWithEvent(this));
		},this);
		this.numbers[0].addClass('current');
		bar.inject(this.wrapper);			
	},
	showThis : function(event){  		
		this.showSlides(event.target.innerHTML);
    },
	showSlides :function(x){
		var arg =$pick(x,this.clock);
		this.clock = arg.toInt()+1;	
		if(this.clock>this.n) this.clock=1;				
		this.numbers.each(function(number){
			number.removeClass('current');	
		})		
		var y = -(this.slides[0].offsetHeight*(arg-1)).toInt();
		this.myFx.start('top',this.pos,(this.pos=y));	
		this.numbers[arg-1].addClass('current');				
	},
	start:function(){
		this.running = this.showSlides.periodical(this.options.speed,this);			
	},
	stop:function(event){
		event.stopPropagation();
		$clear(this.running);
	}
});	 
/*tab切换*/
function cTab(id){
	var c = Math.random(10)+'';
	if($(id)){ c = new TabSwitch(id);	}
}	

window.addEvent('domready',function(){
	var iptTexts = 0 || $$('input[type=text]');
	iptTexts.each(function(ipt){
		ipt.store('dValue',ipt.get('value'))	;
		ipt.addEvent('focus',function(event){
			var tmp = this.retrieve('dValue');
			this.value =(this.value == tmp)? "" : this.value;
		});
		ipt.addEvent('blur',function(event){
			var tmp = this.retrieve('dValue');
			this.value = (this.value == "")? tmp : this.value;
		})
	})
	
})

/*滚动*/
var VScroller = new Class({	   
	Implements: [Options],	   
	options:{ heightOffset : -20, f:5, amount: 2, targetId: 'id' },			 
	initialize:function(options){
		this.setOptions(options);	
		this.content = $(this.options.targetId);
		this.content.getParent().setStyle('position','relative');
		this.content.setStyles({'position':'absolute','top':'0','left':'0','z-index':'52525'});	   	
		this.patch();		
		this.myFx =new Fx.Tween(this.content,{duration: 1000});			   
		this.acc = this.options.amount;
		this.pos = 0;	   	
		this.run();
		this.content.addEvent('mouseenter',this.stop.bindWithEvent(this));
		this.content.addEvent('mouseleave',this.run.bindWithEvent(this));
	},				
	patch:function(){
		var fElements = this.content.getChildren().clone();	
		fElements.each(function(f,index){
			if(index<this.options.f)  { this.content.grab(f,'bottom'); }
		},this);			
	},		 
	doScroll:function(){			 
			this.myFx.start('top',this.pos,this.pos+this.options.heightOffset); this.acc --;			   
			if(this.acc ==0){ this.reset(); } else{ this.pos = this.pos +  this.options.heightOffset; }			
	},			 
	reset:function(){ this.pos = 0; this.acc = this.options.amount; },		
	stop:function(){ $clear(this.running); },			 
	run:function(){ this.running = this.doScroll.periodical(4000,this);				
	}	  
});		  
/*tab*/
function showSmallMenu(name,id,count){		  
			for(var i=0; i<count; i++){		
				document.getElementById(name+"content"+i).style.display = "none";
			}

			for(var i=0; i<count; i++){
				document.getElementById(name+"tab"+i).className = "inactive";			
			}

			document.getElementById(name+"tab"+id).className = "active";
			document.getElementById(name+"content"+id).style.display = "block";
}

/*对联*/
var Couplet = new Class({
	Implements: [Options],
	options:{ targets:'class', screenY: getCoordinates().height*0.25, width:120, sWidth:960 },
	initialize:function(options,which){
		this.setOptions(options);
		this.targets = $$(this.options.targets);	
		this.btns = $$('.coupletBtn');
		this.cImgs = $$('.cImg'); 
		this.tImgs = $$('.tImg');   
		this.margin =5; 

		this.targets.each(function(target,index){
			target.setStyles({'position':'absolute','overflow':'hidden','z-index':'65535'});
			target.addEvent('mouseover',this.showFull.bindWithEvent(this));	  
			this.btns[index].setStyles({'background':'#EEE','width':'120px','text-align':'center','color':'#333','height':'25px','line-height':'25px','overflow':'hidden','display':'block'});
			this.btns[index].addEvent('click',this.close.bindWithEvent(this));	   
			//this.tImgs.setStyle('display','none');	   			
			if(index%2==0){ target.setStyle('left',this.margin); }else{ target.setStyle('right',this.margin); }	 
		},this);

		if(arguments[1]){
			this.targets[which].setStyle('display','none');
		}
		

		if(Browser.Engine.version == 4){ this.doIeSix.periodical(2,this); } else { this.w3cWay(); }
	},	  				
	showFull:function(event){	
			event.stop();
			this.btns.each(function(btn,index){
					   btn.setStyle('display','block');
					   this.cImgs[index].setStyle('display','none');
					   this.tImgs[index].style.display = 'block';
			},this);			
	},
	close:function(event){
			this.targets.dispose();		  				
	},	 
	w3cWay:function(){		
		this.targets[0].setStyle('position','fixed'); this.targets[1].setStyle('position','fixed'); this.targets[0].setStyle('top',this.options.screenY); this.targets[1].setStyle('top',this.options.screenY);
	},
	doIeSix:function(){	
		var y = getScroll().y + this.options.screenY;
		this.targets[0].setStyle('top',y);		
		this.targets[1].setStyle('top',y);
	}	
});	
/*CUP行情*/
function marketMath(id){
	var bPrice;
	var tPrice;
	var result;
	$$(id).each(function(im){
		switch(im.className){
			case 'c2':
				bPrice=im.get('text').substring(1,im.get('text').length).toInt();
				break
			case 'c3':
				tPrice=im.get('text').substring(1,im.get('text').length).toInt();
				break
			case 'c4':
					result=tPrice-bPrice;
					if(result<0){
						im.set('text','↓'+ Math.abs(result));
						im.addClass('down');
					}else if(result>0){
						im.set('text','↑'+ result);
						im.addClass('up');
					}else{
						im.set('text','－');
						im.addClass('nochange');
					}
		}
	})
}
