var BROWSER = {};
var USERAGENT = navigator.userAgent.toLowerCase();
browserVersion({'ie':'msie','firefox':'','chrome':'','opera':'','safari':'','maxthon':'','mozilla':'','webkit':''});
if(BROWSER.safari) {
	BROWSER.firefox = true;
}
BROWSER.opera = BROWSER.opera ? opera.version() : 0;

function browserVersion(types) {
	var other = 1;
	for(i in types) {
		var v = types[i] ? types[i] : i;
		if(USERAGENT.indexOf(v) != -1) {
			var re = new RegExp(v + '(\\/|\\s)([\\d\\.]+)', 'ig');
			var matches = re.exec(USERAGENT);
			var ver = matches != null ? matches[2] : 0;
			other = ver !== 0 ? 0 : other;
		}else {
			var ver = 0;
		}
		eval('BROWSER.' + i + '= ver');
	}
	BROWSER.other = other;
}

function iceo(id) {
	return !id ? null : document.getElementById(id);
}
function display(id) {
	iceo(id).style.display = iceo(id).style.display == '' ? 'none' : '';
}
function zoom(obj, zimg) {
	zimg = !zimg ? obj.src : zimg;
	if(!zoomstatus) {
		window.open(zimg, '', '');
		return;
	}
	if(!obj.id) obj.id = 'img_' + Math.random();
	var menuid = obj.id + '_zmenu';
	var menu = iceo(menuid);
	var imgid = menuid + '_img';
	var zoomid = menuid + '_zimg';
	var maxh = (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) - 70;

	if(!menu) {
		menu = document.createElement('div');
		menu.id = menuid;
		var objpos = fetchOffset(obj);
		menu.innerHTML = '<div onclick="iceo(\'append_parent\').removeChild(iceo(\'' + obj.id + '_zmenu\'))" style="z-index:600;filter:alpha(opacity=50);opacity:0.5;background:#FFF;position:absolute;width:' + obj.clientWidth + 'px;height:' + obj.clientHeight + 'px;left:' + objpos['left'] + 'px;top:' + objpos['top'] + 'px"><table width="100%" height="100%"><tr><td valign="middle" align="center"><img src="' + IMGDIR + '/loading.gif" /></td></tr></table></div>' +
			'<div style="position:absolute;top:-100000px;display:none"><img id="' + imgid + '" src="' + zimg + '"></div>';
		iceo('append_parent').appendChild(menu);
		iceo(imgid).onload = function() {
			iceo(imgid).parentNode.style.display = '';
			var imgw = iceo(imgid).width;
			var imgh = iceo(imgid).height;
			var r = imgw / imgh;
			var w = document.body.clientWidth * 0.95;
			w = imgw > w ? w : imgw;
			var h = w / r;
			if(h > maxh) {
				h = maxh;
				w = h * r;
			}
			iceo('append_parent').removeChild(menu);
			menu = document.createElement('div');
			menu.id = menuid;
			menu.style.overflow = 'visible';
			menu.style.width = (w < 300 ? 300 : w) + 20 + 'px';
			menu.style.height = h + 50 + 'px';
			menu.innerHTML = '<div class="zoominner"><p id="' + menuid + '_ctrl"><span class="y"><a href="' + zimg + '" class="imglink" target="_blank" title="在新窗口打开">在新窗口打开</a><a href="javascipt:;" id="' + menuid + '_adjust" class="imgadjust" title="实际大小">实际大小</a><a href="javascript:;" onclick="hideMenu()" class="imgclose" title="关闭">关闭</a></span>鼠标滚轮缩放图片</p><div align="center" onmousedown="zoomclick=1" onmousemove="zoomclick=2" onmouseup="if(zoomclick==1) hideMenu()"><img id="' + zoomid + '" src="' + zimg + '" width="' + w + '" height="' + h + '" w="' + imgw + '" h="' + imgh + '"></div></div>';
			iceo('append_parent').appendChild(menu);
			iceo(menuid + '_adjust').onclick = function(e) {adjust(e, 1)};
			if(BROWSER.ie){
				menu.onmousewheel = adjust;
			} else {
				menu.addEventListener('DOMMouseScroll', adjust, false);
			}
			showMenu({'menuid':menuid,'duration':3,'pos':'00','cover':1,'drag':menuid,'maxh':maxh+70});
		};
	} else {
		showMenu({'menuid':menuid,'duration':3,'pos':'00','cover':1,'drag':menuid,'maxh':menu.clientHeight});
	}
	if(BROWSER.ie) doane(event);
	var adjust = function(e, a) {
		var imgw = iceo(zoomid).getAttribute('w');
		var imgh = iceo(zoomid).getAttribute('h');
		var imgwstep = imgw / 10;
		var imghstep = imgh / 10;
		if(!a) {
			if(!e) e = window.event;
			if(e.altKey || e.shiftKey || e.ctrlKey) return;
			if(e.wheelDelta <= 0 || e.detail > 0) {
				if(iceo(zoomid).width - imgwstep <= 200 || iceo(zoomid).height - imghstep <= 200) {
					doane(e);return;
				}
				iceo(zoomid).width -= imgwstep;
				iceo(zoomid).height -= imghstep;
			} else {
				if(iceo(zoomid).width + imgwstep >= imgw) {
					doane(e);return;
				}
				iceo(zoomid).width += imgwstep;
				iceo(zoomid).height += imghstep;
			}
		} else {
			iceo(zoomid).width = imgw;
			iceo(zoomid).height = imgh;
		}
		menu.style.width = (parseInt(iceo(zoomid).width < 300 ? 300 : parseInt(iceo(zoomid).width)) + 20) + 'px';
		menu.style.height = (parseInt(iceo(zoomid).height) + 50) + 'px';
		setMenuPosition('', menuid, '00');
		doane(e);
	};
}
function loadcss(cssname) {
	if(!CSSLOADED[cssname]) {
		css = document.createElement('link');
		css.type = 'text/css';
		css.rel = 'stylesheet';
		css.href = 'data/cache/style_' + STYLEID + '_' + cssname + '.css?' + VERHASH;
		var headNode = document.getElementsByTagName("head")[0];
		headNode.appendChild(css);
		CSSLOADED[cssname] = 1;
	}
}
if(BROWSER.ie) {
	document.documentElement.addBehavior("#default#userdata");
}
function initTab(frameId, type) {
	if (typeof document['diyform'] == 'object' || iceo(frameId).className.indexOf('tab') < 0) return false;
	type = type || 'click';
	var tabs = iceo(frameId+'_title').childNodes;
	var arrTab = [];
	for(var i in tabs) {
		if (tabs[i]['nodeType'] == 1 && tabs[i]['className'].indexOf('move-span') > -1) {
			arrTab.push(tabs[i]);
		}
	}
	var counter = 0;
	var tab = document.createElement('ul');
	tab.className = 'tb cl';
	var len = arrTab.length;
	for(var i = 0;i < len; i++) {
		var tabId = arrTab[i].id;
		if (hasClass(arrTab[i],'frame') || hasClass(arrTab[i],'tab')) {
			var arrColumn = [];
			for (var j in arrTab[i].childNodes) {
				if (typeof arrTab[i].childNodes[j] == 'object' && !hasClass(arrTab[i].childNodes[j],'title')) arrColumn.push(arrTab[i].childNodes[j]);
			}
			var frameContent = document.createElement('div');
			frameContent.id = tabId+'_content';
			frameContent.className = hasClass(arrTab[i],'frame') ? 'content cl '+arrTab[i].className.substr(arrTab[i].className.lastIndexOf(' ')+1) : 'content cl';
			var colLen = arrColumn.length;
			for (var k = 0; k < colLen; k++) {
				frameContent.appendChild(arrColumn[k]);
			}
		} else {
			var frameContent = iceo(tabId+'_content');
			frameContent = frameContent || document.createElement('div');
		}
		frameContent.style.display = counter ? 'none' : '';
		iceo(frameId+'_content').appendChild(frameContent);

		var li = document.createElement('li');
		li.id = tabId;
		li.className = counter ? '' : 'a';
		li.innerHTML = arrTab[i]['innerText'] ? arrTab[i]['innerText'] : arrTab[i]['textContent'];
		li.innerHTML = '<a href="javascript:;" onclick="return false;" onfocus="this.blur();">'+li.innerHTML+'</a>';
		li['on'+type] = function(e){switchTabUl(e);};
		tab.appendChild(li);
		iceo(frameId+'_title').removeChild(arrTab[i]);
		counter++;
	}
	iceo(frameId+'_title').appendChild(tab);
}
function switchTabUl (e) {
	e = e || window.event;
	var aim = e.target || e.srcElement;
	var tabId = aim.id;
	var parent = aim.parentNode;
	while(parent['nodeName'] != 'UL' && parent['nodeName'] != 'BODY') {
		tabId = parent.id
		parent = parent.parentNode;
	}
	if(parent['nodeName'] == 'BODY') return false;
	var tabs = parent.childNodes;
	var len2 = tabs.length;
	for(var j = 0; j < len2; j++) {
		tabs[j].className = (tabs[j].id == tabId) ? 'a' : '';
		var content = iceo(tabs[j].id+'_content');
		if (content) content.style.display = tabs[j].id == tabId ? 'block' : 'none';
	}
}
function hasClass(elem, className) {
	return elem.className && (" " + elem.className + " ").indexOf(" " + className + " ") != -1;
}
function runslideshow() {
	var slideshows = [];
	var elements = document.getElementsByTagName('ul');
	for(var i=0,L=elements.length; i<L; i++) {
		if(hasClass(elements[i], 'slideshow')) {
			slideshows.push(elements[i]);
		}
	}
	var elements = document.getElementsByTagName('div');
	for(var i=0,L=elements.length; i<L; i++) {
		if(hasClass(elements[i], 'slideshow')) {
			slideshows.push(elements[i]);
		}
	}
	for(var i=0,L=slideshows.length; i<L; i++) {
		new slideshow(slideshows[i]);
	}
}
function slideshow(el) {
	var obj = this;
	var parent = el;
	while ((parent = parent.parentNode) && parent != document.body) {
		if (hasClass(parent,'content')) {
			this.blockid = parent.id.replace(/_content/g,'');
			break;
		}
	}
	this.blockid = this.blockid ? this.blockid : el.parentNode.parentNode.id;
	if(typeof slideshow.entities == 'undefined') {
		slideshow.entities = [];
	}
	for(var i=0,L=slideshow.entities.length; i<L;i++) {
		if(slideshow.entities[i].blockid == this.blockid) {
			return ;
		}
	}
	this.id = slideshow.entities.length;
	slideshow.entities[this.id] = this;
	this.container = el;
	this.elements = [];
	this.imgs = [];
	this.imgLoad = [];
	this.imgLoaded = 0;
	this.index = this.length = 0;
	var nodes = el.childNodes;
	for(var i=0, L=nodes.length; i<L; i++) {
		if (nodes[i].nodeType == 1) {
			this.elements[this.length] = nodes[i];
			this.length += 1;
		}
	}
	for(var i=0, L=this.elements.length; i<L; i++) {
		this.elements[i].style.display = "none";
	}
	this.container.parentNode.style.position = 'relative';
	this.slidebar = document.createElement('div');
	this.slidebar.className = 'slidebar';
	this.slidebar.style.display = 'none';
	var html = '<ul>';
	for(var i=0; i<this.length; i++) {
		html += '<li onmouseover="slideshow.entities[' + this.id + '].xactive(' + i + '); return false;">' + (i + 1).toString() + '</li>';
	}
	html += '</ul>';
	this.slidebar.innerHTML = html;
	this.container.parentNode.appendChild(this.slidebar);
	this.controls = this.slidebar.getElementsByTagName('li');

	this.active = function(index) {
		this.elements[this.index].style.display = "none";
		this.elements[index].style.display = "block";
		this.controls[this.index].className = '';
		this.controls[index].className = 'on';
		this.index = index;
	};
	this.xactive = function(index) {
		clearTimeout(this.timer);
		this.active(index);
		var ss = this;
		this.timer = setTimeout(function(){
			ss.run();
		}, 8000);
	};
	this.run = function() {
		var index = this.index + 1 < this.length ? this.index + 1 : 0;
		this.active(index);
		var ss = this;
		this.timer = setTimeout(function(){
			ss.run();
		}, 5000);
	};

	var imgs = el.getElementsByTagName('img');
	for(i=0, L=imgs.length; i<L; i++) {
		this.imgs.push(imgs[i]);
		this.imgLoad.push(new Image());
		this.imgLoad[i].src = this.imgs[i].src;
		this.imgLoad[i].onerror = function (){obj.imgLoaded ++;};
	}

	this.getSize = function (img) {
		if (!img) return false;
		this.width = img.width ? parseInt(img.width) : 0;
		this.height = img.height ? parseInt(img.height) : 0;
		var ele = img.parentNode;
		while ((!this.width || !this.height) && !hasClass(ele,'slideshow') && ele != document.body) {
			this.width = ele.style.width ? parseInt(ele.style.width) : 0;
			this.height = ele.style.height ? parseInt(ele.style.height) : 0;
			ele = ele.parentNode;
		}
		return true;
	};

	this.getSize(imgs[0]);

	this.checkLoad = function () {
		var obj = this;
		for(i = 0;i < this.imgs.length;i++) {
			if(this.imgLoad[i].complete && !this.imgLoad[i].status) {
				this.imgLoaded++;
				this.imgLoad[i].status = 1;
			}
		}
		if(this.imgLoaded < this.imgs.length) {
			if (!iceo(this.blockid+'_percent')) {
				var dom = document.createElement('div');
				dom.id = this.blockid+"_percent";
				dom.style.width = this.width ? this.width+'px' : '150px';
				dom.style.height = this.height ? this.height+'px' : '150px';
				dom.style.lineHeight = this.height ? this.height+'px' : '150px';
				dom.style.backgroundColor = '#ccc';
				dom.style.textAlign = 'center';
				dom.style.top = '0';
				dom.style.left = '0';
				el.parentNode.appendChild(dom);
			}
			el.parentNode.style.position = 'relative';
			iceo(this.blockid+'_percent').innerHTML = (parseInt(this.imgLoaded / this.imgs.length * 100)) + '%';
			setTimeout(function () {obj.checkLoad();}, 100)
		} else {
			if (iceo(this.blockid+'_percent')) el.parentNode.removeChild(iceo(this.blockid+'_percent'));
			this.container.style.display = 'block';
			this.slidebar.style.display = '';
			this.index = this.length -1;
			this.run();
		}
	};
	this.checkLoad();
}
