String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

function debug(msg){
	if( typeof(console)=='object'){
		console.log('#MainFrame# debug- '+msg);
	}
}
function getMainFrame(){
	var mf=parent.frames['mainFrame'];
	return mf;
}
function getTopFrame(){
	var tf=parent;
	return tf;
}
function getPlayerFrame(){
	var pf=parent.frames['playerFrame'];
	return pf;
}
/*
function addSong(song_uid){
	if(song_uid!='' && song_uid!=0){
		var tf=getTopFrame();
		tf.PL.addSong(song_uid);
	}
}
*/
function addSong(song_uid, keyword, brnode) {
	if(song_uid != '' && song_uid != 0){
		var tf=getTopFrame();

		tf.PL.addSong(song_uid);

		// update amz widget
		tf.updateAmzWidget(keyword, brnode, '20');
		if (tf.amzIntId)
			clearInterval(tf.amzIntId);
		tf.lastAmzKeyword = keyword;
		tf.lastAmzBrnode = brnode;
		//tf.amzIntId = setInterval("refreshAmz()", tf.amzIntInterval);
		//tf.amzIntId = setTimeout("clearAmz()", tf.amzIntInterval);

		//tf.amzIntId = setInterval("clearAmz()", tf.amzIntInterval);
		tf.amzIntId = setTimeout("clearAmz()", 1000*60*5); // 5 minutes

	}
}

function refreshAmz(){
	var tf=getTopFrame();
//alert('refreshAmz');
	tf.updateAmzWidget(tf.lastAmzKeyword, tf.lastAmzBrnode, '20');
}
function clearAmz(){
	var tf=getTopFrame();
	tf.clearAmzWidget();
	clearInterval(tf.amzIntId);
}
function addJukeBox(jukebox_uid){
	if(jukebox_uid!='' && jukebox_uid!=0){
		var tf=getTopFrame();
		tf.PL.addJukeBox(jukebox_uid);
	}
}
function addAlbum(album_uid){
	if(album_uid!='' && album_uid!=0){
		var tf=getTopFrame();
		tf.PL.addAlbum(album_uid);
	}
}
function sample_play(){
	var tf=getTopFrame();
	var pl = tf.playlist;
	
}

function _setIframeHeight(){
	//alert("_setIframeHeight @sub.js");
	if (parent)
		parent.setIframeHeight('mainFrame');
}

function goSetHeight(){
	//alert("goSetHeight() @sub.js");
	if( parent==window){
		return;
	} else {
		//parent.setIframeHeight('mainFrame');
		//setTimeout("parent.setIframeHeight('mainFrame');",1000);
		//alert("calling setTimeout__");
		//setTimeout("_setIframeHeight()",200);
		_setIframeHeight();
	}
}


function closeSongInfo(){
	$("#Area_SongInfo").hide('fast');
}


function isMainFrame(){
	//alert(document.URL);
	//alert(top.location.href);

	if( document.URL == top.location.href ){
		return false;
	}else{
		return true;
	}
}


function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
   }
   else {
       return document[movieName]
   }
}

function set_xml_url(target_type, url, motion){
	var swf_motion;
	if(motion=='prev'){
		swf_motion='left';
	}else{
		swf_motion='right';
	}
	// call falsh method (read xml)
	var mv = getMovieName("userImageDisplayer_"+target_type);
	mv.set_xml_url( url, swf_motion);
}

function popup_agreement(){
	var width=750;
	var height=440;
	var left=(screen.width)?(screen.width-width)/2:200;
	var my_top=(screen.height)?(screen.height-height)/2:200;
	var win = window.open('/terms_of_use','agreement_win','scrollbars=1,resizable=1,width='+width+',height='+height+',left='+left+',top='+my_top);
	if( win ){
		win.moveTo(left,my_top);
		win.focus();
	}
}
function no_submit_enter(e,input, next_id){
	var key = e.charCode || e.keyCode || 0;
	//alert(key);
	if( key == 13 ){
		var str  = $(input).val();
		if( str!='' && next_id ){
			$('#'+next_id).focus();
		}
		return false;
	}
}

