/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Smart Search :: alteredCart.com
| All code developed by Bill Brewer
| Copyright (c)2009 alteredCart            
| Bill Brewer <bill@alteredcart.com>   
| All rights reserved.
| This code is licensed for use on a single server.
| This code is licensed for use by the original purchaser only.                         
| Full license :: www.alteredcart.com/license.php
|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

function displayAdvancedSearch(display){

		if (display == 'show'){
			ac.$('advancedSearch').style.width = '2px';	
			ac.$('advancedSearch').style.display = '';	
			ac.changeSize({id:'advancedSearch', start:0, end:advancedSearchHeight, xy:'y', millisec:300});	
			setTimeout("ac.changeSize({id:'advancedSearch', start:0, end:advancedSearchWidth, xy:'x', millisec:300})", 300);						
			setTimeout("ac.changeSize({id:'mainSearch', start:ac.$('mainSearch').offsetHeight, end:0, xy:'y', millisec:200})", 600);	
			
			}
		
		else if (display == 'hide'){
			ac.changeSize({id:'mainSearch', start:0, end:mainSearchHeight, xy:'y', millisec:200});
			setTimeout("ac.changeSize({id:'advancedSearch', start:ac.$('advancedSearch').offsetHeight, end:0, xy:'x', millisec:300})", 300);
			setTimeout("ac.changeSize({id:'advancedSearch', start:ac.$('advancedSearch').offsetWidth, end:0, xy:'y', millisec:300})", 300);
			setTimeout("ac.$('advancedSearch').style.display = 'none'", 901);

		}	
	}

// Load the sizes that will be used to display/hide the advanced search //
function loadIdSizes(){
	
	function setStyle(){
		
	ac.$('advancedSearch').style.height = '0px';
	ac.$('advancedSearch').style.width = '0px';
	ac.$('advancedSearch').style.display = 'none';
	ac.$('advancedSearch').style.overflow = 'hidden';
	
	if (searchMode == 'advanced'){
		
			ac.$('advancedSearch').style.display = '';	
			ac.$('advancedSearch').style.height = advancedSearchHeight+'px';
			ac.$('advancedSearch').style.width = advancedSearchWidth+'px';						
			ac.$('mainSearch').style.height = '0px';
		
		}
	}
	
	if (ac.isset(advancedSearchHeight)){
		setStyle();
			return;
		}
	
	mainSearchHeight = ac.$('mainSearch').offsetHeight;
	mainSearchWidth = ac.$('mainSearch').offsetWidth;
	
	advancedSearchHeight = ac.$('advancedSearch').offsetHeight;
	advancedSearchWidth = ac.$('advancedSearch').offsetWidth;
	
	setStyle();
	
	// Update the server with the calculated data //
	var parameters = 'mainSearchHeight='+mainSearchHeight;
		parameters += '&mainSearchWidth='+mainSearchWidth;
		parameters += '&advancedSearchHeight='+advancedSearchHeight;
		parameters += '&advancedSearchWidth='+advancedSearchWidth;
		parameters += '&update_sizes=Y';
	
		ac.ajax({url:'_search.php', method:'POST', parameters:parameters});
	
	advancedDisplay();
	
	}


function advanced_submit(){

	if (!ac.$('all_the_words').value && !ac.$('exact_phrase').value && !ac.$('without_the_words').value && !ac.$('at_least').value){
		alert (error_alert);
			return false;	
		}

	document.smart_search_advanced.submit();
	
	}

// Disable/Enable fields based on the fields the customer is entering data into //
function advancedDisplay(){
	
	markReadonly('exact_phrase', 'off');
	markReadonly('all_the_words', 'off');
	markReadonly('at_least', 'off');
	markReadonly('without_the_words', 'on');
	
	if (ac.$('at_least').value != ''){
		markReadonly('all_the_words', 'on');
		markReadonly('without_the_words', 'off');
	}
	
	if (ac.$('all_the_words').value != ''){
		markReadonly('at_least', 'on');
		markReadonly('exact_phrase', 'on');
		markReadonly('without_the_words', 'off');
	}
	
	if (ac.$('exact_phrase').value != ''){
		markReadonly('all_the_words', 'on');
		markReadonly('at_least', 'on');
		markReadonly('without_the_words', 'off');
		}	
	}


