﻿//这里是本站常用的一些js函数

//document.domain = "splife.com";

var g_baseUrl =	"http://www.splife.com";

var g_showAjaxWorking = true;
// these codes for some effections during the Ajax request
var myGlobalHandlers = {
	onCreate: function() {
		if (g_showAjaxWorking == true)
		{
			var ob;
			if (!$("ajaxWorking")) {
				chkAndCreate("ajaxWorking", "div")
				ob = $("ajaxWorking");
				ob.appendChild(document.createTextNode("正在加载..."));
			} else {
				ob = $("ajaxWorking");
			}
			ob.style.marginTop = document.documentElement.scrollTop + "px";
			ob.style.display = "block";
			ob.style.marginLeft = (document.documentElement.scrollWidth - ob.offsetWidth) + "px";
			//Element.show("ajaxWorking");
		}
		g_showAjaxWorking = true;
	},
	onComplete: function() {
		if (Ajax.activeRequestCount == 0)
		{
			//Element.hide("ajaxWorking");
			$("ajaxWorking").style.display = "none";
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);


var g_deviceJustClicked;
//添加设备到js
function addDevice2Cookie(d)
{
	setCookie("cphonetype", d);
	g_deviceJustClicked = d;
	var u;
	var cc;
	u = g_baseUrl + "/action/setCookies.aspx";
	cc = getCookie("phonetype");
	if(cc == null)
	{
		cc = "";
	}
	var p = "action=phonetype&phonetype=addtype&values=" + d + "&cc=" + cc + "&r=" + Math.random();
	//alert(p);
	var myAjax = new Ajax.Request(
		u,
		{
		method: 'get',
		parameters: p,
		onComplete: addDevice2Cookie2
		});
	
	return false;
}

function addDevice2Cookie2(originalRequest)
{
	var resp = originalRequest.responseText;
	var a = resp.split("\n");
	if(a[0] == "true")
	{
		setCookie("phonetype", a[1]);
		//setCookie("cphonetype", g_deviceJustClicked);
		location.href = "/s/resCatalog.aspx?phonetype=" + g_deviceJustClicked;
		//location.href = g_baseUrl + "/";
	}
	else
	{
		msg2("请求失败！<br />返回信息：" + resp, "0");
	}
}

function addDevice2Cookie3(d)
{
	setCookie("cphonetype", d);
	//location.href = g_baseUrl + "/";
	location.href = "/s/resCatalog.aspx?phonetype=" + d;
	return false;
}

function delAllDevice()
{
	setCookie("cphonetype", "");
	location.href = g_baseUrl + "/";
	return false;
}

function delDevice(d)
{
	g_deviceJustClicked = d;
	var u;
	var cc;
	u = "/action/setCookies.aspx";
	cc = getCookie("phonetype");
	
	if(cc == null)
	{
		cc = "";
	}
	var p = "action=phonetype&phonetype=deltype&values=" + d + "&cc=" + cc + "&r=" + Math.random();
	var myAjax = new Ajax.Request(
		u,
		{
		method: 'get',
		parameters: p,
		onComplete: delDevice2
		});
	
	return false;
}

function delDevice2(originalRequest)
{
	var resp = originalRequest.responseText;
	var a = resp.split("\n");
	if(a[0] == "true")
	{
		/* 删除当前机型 g_deviceJustClicked */
		setCookie("phonetype", a[1]);
		$("top_phone_" + g_deviceJustClicked).style.display = "none";
		/* chkAndDel("top_phone_" + g_deviceJustClicked);
		location.href = "/s/changeDevice.aspx"; */
	}
	
	// if the phonetype just deleted is the current selected phonetype, refresh the page
	if(g_deviceJustClicked == getCookie("cphonetype"))
	{
		delCookie("cphonetype");
		location.reload();
	}
}

//检查id为targetId的elementName元素是否在在，若不存在则嚿/body>前插僿
function chkAndCreate(targetId, elementName)
{
	if($(targetId) == null)
	{
		var pBody = document.getElementsByTagName("body")[0];
		var t = document.createElement(elementName);
		t.id = targetId;
		pBody.appendChild(t);
	}
}

function chkAndDel(targetId)
{
	if($(targetId))
	{
		/* var pBody = document.getElementsByTagName("body")[0];
		pBody.removeChild($(targetId)); */
		document.removeChild($(targetId));
	}
}


//用Ajax得到页面，并在指定层内部显示
/* 参数说明ﺿ

	pageName	要得到的页面，绝对地址或相对地址
	parameter	URL参数，᾿”后面的部分，不包括᾿᾿
	targetId	目标div或span，要在该对象中显示得到的内容
*/
var g_getPage_targetId;
function getPage(pageName, parameter, targetId)
{
	if(parameter.length == 0)
	{
		parameter += "r=" + Math.random();
	}
	else
	{
		parameter += "&r=" + Math.random();
	}
	var myAjax = new Ajax.Request(
		pageName,
		{
		method: 'get',
		parameters: parameter,
		onComplete: getPageDone
		});
	
	g_getPage_targetId = targetId;
	return false;
}

function getPageDone(originalRequest)
{
	chkAndCreate(g_getPage_targetId);
	$(g_getPage_targetId).innerHTML = originalRequest.responseText;
}

var queryString = new Object();
// get a parameter in the URL
function getQuery(k)
{
	var g = new getQuery2();
	if(g[k])
	{
		return g[k];
	}
	else
	{
		return "";
	}
}

function getQuery2()
{
	var qs;
	qs = window.location.search;
	qs = qs.substring(1);
	
	var q = new Object();
	var qs2 = qs.split("&");
	
	for(var i = 0; i < qs2.length; i ++)
	{
		var s = qs2[i].indexOf("=");
		if(s == -1)
		{
			continue;
		}
		
		var aKey = qs2[i].substring(0, s);
		var aValue = qs2[i].substring(s + 1);
		
		q[aKey] = aValue;
	}
	
	return q;
}

function flashAd(w, h, th, aPic, aLink, aText)
{
	var interval_time = 0; //图片停顿时间，单位为秒，为0则停止自动切换
	var focus_width = w;
	var focus_height = h;
	var text_height = th;
	var text_align = "center" //标题文字对齐方式(left、center、right)
	var swf_height = focus_height + text_height; //相加之和最好是偶数,否则数字会出现模糊失真的问题
	var pics = aPic.join("|");
	var links = aLink.join("|");
	var texts = aText.join("|");
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ focus_width +'" height="'+ swf_height +'">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/images/focus.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#F0F0F0">');
	document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
	document.write('<param name="FlashVars" value="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">');
	document.write('<embed src="/images/focus.swf" wmode="opaque" FlashVars="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'" menu="false" bgcolor="#F0F0F0" quality="high" width="'+ focus_width +'" height="'+ focus_height +'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

//将地址栏参数中的键 k 的值替换为 v
function replaceQuery(k, v)
{
	//alert(queryString.items);
	return "";
}

queryString.initialize = function () {
	var qs = window.location.search;
	qs = qs.substring(1);
	var qs2 = qs.split("&");
	var s;
	queryString.items = {};
	for(var i = 0; i < qs2.length; i ++) {
		s = qs2[i].indexOf("=");
		if(s == -1)
			continue;
		
		var aKey = qs2[i].substring(0, s);
		var aValue = qs2[i].substring(s + 1);
		
		queryString.items[aKey] = aValue;
	}
};

queryString.toString = function () {
	var s = "";
	for (var k in queryString.items)
	{
		s += "&" + k + "=" + queryString.items[k];
	}
	return s.substring(1);
};

queryString.update = function (k, v) {
	queryString.items[k] = v;
};

function htmlDecode(s)
{
	s = s.replace(/<br>/ig, "\n");
	s = s.replace(/<br \/>/ig, "\n");
	s = s.replace(/&lt;/ig, "<");
	s = s.replace(/&gt;/ig, ">");
	s = s.replace(/&nbsp;/ig, " ");
	s = s.replace(/&quot;/ig, "\"");
	s = s.replace(/&amp;/ig, "&");
	
	return s;
}

function imgResize(target, maxWidth)
{
    if(target.width > maxWidth)
    {
        target.width = maxWidth;
    }
}

function isValidEmail(emailStr)
{
	if(!emailStr.match(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/))
    {
    	return false;
    }
    
    return true;
}

//操作Cookies
function setCookie(name, value)
{
	var Days = 30;
	var exp  = new Date();    //new Date("December 31, 9998");
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";path=/;domain=splife.com;expires=" + exp.toGMTString();
	//document.cookie = name + "="+ escape (value) + ";path=/;expires=" + exp.toGMTString();
}
function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)) return unescape(arr[2]);
	else return null;
}
function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";path=/;domain=splife.com;expires="+exp.toGMTString();
	//if(cval!=null) document.cookie= name + "="+cval+";path=/;expires="+exp.toGMTString();
}

