﻿/**
 * Project:			soin
 * Author:			qiujf
 * Company: 		9sky.com
 * Created Date:	2007-7-17
 * Description		两个浏览器之间能过Cookie通信
 
 * Copyright @ 2007 9sky.com – Confidential and Proprietary
 * 
 * History:
 * ------------------------------------------------------------------------------
 * Date			|time		|Author	|Change Description		*/


var MSG_COOKIE_NAME = "cookie_msg_1.0";

function MSG()
{
	this.m_receiver = null;
	this.m_message = null;
	this.m_params = null;
	this.m_ptime = null;
}

function MSGListener(name, fn)
{
	var self = this;	
	
	this.m_name = name;
	this.m_hwnd = "player_window_{0}".format(name);
	this.m_milliSeconds = 2000;
	this.m_enable = true;

						
	var v = system.getCookie(this.m_hwnd);
	var exists = ( (v && v == 1) ? true : false);
	exists = false;
	if (exists) 
	{		
		if (fn) { fn(this); }
		this.m_enable = false;
	}
	else
	{
		 
		var date = new Date();
		date.addYears(1);
		system.setCookie(this.m_hwnd, 1, date, document.domain, "/");
		window.onbeforeunload =	function() { self.kill(); }
		document.body.onunload = function() { self.kill(); }
	}
}

MSGListener.prototype.On_Accept = null;
MSGListener.prototype.OnAccept = function(e)
{
	if (this.On_Accept) { this.On_Accept(this, e); }
}

MSGListener.prototype.start = function()
{
	if (this.m_enable)
	{
		var self = this;
		
		var msgStackString = system.getCookie(MSG_COOKIE_NAME);
		
		var arrMSGStack = msgStackString.split("$");
		
		var unhandleMSG = null;
		for(var i=0; i<arrMSGStack.length; i++)
		{	
			var msgString = arrMSGStack[i];
			var msg = this.getMSG(msgString);
			if (msg)
			{
				if (this.m_name == msg.m_receiver)
				{
					this.OnAccept(msg);
				}
				else
				{
					if (unhandleMSG)
					{
						unhandleMSG = "$" + msgString;
					}
					else
					{
						unhandleMSG = msgString;
					}
				}
			}
		}
		
		var date = new Date();
		date.addYears(1);
		system.setCookie(MSG_COOKIE_NAME, unhandleMSG, date, document.domain, "/");
		this.m_ptime = setTimeout(function() { self.start(); }, this.m_milliSeconds);
	}
}

MSGListener.prototype.clearStack = function()
{
	system.setCookie(MSG_COOKIE_NAME, "", null, document.domain, "/");
}

MSGListener.prototype.getMSG = function(msgString)
{
	if (msgString && msgString.toString() != "")
	{
		var arrMSG = msgString.split("/");
		var msg = new MSG();
		
		msg.m_receiver = arrMSG[0];
		msg.m_message = arrMSG[1];
		msg.m_params = arrMSG.slice(2, arrMSG.length);
		
		return msg;
	}
	return null;
}

MSGListener.prototype.kill = function()
{
	var date = new Date();
	date.addYears(1);
	this.stop();
	system.setCookie(this.m_hwnd, -1, date, document.domain, "/");
}

MSGListener.prototype.stop = function()
{
	this.m_enable = false;
	clearTimeout(this.m_ptime);
}

function MSGClient()
{
	this.m_existsWindow = false;
}

MSGClient.prototype.On_Send = null;
MSGClient.prototype.OnSend = function(args)
{
	if (this.On_Send) { this.On_Send(args); }
}

MSGClient.prototype.send = function()
{
	var args = this.send.arguments;
	
	if (args.length < 1) return;
	
	var ieWindow = "player_window_{0}".format(args[0]);

	var v = system.getCookie(ieWindow);
	var exists = ( (v && v == 1) ? true : false);
	
	this.m_existsWindow = exists;
	
	
	var newMsg = "";
	system.foreach(args,	function(e, i)
							{
								newMsg += e.toString();
								if (i < args.length-1) { newMsg += "/"; }
							});
	
	this.OnSend(args);
	
				
	if (exists == false) { return false; }
	
	var msgs = "";
	var msgStackString = system.getCookie(MSG_COOKIE_NAME);
	if (newMsg != "")
	{
		if (msgStackString && msgStackString != "")
		{
			msgs = msgStackString + "$";
		}
		msgs += newMsg;
		
		var date = new Date();
		date.addYears(1);
		system.setCookie(MSG_COOKIE_NAME, msgs, date, document.domain, "/");
	}
}
//MSG Server & Client








var MSG_PLAY			= "play";
var MSG_GETDATA			= "getdata";
var MSG_PLAYLIST_ADD	= "addPlayList";


var ST_KEYWORK			= "keyword";
var ST_KEY_TRACK		= "trk";
var ST_KEY_ARTIST		= "art";
var ST_ALBUM			= "alb";
var ST_ARTISTHOT		= "hot";
var ST_TOP				= "top";
var ST_COMMEND			= "commend";
var ST_ARTISTID			= "artid";
var ST_TAG_USER			= "tag0";
var ST_TAG_INTERNAL		= "tag1";
var ST_TAG_USER_INTERNAL= "tag2";
var ST_TOPID			= "toptrk";
var ST_TRACK_YEAR		= "years";
var ST_PACKAGE			= "package";
var ST_TRACKS			= "trks";

var IE_WINDOW_PLAYER = "Player";

var msgClient = new MSGClient();

msgClient.On_Send = function(args)
{
	//不存在窗口,者弹出
	if (this.m_existsWindow == false)
	{
		var from = args[2].toLowerCase();
		var target = args[3];
		switch(from){
			case ST_TOP: 
			case ST_TOPID: from="h";break;
			case ST_ARTISTID: from="a";break;
			case ST_ALBUM: from="b";break;
			case ST_TRACKS: from="t";break;
			case ST_PACKAGE: from="p";break;
		}
		if(target.toString().split(",").length > 30){
			var _from = document.createElement("INPUT");
			_from.setAttribute("type","hidden");
			_from.setAttribute("name","src");
			_from.setAttribute("value",from+"_"+target);
			var obj = document.createElement("FORM");
			document.body.appendChild(obj);
			obj.appendChild(_from);
			obj.action = "http://playlist2.9sky.com/_playlist/SetCombined.aspx?userid="+getUserID()+"&act=1&index=0&return=http://play.9sky.com/";
			obj.method = "post";
			obj.target = "Player";
			obj.submit();
		}
		else {
			var url = "http://play.9sky.com/{0}_{1}/".format(from, target);
			window.open(url, "Player");
		}
	}
}

function PlayFrom(from, target)
{
	msgClient.send(IE_WINDOW_PLAYER, MSG_PLAY, from, target);
}

function PlayArtist(artID)
{
	PlayFrom(ST_ARTISTID, artID);
}

function PlayAlbum(albID)
{
	PlayFrom(ST_ALBUM, albID);
}

function PlayTop(topID)
{
	PlayFrom(ST_TOPID, topID);
}

function PlayCommend(hotID)
{
	PlayFrom(ST_COMMEND, hotID);
}

function PlayTagUser(kid, userID)
{
	if (kid==null) return;
	msgClient.send(IE_WINDOW_PLAYER, MSG_PLAY, ST_TAG_USER_INTERNAL, kid, userID);
}

function PlayTag(kid)
{
	PlayFrom(ST_TAG_INTERNAL, kid);
}

//增加播放列表
function AddPlayList(from, target)
{
	msgClient.send(IE_WINDOW_PLAYER, MSG_PLAYLIST_ADD, from, target);
}