
//Initialize the Treeview when the page loads
YAHOO.util.Event.addListener(window, "load", wl_init);

//GLOBAL VARIABLES

//Main Wishlist Tree Objects
var wl_tree;
var wl_edit_tree;

//Array to hold all the different panels
var wl_panel_list;

// Variables to keep track of the current 
// and previously open panels 
var wl_close_index;
var wl_prev_close_index;

//Tracks whether the system is in edit_mode
var wl_in_edit_mode;

//Main Z index of the panels (keep them above others)
var wl_z_index;

//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_object
*	Purpose:	A generic object so as to avoid conflict across the site
*
*	@param 	none
*/
function wl_object() {
	
	
}

//-------------------------------------------------------------------------------------------
/**
*	Function: 	wl_init
*	Purpose: 	Initialize the the differen panel, the in_edit_mode variable, and get the
*				the lists
*
*	@param 	none
*/
function wl_init() {
	
	//Initialize the panel list - edit mode settings
	if(wl_panel_list == null) {
		
		wl_panel_list = new Array();
		wl_panel_list['wl_view_panel'] = null;
		wl_panel_list['wl_edit_panel'] = null;
		
		wl_in_edit_mode = false;
		
		wl_z_index = 500;
		
	}
	
	//Get the main lists
	wl_get_lists();
}

//-------------------------------------------------------------------------------------------
/**
*	Function: 	wl_ajax_call
*	Purpose:	Generic function to make an AJAX call - simply give the post values and the parameters
*				for success
*
*	@param params	Object that holds all of the parameters for the call - post_data, and argument to pass to success
*/
function wl_ajax_call(params) {
	
	//Path to the wishlist AJAX processor
	var path = '/resources/ajax/_wishlist.one';
	
	//Make the AJAX call - pass through the parameters
	var request = YAHOO.util.Connect.asyncRequest('POST', path, {success: wl_ajax_success, failure:wl_ajax_fail, argument:params.args}, params.post_data);	
}

//-------------------------------------------------------------------------------------------
/**
*	Function: 	wl_ajax_success
*	Purpose:	Generic function to handle AJAX success - processes the response object and the params passed in from
*				the main function and generates output, calls extra functions, etc.
*
*	@param o	Response object from AJAX call - contains response and parameters for formatting output
*/
function wl_ajax_success(o) {

	//Get the parameter object passed in
	var params = o.argument;
	var results = new Array();
	
	//Check to see if response is JSON or plain text
	if(params.is_json) {
		
		//Get the json array of wishlists
		var json_string = o.responseText;		

		//Parse the json string to form the main array
		results = eval('(' + json_string + ')');		
	}
	else {
		
		//Plain Text - just set the message var
		results['message'] = o.responseText;
	}
	

	//Check to see if panel is needed
	if(params.show_panel) {		
		
		wl_z_index += 5;
		
		if(params.x_loc == null || params.y_loc == null) {
			
			//Get the current window width
			var myWidth = 0;

			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myWidth = window.innerWidth;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				myWidth = document.body.clientWidth;
			}	
			
			params.x_loc = (myWidth-500) / 2;
			params.y_loc = 50;
			
			scroll(0,0);
		}
		
		//Reset the panel to NULL	
		wl_panel_list[params.panel_to_display] = null;

		//Initialize the panel using passed in parameters
		if (wl_panel_list[params.panel_to_display] == null) {

			wl_panel_list[params.panel_to_display] = new YAHOO.widget.SimpleDialog(params.panel_to_display.toString(), { 
					width : params.panel_width,
					visible : false,
					draggable : false,
					fixedcenter : params.fixed_center,
					x: params.x_loc,
					y: params.y_loc,
					modal : true,
					close : true,
					zIndex : wl_z_index,
					buttons : params.buttons					
				}
			);		
		}	

		//Set the header from parameter and set body from response text 
		wl_panel_list[params.panel_to_display].setHeader(params.header);
		wl_panel_list[params.panel_to_display].setBody(unescape(results['message']));
		
		//Display the Panel
		wl_panel_list[params.panel_to_display].render(document.body);
		wl_panel_list[params.panel_to_display].show(); //Show the dialog box		
	}
	
	if(params.extra_function != null) {
		params.extra_function(o);
	}

}

