// 判断是否为IE浏览器
var msie = !+"\v1";

(function(){
	// 自定义对象
	if(!window.lizux) {
		window['lizux'] = {};
	}

	// 自定义扩展方法
	lizux.extend = function(func,obj) {
		if (!obj) {
			obj = this;
		}
		for (var i in func) {
			obj[i] = func[i];
		}
		return obj;
	}

	addDOMLoadEvent=(function(){var e=[],t,s,n,i,o,d=document,w=window,r='readyState',c='onreadystatechange',x=function(){n=1;clearInterval(t);while(i=e.shift())i();if(s)s[c]=''};return function(f){if(n)return f();if(!e[0]){d.addEventListener&&d.addEventListener("DOMContentLoaded",x,false);/*@cc_on@*//*@if(@_win32)d.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");s=d.getElementById("__ie_onload");s[c]=function(){s[r]=="complete"&&x()};/*@end@*/if(/WebKit/i.test(navigator.userAgent))t=setInterval(function(){/loaded|complete/.test(d[r])&&x()},10);o=w.onload;w.onload=function(){x();o&&o()}}e.push(f)}})();

	function byId(id) {
		return document.getElementById(id);
	}

	function byTag(name, elem) {
		return (elem || document).getElementsByTagName(name);
	}

	function byClass(name, tag, elem) {
		var tempNodes = [];
		var tempNode = (elem || document).getElementsByTagName(tag || "*");
		var pattern = new RegExp("\\b"+name+"\\b");
		for (var i = 0; i<tempNode.length; i++) {
			if (pattern.test(tempNode[i].className)) {
				tempNodes.push(tempNode[i]);
			}
		}
		return tempNodes;
	}

	function extractNodes(elem) {
		if (elem.nodeType != 1) {
			return null;
		}
		var tempNode, tempNodes = [];
		for (var i = 0; tempNode=elem.childNodes[i]; i++) {
			if (tempNode.nodeType == 1) {
				tempNodes.push(tempNode);
			}
		}
		return tempNodes;
	}

	function superClass(elem, action, class1, class2) {
		switch (action) {
			case 'swap' :
				elem.className = !superClass(elem, 'check', class1) ? elem.className.replace(class2, class1) : elem.className.replace(class1, class2);
				break;
			case 'add' :
				if (!superClass(elem, 'check', class1)) {
					elem.className += elem.className ? ' '+class1 : class1;
				}
				break;
			case 'remove' :
				var rep = elem.className.match(' '+class1) ? ' '+class1 : class1;
				elem.className = elem.className.replace(rep, '');
				break;
			case 'check' :
				return new RegExp('\\b'+class1+'\\b').test(elem.className);
				break;
		}
	}

	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = name+"="+value+expires+"; path=/";
	}
	function readCookie(name) {
		var nameEQ = name+"=";
		var cookies = document.cookie.split(';');
		for (var i = 0; i<cookies.length; i++) {
			var coo = cookies[i];
			while (coo.charAt(0) == ' ') {
				coo = coo.substring(1, coo.length);
			}
			if (coo.indexOf(nameEQ) == 0) {
				return coo.substring(nameEQ.length, coo.length);
			}
		}
		return null;
	}
	function removeCookie(name) {
		createCookie(name,"",-1);
	}

	function sendAjax(url, callback, postData, callpars) {
		var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		if (!xhr) {
			return;
		}
		var method = (postData) ? "POST" : "GET";		
		xhr.open(method,url,true);
		xhr.setRequestHeader('User-Agent','XMLHTTP');
		if (postData) {
			xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		}
		xhr.onreadystatechange = function() {
			if (xhr.readyState != 4) {
				return;
			}
			if (xhr.status != 200 && xhr.status != 304 && xhr.status != 0) {
				alert('HTTP error '+xhr.status);
				return;
			}			
			(callpars!="undefined")?callback(xhr,callpars):callback(xhr);
		};
		if (xhr.readyState == 4) {
			return;
		}
		xhr.send(postData);
	}

	function getPos(obj) {
		var left = 0;
		var top = 0;
		while (obj.offsetParent) {
			left += obj.offsetLeft;
			top += obj.offsetTop;
			obj = obj.offsetParent;
		}
		return {x:left, y:top};
	}

	function contains(a, b) {
		if (!a || !b) {
			return;
		}
		while (b.parentNode) {
			if ((b=b.parentNode) == a) {
				return true;
			}
		}
		return false;
	}

	function checkArr(array, val) {
		for (var i = 0; i<array.length; i++) {
			if (array[i] == val) {
				return true;
			}
		}
		return false;
	}

	function delArr(array,val) {
		for (var i=0; i<array.length; i++) {
			if (array[i] == val) {
				for (var j=i; j<array.length; j++) {
					array[j] = array[j + 1];
				}
				array.pop();
			}
		}
	}

	function delNode(obj) {
		if (obj != null) {
			obj.parentNode.removeChild(obj);
		}
	}

	window['lizux']['domReady'] = addDOMLoadEvent;
	window['lizux']['id'] = byId;
	window['lizux']['tag'] = byTag;
	window['lizux']['byClass'] = byClass;
	window['lizux']['childs'] = extractNodes;
	window['lizux']['sClass'] = superClass;
	window['lizux']['createCookie'] = createCookie;
	window['lizux']['readCookie'] = readCookie;
	window['lizux']['removeCookie'] = removeCookie;
	window['lizux']['sendAjax'] = sendAjax;
	window['lizux']['getPos'] = getPos;
	window['lizux']['has'] = contains;
	window['lizux']['checkArr'] = checkArr;
	window['lizux']['delArr'] = delArr;
	window['lizux']['delNode'] = delNode;
})();

var IEPNGFix = window.IEPNGFix || {};
IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}
		};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;
	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;
	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
		return;
	}
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = (xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = (yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = xPos + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {
						elm.appendChild(d);
					}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}
	this.hook.enabled = 1;
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};
IEPNGFix.update = function() {
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;
if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}
