// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/**
* Adds an id to a hidden input value specified if the id does not already exist. 
*/
function addItemToArray(element, newItem){	
	item_str = $(element).value.strip();
	if (item_str == '' || item_str == null){
		items = Array();
	} else {
		items = item_str.split(',').compact();
	}
	if(!(items.include(newItem))){
		items.push(newItem);
		$(element).value = items.join(',');
	}
}


/*
* Given a css description eg '#product_results .checkbox_item'
* will find all elements matching the criteria. They should all be checkbox elements. 
* And will check them or uncheck according to the second parameter.
*/
function checkAll(cssClass, checkStatus){
  if (checkStatus == null){
    checkStatus = true
  } 
  $$(cssClass).each(function(elem){
    elem.checked = checkStatus;
  })
  
}


/**
* This will evaluate the present element passed in. If it is checked, then the
* action that is passed is performed.
*/
function doActionOnSelect(element, action){
	if ($(element).checked){
		eval(action); return false;
	}
}


/**
* Takes the element selected and checks to see if the corresponding element in elementToDisplay
* is checked. TODO: Possibly add in an optional parameter to allow for transition effects.
*/
function toggleDisplayOnSelect(elementSelected, elementToDisplay){
	if ($(elementSelected).checked){
		$(elementToDisplay).show();
	} else {
		$(elementToDisplay).hide();
	}
}




/**
* This function will take in an array of elements and toggle their displays.
*/
function toggleDisplays(elements){
	elements.each(function(elem) {
		$(elem).toggle();										 
	});
}

/**
* This function takes in a css class name (without the '.' prefix), and an exceptionId.
* It will find all elements with this class, strip the class from all the elements and then
* set the element with the corresponding id to have the class in question.
*/
function toggleClasses(classname, exceptionId){
	elements = $$("." + classname);
	elements.each(function(item) {
	  $(item).removeClassName(classname);										 	
	});
	$(exceptionId).addClassName(classname);
}


/**
* This function will take in a classname and either a single exception id or an array of exceptionIds.
* The function will then find all the elements with the classname. 
* Note, this will take a class STRING. meaning, the class string should be exactly as it would appear in a css file. 
* e.g. '.special' or 'ul li.special'  
* After which it will display the display area with the id passed in.
* 
*/
function toggleAreasByClass(classname, exceptionIds){
	elements = $$(classname);
	elements.each(function(item) {
		$(item).hide();										 	
	});
	
	if (Object.isString(exceptionIds)){
		exceptionIds = exceptionIds.split(',');
	}	
	exceptionIds.each(function(id){
		$(id).show();
	})
}

/**
* Will blindUp all other areas that are not already hidden and that are not the exception. 
*/
function toggleBlindAreasByClass(classname, exception){
	// Get all 
	areas = $$(classname);
	areas.each(function(item) {
		if (item.visible() && item.id != (exception)){
			Effect.BlindUp(item,{duration:0.2});
		}
	});
	if (!$(exception).visible()){
		Effect.BlindDown($(exception),{duration:0.2});
	}
}


/* Search Javascript
**************************************/

/**
* Will take in a search type 'model_number' or 'full_text' and a prefix for all the identifiers in the set.
* The prefix is used to make sure that there can be multiple search forms on a page. If there is an underscore at the end
* of the prefix supplied, then the prefix will remain as it is. If not, an underscore will be added. 
*
*/
function toggleSearches(search_type, prefix){
  if(prefix == null || prefix.blank()){
	prefix = ''	
  }	else if(!prefix.endsWith('_')) {
	prefix = prefix + '_'
  }

  $(prefix + 'search_type_field').value = search_type; 
  $$('#' + prefix + 'search_form_area a').each(function(item){
    $(item).removeClassName('available_search_type_link');
  }) 
  if (search_type == "model_number"){
	$(prefix + 'search_type_label').innerHTML = 'Model Number';
    $(prefix + 'search_link_full_text').addClassName('available_search_type_link');
  } else if (search_type == "full_text"){
	$(prefix + 'search_type_label').innerHTML = 'Full Text';
    $(prefix + 'search_link_model_number').addClassName('available_search_type_link');
  }
}


/**
* Used in the modal window for address new swap.
*/
function phoneNumberFieldSwap(selectedInput, cancelledInput){
	inputDiv = selectedInput + '_area';
	toggleBlindAreasByClass('address_phone_main_area', 'address_phone', inputDiv);
	$(cancelledInput).value='';
}


//Setup a spinner when ajax is busy. 
Ajax.Responders.register({
	onCreate: function(){
			if ($('ajax_busy') && Ajax.activeRequestCount > 0){
				Effect.Appear('ajax_busy', {duration: 0.5, queue: 'end'});
			}
	},
	onComplete: function(){
		if($('ajax_busy') && Ajax.activeRequestCount == 0){
			Effect.Fade('ajax_busy', {duration: 0.5, queue: 'end'});
		}
	}
});



