var current_popup_id = '';
var current_mouseover_popup_id = '';
var timer = null;

// return absolute coordinates of the left upper corner of an element
function get_absolute_coords(elem){
	var xx = parseInt(elem.offsetLeft) < 0 ? 0 : parseInt(elem.offsetLeft);
	var yy = parseInt(elem.offsetTop) < 0 ? 0 : parseInt(elem.offsetTop);
	for (var parent = elem.offsetParent; parent; parent = parent.offsetParent){
		xx += parseInt(parent.offsetLeft) < 0 ? 0 : parseInt(parent.offsetLeft);
		yy += parseInt(parent.offsetTop) < 0 ? 0 : parseInt(parent.offsetTop);
	}
	return {'x': xx, 'y': yy};
}
function show_popup(popup_id, obj){
	if (current_popup_id != '' && current_popup_id != popup_id){
		hide_popup();
	}

	if (current_popup_id != popup_id){
		current_popup_id = popup_id;

		var coords = get_absolute_coords(obj);
		var popup_obj = document.getElementById(popup_id);
		popup_obj.style.left = coords.x + 'px';
		popup_obj.style.top = (parseInt(coords.y) + parseInt(obj.offsetHeight)) + 'px';
		popup_obj.style.display = 'block';
	}

	set_mouse_over(popup_id);
}
function prepare_hide_popup(){
	timer = setTimeout('hide_popup()', 300);
}
function hide_popup(){
	if (timer != null){
		clearTimeout(timer);
		timer = null;
	}

	if (current_mouseover_popup_id != current_popup_id){
		document.getElementById(current_popup_id).style.display = 'none';
		current_mouseover_popup_id = '';
		current_popup_id = '';
	}
}
function set_mouse_over(id){
	current_mouseover_popup_id = id;
}
function set_mouse_out(){
	current_mouseover_popup_id = '';
	prepare_hide_popup();
}


/*
 * Advanced Search functionality below this point 
 * 
 * Can move this if we want modularity in each function
 */

function advanced_search_glideout(event_obj) {
	var menu = adv_search_obj = document.getElementById('advanced_search');
	var coords = get_absolute_coords(menu);

	if ( adv_search_obj.style.display != 'none' ) {
		adv_search_hide();
		return;
	} else {
		clear_event(event_obj);
	}
	
	menu.style.width = '0px';
	menu.style.height = '0px';
	menu.style.display = 'block';

	coords = get_absolute_coords(document.getElementById('search'));
	var coord_x = coords['x'] + document.getElementById('search').offsetWidth;
	var coord_y = coords['y'];

	menu.style.left = coord_x + 'px';
	menu.style.top = coord_y + 'px';

	expand_width(500,false);
	expand_height(241,true);
	return;
}

function expand_width(width, auto) {
	adv_search_obj.style.width = Math.min(parseInt(adv_search_obj.style.width)+25, width) + 'px';
	if (parseInt(adv_search_obj.style.width) < width) {
		setTimeout("expand_width(" + width + "," + auto + ")", 5);
	} else if (auto) {
		adv_search_obj.style.width = 'auto';
	}
	return;
}
function expand_height(height, auto) {
	adv_search_obj.style.height = Math.min(parseInt(adv_search_obj.style.height)+15,height) + 'px';
	if (parseInt(adv_search_obj.style.height) < height) {
		setTimeout("expand_height(" + height + "," + auto + ")", 5);
	} else if (auto) {
		adv_search_obj.style.height = 'auto';
	}
	return;
}

function toggle_submenu(event_obj, obj, submenu_id){
	var coords = get_absolute_coords(obj);
	var submenu = document.getElementById(submenu_id);

	submenu.style.left = (coords['x'] + obj.offsetWidth - 10) + 'px';
	submenu.style.top = (coords['y'] + 2) + 'px';

	if (submenu.style.display == 'block') {
		adv_search_hide_submenus();
	} else {
		adv_search_hide_submenus();
		submenu.style.display = 'block';
	}
	clear_event(event_obj);
}


function set_value(event_obj, name, value, obj, merged) {
	var oldval = document.adv_search_form[name].value;
	document.adv_search_form[name].value = value;
	adv_search_hide_submenus();
	clear_event(event_obj);

	var disp_id = 'adv_search_' + (merged ? merged : name) + '_selection';
	var selection = document.getElementById(disp_id);
	if ( value !== '' ) {
		// If the option was an image, create a copy
		if ( obj.tagName == 'IMG' ) {
			var img = document.createElement('img');
			img.setAttribute('src',obj.src);
			img.setAttribute('alt','Selected: ' + obj.alt);

			// If the selection already has an img, replace it; otherwise, insert
			if ( selection.firstChild !== undefined && selection.firstChild != null ) {
				selection.replaceChild(img, selection.firstChild);
			} else {
				selection.appendChild(img);
			}

		// Insert text, since it's not an object
		} else {
			selection.innerHTML = obj.innerHTML;
		}
	} else {
		if ( obj.tagName == 'IMG' && selection.firstChild ) {
			selection.removeChild(selection.firstChild);
		} else {
			selection.innerHTML = '';
		}
	}
	if ( name == 'category_direct' ) adv_search_context(value, oldval);
}