function onReady() {
	divShareSongCurrentlyOpen = null;

	$.cookie('TuneAround_current_logic', null); // delete cookie, if any
	if(!isMainFrame()){ // If accessed with sub directories
		//$('#_bodyAll_').html('Loading...');

		var url=document.URL;
		var domain_start = url.indexOf("://");
		url = url.substr(domain_start+3);
		var logic_start = url.indexOf('/');
		var logic_string = url.substr(logic_start);

		if (logic_string.length > 1) { 
			if (logic_string.substr(0, 7) != '/popup_') {
				$.cookie('TuneAround_current_logic', logic_string, {expires:null, path:'/', domain:'tunearound.com'});
				//alert("logic_string="+logic_string);
				location.href = '/';
				return;
			} else { // popup
				//location.href = logic_string;
			}
		}
	}else{
		$('#_loading_').hide();
		$('#_bodyAll_').show();

		$("body").click( function(){
			closeUserLinks();
			closeShareSongDlg();
		});
		getTopFrame().hideIndexLoader();
		goSetHeight();
	}
}

/*****************************************************************************************/
function dlg_test(e,song_uid){
alert("dlg_test()");
	//var url='/popup_song_info/'+song_uid;
	//var url='/popup_check_id_form/'+'sohot';
	//alert(e.pageX);
	//var url='/popup_song_info/'+song_uid;
	//var url='/popup_song_info/'+song_uid+'/simple';

	var my_top,left,width,height;

	//width=$("#Area_SongInfo").width();
	//height=$("#Area_SongInfo").height();

	width=270;
	height=90;

	var doc_width = $(document).width();
	var doc_height = $(document).height();
	//doc_height=450; // 고정길이
	doc_height=doc_height-90;
	
	if( width+e.clientX+50 > doc_width ){
		left = e.clientX - width - 50;
	}else{
		left = e.clientX + 50;
	}
	
	if( left<10 ){
		left=50;
	}

	//left = 345+20-50;
	new_left = e.clientX;
	new_top = e.clientY;

	if( height+e.clientY+5 > doc_height ){
		my_top = doc_height - height - 5;
	}else{
		my_top = e.clientY ;
	}
	
	if( my_top< 10 ){
		my_top=10;
	}
	//debug('my_top:'+my_top);
	//debug('height:'+height);

	if( my_top>= doc_height-height-80){
		my_top = doc_height-height-80;
	}
	
	//debug('doc_width:'+doc_width);
	//debug('doc_height:'+doc_height);
	//debug('my_top:'+my_top);

	my_top = my_top;

	$("#Dlg_test").css('left',new_left);
	$("#Dlg_test").css('top',new_top);
	$("#Dlg_test").html('');
	//$("#Dlg_test").hide();
	//$("#Dlg_test").load(url);

	$("#Dlg_test").show('fast');
/*
	$('#Dlg_test').animate( 
		{'left': left+'px', opacity:'show'},
		'fast'
	);
*/
}

function close_test_dlg(){
	$(".Dlg_test_class").hide();
}

function showUserLinks(e, id){
	closeUserLinks();
	var new_left = e.clientX;
	var new_top = e.clientY;
	var doc_width = $(document).width();
	var width = 190;

	if (doc_width < new_left + width) {
		new_left = doc_width - width;
	}

	$(id).show('fast');
	$(id).css('left',new_left);
	$(id).css('top',new_top);

/*
	$("#userLinks_fta_test").css('left',new_left);
	$("#userLinks_fta_test").css('top',new_top);
	$("#userLinks_fta_test").show('fast');
*/
}

function closeUserLinks(){
	$(".userLinkMenuLayer").hide();
}

