var attempt_counter = 0;
var kupu_sorted = false;

function sort_kupu(){
	kupu_link_button = document.getElementById('kupu-linklibdrawer-button');
	if(kupu_link_button){//if this is a kupu page
		//alert('kupu page');
		//kupu_link_button.onmousedown = function(){sort_link_issue();};
		sort_link_issue();
		kupu_link_button.onmouseup = function(){ click_parent_folder();}; //put the event onmouse up so as to not conflict with any onclick stuff
	}
}


function click_parent_folder(){
	//alert('click_parent_folder() called');
	if(kupu_sorted) //if we've already done this, and the user is clicking on the internal link button for the 2nd time
		return;
	try{
		parent_folder_div = document.getElementById('kupu-resourceitems').getElementsByTagName('div')[0];
		parent_folder_div.onclick(); //if the 'parent folder' link IS there then click it to reveal the folder tree
		kupu_sorted = true;
	}
	catch(e){
		//alert(e);
		attempt_counter ++;
		if(attempt_counter<50)
			t = setTimeout("click_parent_folder();", 100); //wait 10th of a second and try again
	}

}


function sort_link_issue(){
	//If you can make this work you deserve a pay rise
	//Need to insert a space into the editor textarea so that when you create a link in IE before writing anything it doesn't put the link at the top of the page instead of in the editor
	var xzy = document.getElementById('kupu-editor-iframe-text');
	xzy.focus();
	}
	



window.onload = function(){ sort_kupu(); };