//-------------------------------------------------------------------------------------------
/**
*	Function: 	wl_tree_init
*	Purpose:	To create a tree view object and display the nodes according to the 
*				user defined parameters - allows the choice of div, the action of the link,
*				and any other tree actions
*
*	@param	o		The response from the AJAX request (including user passed params)
*/
function wl_tree_init(o) {

	var params = o.argument;
	
	
	//Check for a custom action on links
	if(params.custom_action != null) {
		
		var node_action_function = params.custom_action;
	}
	else {
		
		var node_action_function = 'wl_display_wishlist_item';
	}
	
	//Check for a custom action on links
	if(params.target_div != null) {
		
		var target_div = params.target_div;
	}
	else {
		
		var target_div = 'tree-div';
	}

	//Get the json array of wishlists
	var json_string = o.responseText;	

	//Parse the json string to form the main array
	var wl_my_lists = eval('(' + json_string + ')');
	
	//Initialize the treeview object and the root
	wl_tree = new YAHOO.widget.TreeView(target_div);
	var root = wl_tree.getRoot();

	if(wl_my_lists.length == 0) {
		
		var my_obj = {html: 'No Lists Exist', id:-1} ;
		var tmp_node = new YAHOO.widget.HTMLNode(my_obj, root, false);	
		wl_tree.draw();
		return;
	}

	//Create the tree nodes
	for(key in wl_my_lists) {
		
		var my_obj = {label: wl_my_lists[key]['wishlist_name'], id:key} ;
		var tmp_node = new YAHOO.widget.TextNode(my_obj, root, false);			

		var num_items = wl_my_lists[key]['items'].length;
		
		//Loop through each list's items to set up the individual nodes
		for (var i=0;i<num_items;i++) {
						
			//Check to see if there are any items in the array
			if (wl_my_lists[key]['items'][i]['title'] == null) {
				
				var my_obj2 = {html: 'No Items In This List', id:i} ;
				var tmp_node2 = new YAHOO.widget.HTMLNode(my_obj2, tmp_node, false);
				break;
			}

			else {
				var my_obj2 = {html: '<a href="javascript:' + node_action_function + '(' + wl_my_lists[key]['items'][i]['key'] + ',' + key + ')">' + wl_my_lists[key]['items'][i]['title'] + '</a>', id:i} ;
				var tmp_node2 = new YAHOO.widget.HTMLNode(my_obj2, tmp_node, false);
				
			}
		}
	}

	//Draw the main tree
	wl_tree.draw();
	
	if(params.extra_tree_action == 'expand') {
		
		wl_tree.expandAll();
	}
	

}

//-------------------------------------------------------------------------------------------
/**
*	Function: 	wl_initialize_scraper
*	Purpose:	Integrate the screen scraper into the wishlist functions - pass in the URL
*				from the wishlist form and the function to handle the results to the 
*				main screen scraper function
*
*/
function wl_initialize_scraper() {
	
	//Set up the parameters
	params = new wl_object();
	
	params.url = escape(document.getElementById("wl_product_url").value);
	params.process_function = wl_preview_item;
	
	//Call the screen scraper display_panel function to get the product info
	display_panel(params);	
}


//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_preview_item
*	Purpose:	Pass the results from the screen scraper to the AJAX functions for wishlists
*				to generate the item preview
*
*/
function wl_preview_item() {
	
	//Get the form data
	var post_data = 'description=' + escape(desc_array[current_desc]) + '&title=' + title_array[current_title];
	post_data += '&image=' + image_array[current_image] + '&product_url_display=' + escape(document.getElementById("product_url_display").href);
	post_data += '&price=' + price_array[current_price];

	//Add the action to the post string
	post_data += "&action=generate_wishlist_preview";
	
	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = post_data;
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.extra_function = wl_add_item_preview;
	
	wl_ajax_call(params);
}


