var popwin_thread = null;
var popwin_boxid = "popwin_boxid";
var popwin_iframeid = "popwin_iframeid";
var popwin_contentid = "popwin_contentid";
var popwin_opacityid = "popwin_opacityid";

function popwin(title, msg, opacity, timer, width, height) {
	var title = title ? title : "提示";
	var msg = msg ? msg : "null";
	var w = width ? width : 250;
	var h = height ? height : 150;
	var opacity = opacity ? opacity : false;
    var titleheight = "28px"; // 提示窗口标题高度  
    var bordercolor = "#D0E4FC"; // 提示窗口的边框颜色  
    var titlecolor = "#295F8E"; // 提示窗口的标题颜色  
    var titlebgcolor = "#E1EFFD"; // 提示窗口的标题背景色 
    var titletbordercolor = "#E5F2FF"; // 标题的上边框颜色
    var titlerbordercolor = "#FFFFFF"; // 标题的右边框颜色
    var titlebbordercolor = "#BCE3FE"; // 标题的下边框颜色
    var titlelbordercolor = "#FFFFFF"; // 标题的左边框颜色
    var bgcolor = "#FFFFFF"; // 提示内容的背景色 
     
    var iWidth = document.documentElement.clientWidth;  
    var iHeight = document.documentElement.clientHeight;  

	var bgObj = document.getElementById(popwin_opacityid);
	if(!bgObj) {		
		bgObj = document.createElement("div");		
		bgObj.id = popwin_opacityid;
		// bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;z-index:101;"; 
		document.body.appendChild(bgObj);
		bgObj = document.getElementById(popwin_opacityid);
	} else {
		bgObj.style.display = "block";
	}

	if (opacity) {
		bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=15);opacity:0.15;background-color:#000000;z-index:101;"; 
	} else {
		bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;z-index:101;"; 
	}
     
	var msgObj = document.getElementById(popwin_boxid);
	if(!msgObj) {
		msgObj = document.createElement("div");
		msgObj.id = popwin_boxid;
		msgObj.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;"; 
		msgObj.style.filter = "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#ADACAC,strength=3);";
		document.body.appendChild(msgObj);
		msgObj = document.getElementById(popwin_boxid);
	} else {
		msgObj.style.display = "block";
		msgObj.style.width = w + "px";
		msgObj.style.height = h + "px";
	}
    
	var msgBox, tr, titleBar, closeBtn;
	var tableid = popwin_boxid + "_tableid";
    var table = document.getElementById(tableid);
	if (!table) {
		table = document.createElement("table");
		table.style.cssText = "margin:0px;border:0px;padding:0px;"; 
		table.id = tableid;	
		table.cellSpacing = 0;
		msgObj.appendChild(table);
		table.insertRow(-1);
		table.insertRow(-1);
		table.rows[0].insertCell(-1);
		table.rows[0].insertCell(-1);
		table.rows[1].insertCell(-1);
		table.rows[1].insertCell(-1);
		table = document.getElementById(tableid);
	}
	tr = table.rows[0];
    titleBar = tr.cells[0];
    titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋体';color:"+titlecolor+";border-bottom:1px solid " + bordercolor + ";cursor:move;background:url(/images/popwin/popwin_icon.gif) no-repeat;background-position:3px 5px;background-color:" + titlebgcolor; 
    titleBar.style.paddingLeft = "10px"; 
    titleBar.innerHTML = "<span id='" + popwin_boxid + "_titleid' style='margin:-3px 5px -2px 12px;'>" + title +"</span>"; 

    var moveX = 0; 
    var moveY = 0; 
    var moveTop = 0; 
    var moveLeft = 0; 
    var moveable = false; 
    var docMouseMoveEvent = document.onmousemove; 
    var docMouseUpEvent = document.onmouseup; 
    titleBar.onmousedown = function() { 
        var evt = getEvent(); 
        moveable = true;  
        moveX = evt.clientX; 
        moveY = evt.clientY; 
        moveTop = parseInt(msgObj.style.top); 
        moveLeft = parseInt(msgObj.style.left);
        document.onmousemove = function() { 
            if (moveable) { 
                var evt = getEvent(); 
                var x = moveLeft + evt.clientX - moveX; 
                var y = moveTop + evt.clientY - moveY; 
                if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) { 
                    msgObj.style.left = x + "px"; 
                    msgObj.style.top = y + "px"; 
                } 
            }     
        }; 
        document.onmouseup = function () {  
            if (moveable) {  
                document.onmousemove = docMouseMoveEvent; 
                document.onmouseup = docMouseUpEvent; 
                moveable = false;  
                moveX = 0; 
                moveY = 0; 
                moveTop = 0; 
                moveLeft = 0; 
            }  
        }; 
    } 
     
    closeBtn = tr.cells[1];
    closeBtn.style.cssText = "cursor:pointer;border-bottom:1px solid " + bordercolor + ";padding:2px;background-color:" + titlebgcolor; 
    closeBtn.innerHTML = "<span style='font-size:15pt;background:url(/images/popwin/popwin_close.gif) no-repeat;background-position:0px 1px;color:"+titlecolor+";' title='关闭窗口'>&nbsp;&nbsp;</span>"; 
    closeBtn.onclick = function() {  
        popwinclose();
    }

    msgBox = table.rows[1].cells[0];
    msgBox.colSpan = 2;
	msgBox.id = popwin_contentid;
	msgBox.innerHTML = "";
	msgBox.style.cssText = "font-size:12px;padding:1px;";

	var showloading = false;
	if (msg.indexOf("show=loading") != -1) {
		showloading = true;
		msgBox.innerHTML += '<div id="loading" style="position:relative;margin-top:' + (h / 3.2) + 'px;height:' + (h / 2) + 'px;left:auto;text-align:center;color:#666;"><img src="/images/popwin/loading.gif" border="0" style="margin-bottom:-2px;" /> 正在请求中,请稍等一下...</div>';
	}

	var mark = msg.split("url:");
	var iframeurl = mark[1];
	if (iframeurl != null) {
		var iframe = document.getElementById(popwin_iframeid);
		if (!iframe) {
			var newiframe = document.createElement("iframe");
			newiframe.id = popwin_iframeid;
			newiframe.style.width = "100%";
			newiframe.style.height = (h-30) + "px";
			newiframe.style.display = "block";
			newiframe.src = "about:blank";
			newiframe.marginWidth = 0;
			newiframe.marginHeight = 0;
			newiframe.frameBorder = 0;
			newiframe.scrolling = "no";
			msgBox.appendChild(newiframe);
			iframe = document.getElementById(popwin_iframeid);
		}			
		if (showloading) {
			iframe.style.cssText = "width:99%;height:0px;";
		}
		var rand = Math.random();
		rand = rand * 1000000;
		rand = Math.ceil(rand);	
		if (iframeurl.indexOf("?") == -1) iframeurl = iframeurl + "?";
		iframe.src = iframeurl + "&rand=" + rand;
	} else { 
		msgBox.innerHTML = msg;
	}

	popwinload();
	window.onscroll = popwinload;
	if (timer) { clearTimeout(popwin_thread); popwin_thread = window.setTimeout(function() { popwinclose(); }, timer * 1000); }
	    
    // 获得事件Event对象，用于兼容IE和FireFox 
    function getEvent() { 
        return window.event || arguments.callee.caller.arguments[0]; 
    } 
}

