﻿var hostname = "cococ.com"
document.domain = hostname;
function tryCatch() { return true; } window.onerror = tryCatch;

var modules = null;
var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
try {if (window.opener.modules) {modules = window.opener.modules;}} catch(e) {}

function $(id) {
	return document.getElementById(id) ? document.getElementById(id) : null;
}

function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

var KMusic, userid, username;
KMusic = getcookie("KMusic");
var https = "http://";
var sourl = https + "so." + hostname;
var weburl = https + "www." + hostname;
var boxurl = https + "box." + hostname;
var siteurl = https + window.location.host;
var serverurl = https + "server." + hostname;
var pageurl = window.location.href.toLowerCase();
var refurl = document.referrer.toLowerCase();
if (self != top && refurl != "" && refurl.indexOf(hostname) == -1) top.location.href = weburl;
if (boxurl.indexOf(siteurl) != -1) boxurl = boxurl.replace(siteurl, "");
if (KMusic == 'undefined' || KMusic == null || KMusic == '') {
	userid = -1; username = "游客";
} else {
	var userinfo = KMusic.split("&");
	userid = parseInt(userinfo[0].split("=")[1]);
	username = trim(userinfo[1].split("=")[1]);
}

function checkuserlogin() {
	var controls = "";
	var userloginid = $("userlogin");
	if (!userloginid) return;
	if (userid == -1) {
		controls = '<form id="SigninForm" name="SigninForm" method="post" action="' + boxurl + '/Signin.aspx" target="_top">';
		controls += '登录音乐盒';
		//controls += userloginid.innerHTML;
		controls += ' &nbsp;';
		controls += '<font color=#0058B3>用户名</font> ';
		controls += '<input name="UserName" type="text" style="width:60px" />';
		controls += ' &nbsp; ';
		controls += '<font color=#0058B3>密 码</font> ';
		controls += '<input name="Password" type="password" style="width:60px" />';
		controls += ' &nbsp; ';
		controls += '<button class="loginbutton" type="submit" name="submit" />登录</button>';
		controls += ' &nbsp; ';
		controls += '<button class="loginbutton" onclick="windowopen(\'' + boxurl + '/Signup.aspx\', \'login\')">注册</button>';
		controls += '';
		controls += '<input name="Expires" type="hidden" value="1440" />';
		controls += '<input name="RedirectUrl" type="hidden" value="' + pageurl + '" />';
		controls += '</form>';
	} else {
		controls = '欢迎您 ';
		controls += '<a href="' + boxurl + '/Space?' + userid + '">' + username +'</a>';
		controls += '，';
		controls += '<a href="' + boxurl + '/Space?' + userid + '">打开我的音乐盒</a>';
		controls += '&nbsp; ';
		controls += '<a href="#" onclick="updateuserinfo();return false;">我的资料</a>';
		controls += '&nbsp; ';
		controls += '<a href="#" onclick="getusermessage();return false;"><span id="message">我的短消息</span></a>';
		controls += '&nbsp; ';
		controls += '<a href="' + boxurl + '/Signin.aspx?action=quit&goto=' + pageurl + '" target="_top">安全登出</a>';
		controls += '&nbsp; ';
	}
	if(userloginid) {
		userloginid.innerHTML = controls;
	}
}

function pagereload() {
	window.location.href = pageurl;
}

function windowopen(url, mark) {
	modules = window.open(url, mark);
	try {if (window.opener) {window.opener.modules = modules;}} catch(e) {}
}

function setcookie(name, value, hours) {
	var expire = "";
	if (hours != null) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + encodeURIComponent(value) + "; domain=" + hostname + ";path=/" + expire;
}

function getObject(param) {
	if (document.getElementById) {
		return document.getElementById(param);
	} else if (document.all){
		return document.all[param];
	} else if (document.layers) {
		return document.layers[param];
	} else {
		return null;
	}
}

function getTags(parentobj, tag) {
	if (parentobj == null) {
		return new Array();
	} else if (typeof parentobj.getElementsByTagName != 'undefined') {
		return parentobj.getElementsByTagName(tag);
	} else if (parentobj.all && parentobj.all.tags) {
		return parentobj.all.tags(tag);
	} else {
		return new Array();
	}
}

