﻿try{document.execCommand('BackgroundImageCache',false,true);}catch(e){}
var isIE=!-[1,];
var isGecko=(navigator.userAgent.indexOf("Gecko")>-1)&&(navigator.userAgent.indexOf("KHTML")==-1);
function $(o){if(document.getElementById&&document.getElementById(o)){return document.getElementById(o);}else if (document.all&&document.all(o)){return document.all(o);}else if (document.layers&&document.layers[o]){return document.layers[o];}else{return false;}} 
function setCls(o,c,f){if(!o)return;var oc=o.className=o.className.replace(new RegExp('( ?|^)'+c+'\\b'),'');if(!f)o.className=oc.length>0?(oc+' '+c):c}
String.prototype.trim= function(){return this.replace(/(^\s*)|(\s*$)/g,"");}
if(!Array.prototype.indexOf)Array.prototype.indexOf=function(item,i){i||(i=0);var l=this.length;if(i<0)i=l+i;for(;i<l;i++)if(this[i]===item)return i;return -1}
function isEmail(s){var p=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;return p.test(s)}
function isUrl(s){var p=new RegExp("((^http)|(^https)):\/\/(\\w)+\.(\\w)+");return p.test(s)}
function onEvent(A,B,C){B=B.replace(/^on/,'').toLowerCase();if(A.attachEvent){A[B+C]=function(){C.call(A,window.event)};A.attachEvent('on'+B,A[B+C])}else A.addEventListener(B,C,false);return A}
function unEvent(A,B,C){B=B.replace(/^on/,'').toLowerCase();if(A.attachEvent){A.detachEvent('on'+B,A[B+C]);A[B+C]=null}else A.removeEventListener(B,C,false);return A}
function show(){this.style.display='block'}
function hide(){this.style.display='none'}
function setVisibile(A,B){if(typeof A=='string')A=$(A);if(A)A.style.display=B?'block':'none'}
function bytes(s){if(typeof(s)!="string"){s=s.value}var l=0;for(var i=0;i<s.length;i++){if(s.charCodeAt(i)>127){l++}l++}return l}
function preloadImage(){var args = preloadImage.arguments;if(!document.preloadImageArray)document.preloadImageArray=[];for(var i=0,len=args.length;i<args;i++){document.preloadImageArray.push(new Image());document.preloadImageArray[document.preloadImageArray.length].src=args[i]}}
function Extend(A,B){for(var C in B){A[C]=B[C]}return A}
function CurrentStyle(A){return A.currentStyle||document.defaultView.getComputedStyle(A,null)}
function Bind(A,B){var C=Array.prototype.slice.call(arguments).slice(2);return function(){return B.apply(A,C.concat(Array.prototype.slice.call(arguments)))}}
function forEach(A,B,C){if(A.forEach){A.forEach(B,C)}else{for(var i=0,len=A.length;i<len;i++)B.call(C,A[i],i,A)}}
var Tween={Quart:{easeOut:function(t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b}},Back:{easeOut:function(t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b}},Bounce:{easeOut:function(t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b}}}}


// Tab 切换
function inittab(o,t,n,e,c){
	var tab = $(o);
	if(tab.isinit) return ;
	tab.isinit = true;
	var ul = $(o + '-body');
	if(!tab || !ul)return;
	var tabs = tab.getElementsByTagName(t||'li');
	if(!tabs || tabs.length == 0)return;
	tab = tabs[0].parentNode;
	tab.uls = [];
	if(n && n.indexOf('.') > 0){
		var t = n.split('.');
		var els = ul.getElementsByTagName(t[0]);
		for(var i=0,len=els.length;i<len;i++){
			if(els[i].className.split(' ').indexOf(t[1]) >= 0) tab.uls.push(els[i]);
		}
	}else{
		n = n || 'ul';
		for(var i=0,len=ul.childNodes.length;i<len;i++){
			if(ul.childNodes[i].tagName && ul.childNodes[i].tagName.toLowerCase() == n) tab.uls.push(ul.childNodes[i]);
		}
	}
	if(!tab.uls)return;
	var cls=c||'current';
	e = e ? e : 'onmouseover';
	tab.lis = tabs;
	for(var i=0;i<tabs.length;i++){
		tabs[i].i=i;
		if(i > 0 && tab.uls[i]) tab.uls[i].style.display = 'none';
		tabs[i][e] = function(){
			var u = this.parentNode;
			var idx = u.act ? u.act : 0;
			setCls(u.lis[idx], cls, true);
			if(u.uls[idx])u.uls[idx].style.display = 'none';
			u.act = this.i;
			setCls(this, cls);
			if(u.uls[this.i])u.uls[this.i].style.display = '';
		}
	}
}

