var Yule={
	each:function(o,fn){
		for(var i=0,l=o.length;i<l;i++){
			fn.apply(o[i],[i,o[i]]);
		}
	},
	join:function(){
		return [].join.call(arguments,'');
	},
	htmlencode:function(s){
		return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");
	},
	G:function(id){
		return document.getElementById(id);
	},
	IE:/msie/.test(navigator.userAgent.toLowerCase()) && !/opera/.test(navigator.userAgent.toLowerCase())
};

Yule.MusicBox = {
	init:function(id){
		var inputs=Yule.G(id).getElementsByTagName('input');
		Yule.each(inputs,function(i,v){
			v.onclick = function(){
				if(!v.checked){
					Yule.G(id+"_all").checked = false; 
				}else{
					var all = true;
					Yule.each(inputs,function(i,v){
						if(!v.checked) all=false;
					});
					if(all) Yule.G(id+"_all").checked = true; 
				}
			};
			Yule.G(v.id+'_play') && (Yule.G(v.id+'_play').onclick=function(){
				Yule.MusicBox.play(Yule.G(v.id+'_song').innerHTML+'$$'+window.SingerName+'$$$$',window.AlbumName||window.SingerName);
			});
		});
		Yule.G(id+"_all").onclick=function(){
			if(this.checked){
				Yule.each(inputs,function(i,v){
					v.checked = true;
				});
			}else{
				Yule.each(inputs,function(i,v){
					v.checked = false;
				});
			}
		};
		Yule.G(id+"_play").onclick=function(){
			var songs=[];
			Yule.each(inputs,function(i,v){
				v.checked && songs.push(Yule.G(v.id+"_song").innerHTML+'$$'+window.SingerName+'$$$$');
			});
			Yule.MusicBox.play(songs.join(';;'),window.AlbumName||window.SingerName);
		};	
	},
	play:function(s,album){
		if(s==''){
			alert('ÇëÑ¡ÔñÇúÄ¿£¡');
			return false;
		}
		var url = 'http://box.zhangmen.baidu.com/m?gate=8&songname='+escape(s)+'&albumname='+escape(album)+'&ct=134217728&tn=baidumt,Î´Ñ¡Ôñ¸èÇú%20%20&word=mp3,,,[]&si=;;Î´Ñ¡Ôñ¸èÇú;;;;&lm=16777216'; ;
		window.open(url,'baidumb','width=800,height=542,top=0,left='+(screen.width > 800)?100:0+',scrollbars=0,resizable=0,status=1');
		return false;
	},
	playAlbum:function(el){
		var m = Yule.G(el.id+'x');
		if(!m){
			alert('ÔÝÎ´ÊÕÂ¼¸èÇú');
			return false;
		}
		var nodes = m.getElementsByTagName('a')||[],album = m.getAttribute('al');
		var songs=[];
		Yule.each(nodes,function(i,v){
			songs.push(v.innerHTML+'$$'+window.SingerName+'$$$$');
		});
		Yule.MusicBox.play(songs.join(';;'),album);
	}
}

Yule.resizeImg = function(img,w,h){
	w=w||140;
	h=h||140;
	var iw=img.offsetWidth||0,ih=img.offsetHeight||0,s=img.style;
	if(iw>w || ih>h){
		if(iw>ih){
			s.width=w+'px';
			s.height=w/iw*ih+"px";
		}else{
			s.height=h+"px";
			s.width=h/ih*iw+"px";
		}
	}
}

Yule.Happening = {
	rolling:false,
	elem:null,
	selem:null,
	up_el:null,
	down_el:null,
	step:30,
	height:0,
	interv:null,
	scroll:function(dir){
		var timer,t=0,o=this;
		if(o.rolling) return;
		o.rolling = true;
		(function(){
			if ((t++)*(Math.abs(dir))<o.step){
				o.elem.scrollTop += dir;
				timer = setTimeout(arguments.callee,5);
			}else{
				if((o.elem.scrollTop+o.step) >= o.height*2){
					o.elem.scrollTop = o.height-o.step;
				}else if(o.elem.scrollTop <= o.step){
					o.elem.scrollTop = o.height+o.step;
				}
				o.rolling = false;
				return;
			}
		})();
	},
	up:function(){if(!Yule.Happening.rolling) Yule.Happening.scroll(-2);},
	down:function(){if(!Yule.Happening.rolling) Yule.Happening.scroll(2);},
	init:function(step){
		var o = Yule.Happening;
		o.step = step||o.step;
		o.elem=Yule.G("happening_con");
		o.selem=Yule.G("happening_news");
		o.up_el=Yule.G("happening_up");
		o.down_el=Yule.G("happening_down");
		o.height = o.selem.offsetHeight;
		o.selem.innerHTML += o.selem.innerHTML;
		o.elem.scrollTop = o.height;
		if(o.up_el){ o.up_el.onmousedown = o.up;
		o.up_el.onfocus = function(){this.blur();};}
		if(o.down_el){ o.down_el.onmousedown = o.down;
		o.down_el.onfocus = function(){this.blur();}}
		o.interv = window.setInterval(o.down,3000);	
		Yule.each(o.elem.getElementsByTagName("a"),function(i,v){
			v.onmouseover = function(){o.rolling = true;window.clearInterval(o.interv);}
			v.onmouseout = function(){o.rolling = false;o.interv = window.setInterval(o.down,3000);}
		})
	}
}

Yule.tab = function(pre,id){
	var i=0,li;
	while(li = Yule.G(pre+i)){
		if(i==id){
			li.className='on';
			Yule.G(pre+'_con_'+i) && (Yule.G(pre+'_con_'+i).style.display = 'block');
		}else{
			li.className='';
			Yule.G(pre+'_con_'+i) && (Yule.G(pre+'_con_'+i).style.display = 'none');
		}
		i++;
	}
};
