function showFriendRequestDlg(e,from_uid,to_uid){
	var url='/friend_request/'+from_uid+'/'+to_uid;
	var new_top,new_left;

	var width=$("#Dlg_friend_request").width();
	var height=$("#Dlg_friend_request").height();

	var doc_width = $(document).width();
	var doc_height = $(document).height();

	doc_height=doc_height-90;
	
	new_left = e.clientX - width/2;
	new_top = e.clientY - height/2 ;
	
	if (new_left + width > doc_width)
		new_left = doc_width - width - 10;
	if (new_left < 10)
		new_left = 10;

	if( new_top< 10 ){
		new_top=10;
	}
	if( new_top>= doc_height-height-80){
		new_top = doc_height-height-80;
	}
	
	$("#Dlg_friend_request").hide();
	$("#Dlg_friend_request").css('left',new_left);
	$("#Dlg_friend_request").css('top',new_top);
	$("#Dlg_friend_request").html('');
	$("#Dlg_friend_request").load(url);
	$("#Dlg_friend_request").show('fast');
/*
	$('#Dlg_friend_request').animate( 
		{'left': left+'px', opacity:'show'},
		'fast'
	);
*/
}

function closeFriendRequestDlg() {
	$("#Dlg_friend_request").html('');
	$("#Dlg_friend_request").hide();
}

function showShareSongDlg(e,song_uid,visitor_uid) {
	var url='/share_song/'+song_uid+'/'+visitor_uid;

	var new_left = e.clientX;
	var new_top = e.clientY;
	var doc_width = $(document).width();
	var doc_height = $(document).height();
	var width=$("#Dlg_share_song").width();

	closeShareSongDlg();
	if (doc_width < new_left + width + 25) {
		new_left = doc_width - width - 25;
	}
	if (new_left < 10)
		new_left = 10;

	doc_height=doc_height-90;
	
	if( new_top< 10 ){
		new_top=10;
	}
	
	$("#Dlg_share_song").html(' ');
	$("#Dlg_share_song").css('left',new_left);
	$("#Dlg_share_song").css('top',new_top);
	$("#Dlg_share_song").show('fast');
	$("#Dlg_share_song").load(url);
}

function closeShareSongDlg() {
	$("#Dlg_share_song").html('');
	$("#Dlg_share_song").hide();
}

function showSendMessageDlg(e,from_uid,to_uid){
	var url='/send_message/'+from_uid+'/'+to_uid;
	var new_top,new_left;

	var width=$("#Dlg_send_message").width();
	var height=$("#Dlg_send_message").height();

	var doc_width = $(document).width();
	var doc_height = $(document).height();

	doc_height=doc_height-90;
	
	new_left = e.clientX - width/2;
	new_top = e.clientY - height/2 ;
	
	if (new_left + width > doc_width)
		new_left = doc_width - width - 10;
	if (new_left < 10)
		new_left = 10;

	if( new_top< 10 ){
		new_top=10;
	}
	if( new_top>= doc_height-height-80){
		new_top = doc_height-height-80;
	}
	
	$("#Dlg_send_message").hide();
	$("#Dlg_send_message").css('left',new_left);
	$("#Dlg_send_message").css('top',new_top);
	$("#Dlg_send_message").html('');
	$("#Dlg_send_message").load(url);
	$("#Dlg_send_message").show('fast');
}

function closeSendMessageDlg() {
	$("#Dlg_send_message").html('');
	$("#Dlg_send_message").hide();
}

function addToPlaylist(uid, memberUid, title) {
	var ret = confirm("Add song \""+title+"\" into your playlist?");

	if (ret) {
		$.ajax({
			type: 'POST',
			url: '/add_to_playlist/',
			data: {
				song_uid: uid,
				member_uid: memberUid
			},
			success:function(data, textStatus) {
				alert("Successfully added");
			}
		});
	} else {

	}
}
function deleteFromPlaylist(song_uid,member) {
	if (confirm('Are you sure to remove the song from Playlist?' )) {
		$.ajax({
			url :		'/delete_song_from_playlist/'+song_uid,
			type :		'GET',
			async :		true,
			beforeSend :function() {
						},
			cache:		false,
			complete : 	function() {
						},
			success :	function(data, textStatus) {
							//if (data > 0) {
								//$('#songEntry_playlist_'+song_uid).hide();
								location.href = '/user_home/'+member+'/playlist';
							//}
						}	
		});
	}
}
function startLoaderBig(){
	//var str="<img src='/img/loading_ani.gif' border='0' width='43' height='46' >";
	//var str="<img src='/img3/loader_normal.gif' border='0' width='43' height='46' >";
	var str="<table style='background-color:white;width:100%;height:50px;cellpadding=0 cellspacing='0'><tr valign=center><td align=center><img src='http://files.tunearound.com/img/loader_normal.gif' border='0' width='32' height='32' ></td></tr></table>";
	return str;
}

