// JavaScript Document

function clearText(thefield) {
  if(thefield.defaultValue==thefield.value) { thefield.value = "" }
}

function replaceText(thefield) {
  if(thefield.value=="") { thefield.value = thefield.defaultValue }
}

function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

function closePlayer(){
	stopSong();
	$("#boxloc").animate({ 
        marginBottom: "-35px"
    });
}

function stopSong(){
	$("#previewer").html("");
	return false;
}

function updateStatus(file,title,artist,album,users){
	if(!users){
		var users = '';
	}else{
		var users = [users];
	}
	var actionlinks = [{"text": "Add Music", "href": "http://feeder.fm/"}];
	var attachment = {'name':'Feeder.fm','description':'Music on your news feed!','media':[{'type':'mp3','src':file,'title':title, 'artist':artist, 'album':album}]};
	FB.Connect.streamPublish('Listen to \''+title+'\' by '+artist+':',attachment,actionlinks,users, 'Post this song to your feed');
}

function changePage(page,tab,message){
	scroll(0,0);
	document.top.toplinkhack.focus();
	timeout = setTimeout("void(0)",0);
	clearTimeout(timeout);
	if(!message){
		var message = "Loading";
	}
	var playing = document.getElementById('boxloc').innerHTML;
	$("#right").html("<div align=\"center\" style=\"text-align: center; margin: 120px 0px;\"><img src=\"images/loader_1.gif\" alt=\"\"><h1>"+message+"</h1></div>");
	$.ajax({
		url: ''+page+'',
		type: 'GET',
		dataType: 'html',
		success: function(html){
			$("#content").html(html);
			if(playing){
				document.getElementById('boxloc').innerHTML = playing;
			}
		}
	});
}

