/////////////////////////////////////
// Link - Jonghoon Park (03.21.08) //
/////////////////////////////////////

var linkFormOpen = false;
var linkFormErr = "Only one form can be open at a time";

function Link ( id, arr )
{
	this.id = id;
	this.linkID = arr['linkID']; 			// link number	
	this.title = arr['title'];				// link title
	this.domain = arr['domain'];			// domain of the link url
	this.uri = arr['uri'];			 		// uri of the link url
	this.destination = arr['destination'];	// destination of trip the link belongs to
	this.itinerary = arr['iid'];			// itinerary ID
	this.type = arr['type']; 				// saved trip or page
	this.checked = false;					// checkbox identifier	
	this.file = "/myTrips/Links/saveChanges_ajax.php5";	// a file to which parameters are sent
}

// Occurs when onblur mouse event is triggered at link title area
Link.prototype.saveNclose = function ()
{
	var id = "linkTitle[" + this.id + "]";
	
	var el = $(id);
	
	// save chagnes
	if ( el.value != this.title )
	{
		var pars = "id=" + this.id;
			pars += "&action=save";
			pars += "&linkID=" + this.linkID;
		    pars += "&title=" + escape(el.value);
			
		new Ajax.Request(this.file, { parameters: pars,
						 			  onComplete: this.updateComplete.bind(this) } );			
	}
	else
	{
		this.closeForm();
	}
}

// display error summary if it happens during updating database
Link.prototype.updateComplete = function ( oReq, oJSN )
{
	if ( oJSN["error"] != 'N' )
	{
		alert(oJSN["error"]);
	}
	else
	{
		if ( oJSN["action"] == 'save' )
		{
			this.title = oJSN["title"];
			this.closeForm();
		}
		else if ( oJSN["action"] == "delete" )
		{
			location.reload(true);
		}
	}
}

// Close input form and display link title
Link.prototype.closeForm = function ()
{
	if ( linkFormOpen == true )
	{
		var id = "linkTitle[" + this.id + "]";
		
		var el = $(id);
		
		var parent = el.parentNode;
		parent.removeChild(el);
		
		var a = document.createElement("A");
		a.id = id;
		a.href = this.domain + this.uri;
		a.innerHTML = this.title;
		
		parent.appendChild(a);
		
		linkFormOpen = false;
	}
}

// edit link title
Link.prototype.edit = function ()
{
	if ( linkFormOpen == false )
	{						
		var orgID = this.id; // original ID
		var id = "linkTitle[" + this.id + "]";
		
		var el = $(id);
		
		var parent = el.parentNode;
		parent.removeChild(el);
		
		var input = document.createElement("INPUT");
		input.id = id;
		input.type = "text";
		input.size = "50";
		input.onblur = function () { link[orgID].saveNclose (); };
		input.value = this.title;
		
		parent.appendChild(input);
		
		input.focus();
		
		linkFormOpen = true;
	}
	else
	{
		alert(linkFormErr);
	}
}

// delete link
Link.prototype.deleteLink = function ()
{	
	var value = confirm("Do you really want to delete '"+this.title+"' ?");
	
	// delete this link
	if ( value )
	{		
		var pars = "action=delete";
			pars += "&linkID=" + this.linkID;

		new Ajax.Request(this.file, { parameters: pars,
						 			  onComplete: this.updateComplete.bind(this) } );
	}
}

// triggered when the checkbox is clicked
Link.prototype.checkBox = function ( obj, value)
{
	var that = this;
	
	if ( value ) {
		var el = document.getElementById("checkBox" + that.id);
		el.checked = value;
		that.checked = value;
	} else {
		if ( obj.checked ) {
			that.checked = true;
		} else {
			that.checked = false;
		}
	}
	
	if ( emailFormOpen ) {
		if ( !addCheckedLinksToEmailForm() ) {
			emailObj.closeEmailForm();
		}
	}	
}

/*
// triggered when checkbox is clicked
function checkBox ( origID, value )
{
	var id = "chk[" + origID + "]";
	
	var el = $(id);
	
	if ( value ) {
		el.checked = value;
		link[origID].checked = value;		
	}
	else {
		if ( el )
		{
			if ( el.checked == true )
			{
				link[origID].checked = true;
			}
			else
			{
				link[origID].checked = false;
			}
		}	
	}

	if ( emailFormOpen ) {
		if ( !addCheckedLinksToEmailForm() ) {
			emailObj.closeEmailForm();
		}
	}
}
*/

// delete selected chunk of links
function bulkDelete ()
{
	var anythingChecked = false;
	var value = false;
	var linkIDs = '';
	var titles = '';
	var file = "/myTrips/Links/saveChanges_ajax.php5";
	var j=0;
	
	for ( var i in link )
	{
		if ( link[i].checked )
		{
			titles += "\r\n" + link[i].title;
			linkIDs += "&linkID[" + j + "]=" + link[i].linkID;
			j++;
			anythingChecked = true;
		}
	}
	
	if ( anythingChecked ) {
		value = confirm("Do you really want to delete " + titles + " ?");
		
		if ( value )
		{
			var pars = "action=delete";
				pars += linkIDs;

			new Ajax.Request(file, { parameters: pars,
						 	         onComplete: deleteComplete } );
		}
	} else {
		alert("You have to select at least one link to delete!");
	}	
}

// call after deleting through Ajax
function deleteComplete ( oReq, oJSN )
{	
	if ( oJSN["error"] != 'N' )
	{
		alert(oJSN["error"]);
	}
	else
	{
		location.reload(true);
	}
}

// when a mouse pointer is on the link, a bgcolor is changed
function mouseOver ( obj )
{
	obj.style.backgroundColor = '#FFF8CC';
}

// when a mouse pointer is out of the link, a bgcolor is changed
function mouseOut ( obj )
{
	obj.style.backgroundColor = '#FFFFFF';
}