function showUserHomeLoader() {
	var html_str="<div style='background-color:white;width:100%;height:300px;text-align:center;padding-top:100px'><img src='http://files.tunearound.com/img/loader_normal.gif' width='32px' height='32px'></div>";
	$('.userHomeContentWrapper').html(html_str);
}

function getOSString() {
	if (navigator.appVersion.indexOf("Win")!=-1) 
		return "WIN";
	if (navigator.appVersion.indexOf("Mac")!=-1) 
		return "MAC";
	if (navigator.appVersion.indexOf("X11")!=-1) 
		return "UNIX";
	if (navigator.appVersion.indexOf("Linux")!=-1) 
		return "LINUX";

	return "Unkown";
}

function getBrowserString() {
	var str;
	if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) {
		str = "FF";
	} else if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1) { // Google Chrome
		str = "Chrome";
	} else if (navigator.appName.indexOf("Microsoft") != -1) { // IE
		str = "IE";
	} else {
		str = "Other";
	}
	return str;
}

function getIEVersion() {
	var version = 999;
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		version = parseFloat(navigator.appVersion.split("MSIE")[1]);    
	}
	return version; 
}

var divShareSongCurrentlyOpen;

function showShareSongOptions(song_uid,visitor_uid,div_root,div_append,is_main) {
/*
	if (divShareSongCurrentlyOpen) {
		$('#'+divShareSongCurrentlyOpen).hide('fast', 
									function() {
										doShowShareOptions(song_uid,visitor_uid,div_root,div_append,is_main);
									});
	} else {
	*/
		doShowShareOptions(song_uid,visitor_uid,div_root,div_append,is_main);
	//}
}

function doShowShareOptions(song_uid,visitor_uid,div_root,div_append,is_main) {
	if (is_main) {
		var url_str = '/share_song_options/main?s='+song_uid+'&v='+visitor_uid+'&divappend='+div_append;
	} else {
		var url_str = '/share_song_options?s='+song_uid+'&v='+visitor_uid+'&divappend='+div_append;
	}

	$('#'+div_root+div_append).html('<p><center>Loading...</center></p>');
	$('#'+div_root+div_append).show('fast');
	$.ajax({
		url :		url_str,
		type :		'GET',
		async :		true,
		beforeSend :function() {
					},
		cache:		false,
		complete : 	function() {
					},
		success :	function(data, textStatus) {
						$('#'+div_root+div_append).html(data);
						//$('#'+div_root+div_append).show('slow', function() {
						$('#'+div_root+div_append).slideDown(300, function() {
																goSetHeight();
															});
						divShareSongCurrentlyOpen = ''+div_root+div_append;
						//goSetHeight();
					}	
	});
}

function showDownloadSongOptions(song_uid,div_root,div_append,is_main) {
	if (is_main) {
		var url_str = '/download_song_options/main?s='+song_uid+'&divappend='+div_append;
	} else {
		var url_str = '/download_song_options?s='+song_uid+'&divappend='+div_append;
	}

	$('#'+div_root+div_append).html('<p><center>Loading...</center></p>');
	$('#'+div_root+div_append).show('fast');
	$.ajax({
		url :		url_str,
		type :		'GET',
		async :		true,
		beforeSend :function() {
					},
		cache:		false,
		complete : 	function() {
					},
		success :	function(data, textStatus) {
						$('#'+div_root+div_append).html(data);
						//$('#'+div_root+div_append).show('slow', function() {
						$('#'+div_root+div_append).slideDown(300, function() {
																goSetHeight();
															});
						divShareSongCurrentlyOpen = ''+div_root+div_append;
						//goSetHeight();
					}	
	});
}
function showSongInstInfo(song_uid,sess_id,owner_id,div_root,div_append,is_main) {
	if (is_main) {
		var url_str = '/get_song_inst_info/main?s='+song_uid+'&sess='+sess_id+'&mid='+owner_id+'&divappend='+div_append;
	} else {
		var url_str = '/get_song_inst_info?s='+song_uid+'&sess='+sess_id+'&mid='+owner_id+'&divappend='+div_append;
	}

	$('#'+div_root+div_append).html('<p><center>Loading...</center></p>');
	$('#'+div_root+div_append).show('fast');
	$.ajax({
		url :		url_str,
		type :		'GET',
		async :		true,
		beforeSend :function() {
					},
		cache:		false,
		complete : 	function() {
					},
		success :	function(data, textStatus) {
						$('#'+div_root+div_append).html(data);
						//$('#'+div_root+div_append).show('slow', function() {
						$('#'+div_root+div_append).slideDown(300, function() {
																goSetHeight();
															});
						divShareSongCurrentlyOpen = ''+div_root+div_append;
						//goSetHeight();
					}	
	});
}
/***************************************************************************************************/