function play(song,song_name,artist_name){
	//scroll(0,0);
	$("#previewer").html('<object data="http://www.sabiadesign.com/media/player.swf?autoload=true&amp;autoplay=true&amp;playlist_url=http://www.sabiadesign.com/misc/fb/flash/preview.php?song_id='+song+'" type="application/x-shockwave-flash" width="450" height="14" ><param name="movie" value="http://www.sabiadesign.com/media/player.swf?autoload=true&amp;autoplay=true&amp;playlist_url=http://www.sabiadesign.com/misc/fb/flash/preview.php?song_id='+song+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /></object>\n');
	$("#boxloc").html("<div><a href=\"#\" class=\"right\" style=\"color: #ffffff;\" onclick=\"closePlayer(); return false;\">X</a>Playing... <strong class=\"song\" class=\"artist\">"+song_name+"</strong> <em>by</em> "+artist_name+"</div>");
	$("#boxloc").animate({ 
        marginBottom: "0px"
    });
}

function searchMusic(content,type){
	if(!content){
		var content = $('#searchtext').val();
		var type = $('#searchtype').val();
	}
	var content = escape(content);
	var type = escape(type);
	if(content == ''){
		return alert("Enter a search term dummy!");
	}
	$("#searchresults").html("<div align=\"center\" style=\"text-align: center; margin: 50px 0px;\"><img src=\"images/loader_1.gif\" alt=\"\"><h1>Searching For '"+unescape(content)+"'...</h1></div>");
	$.ajax({
		url: 'ajax.php?call=search&q='+content+'&type='+type+'',
		type: 'GET',
		dataType: 'html',
		success: function(html){
			$("#searchresults").slideToggle(300,function(){
				$("#searchresults").html(html);
				$("#searchresults").slideToggle(300);
				$("#loading").html("");
			});
		}
	});
}

function makeUpdateRequest(file,title,artist,album){
	loadingPage();
	$.getJSON('ajax.php?call=makeUpdateRequest&file='+escape(file)+'&title='+escape(title)+'&artist='+escape(artist)+'&album='+escape(album)+'',
	function(data){
		loadingPage(true);
		if(data.action == 'selectAction'){
			tb_show('Feed It','modules/feed_something.php?file='+escape(file)+'&title='+escape(title)+'&artist='+escape(artist)+'&album='+escape(album)+'&height=360&width=640','1');
		}else if(data.action == 'showUpdater'){
			tb_show('Feed It','modules/feed_something.php?file='+escape(file)+'&title='+escape(title)+'&artist='+escape(artist)+'&album='+escape(album)+'&not_connected=1&height=190&width=640','1');
		}
	});
}

function loadFriends(uid,file,title,artist,album){
	loadingPage();
    FB.XFBML.Host.autoParseDomTree = false;
    var api = FB.Facebook.apiClient;
    api.fql_query("SELECT uid, first_name, last_name, pic_square FROM user WHERE  uid IN (SELECT uid2 FROM friend WHERE uid1 = "+uid+") order by first_name", function(result, ex){
 		loadingPage(true);
		data = '<form id="friends_list" name="friends_list" action="javascript:closeFacebookPopup();updateStatus(\''+file+'\',\''+addslashes(title)+'\',\''+addslashes(artist)+'\',\''+addslashes(album)+'\',getChecked());" method="post">';
       	data += '<div class="friends_box">';
		data += "<table width=\"100%\" cellpadding=\"0\">";
        for (i=0;i<result.length;i++){
            data += "<tr style=\"cursor: pointer;\" onmouseover=\"this.style.background='#dddddd';\" onmouseout=\"this.style.background='#ffffff';\"><td style=\"width: 52px; cursor: pointer;\" onclick=\"$('#friend"+result[i].uid+"').attr('checked','checked'); $('#friends_list').submit();\"><img style=\"cursor: pointer;\" src=\""+result[i].pic_square+"\" alt=\"\" /></td><td style=\"width: 10px;\" onclick=\"$('#friend"+result[i].uid+"').attr('checked','checked'); $('#friends_list').submit();\"><input type='radio' onclick=\"$('#friend"+result[i].uid+"').attr('checked','checked'); $('#friends_list').submit();\" value='"+result[i].uid+"' name='friends' id='friend"+result[i].uid+"' class='friend_checkboxes' /> </td><td onclick=\"$('#friend"+result[i].uid+"').attr('checked','checked'); $('#friends_list').submit();\" style=\"cursor: pointer; font-size: 18px; font-style: italic; color: #aaaaaa;\">"+ result[i].first_name + " " + result[i].last_name+"</td></tr>";
        }
        data += "</table>";
		data += '<input type="hidden" id="friends_array" value="" />';
		data += "</div>";
		data += '</form>';
		closeFacebookPopup();
		facebookPopup(data,'Select friends to feed',622);
    });
}

function loadingPage(clear){
	if(clear){
		$('#searchresults').attr('style','opacity: 1');
	}else{
		$('#searchresults').attr('style','opacity: 0.5');
	}
}

function getChecked(){
	var checked = $('input:checked').val();
	return checked;
}

function closeFacebookPopup(){
	$('.fb_pop_dialog_table').hide();
}

function facebookPopup(content,title,width){
	var html = '<div class="fb_resetstyles fb_popupContainer">';
	html += '<table class="fb_pop_dialog_table fb_popup" id="RES_ID_fb_pop_dialog_table" style="width: '+width+'px; position: fixed; top: 40%; left: 20%;">';
    html += '   <tr>';
    html += '     <td class="fb_pop_topleft"></td>';
    html += '     <td class="fb_pop_border"></td>';
    html += '     <td class="fb_pop_topright"></td>';
    html += '   </tr>';
    html += '   <tr>';
    html += '     <td class="fb_pop_border"></td>';
    html += '     <td id="pop_content" class="fb_pop_content">';
    html += '       <div class="fb_pop_content_container">';
    html += '          <h2 class="fb_resetstyles">';
    html += '            <div class="fb_dialog_icon"></div>';
    html += '            <span id="fb_dialog_header" class="fb_dialog_header">'+title+'</span>';
    html += '            <div id="fb_dialog_loading_spinner" class="fb_dialog_loading_spinner "> </div>';
    html += '            <a onClick="return false;" href="#" title="close dialog" class="fb_dialog_cancel_button" id="fb_dialog_cancel_button"> </a>';
    html += '          </h2>';
    html += '          <div class="fb_dialog_content" id="fb_dialog_content">'+content+'</div></div>';
    html += '       </div>';
    html += '     </td>';
    html += '     <td class="fb_pop_border"></td>';
    html += '   </tr>';
    html += '   <tr>';
    html += '     <td class="fb_pop_bottomleft"></td>';
    html += '     <td class="fb_pop_border"></td>';
    html += '     <td class="fb_pop_bottomright"></td>';
    html += '   </tr>';
    html += '</table>';
	html += '</div>';
	$('#content').prepend(html);
}

//tb_show('Edit this event','event_edit.php?event='+data.id+'&lightbox=login&height=330&width=700','1');