var icoPlus = '/assets/scripts/editor2003/images/explorer/plus.gif';
var icoMinus = '/assets/scripts/editor2003/images/explorer/minus.gif';
var icoShut = '/assets/scripts/editor2003/images/explorer/shut.gif';
var icoOpen = '/assets/scripts/editor2003/images/explorer/open.gif';
var Current = false;
var CurrentOpenFolder = false;
var CurrentNode = false;
var directory = '';

var sRollOverColor = '#FFFF99';
var sRollOverTextColor = 'infotext';
var sRollOutColor = 'window';
var sRollOutTextColor = 'windowtext';
var sHighlightColor = 'highlight';
var sHighlightTextColor = 'highlighttext';

var bInputChecked = false;
var newFolderCount = 0;

var selectedFiles = 0;

function SwitchView()
{
	CurrentNode.onclick();
}
function SetFileCount(intCount)
{
	try{
		eval('window.parent.fileForm.FileCount').value = 'Files: '+intCount;
	}catch(e){
		
	}
}
function setCurrentNode(a)
{
	CurrentNode = a;
}
function NewFolder()
{
	var element = CurrentNode;
	
	if(newFolderCount>0)
	{
		alert('Please save new folder before creating a new one');
		return;
	}
	
	while(element)
	{
		//alert(element.nodeName);
		if(element.nodeName=='LI')
		{			
			break;
		}
		element = element.parentNode;
	}
	if(element.nodeName!='LI')
	{			
		return;
	}
	else
	{
		//Copy the node as a template
		var li = element.cloneNode(true);
		//remove any sub lists
		var uls = li.getElementsByTagName('UL');
		for(var i=0;i<uls.length;i++)
		{
			li.removeChild(uls[i]);
		}
		li.firstChild.firstChild.firstChild.src='/assets/scripts/editor2003/images/explorer/spacer.gif';
		li.firstChild.firstChild.childNodes[1].src=icoShut;//FolderIcon
		li.childNodes[1].firstChild.style.fontWeight = 'normal';//Text
		
		var input = document.createElement('INPUT');
		var Curr = new String(eval('fileForm.CurrentDirectory').value);
		Curr = Curr.split(': ')[1];
		var name = Curr+'¬'+newFolderCount;
				
		input.setAttribute('className','newFolder');
		//input.setAttribute('name','newFolder['+newFolderCount+']');
		input.value = 'New Folder';
		li.childNodes[1].replaceNode(input);
		
		var input = document.createElement('INPUT');
		input.setAttribute('className','newFolderSubmit');
		input.setAttribute('type','submit');
		input.name = 'SaveNewFolders';
		input.value = 'Save';		
		li.appendChild(input);
		
		
		element.appendChild(li);
		
		//element.parentNode.childNodes[1].focus();
		li.childNodes[1].select();
		li.childNodes[1].setAttribute('name','newFolder['+name+']');
		
		//alert(li.childNodes[1].outerHTML);
		newFolderCount++;
	}
	
	//var li = ul.cloneNode(ul.childNodes[0]);
	//alert(li.outerHTML);
	//CurrentNode.appendChild(li);
}
function SelectAll(fieldNameNeedle,checked)
{
	var input = document.getElementById('actionFiles');
	var sFile = '';
	var fieldNameNeedle = new String(fieldNameNeedle);
	var len = fieldNameNeedle.length;
	
	var fields = document.getElementsByTagName('INPUT');
	for(var i=0;i<fields.length;i++)
	{
		var f = fields[i];
		if(f.getAttribute('type')=='checkbox' && f.name.substr(0,len)==fieldNameNeedle)
		{
			if(f.checked!=checked)
			{
				f.checked = checked;
				var tr = f.parentNode.parentNode;
				trClick(tr);
			}
		}
	}
}
function SetActiveImagePreview(a)
{
	var input = document.getElementById('actionFiles');
	var sFile = '';
	
	switch(a.nodeName)
	{
		case 'TR':
		sFile = a.childNodes[2].firstChild.nodeValue;
		if(a.style.backgroundColor.toLowerCase()==sHighlightColor)
		{
			var sFiles = new String(';'+input.value+';');
			sFilelist = sFiles.split(';'+sFile+';').join(';');
			input.value = sFilelist+';';
			a.childNodes[0].firstChild.checked = false;
		}
		else
		{
			input.value+=sFile+';';
			a.childNodes[0].firstChild.checked = true;
		}
		break;
		
		default:
		switch(a.childNodes.length)
		{
			case 3:
			sFile = new String(a.getAttribute('title'));
			sFile = sFile.split('Name: ').join('');
			sFile = sFile.split("\n")[0];
			break;
		
			case 4:
			sFile = new String(a.getAttribute('title'));
			sFile = sFile.split('File: ')[1];
			sFile = sFile.split("\n")[0];
			
			//sFile = a.childNodes[3].nodeValue;
			break;
			
			default:
			alert('error');
			break;
		}
		
		if(a.style.borderColor=='red')
		{
			a.style.borderColor = 'silver';
			var sFiles = new String(';'+input.value+';');
			sFilelist = sFiles.split(';'+sFile+';').join(';');
			input.value = sFilelist+';';
			selectedFiles--;
		}
		else
		{
			a.style.borderColor = 'red';
			input.value+=sFile+';';			
			selectedFiles++;
		}
		var parentWindow = eval('window.parent');
		parentWindow.setSelectedFiles(selectedFiles);
		break;
	}
	input.value = input.value.split(';;').join('');
}