//显示提示信息
/*
	参数：msg		提示信息
	　　　toUrl	提示后转向的url，若䶿0"或为""则保持当前页面，若为"-1"则返回上一響
*/
function msg2(msg, toUrl)
{
	//检查高亮层是否在在，若不存在则创建
	overLay();
	chkAndCreate("lightBox", "div");
	
	//显示寿
	$("lightBox").style.marginTop = -100 + document.documentElement.scrollTop + "px";
	$("lightBox").style.display = "block";
	
	//创建层内嬿
	//var s = "<div id=\"lightBox_top\"><!--<div class=\"left\">智能手机生活网</div>--><div class=\"right\">SPLife.Com</div></div><div id=\"lightBox_msg\">" + msg + "</div>";
	//s += "<div id=\"lightBox_button\"><input type=\"button\" value=\" 确定 \" onclick=\"msg2_close('" + toUrl + "');\" /></div>";
	
	var s = "<div id=\"lightBox_box\">";
	s += "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td id=\"lightBox_left\">";
	s += "<img src=\"http://www.splife.com/images/inform.jpg\" alt=\"inform\" /><br />SPLife &copy; 2007";
	s += "</td><td id=\"lightBox_right\"><div id=\"lightBox_info\">";
	s += msg;
	s += "</div><div id=\"lightBox_btn\">";
	s += "<input type=\"button\" value=\" 确定 \" onclick=\"msg2_close('" + toUrl + "');\" />";
	s += "</div></td></tr></table></div>";
	$("lightBox").innerHTML = s;
	
	return false;
}

