	/**
	 * @author acoote
	 */
	
	function resizeDocumentWindow(){
	
		var height = window.getSize().y -
		$('pagecontent').getPosition().y -
		$('footer').getSize().y  -
		$('footer').getStyle("margin-top").toInt() - 
		$$('body')[0].getStyle("margin-bottom").toInt() -
		($('pagecontent').getStyle("padding-top").toInt() * 2); 
		
		$('pagecontent').setStyle("height",height - 30);
		
		$('documentsidebox').setStyle("height", height-$('documentsidebox').getPosition(($('pagecontent'))).y - 25);
		$('documentsidebox').getChildren().setStyle("height",$('documentsidebox').getStyle("height").toInt());
		$('boxholder').setStyle("width",$('pagecontent').getSize().x - ($('pagecontent').getStyle("padding-left").toInt() * 2) - 25);

		buttonwidth = Math.floor(($('topbuttons').getSize().x / 5) - 30);
		buttons = $('topbuttons').getChildren("li");
		
		for(x=0;x<buttons.length;x++) {
			//buttons[x].setStyle("width",buttonwidth );
			link = buttons[x].getElement("a");
				link.setStyles({
					"width": buttonwidth,
					"background-position": buttonwidth - 5 + "px center",
					"overflow": "hidden"
				});
				buttons[x].getElement("ul").setStyle("width", buttonwidth + 20);
			
		}
	}
	
	window.addEvent('resize',resizeDocAction);
