yoka javascript库

/*
 *  author tiger
 */

;(function(){
	Function.prototype.method = function(name,func){
		if(!this.prototype[name]){
			this.prototype[name] = func;
		}
	};
	if( !window.XMLHttpRequest && window.ActiveXObject){
	    try{
			document.execCommand('BackgroundImageCache', false, true);
		}catch (e){
		};
	}
/*
 * String
 */
	var str = {
		rgbtoHex : function(){
			var s = this.match(/\d{1,3}/g);
			if(!s) return null;
			if(s.length == 4 && s[3]==0) return 'transparent';
			var result = [];
			for(var i=0,l=s.length;i<l;i++){
				s[i] = (s[i]-0).toString(16);
				result.push(s[i].length==1 ? '0'+s[i] : s[i]);
			}
			return '#'+result.join('');
		},
		camelCase : function(){
			return this.replace(/-\D/g,function(m){return m.charAt(1).toUpperCase()})
		},
		hyphenate : function(){
			return this.replace(/[A-Z]/g,function(m){return '-'+m.charAt(0).toLowerCase()})
		}
	};
	
	for(var key in str){
		if(str.hasOwnProperty(key)){
			String.method(key,str[key]);
		}
	}
	
/*
 * Array
 */ 	
	
	
})();


/*
 * Element
 */
