﻿function LSROClient(key, outline_id, keyboard, outliner_div, browser, user_agent)
{
	this.outliner;
	var that = this;
	
	var browser;						// contains the browser name that is in use. We need this for inline editing
	var user_agent;						// stores the user agent the user is using. For the iPad, we render the outline differently
	
	var last_modified_on;
	
	var keyboard;						// set to TRUE signifies that keyboard short cuts are enabled
										// FALSE signifies that they are disabled
										
	var uid;							// ID of Pro user. If is not Pro, we set uid to 0
	var sync;
	
	var files = {};						// this JSON object contains the info about the files owned by this pro user.
										// empty for non-pro users
										
	var file_bubble_timer;				// timer set when we show the file bubble @ row. When the mouse goes out of the bubble, we start it
	
	this.Init=function(key, outline_id, keyboard_status, outliner_div, user_browser, userAgent)
	{
		this.outliner = new LSOutliner(key, outline_id, outliner_div, this);
		this.keyboard = keyboard_status;
		
		this.browser = user_browser;
		this.user_agent = userAgent;
		
		this.uid = 0;					// we don't care about the user, so we set it to 0
		
		this.sync = new LSSync(this);
	
		this.GetOPML();					// this function also renders the outliner
		
		that.DeselectRow();
		
		if (this.keyboard)
			that.HandleKeyboard();
	};
	
	this.GetBrowser=function()
	{
		return this.browser;
	};
	
	this.GetUserAgent=function()
	{
		return this.user_agent;
	};
	
	this.IsUserInteractionEnabled=function()
	{
		return true;
	};
	
	this.IsInReadOnlyMode=function()
	{
		return true;
	}
	
	this.SelectRow=function(row)
	{
		that.DeselectRow();
		that.outliner.SelectRow(row);
		that.outliner.RenderSelectRow();
	};
	
	this.DeselectRow=function()
	{
		that.outliner.RenderDeselectRow();
		that.outliner.DeselectRow();
	};
	
	this.ExpandRow=function(row)
	{
		this.dont_select = true;
		that.outliner.ExpandRow(row);
		that.outliner.Render(row, 'expand');
	};
	
	this.CollapseRow=function(row)
	{
		this.dont_select = true;
		that.outliner.CollapseRow(row);
		that.outliner.Render(row, 'collapse');
	};
	
	this.GetUserId=function()
	{
		return this.uid;
	};
	
	this.GetFilesFromServer=function()
	{
		var file_ids = that.outliner.GetFileIds();
		var file_ids_str = file_ids.join();

		// until we are waiting for the server to respond, we make do with the file info we have in the OPML
		that.files = new Array();
		for (var i = 0; i < file_ids.length; i++)
		{
			n = { "file_id": file_ids[i] };
			that.files[that.files.length] = n;
		}

		new Ajax.Request("/complete/get_files.php",
		{
			method:'post',
			onSuccess: this.StoreFiles,
			parameters: { uid: 0, file_ids: file_ids_str },
			onFailure: function(){ alert('Something went wrong...'); }
		});
	};
	
	// after dumping the files information into the client.files array, we render the outline
	this.StoreFiles=function(files)
	{
		that.files = new Array();		// we over-write the files array that we created in GetFilesFromServer with the actual data

		for (var i = 0; i < files.length; i++)
		{
			n = {	"file_id": files[i].file_id,
					"uid": files[i].uid,
					"filename": files[i].filename,
					"size": files[i].size,
					"raw_size": files[i].raw_size,
					"time": files[i].time,
					"owner": files[i].owner,
					"this_outline": files[i].this_outline
			};

			that.files[that.files.length] = n;
		}
		
		// we now render the outliner		
		that.outliner.Render();
	};
	
	this.GetFiles=function()
	{
		return this.files;
	};
	
	this.GetFileById=function(file_id)
	{
		if (this.files !== 'undefined')
		{
			for (var i = 0; i < this.files.length; i++)
			{
				if (this.files[i]['file_id'] == file_id)
					return this.files[i];
			}
		}
		return false;
	};
	
	this.SetLastModifiedOn=function(mod_on)
	{
		that.last_modified_on = mod_on;
	};
	
	this.GetLastModifiedOn=function()
	{
		return that.last_modified_on;
	};
	
	this.ComputeAmountForAllRows=function()
	{
		if (that.outliner.body.hasChildren())
		{
			for (var i = 0; i < that.outliner.body.children.length; i++)
				that.ComputeAmountForRow(that.outliner.body.children[i]);
		}
	};
	
	// if the row is a parent, it's amount is the sum of its children
	this.ComputeAmountForRow=function(row)
	{
		if (row.hasChildren())
		{
			var sum = 0;
			var children = row.GetChildren();

			for (var i = 0; i < children.length; i++)
				sum += that.ComputeAmountForRow(children[i])*1;

			if (sum)
				row.setAttribute('_amount', sum.toFixed(2));
			else row.unsetAttribute('_amount');
			return sum;
		}
		else return row.getAttribute('_amount');
	};
	
	this.ShowFileBubbleAtRow=function(file_id, file_clip)
	{
	var file_bubble = document.getElementById("row-file-bubble");

	var offset_left = file_clip.offsetLeft - 367;
	var offset_top = file_clip.offsetTop - 46;

		if (file_bubble)
		{
			file_bubble.style.top = offset_top+'px';
			file_bubble.style.left = offset_left+'px';
	
		var file_info = that.GetFileById(file_id);
			document.getElementById('row-bubble-filename').innerHTML = file_info['filename'];
			document.getElementById('row-bubble-filesize').innerHTML = file_info['size'];
			document.getElementById('row-bubble-filetime').innerHTML = "Uploaded: " + file_info['time'];
			document.getElementById('row-bubble-fileowner').innerHTML = "Owner: " + file_info['owner'];
			document.getElementById('row-bubble-thumb').src = "/getfile.php?file_id="+file_id+"&size=thumb";
			
			document.getElementById('row-bubble-view').onclick = function() { if (that.IsUserInteractionEnabled()) that.ViewFile(file_id); };
			document.getElementById('row-bubble-download').onclick = function() { if (that.IsUserInteractionEnabled()) that.DownloadFile(file_id); };
			
			file_bubble.style.display = "block";		// finally we make the bubble display
			file_bubble.onmouseover = that.ClearFileBubbleTimer;
			file_bubble.onmouseout = that.StartFileBubbleTimer;
		}	
	};
	
	this.HideFileBubbleAtRow=function()
	{
	var file_bubble = document.getElementById("row-file-bubble");

		if (file_bubble)
			file_bubble.style.display = "none";
		else alert("no file bubble");
	};
	
	this.ShowAssignedToBubbleAtRow=function(assigned_to, assignee_icn)
	{
	var assigned_to_bubble = document.getElementById("row-assigned-to-bubble");

	var offset_left = assignee_icn.offsetLeft - 367;
	var offset_top = assignee_icn.offsetTop - 46;

		if (assigned_to_bubble)
		{
			assigned_to_bubble.style.top = offset_top+'px';
			assigned_to_bubble.style.left = offset_left+'px';
			
			assigned_to_bubble.style.display = "block";		// finally we make the bubble display
			assigned_to_bubble.onmouseover = that.ClearAssignedToBubbleTimer;
			assigned_to_bubble.onmouseout = that.StartAssignedToBubbleTimer;
			
			var tUid = that.GetUserId();

			new Ajax.Request("/complete/get_assigned_to_bubble_info.php",
			{
				method:'post',
				onSuccess: this.ShowAssignedToBubbleAtRowCallBack,
				parameters: { assigned_to: assigned_to, uid: tUid },
				onFailure: function(){ alert('Something went wrong...'); }
			});
		}	
	};
	
	this.ShowAssignedToBubbleAtRowCallBack=function(t)
	{
	var response = t.responseText;
	
	var assigned_to_bubble = document.getElementById("row-assigned-to-bubble");
		assigned_to_bubble.innerHTML = response;
	};
	
	this.HideAssignedToBubbleAtRow=function()
	{
	var file_bubble = document.getElementById("row-assigned-to-bubble");

		if (file_bubble)
			file_bubble.style.display = "none";
		else alert("no file bubble");
	};
	
	this.StartFileBubbleTimer=function()
	{
		that.file_bubble_timer = setTimeout(that.HideFileBubbleAtRow, 1500);
	};
	
	this.ClearFileBubbleTimer=function()
	{
		clearTimeout(that.file_bubble_timer);
	};
	
	this.StartAssignedToBubbleTimer=function()
	{
		that.assigned_to_bubble_timer = setTimeout(that.HideAssignedToBubbleAtRow, 1500);
	};
	
	this.ClearAssignedToBubbleTimer=function()
	{
		clearTimeout(that.assigned_to_bubble_timer);
	};
	
	this.ViewFile=function(file_id)
	{
		window.open('/getfile.php?file_id='+file_id+'&size=original','mywindow','width=600,height=600');
	};
	
	this.DownloadFile=function(file_id)
	{
		window.location = '/download_file.php?file_id='+file_id;
	};
	
	this.HandleKeyboard=function()
	{	
		shortcut.add('Up',
			function()
			{
				var sel_row = that.outliner.GetSelectedRow();
				if (that.keyboard && sel_row != null)
				{
					prev_row = that.outliner.FindPreviousRow(sel_row);
					if (prev_row)
						that.SelectRow(prev_row);
				}
			},
			{'type':'keydown','disable_in_input':true}
		);
		
		shortcut.add('Down',
			function()
			{
				var sel_row = that.outliner.GetSelectedRow();
				if (that.keyboard && sel_row != null)
				{
					next_row = that.outliner.FindNextRow(sel_row);
					if (next_row)
						that.SelectRow(next_row);
				}
			},
			{'type':'keydown','disable_in_input':true}
		);
		
		shortcut.add('Left',
			function()
			{
			var sel_row = that.outliner.GetSelectedRow();
				if (that.keyboard && sel_row != null)
					that.CollapseRow(sel_row);
			},
			{'type':'keydown','disable_in_input':true}
		);
		
		shortcut.add('Right',
			function()
			{
			var sel_row = that.outliner.GetSelectedRow();
				if (that.keyboard && sel_row != null)
					that.ExpandRow(sel_row);
			},
			{'type':'keydown','disable_in_input':true}
		);
	}
		
	this.GetOPML=function()
	{
		that.sync.CreateRequest('getAllRows');		// creates asynchronous API call
	};
	
	this.Init(key, outline_id, keyboard, outliner_div, browser, user_agent);
}