$(document).ready(function(){
	ad_loadTreeScripts();
});


// load tree scripts 
function ad_loadTreeScripts(){
	ad_treeClicks();	
}



// setting the functions for clicking the tree
function ad_treeClicks(){
	$('.adTreeItem').click(function(){

		// remove all instances of on states
		$('.adTreeOn').each(function(){ $(this).removeClass('adTreeOn'); });																
		
		// add class to item
		$(this).addClass('adTreeOn');
		
		// update the edit window
		var theId = $(this).attr('id').replace('adTree','');
		
		editItems = ad_treeEditItems();
		argItems = ad_treeArgItems();
		ad_loaderBlocker('up');
		$.post('?adEditWindow=update', { guid:theId, editItems:editItems, args:argItems }, function(data){
			$('.adSystemEditWindow').empty().append(data);
			ad_loadScripts();
			ad_loaderBlocker('down');
		});		
	});
	
	// return to top level
	$('.adTreeTop').click(function(){
		$('.adTreeOn').each(function(){ $(this).removeClass('adTreeOn'); });
		$(this).addClass('adTreeOn');
		editItems = ad_treeEditItems();
		argItems = ad_treeArgItems();
		ad_loaderBlocker('up');
		$.post('?adEditWindow=update', { editItems:editItems, args:argItems }, function(data){
			$('.adSystemEditWindow').empty().append(data);
			ad_loadScripts();
			ad_loaderBlocker('down');
		});		
	});
	
	// search controls
	$('.adSystemSearchForm').submit(function(){
		ad_loaderBlocker('up');
		var searchTerm = $(this).children('.adSystemSearchInput').val();
		$('.adSystemTree .adSystemSearch .adSystemSearchClear').css('display','inline-block');
		var theClass = $('.adSystemTreeHolder').attr('class').replace('adSystemTreeHolder ','');
		$('.adSystemTreeHolder').removeClass(theClass).addClass(theClass+'Clear');
		debug(theClass);
		
		editItems = ad_treeEditItems();
		argItems = ad_treeArgItems();

		debug(searchTerm);
		$.post('?adEditWindow=update', { editItems:editItems, args:argItems, searchData:searchTerm }, function(data){
			$('.adSystemEditWindow').empty().append(data);
			ad_loadScripts();
			ad_loaderBlocker('down');
		});		
		return false;	
	});
	
	// clear the search and return to root directory
	$('.adSystemSearchClear').click(function(){
		ad_loaderBlocker('up');
		$('.adSystemSearchInput').val('');
		$('.adSystemTree .adSystemSearch .adSystemSearchClear').hide();
		var theClass = $('.adSystemTreeHolder').attr('class').replace('adSystemTreeHolder ','');
		var newClass = theClass.replace('Clear','');
		$('.adSystemTreeHolder').removeClass(theClass).addClass(newClass);
		
		editItems = ad_treeEditItems();
		argItems = ad_treeArgItems();

		$.post('?adEditWindow=update', { editItems:editItems, args:argItems }, function(data){
			$('.adSystemEditWindow').empty().append(data);
			ad_loadScripts();
			ad_loaderBlocker('down');
		});		
		
		return false;	
	});
}
// breaks up the details for tree posting
function ad_treeEditItems() {
	var editItems = '';
	$('.adSystemTreeArgs ul.adSystemTreeArgsEditItems li').each(function(){
		theClass = $(this).attr('class').replace('adTreeArg_','');
		theVal = $(this).text();
		editItems += theClass+'='+theVal+'|||';
	});
	return editItems;
}
function ad_treeArgItems() {
	var argItems = '';
	$('.adSystemTreeArgs ul.adSystemTreeArgsArgs li').each(function(){
		theClass = $(this).attr('class').replace('adTreeArg_','');
		theVal = $(this).text();
		argItems += theClass+'='+theVal+'|||';
	});
	return argItems;
}

// updates the tree on state
function ad_updateTreeClass(theId) {
	$('.adSystemTreeHolder .adTreeOn').each(function(){ $(this).removeClass('adTreeOn'); });
	$('#adTree'+theId).addClass('adTreeOn');
}











