//I know, I know global vars a bad idea, but I'm lazy, not stupid
var search_terms = [];

$(document).ready(function(){
	init();
});

$(window).load(function(){
	initCorners();
});

function init(){
    initProductSearch();
    var search_term = getUrlVars()["search"];
    //alert(getUrlVars()["search"]);
    if(search_term){
      add_search_term(search_term);
    }
}

function initProductSearch(){
	//Hide all div with class ingredient_description
	$(".ingredient_description").hide();
	//Show the first div with class ingredient_description
	$(".ingredient_description:first").show();

	$(".exclusive_selector").click(function(event){
	  event.preventDefault();

	  //remove selected from each of the others and remove their terms from being searched
	  $(".exclusive_selector").each(function(){
	    $(this).removeClass("selected");
	    remove_search_term(this.id.replace("_selector",""));
	  });
	  //add this to the searched terms
	  add_search_term(this.id.replace("_selector",""));
	  $(this).addClass("selected");
	});

	$(".selector_link").click(function(event){
	  event.preventDefault();
	  if($(this).attr("id")){
	    //update the terms we are searching on
	    update_search_terms(this.id.replace("_selector",""));
	    if($(this).is(".selector_link_selected")){
	      $(this).removeClass("selector_link_selected");
	    }else{
	      $(this).addClass("selector_link_selected");
	    }
	  }
	  return false;
	});

	$(".ingredient_icon").hover(
		//hover over
		function(){
		  $(this).parents("li").addClass("hover");
		  //$(this).parents("li").css("zIndex", "100");
		  var related_div_id = $(this).attr("id").replace("_icon","_description");
		  //hide all description divs
		  $(".ingredient_description").hide();
		  //show just the one related to the hovered over icon
		  $("#"+related_div_id).show();
		  $("#"+related_div_id).css("z-index", 2);
		},
		//hover out
		function(){
		  $(this).parents("li").removeClass("hover");
		});

}
//updates the search_term array, if term exists it removes it, if it doesn't it adds it
function update_search_terms(term){
	if(!remove_search_term(term)){
	  search_terms.push(term);
	}
        update_search_results();
}
//Returns true if the term existed
function remove_search_term(term){
  for(var i=0; i < search_terms.length; i++){
    if(search_terms[i] == term){
      //splice term out of search_terms (ith element, remove a single element)
      search_terms.splice(i,1);
      return true;
    }
  }
  return false;
}
//Adds the term if it doesn't already exist
//Returns true if it was added, false if it was already there
function add_search_term(term){
  for(var i=0; i < search_terms.length; i++){
    if(search_terms[i] == term){
      return false;
    }
  }
  search_terms.push(term);
  update_search_results();
  return true;
}

function update_search_results(){
  if(search_terms.length > 0){
    $(".product").hide();
    var elements = $("."+search_terms[0]);
    //alert(search_terms[0]);
    for(var i=1; i < search_terms.length; i++){
      elements = elements.filter("."+search_terms[i]);
      //alert(search_terms[i]);
    }
    elements.show();
  }else{
    $(".product").show();
  }
}

//From here down was pulled from the old site, it would be nice to use JQuery for all of it
function initCorners()
{
	corner_settings = {
		tl: { radius: 10 },
		tr: { radius: 10 },
		bl: { radius: 10 },
		br: { radius: 10 },
		antiAlias: true,
		autoPad: true,
		validTags: ["div"]
	};
	$('.two_left_column').corner(corner_settings);
	$('.sub_nav_header').corner(corner_settings);
	$('.three_center_column').corner(corner_settings);
	//$('.ingredient_description').corner(corner_settings);
}


function quick_links_button_click()
{
  var control = $("#quick_links_menu");
  if(control.val != "0"){
    //alert(control.val());
    window.location = control.val();
  }
  return false;
}

function clearField(field, defaultValue){
	if (defaultValue==field.value){
		field.value = "";
	}
}

function setColor(elementID, color){
	var e=document.getElementById(elementID);
	e.style.color = color;
}

function setValue(field, value)
{
	document.getElementById(field).value = value;
}


function validate(form){

  var str=form.txtEmail.value;
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(str))
	{
	   alert('Please enter a valid email address');
	   return true;
	}

	return false;
}


// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}