function popwinload() {
	var popwin = document.getElementById(popwin_boxid);
	var scrolltop = document.documentElement.scrollTop;
	var scrollleft = document.documentElement.scrollLeft;
	var clientwidth = document.documentElement.clientWidth;
	var clientheight = document.documentElement.clientHeight;
	if (popwin) {
		popwin.style.top = (scrolltop + (clientheight - popwin.offsetHeight) / 2) + "px";
		popwin.style.left = (scrollleft + (clientwidth - popwin.offsetWidth) / 2) + "px";
	}
}

function popwinmsg(content, closetimer, reftimer) {
	var titleid = document.getElementById(popwin_boxid + "_titleid");
	if (content != null && titleid) {
		var iframe = document.getElementById(popwin_iframeid);
		if (iframe) { iframe.style.display = "none"; }
		var popwin = document.getElementById(popwin_boxid);
		if (popwin) {
			var w = 250, h = 150;
			popwin.style.width = w + "px";
			popwin.style.height = h + "px";
		} titleid.innerHTML = "操作提示";
		var contentid = document.getElementById(popwin_contentid);
		contentid.innerHTML = '<div id="loading" style="position:relative;margin-top:' + (h / 3.2) + 'px;height:' + (h / 2) + 'px;left:auto;text-align:center;color:#666;"><img src="/images/popwin/alert.gif" border="0" /> <span style="font-size:13px;color:red;">' + content + '</span></div>';
	} popwinload();
	if (closetimer) { clearTimeout(popwin_thread); popwin_thread = window.setTimeout(function() { popwinclose(); }, closetimer * 1000); }
	if (reftimer) { clearTimeout(popwin_thread); popwin_thread = window.setTimeout(function() { pagereload(); }, reftimer * 1000); }
}

function popwinclose() {
	window.onscroll = function(){};
	bgObj = document.getElementById(popwin_opacityid);
	msgObj = document.getElementById(popwin_boxid);
	if(bgObj != null) bgObj.style.display = "none";
	if(msgObj != null) msgObj.style.display = "none";
}

function showiframe(x, y) {
	if (x && y)
	{
		var load = document.getElementById("loading");
		if (load) { load.style.display = "none"; }
		var popwin = document.getElementById(popwin_boxid);
		if (popwin) {
			popwin.style.width = x + "px";
			popwin.style.height = y + "px";
		} popwinload();
		var iframe = document.getElementById(popwin_iframeid);
		if (iframe) {
			iframe.style.width = "100%";
			iframe.style.height = (y - 26) + "px";
			iframe.style.display = "block";
		}
	}
}

function pleaselogin() {
	/*
	if (userid == -1) {
		return pleaselogin();
	}
	*/
	alert("请先登录用户后再操作此功能！");
	var signin = $("SigninForm");
	if (signin) {
		signin.UserName.focus();
	}
}

function updateuserinfo() {
	var gotourl = boxurl + "/userbox/updateinfo?" + userid + "&show=loading";
	popwin("我的资料", "url:" + gotourl, true);
}

function getusermessage() {
	var gotourl = boxurl + "/userbox/messagelist?0&show=loading";
	popwin("我的短消息", "url:" + gotourl, true);
}

function addusersingerbox(id) {
	var gotourl = boxurl + "/userbox/addsinger?" + id + "&show=loading";
	popwin("收藏歌手", "url:" + gotourl, true);
}

function adduseralbumbox(id) {
	var gotourl = boxurl + "/userbox/addalbum?" + id + "&show=loading";
	popwin("收藏专辑", "url:" + gotourl, true);
}

function addusersongbox(id) {
	var gotourl = boxurl + "/userbox/addsong?" + id + "&show=loading";
	popwin("音乐盒.收藏歌曲", "url:" + gotourl, true);
}

function addusersongboxs(oo) {
	var id = checkbox("ref", oo);
	if (id.length == 0) {
		return alert("至少选择一首歌曲才能收藏！");
	} else {
		var count = id.split("_").length;
		var gotourl = boxurl + "/userbox/addsong?" + id + "&show=loading";
		popwin("音乐盒.收藏歌曲", "url:" + gotourl, true);
	}
}