/* This deals with issues where adv_search options change based on other options */
function adv_search_context(cat, oldval) {
	if ( cat == oldval ) return;

	var elements = null;
	var enabled = false;

	// Clear all the old selections
	document.adv_search_form['size'].value = '';
	document.adv_search_form['width'].value = '';
	document.adv_search_form['brand'].value = '';
	document.adv_search_form['style'].value = '';

	document.getElementById('adv_search_size_selection').innerHTML = '';
	document.getElementById('adv_search_width_selection').innerHTML = '';
	document.getElementById('adv_search_brand_selection').innerHTML = '';
	document.getElementById('adv_search_style_selection').innerHTML = '';

	// Change which elements are displayed in the "size" group
	if ( search_categories[ cat ][ 'sizes' ] !== undefined ) {
		// Hide all images
		elements = document.getElementById('adv_search_size_context').childNodes;
		for ( var i = 0; i < elements.length; ++i ) {
			if ( elements[i].tagName == 'IMG' )
				elements[i].style.display = 'none';
		}
		// Display images as appropriate
		for ( var i = 0; i < search_categories[ cat ][ 'sizes' ].length; ++i ) {
			if (document.getElementById('adv_search_size_option_'+search_categories[cat]['sizes'][i]) != null) {
				document.getElementById('adv_search_size_option_'+search_categories[cat]['sizes'][i]).style.display = 'block';
				enabled = true;
			}
		}
	}
	if ( enabled ) {
		if ( search_categories[ cat ][ 'use_waist' ] == 1 ) {
			document.getElementById('adv_search_size_label').innerHTML = 'Pick Your Waist Size:';
		} else {
			document.getElementById('adv_search_size_label').innerHTML = 'Pick Your Size:';
		}
		document.getElementById('adv_search_size_context').style.display = 'block';
		document.getElementById('adv_search_size_nocontext').style.display = 'none';
		enabled = false;
	} else {
		document.getElementById('adv_search_size_context').style.display = 'none';
		document.getElementById('adv_search_size_nocontext').style.display = 'block';
	}

	// Change which elements are displayed in the "width_length" group
	if ( search_categories[ cat ][ 'widths' ] !== undefined ) {
		// Hide all images
		elements = document.getElementById('adv_search_width_length_context').childNodes;
		for ( var i = 0; i < elements.length; ++i ) {
			if ( elements[i].tagName == 'IMG' )
				elements[i].style.display = 'none';
		}
		// Display images as appropriate
		for ( var i = 0; i < search_categories[ cat ][ 'widths' ].length; ++i ) {
			if (document.getElementById('adv_search_width_length_option_'+search_categories[cat]['widths'][i]) != null) {
				document.getElementById('adv_search_width_length_option_'+search_categories[cat]['widths'][i]).style.display = 'block';
				enabled = true;
			}
		}
	}
	if ( enabled ) {
		if ( search_categories[ cat ][ 'use_length' ] == 1 ) {
			document.getElementById('adv_search_width_length_label').innerHTML = 'Pick Your Pant Length:';
		} else {
			document.getElementById('adv_search_width_length_label').innerHTML = 'Pick Your Width:';
		}
		document.getElementById('adv_search_width_length_context').style.display = 'block';
		document.getElementById('adv_search_width_length_nocontext').style.display = 'none';
		enabled = false;
	} else {
		document.getElementById('adv_search_width_length_context').style.display = 'none';
		document.getElementById('adv_search_width_length_nocontext').style.display = 'block';
	}

	// Change which brands are displayed
	elements = document.getElementById('adv_search_brand').childNodes;
	for ( var i = 0; i < elements.length; ++i ) {
		if ( cat == '' || elements[i].className == ('cat_' + cat) )
			elements[i].style.display = 'block';
		else
			elements[i].style.display = 'none';
	}

	// Change which styles are displayed
	if ( cat == search_cat_ids['shoes'] ) {
		document.getElementById('adv_search_styles_nocontext').style.display = 'none';
		document.getElementById('adv_search_styles_context').style.display = 'block';
	} else {
		document.getElementById('adv_search_styles_nocontext').style.display = 'block';
		document.getElementById('adv_search_styles_context').style.display = 'none';
	}
}


/*
 * Hides all the advanced search submenus
 */
function adv_search_hide_submenus() {
	for (var i in adv_search_submenu_id_arr) {
		document.getElementById(adv_search_submenu_id_arr[i]).style.display = 'none';
	}
}


/*
 * If the event hasn't been captured by the advanced search box,
 *  hide it here.
 */
function adv_search_hide() {
	if ( adv_search_hide_controlled ) {
		adv_search_hide_controlled = false;
	} else {
		adv_search_hide_submenus();
		document.getElementById('advanced_search').style.display = 'none';	
	}
}


/*
 * Takes control of the onclick hide event (sets the flag) and hides
 *  the submenus.
 */
function adv_search_hide_controller() {
	adv_search_hide_controlled = true;
	adv_search_hide_submenus();
}


/*
 * Clears the fields in the advanced_search element, including
 *  selection display and form inputs.  Afterward hides the element.
 */
function adv_search_clear_all() {
	if ( document.adv_search_form ) {
		for (var i in search_fields) {
			document.getElementById('adv_search_' + search_fields[i] + '_selection').innerHTML = '';
			document.adv_search_form[ search_fields[i] ].value = '';
		}
		document.adv_search_form.clearance_only.checked = false;
	}
	adv_search_hide();
}


/*
 * When a selected value is clicked, this clears the selection.
 *  Includes a call to adv_search_context() in case other selections
 *  are affected.
 */
function adv_search_clear_single(name) {
	var oldval = document.adv_search_form[name].value;
	document.adv_search_form[name].value = '';
	document.getElementById('adv_search_' + name + '_selection').innerHTML = '';
	if ( name == 'category_direct' ) adv_search_context('', oldval);
}


/*
 * Moves the advanced_search element if the window is resized
 */
function adv_search_reposition() {
	var search = document.getElementById('search');
	var adv_search = document.getElementById('advanced_search');
	var coords = get_absolute_coords(search);

	adv_search.style.left = (coords['x'] + search.offsetWidth) + 'px';
	adv_search.style.top = coords['y'] + 'px';
}