function markReadonly(obj, pos){
	
	if (!ac.$(obj))
		return;
	
	if (pos == 'off'){
		ac.$(obj).readOnly = false;
		ac.$(obj).className = '';		
			return;
		}
			
		ac.$(obj).readOnly = true;
		ac.$(obj).className = 'readonly';	
	
	}

	
function autoSearch(q){

	// Make sure there are at least two characters //
	if (q.length < 2){
		displayAutoSearch('hide');
			return;		
	}
	
	// Post Variables //	
	var parameters = 'q='+q;
	
	ac.ajax({url:'_search_ajax.php', method:'POST', return_func:searchPopulateDropdown, parameters:parameters, returned:'text'});
			

	}
	
	
function searchPopulateDropdown(innerHTML){
	
	if (innerHTML.length > 10){ // Padding for whitespace sent from the server //
		
		displayAutoSearch('show');
		ac.$('autoSearchResults').innerHTML = innerHTML;	
		
		} else {
			
			displayAutoSearch('hide');			
		}
	}
	
	
function displayAutoSearch(pos){
	
	if (!displayAutoSearch && pos == 'show')
		return;
	
	if (pos == 'show'){
		
		ac.$('q').setAttribute('autocomplete', 'off');		
		ac.$('autoSuggest').style.display = '';	

		//var offsets = ac.calculateOffsets('autoSuggest');
		//var qoffsets = ac.calculateOffsets('searchBoxTd'); // Use the search field for the X pos //	
	
		//ac.$('autoSuggest').style.top = offsets.y+'px';
		//ac.$('autoSuggest').style.left = qoffsets.x+'px';	
		
		}
		
	else if (pos == 'hide'){
		
		ac.$('autoSuggest').style.display = 'none';		
	
		}	
	}
	
	
function highlightDropdown(opt){
	
// @ opt = direction [up or down], id, autoselected [number of the hovered over item] //
	
	// Rest any div tags within the dropdown //
	var children = ac.$('autoSearchResults').getElementsByTagName('*');
	
	// Check opt //
	if (typeof(opt) == 'undefined'){
		opt = {};
		autoSearchSelected = 0;		
	}
	
	for (var i = 0; i < children.length; i++){
		if (children[i].className == 'autoSuggestSelected')
			children[i].className = 'autoSuggestItem';
	
	}
	
	// Determine if the next object in the chosen direction exists //
	if (opt.direction == 'up'){
		autoSearchSelected -= 1;
		
		if (ac.$('autoItem_'+autoSearchSelected)){
			ac.$('autoItem_'+autoSearchSelected).className = 'autoSuggestSelected';
	
			}
		}
		
	if (opt.direction == 'down'){
		autoSearchSelected += 1;
		
		if (ac.$('autoItem_'+autoSearchSelected)){
			
			ac.$('autoItem_'+autoSearchSelected).className = 'autoSuggestSelected';
				
				} else {
			
			autoSearchSelected = 1;
			ac.$('autoItem_'+autoSearchSelected).className = 'autoSuggestSelected';
			
			}
		}
		
	if (opt.id && ac.$(opt.id)){		
		
			autoSearchSelected = opt.autoselected;
			ac.$(opt.id).className = 'autoSuggestSelected';
		
		}

		// Change the text in the box to the selected text //
		if (ac.$('autoItem_'+autoSearchSelected))
			ac.$('q').value = ac.$('autoItemVal_'+autoSearchSelected).innerHTML;	

		return;

	}
	
	
function autoChangeSelected(e){
	
	if (!e) var e = window.event;	
	
	switch (e.keyCode) {
		
		// Up arrow //
		case 38: 
			highlightDropdown({direction:'up'}); 
			return true;
		
		// Down arrow //
		case 40: 
			highlightDropdown({direction:'down'}); 
			return true;
	
		}
		
		return false;
	}

// Hide an element while hiding another //
function searchShowDescr(hide, show){
	
	ac.$(hide).style.display = 'none';
	ac.$(show).style.display = '';	
	
	}