var resizeDocTimer;
function resizeDocAction() {
	// This function standardises resize behaviour. In Mozilla, the resize event fires when the user
	// FINISHES resizing- in IE and Safari it keeps firing for every pixel the window is moved. Obviously
	// I don't want to fire the background resizer with every pixel, so I'm forcing it to wait until resizing
	// is complete, or at least the user stops resizing briefly.
	clearTimeout(resizeDocTimer);
	resizeDocTimer = setTimeout("resizeDocumentWindow()",100);
}
		
		var FilePane = new Class({
		
		initialize: function(json, owner, previous){
			this.json = json;
			this.owner = owner;
			this.pane = new Element("div", {
				"class": "fileUl",
				styles: {
					width: 290,
					padding: "10px"
				}
			});
			if (previous != null) {
				this.previousPane = previous;
				previous.nextPane = this;
				this.owner.setStyle("width", (this.previousPane.pane.getSize().x + this.previousPane.pane.getPosition(owner).x + 312));
				
			}
			
			this.pane.setStyles({
				"height": this.owner.getStyle("height"),
				"visibility": "visible"
			});
			
			this.owner.adopt(this.pane);
			
			if (this.json.items.length === 0) {
				this.pane.setStyle("background", "#FFFFFF url(/_images/large-loader.gif) no-repeat center center");
			
				var jsonVar = new Request.JSON({
					url:  this.getFullPath() + this.json.json,
					onSuccess: this.receiveJSON.bind(this)
				}).get();
			
			}
			else {
				this.createFileDetails();
			}
			this.owner.getParent().scrollTo(this.pane.getPosition(this.owner).x, 1);
		
		},
		
		receiveJSON: function(jsonObj) {
			for (x=0;x<jsonObj.items.length;x++) {
				this.json.items[this.json.items.length] = jsonObj.items[x];
			}
			jsonObj = null;
			this.createFileDetails();
		},
		imageNotLoad: function() {
			$('docbox').setStyle("background", "#FFFFFF url()");
		},
		createFileDetails: function() {
			
			this.pane.setStyle("background","");
			var title = new Element("h4");
			title.innerHTML = this.json.text;
			
			var thumb = new Element("div", {
				"id": "docbox"
			});
			thumb.set('morph', {duration: 100});
			var img = new Asset.image("/_dynamic/getthumb.aspx?path=" + this.json.truehref + "&pathto=" + this.getFullPath() + "&index=0&dpi=6&borderwidth=0&bordercolor=FFFFFF&highquality=true&defaultfont=", {
				onload: function(img){
					img.setStyle("opacity",0);
					img.injectInside($('docbox'));
					marginNum = 10;
					if ($('docbox').getSize().y - (img.getSize().y + 6)> 0) {
						marginNum = ($('docbox').getSize().y - img.getSize().y + 6) + 10;
					}
					$('docbox').morph({
						height: img.getSize().y + 4
					});
					var fx = new Fx.Morph(img).start({'opacity':1});
				}
			,
				onerror: function() {
			$('docbox').setStyle("background", "#FFFFFF url()");
		}
					});
			var viewLink = new Element("a", {
				href: this.getFullPath() + this.json.truehref,
				target: "_blank"
			});
			viewLink.innerHTML = "View PDF";
			/*var emailLink = new Element("a", {
				href: "javascript:alert('does not work yet')",
				target: "_blank"
			})
			emailLink.innerHTML = "Email document";*/
			
			var initialDetails = new Element("div", {
				styles: {
					"float": "left"
				}
			});
			viewLink.injectInside(initialDetails);
			initialDetails.innerHTML += "<br/><strong>Category:</strong><br/>";
			initialDetails.innerHTML += this.json.items[0].category;
			
			var moreDetails = new Element("div", {
				styles: {
					"clear": "both"
				}
			});
			moreDetails.innerHTML = "<strong>Description:</strong><br/>";
			moreDetails.innerHTML += this.json.items[0].description;
			title.injectInside(this.pane);
			thumb.injectInside(this.pane);
		//	viewLink.injectInside(this.pane);
			initialDetails.injectInside(this.pane);
			moreDetails.injectInside(this.pane);
			//emailLink.injectInside(this.pane);
		},
		getFullPath: function() {
			var returnstring = "";
			if (this.previousPane != null) {
				returnstring =  this.previousPane.getFullPath();
			}
			returnstring += this.json.path;
			return returnstring;
		},
		removeNextPanes: function() {
			if (this.nextPane != null) {
				this.nextPane.removeNextPanes();
				this.nextPane.destroy();
			}
			
		},
		
		destroy: function() {
			this.pane.dispose();
			if (this.previousPane != null) {
				this.previousPane.nextPane = null;
			}
			this.json = null;
			this.pane = null;
		}
		});
		
	
	
	var ItemButton = new Class({
	
		initialize: function(item) {
			this.li = new Element("li");
			this.json = item;
			this.textLength = item.text.length;
			if (item.type == "folder") {
				this.li.addClass("arrow");
			}
			this.a = new Element("a");
			this.setEvents(this.a);
			if(item.text.length > 50) {
				this.a.innerHTML= item.text.substr(0,50) + "...";
			} else {
				this.a.innerHTML= item.text;
			}
			
			this.a.title = item.text;
			//this.a.href = item.href;
			this.a.addClass(item.type);
			this.li.adopt(this.a);
		},
		
		setEvents: function(item) {
			item.removeEvents("click");
			item.addEvents({
					'mouseenter': function() {
						this.getParent().setStyle("background-color","#F7F7F2");
					},
					'mouseleave': function() {
						this.getParent().setStyle("background-color","");
					},
					'click': this.processClick.bind(this)
			});
		},
		
		insertInto: function(owner) {
			this.a.href = owner.getFullPath() + this.json.path + this.json.href;
			owner.pane.adopt(this.li);
			this.owner = owner;
		},
		
		isWrapping: function() {
			if (this.a.getScrollSize().y > this.a.getSize().y) {
				return true;
			} else {
				return false;
			}
		},
		processClick: function(event) {
			event.stop();
			event.target.blur();
			if (this.json.type == "folder") {
				this.createSubPane();
			} else if (this.json.type == "file") {
				this.createFilePane();
			} 
			this.owner.setActive(this);
		},
		
		createSubPane: function() {
			this.owner.removeNextPanes();
			var fPane = new FolderPane(this.json,this.owner.owner,this.owner);
		},
		
		createFilePane: function() {
			this.owner.removeNextPanes();
			var fPane = new FilePane(this.json,this.owner.owner,this.owner);
		},
		
		set: function(flag) {
			if (flag == "on") {
				this.a.fireEvent("mouseleave");
				this.li.addClass("on");
				this.a.removeEvents("mouseenter");
				this.a.removeEvents("mouseleave");
				this.a.removeEvents("click");
				this.a.addEvent("click", function(event) {
					event.stop();
					event.target.blur();
				});
				//this.owner.setActive(this);
			} else {
				this.li.removeClass("on");
				this.setEvents(this.a);
			}
		}
	});
	
	var EmptyFolderButton = new Class({
		initialize: function(item){
			this.li = new Element("li", {
				"class": "noitems"
			});
			this.li.innerHTML = "this folder is empty";
		},
		insertInto: function(owner){
			owner.pane.adopt(this.li);
			this.owner = owner;
		}
	});
	
	