Array.prototype.push = function(value) {
	this[this.length] = value;
	return this.length;
}

function checkbox(oo, id) {
	var o, n, v = [];
	var param = getObject(id);
	if (param == null) return "";
	var a = getTags(param, "input");
	for (var i = 0; i < a.length; i++) {
		o = a[i];
		if (oo == "all") {
			o.checked = true;
		} else if (oo == "opp") {
			if (o.checked) {
				o.checked = false;
			} else {
				o.checked = true;
			}
		} else {
			n = a[i].value;
			if (n > 0 && a[i].checked) {
				v.push(a[i].value);
			}
		}
	}
	if (oo == "ref") {
		return v.join("_");
	}
}

function IsInt(value) {
	var pattern=/^\d+$/;
	if(!pattern.test(value))
		return false;
	return true;
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

function addfave(url, title) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) {
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) {
		return true;
	}
}

function addcopy(text, alertmsg){
	if (is_ie) {
		clipboardData.setData('Text', text);
	} else if (prompt('Press Ctrl+C Copy to Clipboard', text)) {
	} alert(alertmsg);
}

function errimg(obj, image) {
	obj.style.background = 'url(' + image + ')';
	obj.style.padding = obj.height + 'px 0 0 0';
	obj.style.overflow = 'hidden';
	obj.style.height = '0';
	obj.style.border = '0px';
	// obj.onerror = null;
	return true;
}

function player(oo) {
	var id = checkbox("ref", oo);
	if (id.length == 0) {
		return alert("请选择歌曲后再进行连播！");
		checkbox("all", oo); player(oo);
	} else {
		windowopen(weburl + "/music/playlist.html?id=" + id, "_cococ");
	}
}

function addplaylist(obj) {
	var id, playurl;
	if (IsInt(obj)) {
		id = obj.toString();
		playurl = weburl + "/play/" + id + ".html";
	} else {
		id = checkbox("ref", obj);
		playurl = weburl + "/music/playlist.html?id=" + id;
	}
	if (id.length == 0) {
		return alert("请选择歌曲后才能添加到播放列表！");
	} else {
		if (getcookie("PLAYING") == "ON") {
			setcookie("PLAYLIST", id, 24);
			alert("已经添加到播放列表！");
		} else {
			windowopen(playurl, "_cococ");
		}
	}
}

function requeststring(key) {
	var value = window.location.search.match(new RegExp("[\?\&]" + key + "=([^\&]*)(\&?)", "i"));
	return value ? value[1] : value;
}

var counterThread;
function loadscript(getid, fileurl) {
	if (getid != "image") {
		loadscriptrequire(getid, fileurl);
	} else {
		clearTimeout(counterThread);
		counterThread = window.setTimeout(function() { 
			loadscriptrequire(getid, fileurl);
		}, 3000);
	}
}

function loadscriptrequire(getid, fileurl) {
	var html_doc = document.getElementsByTagName('head')[0];
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', fileurl);
	js.setAttribute('id', getid);
	if ($(getid)) {
		html_doc.replaceChild(js, $(getid));
	} else {
		html_doc.appendChild(js);
	}
}

function correlate(total) {
	if ($("CorrelateCount")) $("CorrelateCount").innerHTML = total;
}

function ServerCounter(Mode, ParamID, CallBack) {
	(function(){
		var callback = CallBack ? CallBack : null;
		if(callback != null) {
			loadscript("correlate", serverurl + "/Process/Correlate.aspx?Return=" + callback + "&Mode=" + Mode 
	+"&ParamID=" + ParamID);
		}
		window.setTimeout(function(){ 
			loadscript("counter", serverurl + "/Process/Counter.aspx?Mode=" + Mode +"&ParamID=" + ParamID);
		}, 1000 * 5);
	})();
}

function QueryMessage() {
	if(userid == -1) return;
	(function(){
		window.setTimeout(function(){ 
			LoadMessage();
		}, 1000 * 10);
	})();
}

