//var plowner = "0";


function add_to_jukebox(guid,plowner)
{
	//alert("plowner"+plowner);
	//guid is stream guid
	if (guid!=null && typeof(guid)!="undefined" && guid!="") 
	{
		do_add_item(guid,plowner);
	}
	else
		alert("Invalid to add content to jukebox! Please click add to jukebox again.");
}

function add_to_jukebox_bysong(songguid,plowner)
{
	//guid is stream guid
	//alert(songguid+plowner);
	if (songguid!=null && typeof(songguid)!="undefined" && songguid!="") 
	{
		
		do_add_item_bysong(songguid,plowner);
	}
	else
		alert("Invalid to add content to jukebox! Please click add to jukebox again.");
}

function add_to_jukebox_bysong_services(songguid,plowner)
{
	//guid is stream guid
	//alert(songguid+plowner);
	if (songguid!=null && typeof(songguid)!="undefined" && songguid!="") 
	{
		
		do_add_item_bysong_services(songguid,plowner);
	}
	else
		alert("Invalid to add content to jukebox! Please click add to jukebox again.");
}

function do_add_item(piguid,plowner)
{
	//alert(piguid);
	do_add_item_to_jukebox(plowner, piguid, {onComplete:do_add_item_to_jukebox_complete});
}

function do_add_item_to_jukebox(plowner, piguid, callback)
{
	//alert("plowner="+plowner+"      piguid="+piguid+"       callback="+callback);
	var url = "http://truemusic.truelife.com/home/ctrl/add_mylib.php";
	var param = "plowner=" + plowner + "&piguid=" + piguid + "&type=json";
	//alert(param);
	var options = {method: "post", parameters: param};
	//alert("options=="+options+"        callback==="+callback);
	Object.extend(options, callback);
	var aj = new Ajax.Request(url, options);
}

function do_add_item_bysong(pipguid,plowner)
{
	//alert("guid"+pipguid);
	do_add_item_to_jukebox_bysong(plowner, pipguid, {onComplete:do_add_item_to_jukebox_complete});
}

function do_add_item_to_jukebox_bysong(plowner, pipguid, callback)
{
	//alert("plowner="+plowner+"      pipguid="+pipguid+"       callback="+callback);
	var url = "http://truemusic.truelife.com/xml/add_mylib_bysong_proxy.jsp";
	var param = "plowner=" + plowner + "&pipguid=" + pipguid;
	var options = {method: "post", parameters: param};
	//alert("options=="+options+"        callback==="+callback);
	Object.extend(options, callback);
	var aj = new Ajax.Request(url, options);
	//alert("aj"+aj);
}
function do_add_item_bysong_services(pipguid,plowner)
{
	//alert("guid"+pipguid);
	do_add_item_to_jukebox_bysong_services(plowner, pipguid, {onComplete:do_add_item_to_jukebox_complete});
}

function do_add_item_to_jukebox_bysong_services(plowner, pipguid, callback)
{
	//alert("plowner="+plowner+"      pipguid="+pipguid+"       callback="+callback);
	var url = "http://services.truelife.com/SearchEngine/add_mylib_bysong_proxy.aspx";
	var param = "plowner=" + plowner + "&pipguid=" + pipguid;
	var options = {method: "post", parameters: param};
	//alert("options=="+options+"        callback==="+callback);
	Object.extend(options, callback);
	var aj = new Ajax.Request(url, options);
	//alert("aj"+aj);
}