//-------------------------------------------------------------------------------------------
/**
*  	Function: 	wl_add_item_preview
*	Purpose:	Given the results from the screen scraper, set up the preview panel with the item
*				preview, remove the fixed center property (help if panel is too long), and the 
*				rescroll to the top of the page
*
*	@param	o		The response from the AJAX parsing of the screen scraper item
*/
function wl_add_item_preview(o) {
	
	handle_close_scrape();
	
	//Get the json array of wishlists
	var json_string = o.responseText;		

	//Parse the json string to form the main array
	var results = eval('(' + json_string + ')');
	
	document.getElementById("wl-scraper-results").innerHTML = unescape(results['preview']);
	
	wl_panel_list[wl_close_index].cfg.queueProperty('fixedcenter',false);
	wl_panel_list[wl_close_index].cfg.queueProperty('y',50);
	wl_panel_list[wl_close_index].cfg.fireQueue();
	
	scroll(0,0);
}


//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_add_new_item_form
*	Purpose:	Display the add list item template where a user can enter a URL and add a 
*				product to their wishlist
*
*	@param user_id		The user id of the user adding an item
*	@param user_type	The type of user adding the item
*/
function wl_add_new_item_form(user_id,user_type) {


	if (user_id == null) {
		
		alert('No Access');
		return;
	}

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=load_wishlist_form&form_name=layout_add_list_item.tpl";
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.header = "Add Item to Wishlist:";
	params.args.panel_width = "500px";	
	params.args.fixed_center = true;
	params.args.buttons = [ { text:"Add", handler:wl_add_new_item , isDefault:true },
							{ text:"Cancel", handler:wl_destroy_view_panel, isDefault:false }
						  ]		
	params.args.extra_function = null;
	params.args.panel_to_display = 'wl_view_panel';
	
	wl_hide_view_panel('wl_edit_panel');
	wl_close_index = 'wl_view_panel';
	
	wl_ajax_call(params);
}


//-------------------------------------------------------------------------------------------
/**
*  	Function:	wl_add_new_item
*  	Purpose:	Handle the addition of a new wishlist item with results coming from the
*				screen scraper module
*
*  	@param none
*/
function wl_add_new_item() {
	
	//Get the wishlist values from the form
	var div_source = escape(document.getElementById("wl-scraper-results").innerHTML);
	var selected_id = document.getElementById("wishlist_id").value;

	//Check to see if valid wishlist has been selected
	if(selected_id == -1 ) {
		
		alert('Please select a valid wishlist (or create one).');
		return;
	}	
	
	//Check to see if product has been set up
	if(div_source == '' || escape(document.getElementById("wl_product_url").value) == '') {
		
		alert('Please set up your product first.');
		return;
	}
	
	
	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=add_new_item&source=" + div_source + "&wishlist_id=" + selected_id;

	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.is_json = true;
	params.args.extra_function = wl_get_lists;
		
	//Hide the panel
	wl_panel_manager('wl_edit_panel','wl_view_panel');
	
	wl_ajax_call(params);	
	
}


//-------------------------------------------------------------------------------------------
/**
*  	Function:	wl_add_to_my_list_form
*  	Purpose:	Display the form to addd another user's existing wishlist item to 
*				another users wishlist
*
*  	@param	user_id		The user_id of the person adding the item
*  	@param	item_id		The id of the item being added to the wishlist
*/
function wl_add_to_my_list_form(user_id,item_id) {

	wl_hide_view_panel('wl_view_panel');

	if (user_id == null) {
		
		alert('No Access');
		return;
	}

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=add_item_to_my_wishlist_form&item_id=" + item_id + "&user_id=" + user_id;
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.header = "Add Item to My Wishlist:";
	params.args.panel_width = "500px";	
	params.args.fixed_center = true;
	params.args.buttons = [ { text:"Add", handler:wl_add_item_to_my_wishlist , isDefault:true },
							{ text:"Cancel", handler:wl_destroy_view_panel, isDefault:false }
						  ]		
	params.args.extra_function = null;
	params.args.panel_to_display = 'wl_view_panel';
	wl_close_index = 'wl_view_panel';
	
	wl_ajax_call(params);
}


