/**
 * This file contains functions for the project.php page.
 *
 *  $Log: project_utils.js,v $
 *  Revision 1.1  2010/06/09 22:51:41  ethan
 *  Initial revision
 *
 *
 * Author
 *	Ethan Brown (ethan@ethanbrown.org)
 */

var server_loc = "project_utils.php";


/**
 * Delete project.
 */
function delete_project(project_id)
{
    var really_confirm = confirm("Click OK to really delete this project.");
	if (! really_confirm)
	{
	    return;
	}

    var myAjax = new sack();
    myAjax.requestFile = server_loc;    

    myAjax.setVar('Action', 'ajax_delete_project');
    myAjax.setVar('project_id', project_id);        
    
    myAjax.onCompletion = function() {
	var bottom_section = document.getElementById('bottom_section');    	
	
	var response = myAjax.response;
	var list_values = response.split("\n");
	if (list_values[0] != 'OK')
	{
	    bottom_section.innerHTML = response;
	    return;
	}

	refresh_project_list();
    };
    myAjax.runAJAX();
	
}


/**
 * Show project songs in the page bottom area.
 */
function show_project_songs(project_id)
{
    var myAjax = new sack();
    myAjax.requestFile = server_loc;    

    myAjax.setVar('Action', 'ajax_print_songs_for_project');
    myAjax.setVar('project_id', project_id);    
    
    myAjax.onCompletion = function() {
	handle_song_selection_display(myAjax);
    };
    myAjax.runAJAX();
}


/**
 *  General function for displaying a project song return set.
 *
 *  Create a table of class 'song_search_result'.  Create flash player
 *  for each song if a snippet path is provided.
 *
 *  The response string is of the form
 *  Status:OK|ERROR
 *  song_id<TAB>song_project_id<TAB>snippet_url<TAB>title<TAB>comments<RETURN>
 *  ....
 *
 *  If there are no tabs in the set, it's assumed it is a non-fatal
 *  status error to be printed directly.
 *
 *  Note that the flash music player needs a container div, so
 *  the document is modified and then the flash players are
 *  set.
 */
function handle_song_selection_display(songsAjax)
{
    var bottom_section = document.getElementById('bottom_section');
    
    if (! bottom_section)
    {
	alert('bottom section not found on page!');
	return;
    }
    
    var response = songsAjax.response;
    var list_values = response.split("\n");

    // Successful response string will have "OK" as the first line:
    if (list_values[0].indexOf('OK') == -1)
    {
	alert("Error:\n" + response);
	return;
    }

    // Check for tab in second row.  If there isn't one, print as content.

    var content = "";
    if (list_values[1].indexOf("\t") == -1)
    {
	for (var i = 1; i < list_values.length; i++)
	{
	    content += list_values[i];
	}
	bottom_section.innerHTML = content;
	return;
    }

    // Generate table of search result set

    var res_table = document.createElement("table");
    res_table.className = 'song_search_result';
    var tbody = document.createElement("tbody");

    var row = document.createElement("tr");
    var h0 = document.createElement("th");
    var h1 = document.createElement("th");
    var h2 = document.createElement("th");
    h0.innerHTML = "Play";
    h1.innerHTML = "Title";
    h2.innerHTML = "Comments";
    row.appendChild(h0);
    row.appendChild(h1);
    row.appendChild(h2);
    tbody.appendChild(row);

    var snippets = new Array();
    for (var i = 1; i < list_values.length; i++)
    {
	var row_tokens = list_values[i].split("\t");
	var song_id = row_tokens[0];
	var song_project_id = row_tokens[1];
	var snippet_file = row_tokens[2];
	var title = row_tokens[3];
	var description = row_tokens[4];
	
	if (! title)
	    continue;
	
	var row1 = document.createElement("tr");
	
	var td0 = document.createElement("td");
	if (snippet_file == '-')
	{
	    td0.innerHTML = '-';
	}
	else
	{
	    td0.innerHTML = ""
		+ "<a href='javascript:set_file_for_play(\""
		+ escape_quotes(snippet_file) + "\",\""
		+ escape_quotes(title) + "\")' >"
		+ "<img src='images/icon-speaker.gif' border='0'>"
		+ "</a>";
 	}
	snippets.push(snippet_file);

	
	var td1 = document.createElement("td");
	td1.innerHTML = "<a href='javascript:show_song_detail_window("
	    + song_id
	    + ")' title='Click title for details'>" + title + "</a>";
	var td2 = document.createElement("td");
	td2.innerHTML = description;

	row1.appendChild(td0);
	row1.appendChild(td1);
	row1.appendChild(td2);
	tbody.appendChild(row1);
    }

    clear_bottom_section();
    res_table.appendChild(tbody);
    bottom_section.appendChild(res_table);

    return;
}