function msg2_close(toUrl)
{
	overLay_close();
	$("lightBox").style.display = "none";
	
	if(toUrl == "0" || toUrl.length == 0)
	{		
		return false;
	}
	if(toUrl == "-1")
	{
		history.go(-1);
	}
	else
	{
		location.href = toUrl;
	}
}

function overLay()
{
	chkAndCreate("overLay", "div");
	//隐藏所有select元素
	showSelects(0);
    var h = document.documentElement.scrollHeight;
    if(h < screen.height)
    {
        h = screen.height;
     }
	$("overLay").style.height = h + "px";
	document.body.style.background = "";
	//$("overLay").style.marginTop = document.documentElement.scrollTop + "px";
	$("overLay").style.display = "block";
}

function overLay_close()
{
	$("overLay").style.display = "none";
	//显示所有select元素
	showSelects(1);
	
	document.body.style.background = "#fff url(/images/wrapper_top.gif) repeat-x";
}

function showSize(i)
{
	var s = "";
	if(i < 1024)
	{
		s = i + " K";
	}
	else
	{
		s = (i / 1024).toFixed(2) + " M";
	}
	
	document.write(s);
}

/* 
	显示页面顶部我的手机

*/
/*function top_showMyPhone()
{
	var ob = document.getElementById("top_myPhone");
	if (ob.style.display != "inline")
	{
		ob.style.display = "inline";
		document.getElementById("top_selectPhone_arrow").className = "top_selectPhone_arrow_up_1";
	}
	else
	{
		ob.style.display = "none";
		document.getElementById("top_selectPhone_arrow").className = "top_selectPhone_arrow_down_1";
	}
	
	return false;
}*/

/*function top_showMyPhone_mouseover() {
	var ob;
	if($("top_tab_1"))
	{
		ob = $("top_tab_1").childNodes[0];
		ob.onmouseover = top_showMyPhone;
	}
}*/

/* 显示页面顶部我的手机旁边的小箭头

*/
/*function top_myPhoneArrow(i)
{
	var cn = document.getElementById("top_selectPhone_arrow").className;
	if(i == 1)
	{
		cn = cn + "_1";
	}
	else
	{
		cn = cn.replace("_1", "");
	}
	document.getElementById("top_selectPhone_arrow").className = cn;
	
	return false;
}*/