function openLicencePopup(accountID)
{
	window.open("/assets/popups/addLicence.php?accountID="+accountID,null,"height=450,width=460,status=yes,toolbar=no,menubar=no,location=no");

}
function NodeClick(a,override)
{            	
	//alert(a.parentNode.parentNode.childNodes.length);
	var intChild = 3;
	try{
		a.firstChild.setAttribute('src',icoPlus);     
		//alert(a.parentNode.parentNode.childNodes.length);
    	if(a.parentNode.parentNode.childNodes.length==intChild)
    	{
    		if(a.parentNode.parentNode.childNodes[intChild-1].style.display=='none')
    		{
    			a.parentNode.parentNode.childNodes[intChild-1].style.display = 'block';            			
    			a.firstChild.setAttribute('src',icoMinus);
    			a.childNodes[1].setAttribute('src',icoOpen);
    			//a.childNodes[0].nodeValue = "-";
    		}
    		else
    		{
    			a.parentNode.parentNode.childNodes[intChild-1].style.display = 'none';
    			a.firstChild.setAttribute('src',icoPlus);
    			a.childNodes[1].setAttribute('src',icoShut);
    		}
    	}
	}catch(e){
	}
}
function InitialiseTree()
{
	try{
		var div = eval('ImgDirectoryListing');
		var nodes = div.getElementsByTagName('UL');
		
		
		
		
		for(var i=nodes.length-1;i>0;i--)
		{
			nodes[i].style.display = 'none';
			/*
			var As = nodes[i].parentNode.getElementsByTagName('A');
			var href = new String(As[0].getAttribute('href'));
			href = href.split('dir=')[1];
			var autoOpenSplit = autoOpen.split('/');
			for(var j=0;j<autoOpenSplit.length;j++)
			{
				if(href=='/'+autoOpenSplit[j])
				{
					nodes[i].style.display = 'block';            				
					break;
				}	
				else
				{
					nodes[i].style.display = 'none';            		
				}
			}
			*/
		}
		
		//Images
		var nodes = div.getElementsByTagName('LI');
		for(var i=0;i<nodes.length;i++)
		{       
			var div1 = document.createElement('DIV');
			div1.style.display = 'inline';
			div1.setAttribute('className','AlignMiddle');
			var a = document.createElement('A');
			a.setAttribute('href','javascript:void(0)');
			a.onclick = function(){NodeClick(this);}
			var img = document.createElement('IMG');            		
			if(nodes[i].childNodes.length>1){img.setAttribute('src',icoPlus);}
			else{img.setAttribute('src','/assets/scripts/editor2003/images/explorer/spacer.gif');}
			img.setAttribute('width','15');
			img.setAttribute('height','17');
			img.setAttribute('border','0');
			a.appendChild(img);
			
			var img = document.createElement('IMG');            		
			img.setAttribute('src',icoShut);
			img.setAttribute('width','20');
			img.setAttribute('height','17');
			img.setAttribute('border','0');
			a.appendChild(img);
			
			div1.appendChild(a);
			
			var a = nodes[i].firstChild.firstChild;            		
			a.onmouseover = function(){window.status = this.getAttribute('href');return true;}
			
			//Set userfiles as active by default
			if(nodes[i].firstChild.firstChild.firstChild.nodeValue=='userfiles' && nodes[i].parentNode.parentNode.nodeName!='LI')
			{
				a.style.fontWeight = 'bold';
				eval('fileForm.CurrentDirectory').value = 'Directory: '+a.getAttribute('href').split('dir=')[1];			
				setCurrentNode(a);
			}
			
			
			a.onclick = function()
			{
				setCurrentNode(this);
				intChild = 3;
				if(this.parentNode.parentNode.childNodes.length==intChild)
				{
					this.parentNode.parentNode.childNodes[intChild-1].style.display = 'block';            			
					this.parentNode.parentNode.firstChild.firstChild.firstChild.setAttribute('src',icoMinus);
					this.parentNode.parentNode.firstChild.firstChild.childNodes[1].setAttribute('src',icoOpen);            				      
				}
				else
				{
					if(CurrentOpenFolder)
					{
						CurrentOpenFolder.setAttribute('src',icoShut);
					}
					CurrentOpenFolder = this.parentNode.parentNode.firstChild.firstChild.childNodes[1];
					CurrentOpenFolder.setAttribute('src',icoOpen);            				      
				}
				eval('fileForm.CurrentDirectory').value = 'Directory: '+this.getAttribute('href').split('dir=')[1];
				
				//var div = eval('ImgDirectoryListing');
				var nodes = div.getElementsByTagName('A');
				for(var i=nodes.length-1;i>0;i--)
				{
					if(nodes[i]==this)
					{
						this.style.fontWeight = 'bold';
					}
					else
					{
						nodes[i].style.fontWeight = '';            		
					}
				}
				
			//	document.getElementById('ImgPickerFrame').location = this.getAttribute('href');
			
				var sViewVars = 'List';
				var oViewType = eval('viewType');
				for(var i=0;i<oViewType.childNodes.length;i++)
				{
					try
					{
						if(oViewType.childNodes[i].checked)
						{
							sViewVars = oViewType.childNodes[i].value;
							break;
						}
					}
					catch(e)
					{
					}
				}
			
				//alert(sViewVars);
				parent.ImgPickerFrame.location = this.getAttribute('href')+'&amp;fileView='+sViewVars;
			//	parent.ImgPickerFormFrame.location = this.getAttribute('href');
				return false;
			}
			//a.setAttribute('href','javascript:void(0)');
			
			
			nodes[i].insertBefore(div1,nodes[i].firstChild);
			
		}
		
		var nodes = div.getElementsByTagName('UL');
		for(var i=nodes.length-1;i>0;i--)
		{
			var As = nodes[i].parentNode.getElementsByTagName('A');
			
			//alert(As[1].outerHTML);
			
			var href = new String(As[1].getAttribute('href'));
			href = href.split('dir=')[1];
			
			var path = '';
			var autoOpenSplit = autoOpen.split('/');
			for(var j=1;j<autoOpenSplit.length;j++)
			{			
				path+='/'+autoOpenSplit[j];
				if(href==path)
				{
					//alert(href+' = '+path);
					As = nodes[i].parentNode.getElementsByTagName('A');
					As[0].firstChild.src = icoMinus;
					nodes[i].style.display = 'block';
				}
				else
				{
					//alert(href+' != '+path);
				}
			}
		}
	}catch(e){
		
	}
}
function InitialiseFolderList()
{
	try{
		var div = eval('FolderDirectoryListing');
		var nodes = div.getElementsByTagName('UL');
		
		
		for(var i=nodes.length-1;i>0;i--)
		{
			nodes[i].style.display = 'none';            		
		}
		
		//Images
		var nodes = div.getElementsByTagName('LI');
		for(var i=0;i<nodes.length;i++)
		{       
			
			var div1 = document.createElement('DIV');
			div1.style.display = 'inline';
			div1.setAttribute('className','AlignMiddle');
			var a = document.createElement('A');
			a.setAttribute('href','javascript:void(0)');
			a.onclick = function(){NodeClick(this);}
			var img = document.createElement('IMG');            		
			if(nodes[i].childNodes.length>1){img.setAttribute('src',icoPlus);}
			else{img.setAttribute('src','/assets/scripts/editor2003/images/explorer/spacer.gif');}
			img.setAttribute('width','15');
			img.setAttribute('height','17');
			img.setAttribute('border','0');
			a.appendChild(img);
			
			var img = document.createElement('IMG');            		
			img.setAttribute('src',icoShut);
			img.setAttribute('width','20');
			img.setAttribute('height','17');
			img.setAttribute('border','0');
			a.appendChild(img);
			
			div1.appendChild(a);
			
			var a = nodes[i].firstChild.firstChild;            		
			a.onmouseover = function(){window.status = this.getAttribute('href');return true;}
			a.onclick = function()
			{
				intChild = 3;
				if(this.parentNode.parentNode.childNodes.length==intChild)
				{
					this.parentNode.parentNode.childNodes[intChild-1].style.display = 'block';            			
					this.parentNode.parentNode.firstChild.firstChild.firstChild.setAttribute('src',icoMinus);
					this.parentNode.parentNode.firstChild.firstChild.childNodes[1].setAttribute('src',icoOpen);            				      
				}
				else
				{
					if(CurrentOpenFolder)
					{
						CurrentOpenFolder.setAttribute('src',icoShut);
					}
					CurrentOpenFolder = this.parentNode.parentNode.firstChild.firstChild.childNodes[1];
					CurrentOpenFolder.setAttribute('src',icoOpen);            				      
				}
				document.getElementById('moveTo').value = this.getAttribute('href').split('dir=')[1];
				var nodes = div.getElementsByTagName('A');
				for(var i=nodes.length-1;i>0;i--)
				{
					if(nodes[i]==this)
					{
						this.style.fontWeight = 'bold';
					}
					else
					{
						nodes[i].style.fontWeight = '';            		
					}
				}
			//	document.getElementById('ImgPickerFrame').location = this.getAttribute('href');
			
			
				//parent.ImgPickerFrame.location = this.getAttribute('href')+'&amp;fileView='+sViewVars;
				return false;
			}
			//a.setAttribute('href','javascript:void(0)');
			
			
			nodes[i].insertBefore(div1,nodes[i].firstChild);
			
		}
	}catch(e){
		
	}
}
function trOver(tr)
{
	if(tr.style.backgroundColor.toLowerCase()!=sHighlightColor)
	{
		tr.style.backgroundColor = sRollOverColor;
		tr.style.color = sRollOverTextColor;
	}
}
function trOut(tr)
{
	if(tr.style.backgroundColor.toLowerCase()!=sHighlightColor)
	{
		tr.style.backgroundColor = sRollOutColor;
		tr.style.color = sRollOutTextColor;
	}
}
function trClick(tr)
{
	SetActiveImagePreview(tr);
	if(tr.style.backgroundColor.toLowerCase()==sHighlightColor)
	{
		tr.style.backgroundColor = sRollOutColor;
		tr.style.color = sRollOutTextColor;
		selectedFiles--;		
	}
	else
	{	
		tr.style.backgroundColor = sHighlightColor;
		tr.style.color = sHighlightTextColor;
		selectedFiles++;
	}
	//var Toolbar = eval('window.parent.Toolbar');
	var parentWindow = eval('window.parent');
	//Toolbar.childNodes[1].firstChild.nodeValue = ' '+selectedFiles+' ';
	parentWindow.setSelectedFiles(selectedFiles);
}
function setSelectedFiles(intVal)
{
	document.getElementById('selectedFileCount').firstChild.nodeValue = ' '+intVal+' '; 
}
function DeleteFiles()
{
	if(confirm('Are you sure you wish to delete these files?'))
	{
		var filePicker = eval('ImgPickerFrame.fileForm');
		eval('FolderPicker').style.display = 'none';
		filePicker.deleteFiles.value = 'true';
		filePicker.submit();
	}
}
function MoveFiles()
{
	var filePicker = eval('ImgPickerFrame.fileForm');
	eval('FolderPicker').style.display = 'none';
	filePicker.moveTo.value = document.getElementById('moveTo').value;
	filePicker.submit();
}
function DeleteFolder()
{
	var Curr = new String(eval('fileForm.CurrentDirectory').value);
	Curr = Curr.split(': ')[1];
	if(Curr=='')
	{
		return;
	}
	if(confirm('Are you sure you wish to delete folder '+Curr+'? This will also remove any files in it.'))
	{
		eval('fileForm').submit();
		//alert('TODO: Delete folder');
	}
}
function thMouseDown(th)
{
	th.style.filter = 'progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=#E2E2E4,endColorStr=#F7F8FC)';
}
function thMouseOut(th)
{
	th.style.filter = 'progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=#F7F8FC,endColorStr=#E2E2E4)';
}
function SortRows(th)
{	
	var rowParent = th.parentNode.parentNode;
	
	//Find the column of the <th>
	var childs = rowParent.childNodes[0].childNodes;
	for(var colIndex=0;colIndex<childs.length;colIndex++)
	{
		if(childs[colIndex]==th)
		{
			break;
		}	
	}
	
	var aRows = new Array();
	var rows = rowParent.childNodes;
	for(var row=1;row<rows.length;row++)
	{
		//switch to cope with filesize
		switch(colIndex)
		{
			case 3:
			var value = rows[row].childNodes[colIndex].getAttribute('title');
			break;
			
			default:
			var value = new String(GetValue(rows[row].childNodes[colIndex]));
			break;
		}
		aRows[row] = value.toLowerCase()+'¬'+row;
		window.status = 'Affirming slight exaggeration quotient: '+Math.round((row/rows.length)*100)+'%';
	}
	window.status = '100%';
		
	switch(colIndex)
	{
		case 5:
		case 6:
		case 3:
		notNumberCounter = 0;
		numberCounter = 0;
		aRows = aRows.sort(compareNum);
		break;
		
		default:
		aRows = aRows.sort();
	}
	
	var newRowParent = document.createElement(rowParent.nodeName);
	var tr = th.parentNode.cloneNode(true);
	newRowParent.appendChild(tr);
	
	for(var row=0;row<aRows.length;row++)
	{
		try
		{
			var val = new String(aRows[row]);		
			var newTd = rows[val.split('¬')[1]].cloneNode(true);
			newRowParent.appendChild(newTd);
		}catch(e){
		}
		window.status = 'Randomizing the sorting rules: '+Math.round((row/aRows.length)*100)+'%';
	}
	window.status = 'Finished: 100%';
	//alert(newRowParent.outerHTML);
	rowParent.parentNode.replaceChild(newRowParent,rowParent);
}