var Element = {
	create : function(){
		
	},
	hasClass:function(obj,name){
		return (' '+(obj.className || '')+' ').indexOf(' '+name+' ') > -1 ? true : false;
	},
	addClass : function(obj,name){
		if(this.hasClass(obj,name)) return;
		obj.className += ' ' + name;
	},
	removeClass : function(obj,name){
		obj.className = obj.className.replace(new RegExp('(^|\\s)' +name+ '(?:\\s|$)'),'$1').replace(/\s{1,}/g,' ');
	},
	getStyle : function(obj,style){
		
		var result;
		if(style == 'padding' || style=='margin'){
			result = '';
			for(var key in {top:0,right:0,bottom:0,left:0}){
				result += Element.getStyle(obj,style+'-'+key) + ' ';
			}
			result = result.replace(/\s$/,'');
			return result;
		}
		function getComStyle(property){
			if(obj.currentStyle) return obj.currentStyle[property.camelCase()];
			var computed = window.getComputedStyle(obj, null);
			return (computed) ? computed.getPropertyValue(property.hyphenate()) : null;
		}
		if(style == 'opacity'){
			if(window.ActiveXObject){
				result = getComStyle('filter').replace(/[^0-9\.]/g,'');
				result = result== '' ? 1 : parseInt(result*100)/10000;
				return result;
			}
			result = parseFloat(getComStyle(style));
			result = !result && result != 0 ? 1 : result; 
			
			return result;
		}
		style = style.camelCase();
		
		result = obj.style[style];

		if(!result&&result!==0){
			result = getComStyle(style);
		}
		if(result){
			//if(/rgb/.test(style)){
			//	resutl = result.rgbtoHex();
			//}
			if(/^(width)|(height)$/.test(style)){
				var path = style == 'width' ? ['left','right'] : ['top','bottom'],
					size =0;
				size = (parseInt(this.getStyle(obj,'padding-'+path[0])) || 0) + (parseInt(this.getStyle(obj,'padding-'+path[1])) || 0) + 
					   (parseInt(this.getStyle(obj,'border-'+path[0]+'-width')) || 0 ) + (parseInt(this.getStyle(obj,'border-'+path[1]+'-width')) || 0);
				result = obj['offset'+style.replace(/\b[a-z]/,function(m){return m.toUpperCase();})]-size;
				return result;
			}
			if(result == 'auto' && style == 'zIndex'){
				result = 0;
				return result;
			}
		}
		return result;
	},
	setStyle : function(obj,values){
		var str = ';';
		for(var key in values){
			if(values.hasOwnProperty(key)){
				if(key == 'opacity'){
					str += key + ':' + values[key] + ';filter:alpha(opacity='+ values[key]*100 +');';
					continue;
				}
				if(/(rgb)|(#)/i.test(values[key])){
					str += key +':'+ values[key] +  ';';
					continue;
				}
				str += key +':'+ Math.round(values[key])  + 'px;';
			}
		}
		obj.style.cssText += str;
		str = null;
		return ;
	},
	getPosition:function(obj){
		var o = typeof obj === 'string' ? document.getElementById(obj) : obj,
			x=0,
			y=0;
		while(o){
			x+=o.offsetLeft;
			y+=o.offsetTop;
			o = o.offsetParent;
		}
		return {x:x,y:y}
	},
	getChild:function(obj,node){
		var o = typeof obj === 'string' ? document.getElementById(obj) : obj,
			list = o.childNodes,
			nodes = [];
		for(var i=0,l=list.length;i<l;i++){
			if(node){
				if(list[i].nodeName == node.toUpperCase()){
					nodes.push(list[i]);
				}
			}else{
				if(list[i].nodeType == 1) nodes.push(list[i])
			}
		}
		o=null;list=null;
		return nodes;
	}
}


/*
 * Event
 */
var Event = {
	add : (function(){
		if(document.addEventListener){
			return function(obj,type,fn){ obj.addEventListener(type,fn,false)}
		}
		return function(obj,type,fn){ obj.attachEvent('on'+type,fn)}
	})(),
	remove : (function(){
		if(document.removeEventListener){
			return function(obj,type,fn){ obj.removeEventListener(type,fn,false)}
		}
		return  function(obj,type,fn){ obj.detachEvent('on'+type,fn)}
	})(),
	stop:function(e){
		if(e&&e.stopPropagation){
			e.stopPropagation();
			e.preventDefault();
		}else{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
	}
}


/*
 * Cookie
 */
var Cookie={
	read:function(name){
		var value = document.cookie.match('(?:^|;)\\s*' + name + '=([^;]*)');
		return (value) ? decodeURIComponent(value[1]) : null;
	},
	write:function(value){
		var str = value.name + '=' + encodeURIComponent(value.value);
			if(value.domain){ str += '; domain=' + value.domain;}
			if(value.path){ str += '; path=' + value.path;}
			if(value.day){
				var time = new Date();
				time.setTime(time.getTime()+value.day*24*60*60*1000);
				str += '; expires=' + time.toGMTString();
			}
		document.cookie = str;
		return;
	},
	dispose:function(name){
		var str = this.read(name);
		this.write({name:name,value:str,day:-1});
		return;
	}
}


/*
 * Anima
 */
function Anima(id,options){
	var opts,
		obj,
		step,
		timer,
		cancelFunc,
		transition,
		begin,
		current,
		end,
		style = {
			name : [],
			from : [],
			to : []
		},
		complete;
	
	function init(opt){
		opts = opt || {};
		obj = typeof id === 'string' ? document.getElementById(id) : id;
		step = parseInt((opts.time || 500));
		timer = null;
		cancelFunc = opts.cancel;
		transition = opts.trans || '1';
		begin = 0;
		current = 0;
		end = 0;
		style = {
			name : [],
			from : [],
			to : []
		};
		complete = opts.complete || null;
	}
	init(options);

	function start(opt){
		stop();
		style = {
			name : [],
			from : [],
			to : []
		};
		for(var key in opt){
			style.name.push(key.hyphenate());
			if(typeof opt[key] === 'object'){
				style.from.push(parseFloat(opt[key][0]));
				style.to.push(parseFloat(opt[key][1]));
				continue;
			}
			var result = Element.getStyle(obj,key);
			result = typeof result === 'undefind' ?  opt[key] : result;
			style.from.push(result);
			style.to.push(opt[key]);
			result = null;
		}
		begin = getTime();
		current = getTime();
		end = begin + step;
		play();
	}

	function play(){
		var m = 0;
		function move(){
			current = getTime();
			m = (current - begin)/step;
			if(m>=1){
				m=1;
			}
			var str = {},
				n='';
			for(var i=0,l=style.name.length;i<l;i++){
				if(/(rgb)|(#)/i.test(style.from[i])){
					var froms = setColor(style.from[i]),
						tos = setColor(style.to[i]),
						results = [];
					for(var j=0,k=froms.length;j<k;j++){
						results.push( Math.round(trans((froms[j]-0),(tos[j]-0),m)))
					}
					n =  results.join(',').rgbtoHex();
				}else{
					n = parseFloat(trans( parseFloat(style.from[i]),parseFloat(style.to[i]),m));
				}
				str[style.name[i]] = n;
			}
			Element.setStyle(obj,str);
			
			if(m==1){
				stop();
				onComplete();
				return;
			}
		}
		timer = setInterval(move,15);
		function trans(f,t,a){
			return f + (t-f)*transFunc(a);
		}
		
		function setColor(value){
			var result;
			if(value.indexOf('#')>-1){ 
				value = value.replace(/#/,'');
				if(value.length==3){
					value = value.replace(/(\w)(\w)(\w)/,'$1$1$2$2$3$3');
				}
				result = value.replace(/\w{2}/g,function(m){return parseInt(m.replace(/^0{1}/g,''),16)+','}).replace(/\,$/g,'').split(',');
				return result;
			}
			if(value.indexOf('rgb')>-1){
				result = value.match(/\d{1,3}/g);
			}
			return result;
		}
	}
	


	function trans(s){
		switch(s){
			case '0':
				transFunc = function(m){return m};
				break;
			case '2':
				transFunc = function(m){return Math.pow(m, 2) * (2.618 * m - 1.618)};
				break;
			case '3' : 
				transFunc = function(m){
					return (m<=0.5) ? Math.pow(m, 2) * (2.618 * m - 1.618) : (1 - Math.pow((1-m),2)*(2.618 * (1-m) - 1.618));
				}
				break;
			case '1' :
			default : 
				transFunc = function (m){ return (1-Math.cos(Math.PI*m))/2 };
		}
	}
	trans(transition);

	function pause(){
		stop();
	}

	function reStart(){
		var fix = current - begin;
		current = getTime();
		begin = current - fix;
		end = begin + step;
		play();
	}
	function stop(){
		if(timer){
			clearInterval(timer);
			timer = null;
		}
	}
	function getTime(){
		return (new Date()).getTime();
	}
	function cancel(){
		stop();
		if(cancelFunc) cancelFunc();
	}
	function onComplete(){
		if(complete) complete();
	}
	function setComplete(fn){
		if(fn) complete = fn;
	}

	return {
		start : start,
		cancel : cancel,
		pause : pause,
		reStart : reStart,
		complete : setComplete
	}

};

(function(){
	
	function $(id,tag){
		var re=(id&&typeof id!="string") ? id : document.getElementById(id);
		if(!tag){
			return re;
		}else{
			return re.getElementsByTagName(tag);
		}
	}

	function search(id){
		if(!$(id)) return;
		var obj = $(id),
			dl = $(obj,'dl')[0],
			span = $(obj,'span')[0],
			input = $(obj,'input')[1],
			state = false,
			timer = null;

		Event.add(dl,'click',function(e){
			clear();
			e = e || window.event;
			var tar = e.target || e.srcElement;
			if(state){
				if(tar.nodeName == 'LI'){
					span.innerHTML = tar.innerHTML;
					obj.setAttribute('action',tar.getAttribute('data'));
				}
				Element.removeClass(dl,'on');
				state = false;
				return;
			}
			Element.addClass(dl,'on');
			state = true;
		})
		Event.add(dl,'mouseover',function(e){
			clear();
			find(e,true);
		})
		Event.add(dl,'mouseout',function(e){
			find(e);
			if(timer) return;
			timer = setTimeout(function(){
				clear();
				state = false;
				Element.removeClass(dl,'on');
			},100)
		})
		Event.add(input,'mouseover',function(){
			Element.addClass(input,'on');	
		})
		Event.add(input,'mouseout',function(){
			Element.removeClass(input,'on');
		})
		function find(e,type){
			e = e || window.event;
			var tar = e.target || e.srcElement;
			if(tar.nodeName == 'LI'){
				if(type) {
					Element.addClass(tar,'on');
					return;
				}
				Element.removeClass(tar,'on');
			}
		}
		function clear(){
			if(timer){
				clearTimeout(timer);
				timer = null;
			}
		}
	}

	function upper(m){
		return m.replace(/^[a-z]/g,function(n){ return n.charAt(0).toUpperCase()});
	}


	function scroll(opt){
		if(!opt) return;
		var obj = $(opt.id),
			box = Element.getChild(obj,opt.boxTag)[0],
			list = Element.getChild(box,opt.singleTag),
			loop = opt.loop ? true : false,
			leftBtn = opt.leftBtn ? $(opt.leftBtn) : null,
			rightBtn = opt.rightBtn ? $(opt.rightBtn) : null,
			auto = opt.auto ? true : false,
			c = opt.current ? parseInt(opt.current) : 0,
			path = opt.path ? opt.path : 'left',
			callback = opt.callback ? opt.callback : null,
			step = opt.step ? parseInt(opt.step) : 1,
			anima = null,
			singleSize = 0,
			length = list.length,
			max = 0,
			time = opt.time ? parseInt(opt.time) : 5000,
			state = false,
			from = 0,
			to = 0,
			fix =0,
			range = [0,0],
			move = null;

		
		function init(){
			if(path == 'left' || path =='right'){
				singleSize = list[0]['offsetWidth'];
				fix = Math.floor(obj['offsetWidth']/singleSize);
			}else{
				singleSize = list[0]['offsetHeight'];
				fix = Math.floor(obj['offsetHeight']/singleSize);
			}
			fix = Math.max(1,fix);
			path = (path == 'left' || path == 'right') ? 'marginLeft' : 'marginTop';
			max = singleSize*length;
			anima = Anima(box,500);
			to = -step*singleSize;
			if(auto){
				move = setTimeout(play,time);
				range = [0,length];
				box.innerHTML += box.innerHTML;
			}else{
				range = [0,length-fix];
			}
			reset(c);
			if(callback) callback(c,range);
			handle();
		}
		function handle(){
			if(leftBtn){
				Event.add(leftBtn,'click',function(){
					change();
				})
			}
			if(rightBtn){
				Event.add(rightBtn,'click',function(){
					change(null,true);
				})
			}
			Event.add(box,'mouseover',function(){
				clear();
			})
			Event.add(box,'mouseout',function(){
				if(auto){
					move = setTimeout(play,time);
				}
			})
		}
		function change(m,type){
			clear();
			if(m||m==0){
				c = m;
			}else{
				var n = type ? 1 : 0;
				if(state) return;
				if(c==range[n]){
					if(!auto) return;
					reset(1-n);
				}
				if(n){
					c += step;
				}else{
					c -= step;
				}
			}
			
			to = -c*singleSize;
			state = true;
			var s = {};
			s[path] = to;
			if(callback) callback(c,range);
			anima.start(s);
			anima.complete(function(){
				state = false;
				if(auto && c== range[n]){
					reset(1-n);
				}
				if(auto){
					move = setTimeout(play,time);
				}
			})
		}
		
		function play(){
			change(null,true);
		}
		function clear(){
			clearTimeout(move);
			move = null;
		}
		function reset(n){
			box.style[path] = -Math.abs(range[n]*singleSize) + 'px';
			c = range[n];
		}
		init();
		function setChange(s){
			return change(s);
		}
		return {
			setChange : setChange
		}
	}

	/** 新焦点图,修改了焦点图加载方式 **/
	function focus(id,time,callbackFun){

		var obj = $(id),
			imgs = $(obj,'img'),
			dd = $(obj,'dd')[0],
			lis = null,
			current = 0,
			state = false,
			old = -1,
			time = time || 5000,
			autoTimer = null,
			lazyTimer = null,
			anima = [],
			moving = false,
			imgState = [];

		function init(){
			var str = '<ul>';
			for(var i=0,l=imgs.length;i<l;i++){
				imgState[i] = false;
				changeState(i);
				str += '<li>'+ (i+1) +'</li>';
				anima[i] = Anima(imgs[i]);
			}
			str += '</ul>';
			dd.innerHTML = str;
			imgs[0].style.cssText += ';opacity:1;filter:alpha(opacity=100);display:block;z-index:2;';
			handle();
			Event.add(obj,'mouseover',function(){
				clear();
			})
			Event.add(obj,'mouseout',function(){
				play();
			})
			//play();
			preLoad(0,play);
			if(callbackFun) callbackFun(0);
		}

		function preLoad(n,callback){
			if(imgs[n].src == '' ) imgs[n].src = imgs[n].getAttribute('_src');
			callback ? changeState(n,callback) : changeState(n);
		}

		function changeState(n,callback){
			//imgState[n] = true;
			//if(callback) callback();
			
			if(imgs[n].complete || imgs[n].readyState == 'loaded' || imgs[n].readyState == 'complete'){
				imgState[n] = true;
				if(callback) callback();
			}else{
				Event.add(imgs[n],'load',function(){
					imgState[n] = true;
					if(callback) callback();
					Event.remove(imgs[n],'load',arguments.callee);
				})
				Event.add(imgs[n],'error',function(){
					imgState[n] = true;
					if(callback) callback();
					Event.remove(imgs[n],'error',arguments.callee);
				})
			}
			
			var m = n+1;
			if( m <= imgs.length-1 && !imgState[m]){
				preLoad(m);
			}
		}
		function auto(){
			var next = current;
			next++;
			if( next > imgs.length-1){
				next = 0;
			}
			if(!imgState[next]){
				clear();
				if(imgs[next].src == ''){
					preLoad(next,auto);
					return;
				}
				lazyTimer = setTimeout(function(){
					lazyTimer = null;
					auto();
				},300);
				return;
			}
			change(next);
		}

		function change(c){
			moving = true;
			if(callbackFun) callbackFun(c);
			clear();
			if(old>-1){ 
				anima[old].cancel();
				imgs[old].style.cssText = 'display:none';
			}
			old = current;
			anima[old].cancel();
			imgs[old].style.cssText += ';display:block;opacity:1;filter:alpha(opacity=100);z-index:1;';
			Element.removeClass(lis[old],'on');
			current = c;
			imgs[current].style.cssText += ';display:block;opacity:0;filter:alpha(opacity=0);z-index:2;';
			Element.addClass(lis[current],'on');
			anima[current].start({opacity:1});
			anima[current].complete(function(){
				moving = false;
				play();
			});
		}

		function handle(){
			lis = $(dd,'li');
			Element.addClass(lis[0],'on');
			for(var i=0,l=lis.length;i<l;i++){
				fun(i);
			}
			function fun(n){
				Event.add(lis[n],'click',function(){
					if(moving) return;
					if(n==current) return;
					if(imgs[n].src == '') imgs[n].src = imgs[n].getAttribute('_src');
					change(n);
				})
			} 
		}
		function clear(){
			clearTimeout(autoTimer);
			clearTimeout(lazyTimer);
			lazyTimer = null;
			autoTimer = null;
		}
		function play(){
			clear();
			autoTimer = setTimeout(auto,time);
		}
		init();
	}
	


    /** 原切换 修改了获取元素方法,切换设置;添加了滑过延时效果 **/
	function tagSwitch(tit,box,s,fn,time,show){
		tit=tit.split('/');
		box=box.split("/");
		!s&&(s="mouseover");!show&&(show=0);
		var ts=$(tit[0]),
			bs=$(box[0]),
			n=0,
			tx=tit[2],
			bx=box[2],
			now=-1,i,c,
			old=-1;
		ts = Element.getChild(ts,tit[1]);
		bs = Element.getChild(bs,box[1]);
		n=ts.length;

		for(i=0;i<n;i++){reg(ts[i],bs[i],i);};
		function reg(tv,bv,i)
		{
			var timer = null;
			Element.removeClass(tv,'on');
			Element.removeClass(bv,'on');
			tv.old = tv.className || '';
			bv.old = bv.className || '';
			Event.add(tv,s,function(){
				if(timer) return;
				timer = setTimeout(function(){
					timer = null;
					clearInterval(c);
					init(i);	
				},50);
			});
			Event.add(tv,'mouseout',function(){
				if(timer){
					clearTimeout(timer);
					timer = null;
				}
			})
			if(show!=-1&&time){
				Event.add(bv,"mouseover",function(){clearInterval(c);});
				Event.add(bv,"mouseout",go);
				Event.add(tv,"mouseout",function(){
					if(timer){
						clearTimeout(timer);
						timer = null;
					}
					go();
				});
			}
			else if(show==-1&&s=="mouseover"){
				Event.add(tv,"mouseout",function(){init(0);});
			}
		}
		init(0);
		if(show!=-1&&time){c=setInterval(auto,time);}
		function go(){clearInterval(c);c=setInterval(auto,time);}
		function init(m){
			if(m==now) return;
			if(old>-1){
				Element.removeClass(ts[old],'old');
				Element.removeClass(bs[old],'old');
			}
			if(now>-1){
				Element.addClass(ts[now],'old');
				Element.addClass(bs[now],'old');
				Element.removeClass(ts[now],'on');
				Element.removeClass(bs[now],'on');
				old = now;
			}
			if(m>-1){
				Element.addClass(ts[m],'on');
				Element.addClass(bs[m],'on');
				now = m;
			}
			fn&&fn(ts[m],bs[m],m);
		}

		function auto(){
			var s = now;
			(s<n-1)?s++:s=0;
			init(s);
		};
	}

    /** 试用效果 **/
	function tryCon(id,tag){
		var obj = $(id),
			dds = $(obj,tag);
		for(var i=0,l=dds.length;i<l;i++){
			handle(i);
		}
		function handle(n){
			var timer = null;
			Event.add(dds[n],'mouseover',function(){
				if(timer){
					clearTimeout(timer);
					timer = null;
				}
				Element.addClass(dds[n],'on');
			})
			Event.add(dds[n],'mouseout',function(){
				if(timer) return;
				timer = setTimeout(function(){
					Element.removeClass(dds[n],'on');
				},100)
			})
		}
	}
	
	/** 新品上市 && 优享团 **/
	function newProduct(opt){
		var page = $(opt.pageId),
			length = Math.ceil($(opt.id,opt.singleTag).length/opt.step),
			spans = null,
			old = null,
			step = opt.step || 1,
			scroll = null;
		function call(m,rag){
			if(old || old == 0) Element.removeClass(spans[old],'on');
			Element.addClass(spans[m/step],'on');
			old = m/step;
		}
		function init(){
			var str = '';
			for(var i=0;i<length;i++){
				str += '<span></span>';
			}
			page.innerHTML = str;
			spans = $(page,'span');
			for(var i=0,l=spans.length;i<l;i++){
				handle(i);
			}
		}
		function handle(n){
			Event.add(spans[n],'mouseover',function(){
				scroll.setChange(n*step);
			})
		}
		init();
		opt.callback = call;
		scroll = yo.scroll(opt);
	}

	/** 明星库 **/
	function starList(id,tarId){
		var obj = $(id),
			tar = $(tarId),
			timer = null,
			position = Element.getPosition(obj),
			minWidth = 89,
			maxHeight = 250,
			state = false,
			ie = window.ActiveXObject ? true : false,
			ieTimer = null;

		function init(){
			var width = Math.max(minWidth,obj.offsetWidth),
				scroll = tar.offsetHeight > maxHeight ? 'auto' : 'hidden',
				height = scroll == 'auto' ? 250 : tar.offsetHeight;
			tar.style.cssText = 'display:none; top:' + (position.y + obj.offsetHeight) + 'px;left:'+ position.x + 'px; width:' + width + 'px; overflow-y:' + scroll + ';height:'+ (height-3) +'px';
			if(ie){
				ieTimer = setInterval(reset,50);
			}
			Event.add(window,'resize',reset);
		}
		function reset(){
			if(ieTimer){
				clearInterval(ieTimer);
				ieTimer = null;
			}
			position = Element.getPosition(obj);
			tar.style.cssText += ';left:'+ position.x +'px;top:'+ (position.y + obj.offsetHeight) +'px;';
		}
		

		Event.add(obj,'click',function(e){
			clear();
			if(state){
				tar.style.display = 'none';
				state = false;
				return;
			}
			state = true;
			reset();
			tar.style.display = 'block';
		})

		Event.add(obj,'mouseout',function(){
			if(timer) return;
			timer = setTimeout(function(){
				clear();
				tar.style.display = 'none';
				state = false;
			},100)
		})
		Event.add(tar,'mouseover',function(e){
			clear();
			find(e,true);
			state = true;
			tar.style.display = 'block';
		})
		Event.add(tar,'mouseout',function(e){
			if(timer) return;
			find(e);
			timer = setTimeout(function(){
				clear();
				tar.style.display = 'none';
				state = false;
			},100)
		})
		function find(e,type){
			e = e || window.event;
			var tar = e.target || e.srcElement;
			if(tar.nodeName == 'A'){
				tar = tar.parentNode;
			}
			if(tar.nodeName == 'LI'){
				if(type) {
					Element.addClass(tar,'on');
					return;
				}
				Element.removeClass(tar,'on');
			}
		}
		function clear(){
			if(timer){
				clearTimeout(timer);
				timer = null;
			}
		}
		init();
	}

	function mall(opt){
		var leftBtn = $(opt.leftBtn),
			rightBtn = $(opt.rightBtn),
			opt = opt;
		
		function call(m,rag){
			Element.removeClass(leftBtn,'end');
			Element.removeClass(rightBtn,'end');
			if(m == rag[0]){
				Element.addClass(leftBtn,'end');
				return;
			}
			if(m == rag[rag.length-1]){
				Element.addClass(rightBtn,'end');
				return;
			}
		}
		opt.callback = call;
		yo.scroll(opt);
	}

	function inputFocus(id,defaultValue){
		var obj = $(id),
			v = defaultValue;
		Event.add(obj,'focus',function(){
			if(obj.value == v){
				obj.value = '';
				Element.addClass(obj,'on');
			}
		})
		Event.add(obj,'blur',function(){
			if(obj.value == '' || obj.value == v){
				obj.value = v;
				Element.removeClass(obj,'on');
			}
		})
	}

	function rss(text,btn,url){
		var v = '',
			btn = $(btn),
			url = url || 'http://edm.yoka.com/Mail.aspx?&orderhref='+ window.location + '&yemail=' ;
		Event.add(btn,'click',function(){
			v = $(text).value;
			if(v==''){
				return;
			}
			window.location.href = url + v;
		})
	}
	
	function loadScript(url,callback,charset){
		var script = document.createElement('script');
			script.setAttribute('async',true);
			script.src = url;
			if(charset) script.charset = charset;
			script.readyState ? script.onreadystatechange = function(){
				if(script.readyState == 'loaded' || script.readyState =='complete'){
					setTimeout(function(){
						if(callback) callback();
					},100)

				}
			}:
			script.onload = function(){
				if(callback) callback();
			}
			document.getElementsByTagName('head')[0].appendChild(script);
	}


	function lazyLoad(id,tarid,type){
		if(!id || !tarid) return;
		var obj = document.getElementById(id),
			tar = document.getElementById(tarid),
			n = 0,
			inner = '',
			div = document.createElement('div');
		if(!obj || !tar) return;
		if(type){
			obj.appendChild(tar);
			return;
		}
		inner = tar.innerHTML.replace(/\<script[^\>]{1,}(src)[^\>]{1,}\>\<\/script\>/i,'');
		inner = inner.replace(/\<div\s{1,}adCount.*\<\/div\>/i,'');
		div.innerHTML = inner;
		obj.appendChild(div);
		//tar.innerHTML = ''; /** innerHTMl内的 script,style不执行 **/ 
		inner = null;
	}


	function rss(opt){
		var text = $(opt.emailText),
			btn = $(opt.rssBtn),
			obj = $(opt.rssSub),
			closeBtn = $(obj,'span')[0],
			title = [$(obj,'i')[0],$(obj,'em')[0]],
			success = $(opt.rssSuccess),
			exitBtn = $(success,'a')[0],
			exit = $(opt.rssExit),
			exitInput = $(exit,'input'),
			errorText = [$(opt.errorEmail),$(exit,'span')[0]],
			dl = $(exit,'dl')[0],
			exitSuccess = $(exit,'b')[0],
			reg = new RegExp('^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$'),
			url_sub = 'http://g.yoka.com/edm/index.php',
			head = $(document,'head')[0];

		Event.add(btn,'click',function(){
			if(text.value == ''){
				errorText[0].innerHTML = '请输入邮箱地址';
				errorText[0].style.display = 'block';
				return;
			}
			if(!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(text.value)){
				errorText[0].innerHTML = '请输入正确的Email地址';
				errorText[0].style.display = 'block';
				return;
			}
			create(url_sub + '?email='+text.value + '&action=order&callback=yo.rss.sub')
		})
		Event.add(text,'focus',function(){
			errorText[0].innerHTML = '';
			errorText[0].style.display = 'none';
		})
		function sub(v){
			if(v==-2){
				alert('订阅失败');
				return;
			}
			text.value = '';
			obj.style.display = 'block';
			success.style.display = 'block';
			title[0].style.display = 'block';

			title[1].style.display = 'none';
			exit.style.display = 'none';
		}
		Event.add(exitBtn,'click',function(e){
			Event.stop(e);
			
			success.style.display = 'none';
			title[0].style.display = 'none';

			exit.style.display = 'block';
			title[1].style.display = 'block';
			exit.style.display = 'block';
			dl.style.display = 'block';
			errorText[1].style.display = 'none';
			exitSuccess.style.display = 'none';
		})
		Event.add(exitInput[1],'click',function(){
			if(exitInput[0].value == ''){
				errorText[1].innerHTML = '请输入邮箱地址';
				errorText[1].style.display = 'block';
				return;
			}
			if(!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(exitInput[0].value)){
				errorText[1].innerHTML = '请输入正确的Email地址';
				errorText[1].style.display = 'block';
				return;
			}
			create(url_sub+'?email='+exitInput[0].value+'&action=unorder&callback=yo.rss.exitFn');
		})
		Event.add(closeBtn,'click',function(){
			obj.style.display = 'none';
			success.style.display = 'block';
			title[0].style.display = 'block';

			title[1].style.display = 'none';
			exit.style.display = 'none';
			dl.style.display = 'block';
			errorText[1].innerHTML = '';
			exitInput[0].value = '';
			errorText[1].style.display = 'none';
		})
		
		function exitFn(){
			exitInput[0].value = '';
			dl.style.display = 'none';
			errorText[1].style.display = 'none';
			exitSuccess.style.display = 'block';
		}
		function create(s){
			var script = document.createElement('script');
			script.src = s;
			head.appendChild(script);
		}

		window['yo']['rss']={
			sub : sub,
			exitFn : exitFn
		}
	}


	window['yo'] = {
		'search' : search,
		'scroll' : scroll,
		'focus' : focus,
		'tagSwitch' : tagSwitch,
		'tryCon' : tryCon,
		'newProduct' : newProduct,
		'starList' : starList,
		'mall' : mall,
		'inputFocus' : inputFocus,
		'rss' : rss,
		'loadScript' : loadScript,
		'lazyLoad' : lazyLoad,
		'rss' : rss
	}
	
})();




/** 原广告效果 **/

var Class={create:function(){return function(){this.initialize.apply(this,arguments);}}}
var Zooms=Class.create();
Zooms.prototype=
{
	bindFun:function(o,fun){return function(){return fun.apply(o,arguments);}},
	addEvent:function(o,s,fun){o.attachEvent?o.attachEvent("on"+s,fun):o.addEventListener(s,fun,false);return o;},
	initialize:function(n)
	{
		this.width=	n.width;
		this.height=n.height;
		this.api=	n.api,							
		this.id=	document.getElementById(n.id);	
		this.path=	n.path;							
		this.url = n.url;							
		this.full=	n.full;				           
		this.closetime=n.closetime || 10000;			
		this.noWait = n.noWait || false;            
		this.o=this.id.getElementsByTagName("dt")[0];
		this.c = document.getElementById(n.closeObj); 
		this.addEvent(this.c,"click",this.bindFun(this,this.Close));
		this.max=this.height;
		this.v=0;
		this.go=null;
		this.cols=null;
		this.wmode= n.wmode || 'transparent';
		this.id.style.height="1px";
		this.bgcolor = n.bgcolor || 'none';
		
		if(this.full)
		{
			this.nh = window.innerWidth?window.innerHeight:document.documentElement.clientHeight; //文档可视区域高度
			this.nw = window.innerWidth?window.innerWidth:document.documentElement.clientWidth;   //文档可视区域宽度
			if( !(navigator.userAgent.indexOf('MSIE 6.0')>0 || navigator.userAgent.indexOf('MSIE 7.0')>0) ){
				this.nw -= 17;
			}
			this.height=n.height>this.nh?this.nh:n.height;
			this.width=n.height>this.nh?this.height*n.width/n.height:n.width;
			
		}
	},
	start:function()
	{ 
		if(this.noWait){
			if(this.full){
				this.id.style.cssText='background:' + this.bgcolor + ";display:block;z-index:98888;left:0px;top:0;width:"+this.nw+"px;height:2000px;position:fixed;_position:absolute;overflow:hidden;";
				this.c.style.right=(this.nw-this.width)/2 +'px';
				this.c.style.top=((this.nh-this.height)/2+this.height-17)+'px';
				this.set();
			}else{
				this.Open();
			}
		}else{
			this.set();
		}
	},
	set:function(){
			this.o.innerHTML="<object id='"+this.api+"swf' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0' width='1' height='1'><param name='movie' value='http://images.yoka.com/pic/div/yokajs/HomepageADnew.swf'><param name='wmode' value='transparent'><param name='allowScriptAccess' value='always'><param name='allowFullScreen' value='false' /><param name='quality' value='high'><param name='menu' value='false'><param name='FlashVars' value='path="+this.path+"&api="+this.api+"'><embed FlashVars='path="+this.path+"&api="+this.api+"' width='1' height='1' src='http://images.yoka.com/pic/div/yokajs/HomepageADnew.swf' name='"+this.api+"swf' allowScriptAccess='always' allowFullScreen='false' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='transparent' type='application/x-shockwave-flash'></embed></object>";					
	},
	Open:function()
	{
		clearInterval(this.cols);this.cols=setTimeout(this.bindFun(this,this.Close),this.closetime);
		if (this.full){this.fullScreenOpen();}
		else{this.max=this.height;this.StartOpen();this.reNew();}
	},
	setInner : function(){
		var str = "<object  id='"+this.api+"vf' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0' width='"+this.width+"' height='"+this.height+"'><param name='movie' value='"+this.path+"'><param name='wmode' value='"+this.wmode+"'><param name='allowScriptAccess' value='always'><param name='allowFullScreen' value='false'><param name='quality' value='high'><param name='menu' value='false'><embed width='"+this.width+"' height='"+this.height+"' src='"+this.path+"' name='"+this.api+"vf' allowScriptAccess='always' allowFullScreen='false' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='"+this.wmode+"' type='application/x-shockwave-flash'></embed></object>";
		if(this.url && this.url != '') {str+= "<a href='"+ this.url +"' target='_blank'><img src=\"http://p1.yokacdn.com/pic/div/public/img/space.gif\" style=\"width:"+ this.width +"px;height:"+ this.height +"px;position:absolute;top:0;left:0;\" /></a>";}
		this.o.innerHTML = str;
	},
	reNew:function()
	{
		clearInterval(this.go);
		this.go=setInterval(this.bindFun(this,Goto),20);
		function Goto()
		{
			this.v=this.max>this.id.offsetHeight?Math.ceil((this.max-this.id.offsetHeight)/4):Math.floor((this.max-this.id.offsetHeight)/4);
			this.id.style.height=(this.id.offsetHeight+this.v)+"px";
			if(this.v==0)
			{
				clearInterval(this.go);
				if(this.max!=1){this.o.style.height=this.height+"px";this.o.style.width=this.id.style.width=this.width+"px";this.c.style.right='0px';this.c.style.bottom='0px';  this.EndOpen();this.setInner();}
				else{
					this.o.style.height='1px';this.id.style.height="0";this.o.style.width=this.id.style.width="1px";this.EndClose();
				}
			}
		}
	},
	fullScreenOpen:function()
	{	
		this.id.style.cssText='background:' + this.bgcolor + ";display:block;z-index:98888;left:0px;top:0;width:"+this.nw+"px;height:2000px;position:fixed;_position:absolute;overflow:hidden;";
		this.o.style.cssText="height:"+this.nh+"px;width:"+this.width+"px;display:block;overflow:hidden;margin:0 auto;padding-top:"+((this.nh-this.height)/2)+"px";
		this.c.style.right=(this.nw-this.width)/2 +'px';
		this.c.style.top=((this.nh-this.height)/2+this.height-17)+'px';
		if(navigator.appName=='Microsoft Internet Explorer' && navigator.appVersion.indexOf('6.0')>0){
			this.id.style.position="absolute";
			this.addEvent(window,'scroll',this.bindFun(this.id,function(){this.style.top=document.documentElement.scrollTop +'px';}))
		}
		this.setInner();
	},
	Close:function()
	{
		clearInterval(this.cols);this.o.innerHTML='';
		if(this.full){this.id.style.cssText="position:absolute;left:0px;top:0px;width:0px;height:0px;overflow:hidden;display:none;";this.EndClose();}
		else{this.max=1;this.StartClose();this.reNew();}
	},
	StartOpen:function(){},
	StartClose:function(){},
	EndOpen:function(){},
	EndClose:function(){}
}


/**
 * @author {tiger}
 */
function bindFun(o,fun){var ar=Array.prototype.slice.call(arguments).slice(2);return function(){return fun.apply(o,ar);};};
var fixAd = Class.create();
fixAd.prototype = {
	initialize:function(el,src,options,time,closeFn,player){
		this.el = document.getElementById(el);
		this.api = el;
		this.opt = options || {};
		this.time = time;
		this.cssText = '';
		this.top=options.top || 0;
		this.link = this.opt.link || null;
		this.countUrl = this.opt.countUrl || null;
		this.fn;
		this.closeFunc = closeFn;
		this.player = player || -1;
		this.playerInfo = [['http://p1.yokacdn.com/pic/zhangbinflash/video/swfPlay.swf',343],['http://p1.yokacdn.com/pic/ads/2011/0901/sony2222222222.swf',300],['http://p1.yokacdn.com/pic/ads/2011/1116/hpricgmieda0905111.swf',300],['http://p1.yokacdn.com/pic/ads/2011/0905/hprichmediafalse0905.swf',300]];
		this.playerSrc = this.player != -1 ? this.playerInfo[this.player][0] : src;
		this.height = this.player != -1 ? this.playerInfo[this.player][1] : 300;
		this.src = src;
		
		this.timer = null;
		if(this.time){setTimeout(bindFun(this,this.start),this.time);}else{this.start();}
	},
	start:function(){
		for(var p in this.opt){
			if(p=='bottom'){this.top =  document.documentElement.clientHeight - this.height - this.opt[p];}
			else{this.cssText += p+':'+this.opt[p]+'px;'}
		}
		this.cssText += 'top:'+(this.top+document.documentElement.scrollTop)+'px;';
		this.el.style.cssText = this.cssText+this.el.style.cssText + ';width:300px;height:'+ this.height +'px;overflow:hidden;';
		
		var str = "<object  id='"+this.api+"fixad' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0' width='300' height='"+ this.height +"'><param name='movie' value='"+ this.playerSrc +"'><param name='wmode' value='Opaque'><param name='allowScriptAccess' value='always'><param name='flashVars' value='swfurl="+ this.src +"&callback="+ this.closeFunc +"'><param name='allowFullScreen' value='false'><param name='quality' value='high'><param name='menu' value='false'><embed width='300' height='"+ this.height +"' src='"+  this.playerSrc +"' name='"+this.api+"fixad' allowScriptAccess='always' allowFullScreen='false' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='opaque' flashVars='swfurl="+ this.src +"&callback="+ this.closeFunc +"' type='application/x-shockwave-flash'></embed></object>";

		if(this.link){ 
			str += '<a href="'+ this.link +'" target="_blank"><img src="http://p1.yokacdn.com/pic/div/public/img/space.gif" style="width:300px;height:'+ this.height +'px;position:absolute;z-index:2;cursor:pointer;left:0;top:0;" /></a>';
		}
		this.el.innerHTML = str;

		if(this.el.getElementsByTagName('img')[0] && this.countUrl){
			var that = this;
			this.el.getElementsByTagName('img')[0].onclick = function(){
				var img = new Image();
				img.src = that.countUrl;
			}
		}
		
		if(navigator.appName=='Microsoft Internet Explorer' && navigator.appVersion.split(';')[1].indexOf('MSIE 6.0')>0){
			this.el.style.position = 'absolute';
			addEvent(window,'scroll',bindFun(this,this.resize))
			addEvent(window,'resize',bindFun(this,this.resize))
		}else{
			this.el.style.position = 'fixed';
		}
	},
	count:function(){
		var img = new Image();
		img.src = this.countUrl;
	},
	move:function(){
		if(this.timer){
			clearTimeout(this.timer);
			this.timer = null;
		}
		var from = parseInt(this.el.style.top),
			to = this.to,
			step=30,
			n=0,
			that = this;
		function m(){
			n++;
			that.el.style.top = trans(from,to,n/step) + 'px';
			if(n==step){
				clearTimeout(that.timer);
				that.timer = null;
				return;
			}
			that.timer = setTimeout(m,15);
		}
		m();
		function trans(f,t,m){
			return f + (t-f) * (1-Math.cos(Math.PI*m))/2;
		}
	},
	resize:function(){
		clearTimeout(this.timer);
		this.timer = null;
		this.to = document.documentElement.scrollTop + document.documentElement.clientHeight - this.height;
		setTimeout(bindFun(this,this.move),100)
	},
	close:function(){
		this.el.innerHTML = '';
		this.el.style.display = 'none';
	}
}



;(function(){
	if(typeof dataAd === 'undefined') {
		window.writeAd = function(){}
		return;
	}
	var type = Cookie.read('yokaIndexAd2') || (Math.round(Math.random()) == 1 ? 'b' : 'a') ,data;
	data = dataAd[type];
	Cookie.write({
		name:'yokaIndexAd2',
		day : 1,
		value : (type=='a'?'b':'a')
	});
	if(!data){
		data = dataAd['a'];
	}

	window.writeAd = function(name){
		var str = '',
			value = data[name];
		if(!value || value=='') return;
		if(name == 'bothSide'){
			str = '<style>'+
					'body{background:'+ (value.background.color||'') +' url('+ (value.background.url||'') +') '+ (value.background.repeat || 'no-repeat') +' center top '+ (value.background.attachment || '') + ';} '+
					'.bothSideAd{ '+ (value.scroll ? 'position:fixed; _position:relative;' : 'position:relative;') +' width:980px; margin:0 auto;}'+
					'.bothSideAd div{position:absolute;top:0px;left:-140px;width:140px;} '+
					'.bothSideAd span{position:absolute;display:block;top:0px;right:-140px;width:140px;}'+
					'</style> ';
			if(value.img[0] && value.link && value.img[1] && value.img[1] != '' && value.img[0] != '' ){
				str += '<div class="bothSideAd" id="bothSideAd"> '+
						'<div> '+
							'<a href="'+ value.link +'"><img src="'+ value.img[0] +'"/></a>	 '+
						'</div> '+
						'<span> '+
							'<a href="'+ value.link +'"><img src="'+ value.img[1] +'"/></a>	'+
						'</span> '+
					'</div>';
			};
			if(value.scroll){
				str+= '<script>'+
						'(function(){'+
							'function bothSideAd(id){'+
								'var ie6 = !window.XMLHttpRequest && window.ActiveXObject ? true :false;'+
								'var o = document.getElementById(id);'+
								'if (!ie6 || !o) return;'+
								'var t= document.body.scrollTop || document.documentElement.scrollTop;'+
									'o.style.top = t +"px";'+
									'window.attachEvent("onscroll",function(){'+
										't = document.body.scrollTop || document.documentElement.scrollTop;'+
										'o.style.top = t +"px";'+
									'})'+
							'}'+
							'bothSideAd("bothSideAd");'+
						'})();'+
						'</script>';
			}
			document.write(str);
			str = null;
			return;
		}
		document.write('<script src="'+ value +'"></script>')
	}

})();


/**
 * @author {tiger}
 */
function lazyload(){
	function each(ar,fn){
		for(var i=0,l=ar.length;i<l;i++){
			fn.call(ar[i],i);
		}
	}
	var addEvent = (function(){ 
		return document.attachEvent ? 
			function(obj,type,fn){ 
				obj.attachEvent('on'+type,fn)
			} : 
			function(obj,type,fn){ 
				obj.addEventListener(type,fn,false)
			}
		})()
	var removeEvent = (function(){ 
			return document.attachEvent ? 
				function(obj,type,fn){ 
					obj.detachEvent('on'+type,fn)
				} : 
				function(obj,type,fn){ 
					obj.removeEventListener(type,fn,false)
				}
		})()
	
	var imgs = document.getElementsByTagName('img'),
		body = document.getElementsByTagName('body')[0],
		sHeight = document.documentElement.clientHeight || document.body.clientHeight,
		sWidth = document.documentElement.clientWidth || document.body.clientWidth,
		overflow = [ sWidth , sHeight ] ,
		range ={
			x:[0,overflow[0]],
			y:[0,overflow[1]]
		};
	
	function filter(){
		if(imgs.length ==0){
			removeEvent(window,'scroll',filter);
			return;
		}
		var _ar = [],
			site = {
				x:document.body.scrollLeft || document.documentElement.scrollLeft,
				y:document.body.scrollTop || document.documentElement.scrollTop
			},
			range = {
				x:[site.x-overflow[0]/2 , site.x+overflow[0]],
				y:[site.y-overflow[1]/2 , site.y+overflow[1]]
			},
			temp = null;
		each(imgs,function(){
			temp = getSite(this);
			//if(temp.x>=range.x[0] && temp.x <= range.x[1] && temp.y >= range.y[0] && temp.y <= range.y[1]){
			if(temp.y >= range.y[0] && temp.y <= range.y[1]){
				this.src = this.getAttribute('_src');
				//this.removeAttribute('_src');
			}else{
				_ar.push(this);
			}
		})
		imgs = [].concat(_ar);
		_ar = null;
		return imgs;
	}
	
	function getSite(obj){
		var s={x:0,y:0};
			while(obj){
				s.x+=obj.offsetLeft;
				s.y+=obj.offsetTop;
				obj=obj.offsetParent;
			}
		return s;
	}
	
	function init(){
		var ar = [];
		each(imgs,function(){
			if(this.getAttribute('_src')){
				ar.push(this)
			}
		})
		imgs = [].concat(ar);
		ar = null;
		filter();
	}
	
	init();
	
	addEvent(window,'scroll',filter);
	addEvent(window,'resize',function(){
		sHeight = document.documentElement.clientHeight || document.body.clientHeight;
		sWidth = document.documentElement.clientWidth || document.body.clientWidth;
		overflow = [ sWidth , sHeight ];
		filter();
	})

}

posted on 2012-08-29 18:04  lzf0514  阅读(178)  评论(0编辑  收藏  举报

导航