//-------------------------------------------------------------------------------------------
/**
*  	Function:	wl_add_item_to_my_wishlist
*  	Purpose:	To add an existing wishlist item to a user's wishlist - gets the details from
*				the add to my wishlist form and adds the item to the new user's wishlist
*
*  	@param	none
*/
function wl_add_item_to_my_wishlist() {
	
	
	//Get the values from the form
	var item_id = document.getElementById("wl_item_id").value;
	var selected_id = document.getElementById("wishlist_id").value;	
	
	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=add_item_to_my_wishlist&item_id=" + item_id + "&wishlist_id=" + selected_id;

	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.is_json = true;
	params.args.header = "Wishlist Item Status:";
	params.args.panel_width = "500px";	
	params.args.fixed_center = true;
	params.args.buttons = [ { text:"OK", handler:wl_destroy_view_panel, isDefault:false }
						  ]		
	params.args.extra_function = null;
	params.args.panel_to_display = 'wl_view_panel';

	wl_destroy_view_panel();
	wl_close_index = 'wl_view_panel';
	
	wl_ajax_call(params);	
	
}


//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_get_lists
*	Purpose:	Given the owner id of the list, get all the list items and initialize the main
*				wishlist tree - if the system is in edit mode, then build the edit tree view
*				first
*
*	@param	none
*/
function wl_get_lists() {

	var list_owner = document.getElementById("wl_owner_id").value;

	if(wl_in_edit_mode == true) {
		
		wl_build_edit_list();	
	}

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=get_lists&owner_id=" + list_owner;
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.extra_function = wl_tree_init;
	params.args.custom_action = null;
	
	wl_ajax_call(params);

}

//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_build_edit_list
*	Purpose:	Build a wishlist that is in edit mode - change the link action of wishlist items
*				to delete rather than display an item and expand the tree
*
*	@param	none
*/
function wl_build_edit_list() {

	var list_owner = document.getElementById("wl_owner_id").value;

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=get_lists&owner_id=" + list_owner;
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.extra_function = wl_tree_init;
	params.args.custom_action = 'wl_confirm_delete';
	params.args.target_div = 'edit-wishlist-tree-holder';
	params.args.extra_tree_action = 'expand';
	
	wl_ajax_call(params);

}

//-------------------------------------------------------------------------------------------
/**
*	Function: 	wl_display_wishlist_item
*	Purpose:	Display the an item preview from an already created wishlist item
*
*	@param	item_id		The item to display
*/
function wl_display_wishlist_item(item_id) {

	//Check to see if the viewer is also the owner (needed for template)
	var list_owner = document.getElementById("wl_is_owner").value;

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=get_item_details&item_id=" + item_id + "&is_owner=" + list_owner;
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.header = "My Wishlist Item:";
	params.args.panel_width = "350px";	
	params.args.fixed_center = true;
	params.args.buttons = [ { text:"OK", handler:wl_destroy_view_panel, isDefault:true } ];	
	params.args.panel_to_display = 'wl_view_panel';
	params.args.extra_function = null;
	
	wl_close_index = 'wl_view_panel';
	
	wl_ajax_call(params);
}