function do_add_item_to_jukebox_complete(r)
{
//	alert("done");
	
//	alert(r.responseText);

	eval("var jo = " + r.responseText);
	//window.returnValue = "";
	/*
	var o = r.responseXML;
	var rcode = o.documentElement.selectSingleNode("result-header/res_code").text;
	var rdesc = o.documentElement.selectSingleNode("result-header/res_desc").text;
	var item_count = o.documentElement.selectSingleNode("result-header/item_count").text;
	var max_item_allowed = o.documentElement.selectSingleNode("result-header/max_item_allowed").text;
	var debugmsg = o.documentElement.selectSingleNode("result-header/debug").text;
	*/
	var rcode = jo[0];
	var rdesc = jo[1];
	var item_count = jo[2];
	var max_item_allowed = jo[3];
//	alert("code"+rcode);
	switch(rcode)
	{
		case "200":
			alert("เพิ่มเพลงลงใน Jukebox เรียบร้อยแล้วค่ะ\n\nคุณมี " + item_count + " เพลงจาก " + max_item_allowed + " เพลงค่ะ");

			//alert(jkplayer);
			 try{
					
						if(jkplayer==null)
						{
							 jukeboxPlayer();
						}
						else
						{
							jkplayer.location.href="http://truemusic.truelife.com/home/jukebox/index.php?session="+Math.floor(Math.random()*1000);
							jkplayer.focus();
						}
				}catch(error){
						  //error
						  if (error.number == "-2147467259")
						  {
							jukeboxPlayer();
						  }
						 
				}
			break;
		case "201":
			alert("เพิ่มเพลงลงใน Jukebox เรียบร้อยแล้ว แต่มีบางเพลงที่อาจจะซ้ำกับเพลงที่มีอยู่แล้วค่ะ\n\nคุณมี " + item_count + " เพลงจาก " + max_item_allowed + " เพลงค่ะ");

			//alert(jkplayer);
			 try{
					
						if(jkplayer==null)
						{
							 jukeboxPlayer();
						}
						else
						{
							jkplayer.location.href="http://truemusic.truelife.com/home/jukebox/index.php?session="+Math.floor(Math.random()*1000);
							jkplayer.focus();
						}
				}catch(error){
						  //error
						  if (error.number == "-2147467259")
						  {
							jukeboxPlayer();
						  }
						 
				}
			break;
		case "599":
			alert("Jukebox ของคุณเต็มแล้วค่ะ กรุณาลบเพลงที่ไม่ต้องการออกก่อนนะคะ");
		case "601":
			alert("เพลงนี้มีใน Jukebox แล้วค่ะ");
			
			 try{
				
					if(jkplayer==null)
					{
						 jukeboxPlayer();
					}
					else
					{
						jkplayer.location.href="http://truemusic.truelife.com/home/jukebox/index.php?session="+Math.floor(Math.random()*1000);
						jkplayer.focus();
					}
			}catch(error){
					  //error
					  if (error.number == "-2147467259")
					  {
						jukeboxPlayer();
					  }
					 
			}
			break;
		case "500": 
			alert("ไม่สามารถเพิ่มเพลงได้ค่ะ");
		default:
			alert("กรุณา login ก่อนค่ะ"+rcode);// ไม่สามารถเพิ่มเพลงใน Jukebox
			//openLogin("", true, "track");
	}
		//alert("Sorry, insertion failed with the following code\n\n" + rcode + ":" + rdesc + "\n\nPlease try again later.");
		//alert("Debug message:\n\n" + debugmsg);
	//}
}

var floatingWindow;

function openLogin(redirectURL, force, type) {
	dojo.require("dojo.widget.Dialog");
	
	if (floatingWindow)
	{
		floatingWindow.hide();
	}
	
	var properties = {
		hasShadow: false,
		id: "FloatingLogin" + new Date().getTime(),
		resizable: false
	};

	node = document.createElement(properties.id);
	document.body.appendChild(node);
	floatingWindow = dojo.widget.createWidget("Dialog", properties, node);
	
	if ((!redirectURL) || (redirectURL.length == 0))
		redirectURL = location.href;
	
	var forceLogin = "";
	var height = 267;
	if(force)
	{
		forceLogin = "&f=true";
		height = 299;
	}
	var forceType = "";
	if(type && type.length != 0)
	{
		forceType = "&ft=" + type;
	}
	
	floatingWindow.setContent("<div style=\"width:420px;background-color:White;text-align:right;\">"
	+ "<img onclick=\"closeFloatingWindow();\" style=\"padding:4px;cursor:pointer;\" src=\"http://resources.imeem.com/resources/graphics/close.gif\">111</div>"
	+ "<iframe src='juke_login.php' frameborder='0' width='420px' height='"+height+"' marginwidth='0' marginheight='0' bgcolor='#ffffff'></iframe>");
	
	floatingWindow.show();
	try {
		 hideVideo();
	} catch (e) { }
}

function closeFloatingWindow()
	{
		floatingWindow.hide();
		
		try {
			showVideo();
		} catch (e) { }
	}

 function jukeboxPlayer(){
   // var windowprops = "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=0,resizable=0,left=0, top=0, width=900,height=590";
    jkplayer = window.open("http://truemusic.truelife.com/home/jukebox/index.php?session="+Math.floor(Math.random()*1000),"jkPlayer","");
	jkplayer.focus();
  }



/*====Add to jukebox====*/
/*function add_to_jukebox(plowner)
{
		
		id = current_id;
		if (id!=null)
		{
			var item = col.item(id);
			if (item!=null) guid = item.guid;
		}
		if (guid!=null && typeof(guid)!="undefined" && guid!="")
		{
			do_add_item(plowner,guid);
			//log("  [INFO] add song id : "+ guid +" to jukebox to centralid = " + plowner);
		}
		else
			alert("Invalid to add content to jukebox! Please click add to jukebox again.");
}*/




