//显示所有select元素
/*
	参数：o		0, 隐藏ﺿ, 显示
*/
function showSelects(o)
{
	var v = "visible";
	if(o == 0)
	{
		//隐藏
		v = "hidden";
	}
	
	var selects = document.getElementsByTagName("select");
	for(var i = 0; i < selects.length; i ++)
	{
		selects[i].style.visibility = v;
	}
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != "function")
	{
		window.onload = func;
	}
	else
	{
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

function pop_div_inform()
{	
	if(getCookie("show_pop_div_inform") == "no" || $("wrapper") == null)
		return false;
	var d = new Date();
	var d2 = new Date(Date.UTC(2009, 1, 4, 10, 30));
	if(d.getTime() > d2.getTime())
		return false;
	
	var t = document.createElement("div");
	t.id = "pop_div_inform";
	$("wrapper").appendChild(t)
	var ob = $("pop_div_inform");
	ob.style.position = "absolute";
	ob.style.top = "0";
	ob.style.left = "0";
	ob.style.color = "#900";
	ob.style.zIndex = 10;
	ob.style.padding = "10px 1em 10px 1em";
	ob.style.margin = "80px 0 0 0";
	ob.style.width = "200px";
	ob.style.height = "313px";
	//ob.style.background = "#fff url(/images/newyear_2008.jpg) no-repeat";
	ob.style.background = "#ffffff";
	ob.style.border = "1px solid #e0e0e0";
	ob.style.marginLeft = (g_uAgent.screenWidth - ob.offsetWidth - 30) + "px";
	ob.innerHTML = "<span class=\"right\" style=\"cursor:pointer\" onclick=\"pop_div_inform_close()\"><img src=\"/images/close.gif\" /></span>" + 
					"<br /><br /><br /><center><strong style=\"font-size: 14px;\">元旦节假日通知</strong></center><br />" + 
					"尊敬的用户：<br/>　　元旦节期间，" +
					"2009年1月1日-1月3日休息，" + 
					"1月4日正常工作。<br/><br/>" + 				
					"　　　　　　　　　SPLife.com <br/>　　　　　　　　2008年12月31日<b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b>";// + 
					//"<br /><br /><center><a href=\"/static/help.aspx?t=1&r=6\" title=\"联系我们\"><strong>点击这儿联系我们</strong></a></center>";
}

function pop_div_inform_close()
{
	$("pop_div_inform").style.display = "none";
	setCookie("show_pop_div_inform", "no");
}

function popInform(parentNode, msgContent, popId) {
	var s = "";
	//s += "<div id=\"_popInform\" style=\"position:absolute;\">";
	s += "<table width=\"200\" cellpadding=\"0\" cellspacing=\"0\">";
	s += "<tr><td width=\"10\" height=\"10\" style=\"padding:0;background:url(/images/pop_inform_1.gif) no-repeat;\"></td>";
	s += "<td width=\"180\" height=\"10\" style=\"padding:0;background:url(/images/pop_inform_2.gif) repeat-x;\"></td>";
	s += "<td width=\"10\" height=\"10\" style=\"padding:0;background:url(/images/pop_inform_3.gif) no-repeat;\"></td></tr>";
	s += "<tr><td width=\"10\" style=\"padding:0;background:url(/images/pop_inform_4.gif) repeat-y;\"></td>";
	s += "<td width=\"180\" style=\"padding:0;font-size:12px;color:#333;background:url(/images/pop_inform_5.gif);\">";
	s += "<span class=\"right\" style=\"cursor:pointer;\" onclick=\"popInform_close(this);\"><img src=\"/images/close.gif\" border=\"0\" /></span>";
	s += msgContent;
	s += "</td>";
	s += "<td width=\"10\" style=\"padding:0;background:url(/images/pop_inform_6.gif) repeat-y;\"></td></tr>";
	s += "<tr><td width=\"10\" height=\"10\" style=\"padding:0;background:url(/images/pop_inform_7.gif) no-repeat;\"></td>";
	s += "<td width=\"180\" height=\"10\" style=\"padding:0;background:url(/images/pop_inform_8.gif) repeat-x;\"></td>";
	s += "<td width=\"10\" height=\"10\" style=\"padding:0;background:url(/images/pop_inform_9.gif) no-repeat;\"></td></tr>";
	s += "</table>";
	s += "<div style=\"margin: -1px 0 0 30px;\"><img src=\"/images/pop_inform_10.gif\" /></div>";
	//s += "</div>";
	
	this.xhtml = s;
	this.pop = document.createElement("div");
	this.pop.innerHTML = s;
	this.pop.style.display = "none";
	this.pop.style.position = "absolute";
	if (popId) this.pop.id = popId;
	
	if (typeof(parentNode) == "string") parentNode = $(parentNode);
	if (parentNode.firstChild)
		parentNode.insertBefore(this.pop, parentNode.firstChild);
	else
		parentNode.appendChild(this.pop);
}
popInform.prototype.close = function () {
	this.pop.style.display = "none";
};
popInform.prototype.show = function () {
	this.pop.style.display = "inline";
	var _h = this.pop.offsetHeight;
	this.pop.style.marginTop = (10 - _h) + "px";
};

function popInform_close(ob) {
	var d = ob;
	while (d.nodeName != "DIV") {
		d = d.parentNode;
	}
	d.style.display = "none";
}

/*function adjustPage() {
	if ($("c_right"))
		$("c_right").style.height = $("c_right").offsetHeight + "px";
}*/

// this function is used for make xhtml codes for gif/swf ad
function Mkaddiv(adType, adFile, adUrl, adWidth, adHeight, adTarget, adAlt)
{
	this.type = adType;
	this.file = adFile;
	this.url = adUrl;
	this.target = adTarget;
	this.width = adWidth;
	this.height = adHeight;
	this.alt = adAlt ? adAlt : "splife.com";
}

Mkaddiv.prototype.xhtml = function () {
	var s = "";
	if (this.type == "gif" || this.type == "jpg")
	{
		s += "<a href=\"" + this.url + "\"";
		if (this.target != "")
		{
			s += " target=\"" + this.target + "\"";
		}
		s += ">";
		s += "<img src=\"" + this.file + "\" alt=\"" + this.alt + "\" style=\"width:" + this.width + ";height:" + this.height + ";\" />";
		s += "</a>";
	}
	if (this.type == "swf")
	{
		s += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"" + this.width + "\" height=\"" + this.height + "\">";
		s += "<param name=\"movie\" value=\"" + this.file + "\" />";
		s += "<param name=\"quality\" value=\"high\" />";
		s += "<embed src=\"" + this.file + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + this.width + "\" height=\"" + this.height + "\"></embed>";
		s += "</object>";
	}
	return s;
};

String.prototype.getExtName = function () {
	var a = this.split(".");
	a1 = a[a.length - 1];
	return a1;
};

var g_page = getQuery("page");
var g_uAgent = new Object;
g_uAgent.sUserAgent = navigator.userAgent;
g_uAgent.isIE = g_uAgent.sUserAgent.indexOf("compatible") > -1 && g_uAgent.sUserAgent.indexOf("MSIE") > -1;
g_uAgent.screenWidth = screen.width;

//addLoadEvent(top_showMyPhone_mouseover);
//addLoadEvent(pop_div_inform);
addLoadEvent(queryString.initialize);
//addLoadEvent(adjustPage);


/************************************************/

var page = {};

page.searchType = function () {
	var aa = $("search_bar_class").getElementsByTagName("a");
	$A(aa).each(function (a) {
		a.className = "";
		a.style.backgroundImage = "";
	});
	this.className = "selected";
	var nv = this.firstChild.nodeValue;
	this.style.backgroundImage = "url(/images/search_box_" + Math.floor(nv.length / swt.cnLen) + "c.gif)";
	this.blur();
	switch (nv) {
		case "软件" :
			$("iptSearchResType").value = "1";
			break;
		case "游戏" :
			$("iptSearchResType").value = "2";
			break;
		case "资讯" :
			$("iptSearchResType").value = "3";
			break;
		default :
			$("iptSearchResType").value = "9";
			break;
	}
	
	return false;
};

function spl_init() {
	//search_bar_class
	if ($("search_bar_class")) {
		var resType = queryString.items ? queryString.items["resType"] : 9;
		if($("iptSearchResType").value.length == 0)
			$("iptSearchResType").value = resType;
		$A($("search_bar_class").getElementsByTagName("a")).each(function (a) {
			a.onclick = page.searchType;
			if (resType == "1" || resType == "2" || resType == "3") {
				a.className = "";
				a.style.backgroundImage = "";
				if (a.firstChild.nodeValue == "软件" && resType == "1") {
					a.className = "selected";
					a.style.backgroundImage = "url(/images/search_box_2c.gif)";
				} else if (a.firstChild.nodeValue == "游戏" && resType == "2") {
					a.className = "selected";
					a.style.backgroundImage = "url(/images/search_box_2c.gif)";
				} else if (a.firstChild.nodeValue == "资讯" && resType == "3") {
					a.className = "selected";
					a.style.backgroundImage = "url(/images/search_box_2c.gif)";
				}
			}
		});
	}
	
	$("tab_my").onmouseover = function () {
		$("top_phone").style.display = "block";
	};
	$("top_phone").onmouseout = function () {
		this.style.display = "none";
	};
}

swt.addLoadEvent(spl_init);