//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_display_new_list_form
*	Purpose:	Display the form to allow the user to add an entirely new wishlist
*
*	@param	user_id		The user_id of the user wanting to add a wishlist
*	@param	user_type	The type of user adding the list
*/
function wl_display_new_list_form(user_id,user_type) {
	
	//Make sure there is a user_id passed in
	if (user_id == null) {
		
		alert('No Access');
		return;
	}

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=load_wishlist_form&form_name=layout_add_wishlist.tpl";
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.header = "Add New Wishlist:";
	params.args.panel_width = "500px";	
	params.args.fixed_center = true;
	params.args.buttons = [ { text:"Create", handler:wl_create_new_wishlist, isDefault:true },
							{ text:"Cancel", handler: wl_destroy_view_panel, isDefault:false }
						  ]		
						  
	params.args.panel_to_display = 'wl_view_panel';
	params.args.extra_function = null;
	
	wl_hide_view_panel('wl_edit_panel');
	wl_close_index = 'wl_view_panel';	
	
	wl_ajax_call(params);
}

//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_create_new_wishlist
*	Purpose:	Handle a user submission of a new wishlist - make the AJAX call to create the wishlist
*
*	@param	none
*/
function wl_create_new_wishlist() {
	
	//Get the values from the form
	var new_name = document.getElementById("new_wishlist_name").value;
	//var new_description = document.getElementById("new_wishlist_description").value;

	//Set up main part of AJAX call
	params = new wl_object();
	//params.post_data = "action=create_new_wishlist&name=" + new_name + "&description=" + new_description;
	params.post_data = "action=create_new_wishlist&name=" + new_name;

	//Set up params to pass to success
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.is_json = true;
	params.args.extra_function = wl_get_lists;

	//Hide the panel
	wl_panel_manager('wl_edit_panel','wl_view_panel');

	wl_ajax_call(params);
}



//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_display_delete_list_form
*	Purpose:	Display the selection form showing the users which lists they are able to 
*				remove and allow them to delete multiple lists (preserving the items)
*
*	@param	user_id		The user_id of the user wanting to delete a wishlist
*	@param	user_type	The type of user wanting to delete a list
*/
function wl_display_delete_list_form(user_id,user_type) {
	
	//Make sure there is a user id
	if (user_id == null) {
		
		alert('No Access');
		return;
	}

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=load_wishlist_form&form_name=layout_remove_wishlist.tpl";
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.header = "Remove Wishlist(s):";
	params.args.panel_width = "500px";	
	params.args.fixed_center = true;
	params.args.buttons = [ { text:"Remove", handler:wl_remove_wishlist, isDefault:true },
							{ text:"Cancel", handler:wl_destroy_view_panel, isDefault:false }
						  ]			
	params.args.extra_function = null;
	params.args.panel_to_display = 'wl_view_panel';
	
	wl_hide_view_panel('wl_edit_panel');
	wl_close_index = 'wl_view_panel';
	
	wl_ajax_call(params);
}


//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_remove_wishlist
*	Purpose:	Handles the removal of a wishlist by submitting AJAX request with the selected
*				lists - hides the view panel and displays the edit panel
*
*	@param	none
*/
function wl_remove_wishlist() {

	//Get the form data
	var data = this.getData();
	var in_lists = data.lists;
	
	if(in_lists == true) {
		
		in_lists = document.getElementById("lists").value;
	}
	
	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = 'lists=' + in_lists + '&action=remove_wishlist';
		
	//Set up the args to pass to success
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.is_json = true;
	params.args.extra_function = wl_get_lists;

	//Show/hide and set up close indices
	wl_panel_manager('wl_edit_panel','wl_view_panel');
	
	wl_ajax_call(params);
	
}

//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_edit_form
*	Purpose:	Sets up the main wishlist editing form and makes the call to initialize the
*				edit tree view
*
*	@param	none
*/
function wl_edit_form() {

	//Set edit mode
	wl_in_edit_mode = true;	
	
	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=load_wishlist_form&form_name=layout_edit_wishlist.tpl";
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = true;
	params.args.header = "Edit My WishList:";
	params.args.panel_width = "500px";	
	params.args.fixed_center = false;	
	params.args.panel_to_display = 'wl_edit_panel';
	params.args.buttons = [ { text:"OK", handler:wl_destroy_view_panel, isDefault:true }
						  ]		
	//Set the Extra function
	params.args.extra_function = wl_build_edit_list;
	
	//Set the close index
	wl_close_index = 'wl_edit_panel';
	
	wl_ajax_call(params);	
	
}