var notNumberCounter;
var numberCounter;
function compareNum(a,b)
{
	notNumberCounter = 0;
	try
	{
		a = new String(a);
		b = new String(b);
	
		a = a.split('¬')[0];
		b = b.split('¬')[0];
	
		
		if(parseFloat(a)==a && parseFloat(b)==b)
		{
			if(parseFloat(a)>parseFloat(b))
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
		else
		{
			return 100000+notNumberCounter++;
		}
	}
	catch(e)
	{
		return 0;
	}
	//}
}


function alertArray(arr)
{
	var msg = '';
	for(var i=0;i<arr.length;i++)
	{
		msg+=i+' => '+arr[i]+'\n';
	}
	alert(msg);
}

function GetValue(node)
{
	var sGetValue = '';
	if(node.nodeType==1)
	{
		childNodes = node.childNodes;
		for(var i=0;i<childNodes.length;i++)
		{
			sGetValue+=GetValue(childNodes[i]);
		}
	}
	else
	{
		sGetValue+=node.nodeValue;
	}
	
	return sGetValue;
}

function GetCurrentFolderPath()
{
	var Curr = new String(eval('fileForm.CurrentDirectory').value);
	Curr = Curr.split(': ')[1];
	return Curr;
}

function ExpandWindow(Window,PixelWidth,PixelHeight)
{
	document.getElementById(Window).style.height = PixelHeight;
	document.getElementById(Window).style.width = PixelWidth;
}
