	$(document).ready(function()
	{

		$("#back-button").click(function(){
		if($(this).hasClass('enabled'))
		{
			$('.popup-menu').hide();
			$.post("/files/go_to_parent", {name:'..'},function(data){
				$("#file_view").html(data);
				$.post("/files/current_path",function(data){
					$("#location").html(data);
				});
			}, "html");
		}
		
	});
	$("#create_folder").dialog({
		autoOpen:false,
		width:400,
		height:130,
		modal:true,
		resizable:false,
		draggable:false,
		title:"Create Folder",
		closeOnEscape:true,
		buttons:{
			'Create':function(event){
				var folder_name = $.trim($("#folder_name").val());
				if(folder_name.length>0)
				{
					$.post("/files/create_folder", {folder_name:folder_name}, function(data){
					$("#file_view").html(data);
					}, "html");
				}
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	},
	close:function(){
		$("#folder_name").val('');
	}
	});

	$(".file-item").click(function(event){
		$('.popup-menu').hide();
		if($(this).children('span').hasClass('directory'))
		{
			$.post("/files/go_to_dir", {name:$(this).children('.enkey').html()},function(data){
			$("#file_view").html(data);
			$.post("/files/current_path",function(data){
				var tempr =  data;	
				var mypath;
				if(document.getElementById('mypath'))
				{
				var sp='';
				sp = tempr.split(':');
				mypath = sp[1];				
				document.getElementById('mypath').value=mypath;
				mytest();
				}	
				$("#location").html(data);
			});
		}, "html");
			
			event.stopPropagation();
		}
		else if($(this).children('span').hasClass('file'))
		{
			$(this).attr("href","/files/do_download/"+$(this).children('.enkey').html());
			event.stopPropagation();
		}
	});
	
	$("#check_all").click(function(){
		$("#myTable tbody").find(':checkbox').attr('checked',$(this).attr('checked'));
		$('.popup-menu').hide(); 
	});

	
	$("#create_folder").prev().removeClass('ui-widget-header').addClass('dialog-header');


	
	$("#delete_item").dialog({
		autoOpen:false,
		width:400,
		height:130,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Delete':function(event){
				$.post("/files/delete_item", {item_name:$("#delete_item").data('item_code')}, function(data){
				$("#file_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#delete_item").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	$("#delete_items").dialog({
		autoOpen:false,
		width:400,
		height:130,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Delete':function(event){
				$.post("/files/delete_items", {items:$("#delete_items").data('item_codes')}, function(data){
				$("#file_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#delete_items").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	$("#rename_item").dialog({
		autoOpen:false,
		width:400,
		height:130,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Rename':function(event){
				var rename_name = $.trim($("#rename_name").val());
				if(rename_name.length>0)
				{
					$.post("/files/rename_item", {item_name:$("#rename_item").data('item_code'),new_name:rename_name}, function(data){
					$("#file_view").html(data);
					}, "html");
				}
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	},
	close:function(){
		$("#rename_name").val('');
	}
	});
	
	$("#rename_item").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	$("#copy_item").dialog({
		autoOpen:false,
		width:400,
		height:300,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Copy':function(event){
				$.post("/files/copy_item", {item_name:$("#copy_item").data('item_code'),destination:$('.file-tree').data('destination')}, function(data){
				$("#file_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#copy_item").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	
	$("#copy_items").dialog({
		autoOpen:false,
		width:400,
		height:300,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Copy':function(event){
				$.post("/files/copy_items", {items:$("#copy_items").data('item_codes'),destination:$('.file-tree').data('destination')}, function(data){
				$("#file_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#copy_items").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	$("#move_item").dialog({
		autoOpen:false,
		width:400,
		height:300,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Move':function(event){
				$.post("/files/move_item", {item_name:$("#move_item").data('item_code'),destination:$('.file-tree').data('destination')}, function(data){
				$("#file_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#move_item").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	
	$("#move_items").dialog({
		autoOpen:false,
		width:400,
		height:300,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Move':function(event){
				$.post("/files/move_items", {items:$("#move_items").data('item_codes'),destination:$('.file-tree').data('destination')}, function(data){
				$("#file_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#move_items").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	/*var share_title = $("#share_title"),
	share_desc = $("#share_desc"),
	share_protect = $("#share_protect"),
	share_password = $("#share_password"),
	allFields = $([]).add(share_title).add(share_desc).add(share_protect).add(share_password),
	tips = $(".validateTips");*/
	
	var tips = $(".validateTips");
	
	function updateTips(t) {
		tips
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}
	
	
	function checkEmailIds(o,regexp,n) {
		var eids = o.val();
		eids = eids.split(',');
		for(var i=0;i<eids.length;i++)
		{
			if ( !( regexp.test( eids[i] ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			}
		}
		return true;

	}

	$("#share_item").dialog({
		autoOpen:false,
		width:400,
		height:350,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Share':function(event){
				var bValid = true;
				allFields.removeClass('ui-state-error');
		
				bValid = bValid && checkLength(share_title,"Title",3,100);
				bValid = bValid && checkLength(share_desc,"Description",0,100);
				if(share_protect.attr('checked'))
					bValid = bValid && checkLength(share_password,"Password",5,16);
		
				bValid = bValid && checkRegexp(share_title,/^([0-9a-z_ ])+$/i,"Title may consist of a-z, 0-9, underscores.");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				// bValid = bValid && checkRegexp(share_desc,/^([0-9a-z_ ])+$/,"Description may consist of a-z, 0-9, underscores.");
				if(share_protect.attr('checked'))
					bValid = bValid && checkRegexp(share_password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
				
				if (bValid) {
					$.post("/shares/share_item",
							{
								item_name:$("#share_item").data('item_code'),
								title:share_title.val(),
								desc:share_desc.val(),
								is_protected:share_protect.attr('checked'),
								password:share_password.val()
							}, function(data){
						$("#menu").tabs({selected:2});
					}, "html");
					
					$(this).dialog('close');
				}
		
				
			},
			'Share & close':function(event){
				var bValid = true;
				allFields.removeClass('ui-state-error');
		
				bValid = bValid && checkLength(share_title,"Title",3,16);
				bValid = bValid && checkLength(share_desc,"Description",0,100);
				if(share_protect.attr('checked'))
					bValid = bValid && checkLength(share_password,"Password",5,16);
		
				bValid = bValid && checkRegexp(share_title,/^([0-9a-z_ ])+$/i,"Title may consist of a-z, 0-9, underscores.");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				// bValid = bValid && checkRegexp(share_desc,/^([0-9a-z_ ])+$/,"Description may consist of a-z, 0-9, underscores.");
				if(share_protect.attr('checked'))
					bValid = bValid && checkRegexp(share_password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
				
				if (bValid) {
					$.post("/shares/share_item",
							{
								item_name:$("#share_item").data('item_code'),
								title:share_title.val(),
								desc:share_desc.val(),
								is_protected:share_protect.attr('checked'),
								password:share_password.val()
							}, function(data){
					}, "html");
					
					$(this).dialog('close');
				}
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
			tips.html('');
		}

	});
	$("#share_item").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	$("#share_protect").click(function(){
		if($(this).attr('checked'))
		{
			$("#share_password_cont").show();
		}
		else
		{
			$("#share_password_cont").hide();
		}
	});
	
	$("#share_delete_item").dialog({
		autoOpen:false,
		width:400,
		height:130,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Delete':function(event){
				$("#share_view").html('<p style="width:100%; height:100%;"><img src="/images/ajax-loading.gif"/></p>');
				$.post("/shares/delete_item", {short_code:$("#share_delete_item").data('short_code')}, function(data){
				$("#share_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#share_delete_item").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	$("#share_delete_items").dialog({
		autoOpen:false,
		width:400,
		height:130,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Delete':function(event){
				$("#share_view").html('<p style="width:100%; height:100%;"><img src="/images/ajax-loading.gif"/></p>');
				$.post("/shares/delete_items", {items:$("#share_delete_items").data('item_codes')}, function(data){
				$("#share_view").html(data);
				}, "html");
				
				$(this).dialog('close');
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
	}
	});
	$("#share_delete_items").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	var email_ids = $("#email_ids"),
	email_message = $("#email_message"),
	mailFields = $([]).add(email_ids).add(email_message);

	$("#send_mail").dialog({
		autoOpen:false,
		width:400,
		height:250,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Send':function(event){
				var bValid = true;
				mailFields.removeClass('ui-state-error');
		
				bValid = bValid && checkLength(email_ids,"Email",6,100);
				bValid = bValid && checkLength(email_message,"Message",0,500);
				
				bValid = bValid && checkRegexp(email_ids,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. name@domain.com");

				if (bValid) {
					$.post("/shares/send_mail",
							{
								email_ids:email_ids.val(),
								email_message:email_message.val(),
								short_codes:$("#send_mail").data('item_codes')
							}, function(data){
					}, "html");
					
					$(this).dialog('close');
				}
		
				
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
		},
		close: function() {
			mailFields.val('').removeClass('ui-state-error');
			tips.html('');
		}

	});
	$("#send_mail").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	$("#share_this_file").dialog({
		autoOpen:false,
		width:400,
		height:230,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'OK':function(event){
					$(this).dialog('close');
			}
		}
	});
	
	$("#share_this_file").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	var email_ids = $("#email_ids"),
	mailFields = $([]).add(email_ids);
	
	$("#email_this_file").dialog({
		autoOpen:false,
		width:400,
		height:230,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Send':function(event){
				var bValid = true;
				var short_code = $("#short_code");
				mailFields.removeClass('ui-state-error');
				bValid = bValid && checkLength(email_ids,"Email",6,100);

				bValid = bValid && checkEmailIds(email_ids,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. name@domain.com");

				if (bValid) {
					$.post("http://pwnpics.com/mail_proxy_wikiupload.php",
							{
								email_ids:email_ids.val(),
								short_codes:short_code.val()
							}, function(data){
					}, "html");
					
					$(this).dialog('close');
				}
		
				
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
		},
		close: function() {
			mailFields.val('').removeClass('ui-state-error');
			tips.html('');
		}
	});
	
	$("#email_this_file").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	$("#edit_this_file").dialog({
		autoOpen:false,
		width:400,
		height:260,
		modal:true,
		resizable:false,
		draggable:false,
		closeOnEscape:true,
		buttons:{
			'Save':function(event){
				var bValid = true;
				var short_code = $("#short_code");
				var edit_title = $("#edit_title"),
				edit_desc = $("#edit_desc"),
				editFields = $([]).add(edit_title).add(edit_desc);
				editFields.removeClass('ui-state-error');
		
				bValid = bValid && checkLength(edit_title,"Title",3,16);
				bValid = bValid && checkLength(edit_desc,"Description",0,100);
				bValid = bValid && checkRegexp(edit_title,/^([0-9a-z_. ])+$/i,"Title may consist of a-z, 0-9, underscores.");
				
				if (bValid) {
					$.post("/shares/edit_item",
							{
								short_code:short_code.val(),
								title:edit_title.val(),
								desc:edit_desc.val()
							}, function(data){
					}, "html");
					
					$(this).dialog('close');
				}
			},
			Cancel: function(event){
				$(this).dialog('close');
			}
		},
		close: function() {
			tips.html('');
			editFields.val('').removeClass('ui-state-error');// not working..since out of scope.
		}
	});
	
	$("#edit_this_file").prev().removeClass('ui-widget-header').addClass('dialog-header');
	
	
	
	$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
		$('.file-tree').data('destination',file);
	});




	$("#folder-button").click(function(event){	
		$('.popup-menu').hide();
		$("#create_folder").dialog('open');
		$("#folder_name").focus();
	});

	$("#more_menu").click(function(event){
		var tds = $("#myTable tbody").find('td.selected');
		tds.first().css('border-left', '1px solid #fff');
		tds.last().css('border-right', '1px solid #fff');
		tds.css({'background-color': '#fff','border-top': '1px solid #fff','border-bottom': '1px solid #fff'});
		tds.removeClass('selected').css({'background-color': '#fff','border-top': '1px solid #fff','border-bottom': '1px solid #fff'});
		$('.popup-menu').hide();
		
		var x = $(this).position().left;
		var y = $(this).position().top+31;
		
		var items = $("#myTable tbody").find('input:checked'); 
		
		if(items.length>1)
		{
			$('#menu_for_multi').css({'left':x, 'top':y});
			$('#menu_for_multi ul li .for-file').show();
			if(items.parent().parent().find('span').hasClass('directory'))
			{
				$('#menu_for_multi ul li .for-file').hide();
			}
			$('#menu_for_multi').show();
		}
		else if(items.length==1)
		{
			if(items.parent().next().children().children('span').hasClass('file'))
			{
				$('#menu_for_file').css({'left':x, 'top':y});
				$('#menu_for_file').show();
			}
			else
			{
				$('#menu_for_folder').css({'left':x, 'top':y});
				$('#menu_for_folder').show();
			}
			
		}
		else if(items.length==0)
		{
			$('#menu_for_none').css({'left':x, 'top':y});
			$('#menu_for_none').show();
		}
		
	});
	
	
	
	
$(".popup-menu-item").click(function(event){
		
		if($(this).hasClass('folder')) //folder
		{
			switch($(this).html())
			{
				case "Delete...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#delete_item").data('item_code', item_code);
					$("#delete_item").dialog({title:"Delete Folder"});
					$("#delete_item p").html('Are you sure you want to delete '+item_name+'?');
					$("#delete_item").dialog('open');
					break;
				case "Rename...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#rename_item").data('item_code', item_code);
					$("#rename_item").dialog({title:"Rename Folder"});
					$("#rename_item").dialog('open');
					$("#rename_name").val(item_name);
					$("#rename_name").select();
					break;
				case "Copy...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#copy_item").data('item_code', item_code);
					$("#copy_item").dialog({title:"Copy Folder"});
					$("#copy_item").dialog('open');
					$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
						$('.file-tree').data('destination',file);
					});
					break;
				case "Move...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#move_item").data('item_code', item_code);
					$("#move_item").dialog({title:"Move Folder"});
					$("#move_item").dialog('open');
					$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
						$('.file-tree').data('destination',file);
					});
					break;
				case "Download folder":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$(this).attr("href","/files/download_folder/"+item_code);
					
					break;
				default:
					$('.popup-menu').hide();
					alert('Not yet implemented');
					break;
			}
		}
		else if($(this).hasClass('file')) //file
		{
			switch($(this).html())
			{
				case "Delete...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#delete_item").data('item_code', item_code);
					$("#delete_item").dialog({title:"Delete File"});
					$("#delete_item p").html('Are you sure you want to delete '+item_name+'?');
					$("#delete_item").dialog('open');
					break;
				case "Rename...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#rename_item").data('item_code', item_code);
					$("#rename_item").dialog({title:"Rename File"});
					$("#rename_item").dialog('open');
					$("#rename_name").val(item_name);
					$("#rename_name").select();
					break;
				case "Copy...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#copy_item").data('item_code', item_code);
					$("#copy_item").dialog({title:"Copy File"});
					$("#copy_item").dialog('open');
					$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
						$('.file-tree').data('destination',file);
					});
					break;	
				case "Move...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#move_item").data('item_code', item_code);
					$("#move_item").dialog({title:"Move File"});
					$("#move_item").dialog('open');
					$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
						$('.file-tree').data('destination',file);
					});
					break;
				case "Download file":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$(this).attr("href","/files/do_download/"+item_code);
					break;
				case "Share this file":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#share_this_file").data('item_code', item_code);
					$("#share_this_file").dialog({title:"Share this file"});
					$("#share_this_file").dialog('open');
					$('.get-file-share-info').fileInfo({name:item_code, type:'Share'});
					break;
				case "Email this file":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#email_this_file").data('item_code', item_code);
					$("#email_this_file").dialog({title:"Email this file"});
					$("#email_this_file").dialog('open');
					$('.get-file-email-info').fileInfo({name:item_code, type:'Email'});
					break;
				case "Edit...":
					$('.popup-menu').hide();
					
					var item;
					var tds = $("#myTable tbody").find('td.selected');
					if(tds.length>0)
					{
						item = tds.first().next().children();
					}
					else
					{
						tds = $("#myTable tbody").find('input:checked');
						item = tds.parent().next().children();
					}
					
					var item_name = item.children('span.text').html();
					var item_code = item.children('span.enkey').html();
					
					$("#edit_this_file").data('item_code', item_code);
					$("#edit_this_file").dialog({title:"Edit File"});
					$("#edit_this_file").dialog('open');
					$('.get-file-edit-info').fileInfo({name:item_code, type:'Edit'});
					break;
				default:
					$('.popup-menu').hide();
					alert('Not yet implemented');
					break;
			}
		}
		else if($(this).hasClass('multi')) // for multi selection
		{
			switch($(this).html())
			{
				case "Delete items":
					$('.popup-menu').hide();
					
					var	tds = $("#myTable tbody").find('input:checked');
					var item_codes=[];
					tds.each(function(){
						var item = $(this).parent().next().children();
						var item_code = item.children('span.enkey').html();
						item_codes.push(item_code);
					}) ;
					
					$("#delete_items").data('item_codes', item_codes.join(','));
					$("#delete_items").dialog({title:"Delete items"});
					$("#delete_items p").html('Are you sure you want to delete '+tds.length+' items?');
					$("#delete_items").dialog('open');
					break;
				case "Copy items":
					$('.popup-menu').hide();
					
					var	tds = $("#myTable tbody").find('input:checked');
					var item_codes=[];
					tds.each(function(){
						var item = $(this).parent().next().children();
						var item_code = item.children('span.enkey').html();
						item_codes.push(item_code);
					}) ;
					
					$("#copy_items").data('item_codes', item_codes.join(','));
					$("#copy_items").dialog({title:"Copy items"});
					$("#copy_items").dialog('open');
					$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
						$('.file-tree').data('destination',file);
					});
					break;	
				case "Move items":
					$('.popup-menu').hide();
					
					var	tds = $("#myTable tbody").find('input:checked');
					var item_codes=[];
					tds.each(function(){
						var item = $(this).parent().next().children();
						var item_code = item.children('span.enkey').html();
						item_codes.push(item_code);
					}) ;
					
					$("#move_items").data('item_codes', item_codes.join(','));
					$("#move_items").dialog({title:"Move items"});
					$("#move_items").dialog('open');
					$('.file-tree').fileTree({root:'', script: '/files/get_dirs' }, function(file) { 
						$('.file-tree').data('destination',file);
					});
					break;
				default:
					$('.popup-menu').hide();
					alert('Not yet implemented');
					break;
			}
		}
		else
		{
			alert('Oops... Something went wrong...');
		}
	});

	$("#back-button").click(function(){
		if($(this).hasClass('enabled'))
		{
			$('.popup-menu').hide();
			$.post("/files/go_to_parent", {name:'..'},function(data){
				$("#file_view").html(data);
				$.post("/files/current_path",function(data){
					$("#location").html(data);
				});
			}, "html");
		}
		
	});

	
});