//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_confirm_delete
*	Purpose:	Prompt the user to either continue with item deletion or cancel the deletion
*				and handle the response
*
*	@param	item_id			The item to be removed
*	@param	wishlist_id		The wishlist to remove the item from
*/
function wl_confirm_delete(item_id,wishlist_id) {
	
	//Display the javascript confirm window
	input_box = confirm("Click OK to Delete this Item \nor hit Cancel");	
	
	//If they confirm, delete the item and redraw the edit and regular list
	if (input_box == true) {
		
		wl_delete_list_item(item_id,wishlist_id);				
		wl_build_edit_list();
		wl_get_lists();
	}
	else {
		
		return;
	}
	
}

//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_delete_list_item
*	Purpose:	After confimation, delete an item from the user's wishlist - but keep the item
*				intact in case other users have added the item to their lists
*
*	@param	item_id			The item to be deleted
*	@param	wishlist_id		The wishlist to remove the item from
*/
function wl_delete_list_item(item_id,wishlist_id) {

	//Set up main part of AJAX call
	params = new wl_object();
	params.post_data = "action=delete_list_item&item_id=" + item_id + "&wishlist_id=" + wishlist_id;
	
	//Set up the args to pass to success	
	params.args = new wl_object();
	params.args.show_panel = false;
	params.args.extra_function = null;
	
	wl_ajax_call(params);

}


//-------------------------------------------------------------------------------------------
/**
*	Function:	wl_ajax_fail
*	Purpose:	Generic function to handle an AJAX failure - alert for debugging or nothing for prod
*
*	@param o	Response from the AJAX request
*/
function wl_ajax_fail(o) {

	alert('Failed AJAX Request');
}


//-------------------------------------------------------------------------------------------
/*
*	Function: 	wl_destroy_view_panel
*	Purpose:	Hide and destroy the currently open panel and update the running index
*/
function wl_destroy_view_panel() {

	//Make sure panel is not null
	if(wl_panel_list[wl_close_index] != null) {
		
		//Hide and destroy the panel and scroll to top of page
		wl_panel_list[wl_close_index].hide();
		wl_panel_list[wl_close_index] = null;
		scroll(0,0);

		//Check to see if the editor window is being closed
		if(wl_close_index == 'wl_edit_panel') {
			
			//Take state out of edit mode
			wl_in_edit_mode = false;
		}
		
		//Get the last index and keep the state
		wl_close_index = wl_prev_close_index;
		wl_prev_close_index = null;
		
	}
}


//-------------------------------------------------------------------------------------------
/*
*	Function: 	wl_hide_view_panel
*	Purpose:	Hide an arbitrary panel
*
*	@param	panel_id	The panel to hide
*/
function wl_hide_view_panel(panel_id) {

	//Make sure panel is not null
	if(wl_panel_list[panel_id] != null) {
		
		//Hide the panel 
		wl_panel_list[panel_id].hide();		
		
	}
}


//-------------------------------------------------------------------------------------------
/*
*	Function: 	wl_show_view_panel
*	Purpose:	Show an arbitrary panel
*
*	@param	panel_id	The panel to show
*/
function wl_show_view_panel(panel_id) {

	//Make sure panel is not null
	if(wl_panel_list[panel_id] != null) {
		
		//Hide the panel 
		wl_panel_list[panel_id].show();		
		
	}
}

//-------------------------------------------------------------------------------------------
/*
*	Function: 	wl_show_view_panel
*	Purpose:	Hide and show two arbitrary panels
*
*	@param	panel_show	The panel to show
*	@param	panel_hide	The panel to hide
*/
function wl_panel_manager(panel_show,panel_hide) {
	
	wl_hide_view_panel(panel_hide);
	wl_show_view_panel(panel_show);
	wl_close_index = panel_show;

}