var FolderPane = new Class({
		
		initialize: function(json,owner,previous) {
			this.json = json;
			this.owner = owner;
			this.pane = new Element("ul", {
				"class": "paneUl"
			});
			if (previous != null) {
				this.previousPane = previous;
				previous.nextPane = this;
				this.owner.setStyle("width",(this.previousPane.pane.getSize().x + this.previousPane.pane.getPosition(owner).x + 172));
	
			}
			
			
			this.pane.setStyles({
					"height":this.owner.getStyle("height"),
					"visibility":"visible"
				});
			
			this.owner.adopt(this.pane);
			if (this.json.items.length === 0) {
				this.pane.setStyle("background", "url(/_images/large-loader.gif) no-repeat center center");
				var jsonVar = new Request.JSON({
					url:  this.getFullPath() + this.json.json,
					onSuccess: this.receiveJSON.bind(this)
				}).get();
			}
			else {
				this.createItems();
			}
			this.owner.getParent().scrollTo(this.pane.getPosition(this.owner).x,0);
			/*while (this.owner.getParent().getScroll().y == 1){
				this.shrink();	
			}*/
			
	
		},
		insertInto: function(target) {
			$(target).adopt(this.pane);
		},
		shrink: function() {
			this.pane.setStyle("height",this.pane.getStyle("height").toInt() - 5);
			if (this.previousPane) {
				this.previousPane.shrink();
			}
		},
		receiveJSON: function(jsonObj) {
			for (x=0;x<jsonObj.items.length;x++) {
				this.json.items[this.json.items.length] = jsonObj.items[x];
			}
			jsonObj = null;
			this.createItems();
		},
		
		createItems: function() {
			this.pane.setStyle("background","");
			var longestItem = null;
			var item;
			if (this.json.items.length == 1) {
				item = new EmptyFolderButton();
				item.insertInto(this);
			}
			else {
				for (x = 0; x < this.json.items.length; x++) {
					// I had to make Net-It create a dummy object because it can't tell when it's reached the end of the file list
					// - so it'd put erroneous commas in.
					if (this.json.items[x].type != "dummy") {
						item = new ItemButton(this.json.items[x]);
						$(item.li).setStyle("visibility", "hidden");
						// We need to establish the biggest file so that we can resize the pane
						// accordingly
						if (longestItem === null || item.textLength > longestItem.textLength) {
							longestItem = item;
						}
						item.insertInto(this);
					}
					while (item.isWrapping()) {
					this.pane.setStyle("width", this.pane.getStyle("width").toInt() + 10);
					if (this.owner.getStyle("width") != "auto") {
						this.owner.setStyle("width", this.owner.getStyle("width").toInt() + 10);
					}
				}
				}
				
			}
			this.pane.getChildren().setStyle("visibility","visible");
		},
		
		getFullPath: function() {
			var returnstring = "";
			if (this.previousPane != null) {
				returnstring =  this.previousPane.getFullPath();
			}
			returnstring += this.json.path;
			return returnstring;
		},
		
		removeNextPanes: function() {
			if (this.nextPane != null) {
				this.nextPane.removeNextPanes();
				this.nextPane.destroy();
			}
			
		},
		setActive: function(item) {
			if (this.activeItem != null) {
				this.activeItem.set("off");
			}
			this.activeItem = item;
			this.activeItem.set("on");
		},
		
		destroy: function() {
			this.pane.dispose();
			if (this.previousPane != null) {
				this.previousPane.nextPane = null;
			}
			this.json = null;
			this.pane = null;
		}
		
	
	});


	window.addEvent('domready',function(){
		$('page').addClass("fullwidth");
		resizeDocumentWindow();
		
		// We need to encode the existing items into JSON- it saves an extra AJAX call
		// When we already have the data here anyway.
		
		var jsonString = "{";
		jsonString += "\"text\" : \"/\"";
		jsonString += ",\"href\" : \"\"";
		jsonString += ",\"path\" : \"\"";
		jsonString += ",\"items\": [";
		
		theLis = $('documentsidebox').getElement("ul").getElements("li");
		for (x=0;x<theLis.length;x++) {
			hrefString = String(theLis[x].getElement("a").href).replace("http://" + document.location.host,"");
			hrefSplit = hrefString.split("/");
			jsonString += "{";
			if (theLis[x].hasClass("folder")) {
				jsonString += "\"type\": \"folder\"";
				jsonString += ",\"json\": \"folder.json\"";
				jsonString += ",\"path\": \"" + hrefSplit[hrefSplit.length -2] + "/\"";
				jsonString += ",\"href\": \"\"";
			}
			else 
			if (theLis[x].hasClass("file")) {
				jsonString += "\"type\": \"file\"";
				jsonString += ",\"json\": \"file.json\"";
				jsonString += ",\"path\": \"" + hrefSplit[hrefSplit.length -2] + "/\"";
				jsonString += ",\"href\": \"info.htm\"";
				jsonString += ",\"truehref\": \"" + hrefSplit[hrefSplit.length -2]+ ".pdf\"";
			}
			
			jsonString += ",\"text\": \"" + theLis[x].getElement("a").innerHTML + "\"";
			jsonString += ",\"items\": []";
			jsonString += "}";
	
				jsonString += ",";
			
		}
		jsonString += "{\"type\": \"dummy\"}";
		jsonString += "] } ";
		$('documentsidebox').innerHTML = "";
		var firstPane = new FolderPane(JSON.decode(jsonString),$('documentsidebox'));
		//firstPane.insertInto();
		
		});
