function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function set_upload_dir(fpPath)  {


	var upload_path = fpPath.replace('/client/projects/', '');
	swfupload_settings.post_params = {'upload_path': upload_path };
	$('#current_path').html(upload_path);
	$('#project_path').val(upload_path);
}



if(jQuery) (function($){

	$.extend($.fn, {
		fileTree: function(o, h) {
			// Defaults
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '/';
			if( o.script == undefined ) o.script = 'jqueryFileTree.php';
			if( o.folderEvent == undefined ) o.folderEvent = 'click';
			if( o.expandSpeed == undefined ) o.expandSpeed= 500;
			if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
			if( o.expandEasing == undefined ) o.expandEasing = null;
			if( o.collapseEasing == undefined ) o.collapseEasing = null;
			if( o.multiFolder == undefined ) o.multiFolder = true;
			if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';

			$(this).each( function() {
// 				console.log($(this).attr('id'));
				
				function showTree(c, t) {
					$(c).addClass('wait');
					$(".jqueryFileTree.start").remove();
					$.post(o.script, { dir: t }, function(data) {
						$(c).find('.start').html('');
						$(c).removeClass('wait').append(data);
						if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
						bindTree(c);
					});
				}

				function bindTree(t) {
					$(t).find('LI A').bind(o.folderEvent, function() {

						if( $(this).parent().hasClass('directory')) {
							if( $(this).parent().hasClass('collapsed') ) {

								// Expand
								if( !o.multiFolder ) {
									$(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
									$(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
								}
								
								$(this).parent().find('UL').remove(); // cleanup
								showTree( $(this).parent(), escape($(this).attr('rel').match( /.*\// )) );
								$(this).parent().removeClass('collapsed').addClass('expanded');

// 								console.log($(this).parent().attr('id');

								
							} else {
								// Collapse
								$(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
								$(this).parent().removeClass('expanded').addClass('collapsed');
							}

							var fpPath = $(this).attr('href') +'/';

 							set_upload_dir(fpPath);


//  							$(this).child().trigger('click');

						} else {
							h = $(this).attr('href');
// 							return true;
						}
						return false;

					});

					// Prevent A from triggering the # on non-click events
					if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });
				}
				// Loading message
				$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
				// Get the initial file list
				showTree( $(this), escape(o.root) );
			});
		}
	});

})(jQuery);


function popUp(url)  {
	newwindow = window.open(url, 'popUp', config='height=500, width=500, toolbar=no, menubar=no, scrollbars=1, resizable=no, location=no, directories=no, status=no, left=300, top=150');
	if (window.focus) {newwindow.focus()}
}


function remove_file(e, p)  {

	c = confirm("You are about to delete this file\r\n\r\nAre you sure you want to do this?");

	if (c)  {

// 		alert(e +" "+ p);

		var url = 'ajax.php';
		var qs = 'op=remove_file&file='+ p;

  		$.ajax({
			type: 'POST',
			url: url,
			data: qs,
			start: function()  {
				$(e).css('opacity', 0.4);
			},
			success: function(responseText){
				var r = eval('('+ responseText +')');

				if (r.status == 'success')  {
					$(e).fadeOut().remove();
				}  else  {
					$(e).css('background', 'red');
				}
				
    				
			}
		});
	}
return false;
}



function remove_dir(e, p)  {

	c = confirm("You are about to delete this directory and all it\'s contents.\r\n\r\nAre you sure you want to do this?");

	if (c)  {

		
		var url = 'ajax.php';
		var qs = 'op=remove_dir&dir='+ p;

  		$.ajax({
			type: 'POST',
			url: url,
			data: qs,
			start: function()  {
				$(e).css('opacity', 0.4);
			},
			success: function(responseText){
				var r = eval('('+ responseText +')');

				if (r.status == 'success')  {

					p = p +'/';

					$('.directory a').each(function(){
							
						var rel = $(this).attr('rel');

// 						console.log(rel +" "+ p);
						
						if (rel == p)  {
							$(this).parent('li').fadeOut().remove();
// 							alert($(this).parent('li').attr('id'));
						}
					});
// 					$(e).fadeOut().remove();
				}  else  {
					$(e).css('background', 'red');
				}
			}
		});
	}
return false;
}


function follow_link(l)  {

	url = 'http://www.fitzpatrickarchitects.com'+ l;

	window.open(l);
}