/**
 * @author jhanson
 */

// On page load
$(document).ready( function(){
	// Associate the white rabbit wit the developer debug content
	$('#white_rabbit').click( function(){
		$('#developer_debug').toggle();
	})
	
	// Associate the debug page buttons with the appropriate pages
	$('li#debug_db_queries_menu').click( function() {
		$('div.debug_page').hide();
		$('div#debug_db_queries').show();
	});
	$('li#debug_validation_errors_menu').click( function() {
		$('div.debug_page').hide();
		$('div#debug_validation_errors').show();
	});

});


/**
 * Handle the initial page setup.  Called on body load
 */
function initPage() {

	// Associate all the sub categories in the menu bar with their parents
	associateMenuEvents('product_categories');
}


/**
 * 
 * @param {Object} id
 */
function associateMenuEvents(id) {
	
	// If the target container exists
	if( document.getElementById(id) ) {

		// Inside the target, find any list tags
		var arr_section_menus = document.getElementById(id).getElementsByTagName('UL');
		
		// Loop over all the lists in the target element
		for( var i=0; i<arr_section_menus.length; i++ ) {
			
			// The target UL 
			targetmenu = arr_section_menus[i];
			
			// the parent element of the target
			menuparent = targetmenu.parentNode;
	
			// If the parent element is an LI, then this is a valid link
			if(menuparent.tagName == 'LI') {
				
				// Generate a link between the parent an the child that will handle opening and closing the child
				at_attach(menuparent.id, targetmenu.id, "hover", "x", "pointer");	
			}
		}
	}		
}

/**
 * Add the specified CSS class from the target
 */
function removeClass(target, class_name)
{
	target.className = target.className.replace(class_name, '');
}

/**
 * Remove the specified CSS class from the target
 */
function addClass(target, class_name)
{
	if( target.className.indexOf(class_name) == -1 ) {
		target.className += ' '+class_name;
	}
}


function showPopup(id)
{
	target = document.getElementById(id);
	
	removeClass(target, 'hidden');
	
}

function hidePopup(id)
{
	target = document.getElementById(id);
	
	addClass(target, 'hidden');
}