// 焦点图片播放器
function renderPicPlayer(id){
	var interv=4000; //切换间隔时间
	var intervSpeed=10; //切换速度
	var cpic=0;
	var tpic=1;
	var timer, timer1, timer2;
	
	var list=$(id + '-list');
	if(list){list=list.getElementsByTagName('li')}
	var change = $(id + '-change');
	if(!list || !list.length || list.length < 2 || !change){return}
	
	var lis = cls = '';
	var picnum = list.length;
	for(var i=0;i<picnum;i++){cls=i==0?' class="active"':'';lis+='<li'+cls+'>'+(i+1)+'</li>'}
	change.innerHTML = lis;
	change = change.getElementsByTagName('li');
	var div = list[0].getElementsByTagName('div')[0];
	var img_fit_with = div.offsetWidth, img_fit_height = div.offsetHeight;
	for(var i=0;i<picnum;i++){
		change[i].index = i;
		var img = list[i].getElementsByTagName('img');
		if(img && img[0]){
			//img[0].onload = function(){resizeImage(this, img_fit_with, img_fit_height, true)}
		}
		if(i>0){
			list[i].opacity = 0;
			alpha(list[i]);
		}else{
			list[i].opacity = 100;
		}
		change[i].onmouseover = function(){
			list[cpic].opacity = 0;
			alpha(list[cpic]);
			setActive(cpic);
			cpic = tpic = this.index;
			list[tpic].opacity = 100;
			alpha(list[tpic]);
			setActive(tpic,true);
			tpic = tpic == (picnum - 1) ? 0 : tpic + 1;
			window.clearInterval(timer);
			timer = window.setInterval(loop, interv);
		}
	}
	function setActive(n,f){change[n].className=f?'active':''}
	if(picnum < 2){return}
	//控制图层透明度
	function alpha(o){if(isIE){o.style.filter="alpha(opacity="+o.opacity+")";}else{o.style.opacity=(o.opacity/100)}o.style.display=o.opacity>0?'':'none'}
	//渐显
	var fadeon=function(){setActive(tpic,true);var o=list[tpic];o.opacity+=5;alpha(o);if(o.opacity<100){window.clearTimeout(timer1);timer1=setTimeout(fadeon,intervSpeed)}else{cpic=tpic;tpic=tpic==(picnum-1)?0:tpic+1;}}
	//渐隐
	var fadeout=function(){setActive(cpic);var o=list[cpic];o.opacity-=10;alpha(o);if(o.opacity>0){window.clearTimeout(timer2);timer2=setTimeout(fadeout,intervSpeed)}else{o.opacity=0;}}
	//循环
	var loop = function(){fadeout();setTimeout(fadeon,intervSpeed+50)}
	timer = window.setInterval(loop, interv);
}

// 分享
function shareContent(app_url, url_param, title_param){
	app_url += app_url.indexOf('?') < 0 ? '?' : '&';
	app_url += url_param + '=' + encodeURIComponent(window.location.href);
	if(title_param){
		var title = $('content_title');
		title = title ? title.innerHTML : document.title;
		app_url += '&' + title_param + '=' + encodeURIComponent(title);
	}
	window.open(app_url);
}
// 收藏
var favoriteeditor;
function addToFavorite(contentid,url){
	if(!favoriteeditor){
		ajax.request('get',url,{
			success:function(r){
				var v=ajax.jsondecode(r.responseText);
				if(v && v.success && v.success == -1){
					Account.login({callback:function(){addToFavorite(contentid,url)}});
				}else{
					favoriteeditor = Account.window({id:'addfavorite', title:'添加新收藏',width:525});
					//favoriteeditor.body.style.overflow = 'auto';
					favoriteeditor.body.style.height = 'auto';
					ajax.setInnerHTML(favoriteeditor.body, r.responseText);
					var f = favoriteeditor.body.getElementsByTagName('form')[0];
					FormPanel.renderInput(f);
					var title = $('content_title');
					f.title.value = title ? title.innerHTML : document.title;
					
					var description = $('content_description');
					if(description){
						f.description.value = description.innerHTML;
					}else{
						var metas = document.getElementsByTagName('meta');
						for(var i=0,len=metas.length;i<len;i++){
							var meta_name = metas[i].getAttribute('name');
							if(meta_name && meta_name.toLowerCase() == 'description'){
								f.description.value = metas[i].getAttribute('content') ? metas[i].getAttribute('content') : '';
								break;
							}
						}
					}
					f.url.value = window.location.href;
					favoriteeditor.show();
				}
			}
		},{action:'editor'});
	}else{
		Account.hiddenAllError($('ajaxFavoriteEditor'));
		favoriteeditor.show();
	}
}




