var tipEndStr = '<br /><br />感谢对5iKFC(<a href="http://www.5ikfc.com">http://www.5ikfc.com</a>)的关注<br /><a href="#" onclick="removeTip();return(false);" title="关闭提示窗口">关闭此提示框&raquo;</a>';

var $ = function(id) {
	return(document.getElementById(id));
};

function rootElement() {
	if(document.documentElement)
		if(document.documentElement.clientHeight > 0)
			return(document.documentElement);
		else
			return(document.body);
	else
		return(document.body);
}

function fetchLength(str) {
	var totalLength = 0;
	for (var i=0; i<str.length; i++){
		var intCode = str.charCodeAt(i);
		if(intCode >=0 && intCode <= 128)
			totalLength++;
		else
			totalLength += 2;
	}
	return(totalLength);
}

function addToBookmark(title,url) {
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) {
			window.external.AddFavorite( url, title);
		} else if( window.opera && window.print ) {
			return(true);
	}
}

function writeCookie(name, value, expires, path, domain, secure){
	var today = new Date();
	if(expires) {
		expires = expires * 1000 * 60 * 60 * 24;
		var expiresDate = new Date(today.getTime() + expires);
	}
	document.cookie = name + "=" + value + (expires ? "; expires=" + expiresDate.toGMTString() : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure=" + secure : "");
}

function readCookie(name) {
	var cookieA = document.cookie.split("; ");
	for (var i=0; i<cookieA.length; i++){
		var crumbA = cookieA[i].split("=");
		if (name == crumbA[0]){
			return(crumbA[1]);
		}
	}
	return(false);
}

function clearCookie(name) {
	if(readCookie(name))
		document.cookie = name + "=" + "null" + ";expires=Fri, 31 Dec 1999 23:59:59 GMT;";
	else
		return(false);
}

function addToFavorite(v) {
	v = v.toString();
	var couponFavorite = readCookie("couponFavorite");
	var added = false;
	if(couponFavorite == false) {
		writeCookie("couponFavorite", v, 365, "/", ".5ikfc.com", null);
		createTip(-1, -1, 400, 300, '收藏成功！', '优惠券收藏成功！<br />您可以点击本站的【我的收藏】查看您收藏的优惠券。' + tipEndStr);
	}else{
		var crumbA = couponFavorite.split(",");
		for (var i=0; i<crumbA.length; i++) {
			if(v == crumbA[i]){
				added = true;
				break;
			}
		}
		if(!added){
			writeCookie("couponFavorite", couponFavorite + "," + v, 365, "/", ".5ikfc.com", null);
			createTip(-1, -1, 400, 300, '收藏成功！', '优惠券收藏成功！<br />您可以点击本站的【我的收藏】查看您收藏的优惠券。' + tipEndStr);
		}else{
			createTip(-1, -1, 400, 300, '重复收藏！', '您已收藏过这张优惠券，无需再次收藏。' + tipEndStr);
		}
	}
	return(false);
}

function removeFromFavorite(v) {
	v = v.toString();
	var couponFavorite = readCookie("couponFavorite");
	if(couponFavorite != false) {
		var re;
		re = new RegExp("^" + v + "$");
		couponFavorite = couponFavorite.replace(re, '');
		re = new RegExp("^" + v + ",");
		couponFavorite = couponFavorite.replace(re, '');
		re = new RegExp("," + v + ",");
		couponFavorite = couponFavorite.replace(re, ',');
		re = new RegExp("," + v + "$");
		couponFavorite = couponFavorite.replace(re, '');
		writeCookie("couponFavorite", couponFavorite, 365, "/", ".5ikfc.com", null);
		location.reload();
	}
}

function createMask() {
	var root = rootElement();
	var documentHeight = root.scrollHeight;
	var documentWidth = root.scrollWidth;
	var mask = document.createElement("div");
	mask.id = "mask";
	mask.className = "mask";
	mask.style.width = documentWidth + "px";
	mask.style.height = documentHeight + "px";
	try{
		mask.style.filter = "alpha(opacity=30)";
		mask.style.opacity = "0.3";
	}catch(e){};
	document.body.appendChild(mask);
	window.onresize = function() {
		var root = rootElement();
		var w = root.scrollWidth;
		var documentWidth = w.toString() + "px";
		if($("mask"))
			$("mask").style.width = documentWidth;
	};
}

function removeMask() {
	var mask = $("mask");
	if(mask)
		mask.parentNode.removeChild(mask);
	window.onresize = function() {};
}

function createTip(x, y, w, h, tle, con) {
	createMask();
	var root = rootElement();
	var tip = document.createElement("div");
	tip.id = "tip";
	tip.className = "tip";
	if(x < 0) x = Math.ceil((root.clientWidth - w) / 2);
	if(x < 0) x = 0;
	tip.style.left = x.toString() + "px";
	if(y < 0) y = Math.ceil((root.clientHeight - h) / 2);
	if(y < 0) y=0;
	y = y + root.scrollTop;
	tip.style.top = y.toString() + "px";
	tip.style.width = w.toString() + "px";
	var iStr = "<div class=\"bd1\"></div><div class=\"bd2\"></div><div class=\"bd3\"></div>";
	iStr += "<div class=\"bd\" style=\"height:18px\">";
	iStr += "<span id=\"tipTle\">";
	iStr += tle;
	iStr += "</span><span id=\"tipClose\">×</span>";
	iStr += "</div>";
	iStr += "<div class=\"bd\" style=\"padding-top:0;\">";
	iStr += "<div id=\"tipCon\">";
	iStr += con;
	iStr += "</div>";
	iStr += "</div>";
	iStr += "<div class=\"bd3\"></div><div class=\"bd2\"></div><div class=\"bd1\"></div>";
	document.body.appendChild(tip);
	tip.innerHTML = iStr;
	closeTip();
}

function removeTip() {
	var mask = $("mask");
	if(mask)
		mask.parentNode.removeChild(mask);	
	window.onresize = function() {};
	var tip = $("tip");
	if(tip)
		tip.parentNode.removeChild(tip);
}

function closeTip(){
	var tipClose = $("tipClose");
	if(tipClose)
		tipClose.onclick = function(){ removeTip(); };
}

function delayRemoveTip() {

}

function setPng(img, w, h) {
	ua = window.navigator.userAgent.toLowerCase();
	if(!/msie/.test(ua))
		return;
	imgStyle = "display:inline-block;" + img.style.cssText;
	strNewHTML = "<span style=\"width:" + w + "px; height:" + h + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "', sizingMethod='scale');\"></span>";
	img.outerHTML = strNewHTML;
}