var gti;
var ref;

$(document).ready(function() {
  document.getElementsByTagName('h1')[0].onclick = function() {
    window.location = "http://www.selectnovascotia.ca/";
  }
$(".productList li ul").hide();
  $("#products input").autocomplete("dr.php", 
    { 
			delay:10,
			minChars:1,
      selectFirst:true,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true,
      extraParams: { a: "pl" }
    } 
  );
  $("#recipes input").autocomplete("dr.php", 
    { 
			delay:10,
			minChars:1,
      selectFirst:true,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true,
      extraParams: { a: "rl" }
    } 
  );
  $("#companies input").autocomplete("dr.php", 
    { 
			delay:10,
			minChars:1,
      selectFirst:true,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true,
      extraParams: { a: "cl" }
    } 
  );

  

  document.getElementById('search').onclick = function(e) {
    var t = getTarget(e);
    if ( t.nodeName.toLowerCase() === 'input' || t.nodeName.toLowerCase() === 'a' ) {
      switch( t.parentNode.id ) {
        case "recipes":
          gti = 7;
          break;
        case "products":
          gti = 6;
          break;
        case "companies":
          gti = 12;
          break;
      }
      if (t.nodeName.toLowerCase() === 'input') {
        t.onblur = function(e){
          var t = getTarget(e);
          if (t.nodeName.toLowerCase() === 'input') {
            if (t.value.length == 0) {
              switch (t.parentNode.id) {
                case "companies":
                  t.value = "Search for Companies";
                  break;
                case "products":
                  t.value = "Search for Products";
                  break;
                case "recipes":
                  t.value = "Search for Recipes";
                  break;
              }
            }
          }
        }
        if (t.value.indexOf("Search for Companies") !== -1 ||
        t.value.indexOf("Search for Products") !== -1 ||
        t.value.indexOf("Search for Recipes") !== -1) {
          t.value = "";
        }
      }
    }
  }
  $("#quickLinks").change(function() {
    if ( this.value.length != 0 ) {
      window.location = '?cid=' + this.value;
    } else {
      this.selectedIndex = 0;
    }
  });
  $("#upcomingEvents").change(function() {
    if ( this.value.length != 0 ) {
      window.location = '?cid=8';
    } else {
      this.selectedIndex = 0;
    }
  });
});

function getTarget(x) {
  x = x || window.event;
  return x.target || x.srcElement;
}
function findValue(li) {
	if( li == null ) {
    var loc;
    switch( gti ) {
      case 6:
        if ($("#products input").val().length == 0 || $("#products input").val() == 'Search for Products') {
          loc = '?cid=6';
        } else {
          loc = '?cid=12&pn=' + $("#products input").val();
        }
        break;
      case 12:
        if ($("#companies input").val().length == 0 || $("#companies input").val() == 'Search for Companies') {
          loc = '?cid=51';
        } else {
          loc = '?cid=12&cn=' + $("#companies input").val();
        }
        break;
      case 7:
        if ($("#recipes input").val().length == 0 || $("#recipes input").val() == 'Search for Recipes') {
          loc = '?cid=7';
        } else {
          loc = '?cid=7&rn=' + $("#recipes input").val();
        }
        break;
    }
  } else {
    if ( gti == 6 ) {
      loc = '?cid=12&pn=' + $("#products input").val();
    } else if ( gti == 12 ) {
      loc = '?cid=12&cn=' + $("#companies input").val();
    } else {
        
      if (li.extra[1] == 'on') // featured
        loc = '?cid=75&id=' + li.extra[0] + '&featured=true';
      else
        loc = '?cid=' + gti + '&id=' + li.extra[0];
    }
  }
  window.location = 'http://www.selectnovascotia.ca/' + loc;
}
function formatItem(row) {
	return row[0];
}
function s(el) {
	var oSuggest = $("#" + el.parentNode.id + " input")[0].autocompleter;
	oSuggest.findValue();
  return false;
}

function ismaxlength(obj)
{
  var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
  if (obj.getAttribute && obj.value.length>mlength)
  {
    obj.value=obj.value.substring(0,mlength);
  }
}

function cook(n,v,d) {
  var t = new Date();
  var e = new Date();
  if (d==null || d==0) d=1;
  e.setTime(t.getTime() + 3600000*24*d);
  document.cookie = n+"="+escape(v) + ";expires="+e.toGMTString();
}