function LoadMessage() {
	(function(){
		loadscript("querymessage", boxurl + "/QueryMessage.aspx?Return=ShowMessage&Time=" + new Date().getTime());
		var msgThread = window.setInterval(function(){ 
			clearInterval(msgThread);
			LoadMessage();
		}, 1000 * 100);
	})();
}

function ShowMessage(Number) {
	var obj = $("message");
	if(!obj || !IsInt(Number)) return;
	var flash, msg = "我的短消息";
	if (Number > 0) {
		msg = "<font color=red>您有新短消息!</font>";
		flash = document.createElement("embed");
		flash.src = weburl + "/images/sound/message.swf";
		flash.setAttribute("loop", "false");
		flash.setAttribute("hidden", "hidden");
		flash.setAttribute("volume", "50");
		flash.setAttribute("autostart", "true");
		flash.setAttribute("mastersound", "mastersound");
		flash.style.width = "0";
		flash.style.height = "0";
	} 
	obj.innerHTML = msg;
	// if (flash) obj.appendChild(flash);
}

/* web */
var searchtarget = "song";
function switchtarget(F, G) {
	searchtarget = G;
	if ($("t_song")) $("t_song").className = "so_select";
	if ($("t_lyric")) $("t_lyric").className = "so_select";
	if ($("t_album")) $("t_album").className = "so_select";
	if ($("t_singer")) $("t_singer").className = "so_select";	
	if ($("t_" + searchtarget)) $("t_" + searchtarget).className = "so_selected";
}

function changeselectsearch() {
    if ($("t_" + searchtarget)) $("t_" + searchtarget).className = "so_selected";
	// 更改帮助
	var feedback = $("feedback");
	if (feedback) {
		//feedback.style.cssText = "color:red;";
		feedback.innerText = "新版问题反馈";
		feedback.href = "http://spreadsheets.google.com/viewform?key=pmMirw7akB-e1F_kah0F8Wg";
	}
	// 搜索框右侧
	/*
	var html = ""; // 打印关键字
	var keyword = ("青花瓷,每天爱你多一些").split(","); // 用逗号分割
	for (i = 0; i < keyword.length; i++) {
		html += "<a href=" + sourl + "/" + searchtarget + "?" + window.encodeURIComponent(keyword[i]) + ">" + keyword[i] + "</a> ";
	} if ($("hot_search")) $("hot_search").innerHTML = html;
	*/
	if ($("hot_search")) {
		$("hot_search").innerHTML = '<iframe src="/click/roll.html" marginheight=0 marginwidth=0 frameborder=0 scrolling=no height="45" width="200"></iframe>';
	}
	
	// 登录框右侧
	if ($("headerkeyword")) {
		$("headerkeyword").innerHTML = ' &nbsp; <a href="http://www.cococ.com/player.html" target="_blank"><font color="red"><b>点击这里随便听几首歌曲！</b></font> </a> &nbsp; &nbsp;<a href="http://track.cococ.com/c.php?id=7" target="_n">打呼噜事小—猝死事大</a> &nbsp; &nbsp;<a href="http://track.cococ.com/c.php?id=6" target="_n">失眠--抑郁--有救了！！</a>';
	}
}

function selectsearch() {
	var key = trim($("SearchKey").value);
	if(key != "") { 
	    key = key.replace("/", "");
	    key = key.replace("\\", "");
	    var searchurl = sourl + "/" + searchtarget + "?" + window.encodeURIComponent(key).replace(/'/g, "%27");
	    windowopen(searchurl, "search");
	}
}

function showdiv(id, num) {
	var tab = $(id + "_Tab");
	if (!tab) return;
	var lis = getTags(tab, "li");
	for (var i = 0; i < lis.length; i++) {
		var ids = i + 1;
		var div = $(id + "_List_" + ids);
		if (ids != num)	{
			lis[i].className = "";
			if (div)
				div.className = "hide";
		} else {
			lis[i].className = "up";
			if (div)
				div.className = "show";
		}
	}
}