function LaunchApp(set_uid, sess_id, user_id, _set_title, tar_user_id, _tar_song_title, song_uid)
{
	var set_title = _set_title.replace(/ /g, '+');
	var tar_song_title = _tar_song_title.replace(/ /g, '+');

	var pluginObj = document.getElementById("plugin");

	if (!set_uid)
		set_uid = 0;

	if (getBrowserString() == "Chrome") {
		alert("This feature is currently supported for FireFox, Safari, and IE.\r\nBut, it will be soon available on Google Chrome.\r\nSorry for the inconvenience!");
		return;

	} else if (getBrowserString() == "IE") {
		var axInstalled = true;
		// use try-catch, otherwise it will fail
		try {
			var axobj = new ActiveXObject("TUNEAROUNDAX.TunearoundAXCtrl.1");
			axInstalled = (axobj != null);
		} 
		catch(e) {
			//axInstalled = false;
			alert("You must install TuneAroundStudio to activate this feature.\r\nMake sure you close and restart web-browsers after installation.");
			getMainFrame().location.href='/download';
			return;
		}

		if (axInstalled) {
			if (!document.TunearoundLauncher) {
				pluginObj.innerHTML = "<object id='TunearoundLauncher' width=0 height=0 classid='CLSID:C86C037B-F399-4658-BF0F-3BBE4B3926FE'></OBJECT>";
			}
//alert(set_uid+" "+sess_id+" "+user_id+" "+set_title+" "+tar_user_id+" "+tar_song_title+" ");
			TunearoundLauncher.LaunchApp(set_uid, sess_id, user_id, set_title, tar_user_id, tar_song_title, song_uid);
		} else {
			alert("You must install TuneAroundStudio to activate this feature.\r\nMake sure you close and restart web-browsers after installation.");
			getMainFrame().location.href='/download';
			return;
		}
	} else { // FF, Safari
		var mt = navigator.mimeTypes["application/tunearound-launcher-plugin"];
		if (!mt) {
			alert("You must install TuneAroundStudio to activate this feature.\r\nMake sure you close and restart web-browsers after installation.");
			getMainFrame().location.href='/download';
		} else {
			pluginObj.innerHTML = "<embed type='application/tunearound-launcher-plugin' width=0 height=0>";
			try {
				var embed = document.embeds[0];
				if (embed) {
					embed.LaunchApp(set_uid, sess_id, user_id, set_title, tar_user_id, tar_song_title, song_uid);
				} else {
					alert("You must install TuneAroundStudio to activate this feature.\r\nMake sure you close and restart web-browsers after installation.");
					getMainFrame().location.href='/download';
				}
			} 
			catch(e) {
				alert("You must install TuneAroundStudio to activate this feature.\r\nMake sure you close and restart web-browsers after installation.");
				getMainFrame().location.href='/download';
			}
		}
	}
}

function PassParameter()
{
	if (StringInput.value != " ")
	{
		TunearoundLauncher.InputString = StringInput.value;
		//TunearoundLauncher.LoadParameter("input argument");
		TunearoundLauncher.LaunchApp("input argument", "asdf");
	}
}

function reloadTab(num) {
	$('#tabContainer > ul').tabs('load', num);
}
function selectTab(num) {
	$('#tabContainer > ul').tabs('select', num);
}