/**
 * 幻灯图片播放
 * @param {Object} config
 */
var renderPicPlayera = function(config){
	this.body = $(config.el);
	if(!this.body) return;
	this.init(config);
	this.fullmode = config.fullmode || false;
	this.items = this.body.getElementsByTagName('li');
	this.total = this.items.length;
	if(this.total == 0)return;
	this.txtct = document.createElement('div');
	this.body.appendChild(this.txtct);
	this.txtct.className = 'pic-player-txt';
	this.switcher = document.createElement('div');
	this.body.nextSibling ? this.body.parentNode.insertBefore(this.switcher, this.body.nextSibling) : this.body.parentNode.appendChild(this.switcher);
	this.switcher.className = 'pic-player-switcher';
	var h = '';
	if(this.fullmode){
		this.txtct.innerHTML = '<h2></h2><p></p>';
		s = document.createElement('span');
		this.body.appendChild(s);
		s.className = 'pic-player-oper';
		s.innerHTML = '<a href="javascript:;" class="switcher-previous" title="上一张"></a><span class="pic-player-number"><strong>1</strong>/' + this.total + '</span><a href="javascript:;" class="switcher-next" title="下一张"></a>';
		s.firstChild.onclick=function(){tthis.prev()}
		s.lastChild.onclick=function(){tthis.next()}
		this.indexCt = s.childNodes[1].childNodes[0];
	}
	var i, t, d, a, s, w = this.change = this.items[0].offsetWidth, unshow;
	if(this.fullmode && (this.total * 90) > w) w -= 50;
	else unshow = ' style="display:none"';
	var h = '<a href="javascript:;" class="next"' + unshow + '></a><a href="javascript:;" class="prev"' + unshow + '></a><div><ul>';
	for (i = 0; i < this.total; i++) {
		a = this.items[i].getElementsByTagName('a')[0].href;
		t = this.items[i].getElementsByTagName('h2')[0].innerHTML;
		d = this.items[i].getElementsByTagName('p');
		d = d[0] ? d[0].innerHTML : '';
		if(d) d += '<a href="' + a + '" class="cred">[详细]</a>';
		this.items[i].values = {href:a, title:t, desc: d};
		s = this.items[i].getElementsByTagName('img')[0].src;
		h += '<li><a href="javascript:;""><img src="' + s + '" title="' + t + '"index="' + i + '" /></a></li>';
	} 
	this.switcher.innerHTML = h + '</ul></div>';
	this._w = w;
	this.switcher.childNodes[2].style.width = w + 'px';
	var tthis = this;
	this._slider = this.items[0].parentNode; 
	this.current = this.items[0];
	this.titlect = this.fullmode ? this.txtct.firstChild : this.txtct;
	this.switcher.childNodes[2].onclick = function(e){
		e = window.event ? window.event.srcElement : e.target;
		var n = e.tagName.toLowerCase();
		if(n != 'a' && n!= 'img' ) return;
		if(e.attributes['index']) tthis.run(parseInt(e.attributes['index'].value));
	}
	this.run(0);
	if(!this.fullmode) return ;
	this.control = document.createElement('span');
	this.body.insertBefore(this.control, this.body.firstChild);
	this.control.innerHTML = '<a href="javascript:;" class="pic-player-prev" title="上一张"></a><a href="javascript:;" class="pic-player-next" title="下一张"></a>';
	this.body.prev = this.control.firstChild;
	this.body.next = this.control.lastChild;
	this.body.prev.onclick=function(){tthis.prev()}
	this.body.next.onclick=function(){tthis.next()}
	this.control.style.display = 'none'; 
	this.body.onmouseover=function(e){tthis.showDesc(e)};
	this.body.onmouseout=function(e){tthis.hideDesc(e)};
	if(!unshow){
		this._fw = this.total * 82 - 4;
		this.switcher.childNodes[0].onmousedown = function(){tthis.srollto='right';tthis.sroll()}
		this.switcher.childNodes[1].onmousedown = function(){tthis.srollto='left';tthis.sroll()}
		this.switcher.childNodes[0].onmouseup = this.switcher.childNodes[1].onmouseup = function(){tthis.srollto=false}
	}
}
renderPicPlayera.prototype = {
	_timer : null, //定时器
	_oth : 28, // 标题高度
	index : 0, // 当前索引
	srollpx:30,
	_srollpx:30,
	// 设置默认属性
	init: function(config) {
		this.config = { // 默认值
			auto:		true, // 是否自动
			time:		1, // 滑动延时
			pause:		3000 // 停顿时间(Auto为true时有效)
		};
		Extend(this.config, config || {});
	},
  	setTitle : function(){
		var v = this.current.values;
		this.titlect.innerHTML = '<a href="'+v.href+'" target="_blank">'+v.title+'</a>';
		if(this.fullmode) this.txtct.lastChild.innerHTML = v.desc;
		if(this.txtct.offsetHeight > 32) this.txtct.style.height = this.txtct.lastChild.offsetHeight + 32 + 'px';
	},
	showDesc: function(e){
		this.control.style.display = '';
		if(this.txtct.lastChild.innerHTML == '')return;
		this.txtct.timer && clearTimeout(this.txtct.timer);
		var h = this.txtct.lastChild.offsetHeight + 32;
		if(this.txtct.offsetHeight >= h){this.txtct.style.height = h + 'px';return}
		this.setTxtctHeight(h);
	},
	hideDesc:function(e){
		this.txtct.timer && clearTimeout(this.txtct.timer);
		if(this.txtct.offsetHeight == this._oth) return;
		this.control.style.display = 'none';
		if(this.txtct.lastChild.innerHTML == '')return;
		this.setTxtctHeight(this._oth);
	},
	setTxtctHeight: function(A){
		var B=this.txtct, C=parseInt(CurrentStyle(B)['height']);
		if(A==C) return;
		if(B.timer) clearTimeout(B.timer);
		var D=C<A?1:-1, E=D*(A-C), F=Math.ceil(E/2);
		C+=D*F;
		B.style.height=C+'px';
		var tthis = this;
		B.timer = setTimeout(function(){tthis.setTxtctHeight(A)}, 10);
	},
	run: function(index) {
		//修正index
		index == undefined && (index = this.index);
		index < 0 && (index = this.total - 1) || index >= this.total && (index = 0);
		setCls(this.switcher.childNodes[2].firstChild.childNodes[this.c_index||0], 'current' ,1);
		// 设置参数
		this._target = -Math.abs(this.change) * (this.index = index);
		this.c_index = this.index;
		this.current = this.items[this.index];
		if(this.fullmode) this.indexCt.innerHTML = index + 1;
		this.setTitle();
		setCls(this.switcher.childNodes[2].firstChild.childNodes[this.index], 'current');
		if(this.fullmode) this.scrollSet();
		this.move();
	},
	// move
	move: function() {
		var A=this._target, B=this._slider, C=parseInt(CurrentStyle(B)['left']) || 0;
		if(this._timer) clearTimeout(this._timer);
		if(A==C){this.config.auto && (this._timer = setTimeout(Bind(this, this.next), this.config.pause||3000));return}
		var D=C<A?1:-1, E=D*(A-C), F=Math.ceil(E/3);
		C+=D*F;
		this.moveto(C);
		var tthis = this;
		this._timer = setTimeout(function(){tthis.move(A)}, 10);
	},
	scrollSet:function(){
		var t = this.switcher.childNodes[2].firstChild.childNodes[this.index];
		if(!t._left){
			var w = this.index * 82 + t.offsetWidth / 2;
			var _w = this._w / 2; 
			w > _w ? w -= _w : w = 0;
			if((this._fw - w) < this._w) w = this._fw - this._w;
			if(w < 0) w = 0;
			t._left = -parseInt(w);
		}
		t.parentNode.style['left'] = t._left + 'px';
	},
	sroll:function(){
		if (!this.srollto){this._srollpx = this.srollpx;return}
		var t = this.switcher.childNodes[2].firstChild;
		var l = parseInt(CurrentStyle(t)['left']);
		if(isNaN(l)) l = 0;
		if((l == 0 && this.srollto == 'left') || (this.srollto == 'right' && this._fw && l == (this._w - this._fw))) return;
		l += this.srollto == 'right' ? -this._srollpx : this._srollpx;
		if(l > 0) l = 0;
		else if((l + this._fw) < this._w) l = this._w - this._fw;
		t.style['left'] = l + 'px';
		this._srollpx = 10;
		this._stimer = setTimeout(Bind(this, this.sroll), 15);
	},
	moveto: function(i){this._slider.style['left']=i+'px'},
	prev: function(){this.run(--this.index)},
	next: function(){this.run(++this.index)},
	stop:function(){clearTimeout(this._timer);this.moveto(this._target)}
};