function set_file_for_play(file_url, title)
{
    var audio_wrapper = document.getElementById('audioplayer_wrapper');    
    audio_wrapper.innerHTML = "<div id='audioplayer_container'> </div>";    
    
    AudioPlayer.embed('audioplayer_container',
		      {soundFile: file_url,
		      autostart: "yes"});
}    

function clear_bottom_section()
{
    var bottom_section = document.getElementById('bottom_section');    

    if (bottom_section)
    {
	bottom_section.innerHTML = "";
    }
}

/**
 *  Precede any single or double quotes with a backslash.
 */
function escape_quotes(str)
{
    str = str.replace(/\'/g, "&#39;");
    str = str.replace(/\"/g, '\\"');

    return str;
}


// Attribution:  http://www.somacon.com/p355.php
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


/**
 *  This function is called when the user clicks on the song title
 *  in the search result set.  It pops up a separate window with
 *  the song details.
 */
function show_song_detail_window(song_id)
{
    var window_url = "song_detail.php?songID=" + song_id;
    window.open (window_url,"popwin",
		 config="width=770,height=430,location=no,status=no,directories=no,toolbar=no,scrollbars=no,menubar=no,resizable=no,top=100,left=50");
}

function add_project()
{
    var myAjax = new sack();
    myAjax.requestFile = server_loc;    

    myAjax.setVar('Action', 'ajax_print_new_project_form');
    
    myAjax.onCompletion = function() {
	var bottom_section = document.getElementById('bottom_section');    	
	bottom_section.innerHTML = myAjax.response;
    };
    myAjax.runAJAX();

}

function handle_new_project()
{
    var name_el = document.getElementsByName('name')[0];
    var description_el = document.getElementsByName('description')[0];
    if (! name_el)
    {
	alert("Couldn't find name element.");
	return;
    }
    if (! description_el)
    {
	alert("Couldn't find description element.");
	return;
    }

    var name = trim(name_el.value);
    var description = trim(description_el.value);

    if (! name)
    {
	alert("Please enter a project name.");
	return;
    }
    if (! description)
    {
	alert("Please enter a project description.");
	return;
    }

    var myAjax = new sack();
    myAjax.requestFile = server_loc;    

    myAjax.setVar('Action', 'ajax_handle_add_project');
    myAjax.setVar('name', name);
    myAjax.setVar('description', description);
    
    myAjax.onCompletion = function() {
	var bottom_section = document.getElementById('bottom_section');    	
	
	var response = myAjax.response;
	var list_values = response.split("\n");
	if (list_values[0] != 'OK')
	{
	    bottom_section.innerHTML = response;
	    return;
	}

	refresh_project_list();
    };
    myAjax.runAJAX();

}

/**
 * Refresh the project list div contents with the current project list.
 */
function refresh_project_list()
{

    var myAjax = new sack();
    myAjax.requestFile = server_loc;    

    myAjax.setVar('Action', 'ajax_print_project_list_content');
    
    myAjax.onCompletion = function() {
	var project_section = document.getElementById('project_list');
	var bottom_section = document.getElementById('bottom_section');
	
	var response = myAjax.response;
	project_section.innerHTML = response;
	bottom_section.innerHTML = '';
    };
    myAjax.runAJAX();
    
}
    

