var Cheecha = {};
//////////////////////////////////////////////////////////////////
function test()
{
  alert("working");
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.setOriginal = function(isHome)
{
  if(isHome)
    $("div#masterContainer").css("background-image", "url(img/backgrounds/home-original.jpg)");
  else
    $("div#masterContainer").css("background-image", "url(img/backgrounds/original.jpg)");
  
  $("body").css("background-image", "url(img/backgrounds/original-gradient.png)");
  $("body").css("background-color", "rgb(191, 112, 45)");
  
  document.cookie = "theme=original;max-age=31536000";
  return;
}
//////////////////////////////////////////////////////////////////
 Cheecha.setLime = function(isHome)
 {
   if(isHome)
     $("div#masterContainer").css("background-image", "url(img/backgrounds/home-lime.jpg)");
   else
     $("div#masterContainer").css("background-image", "url(img/backgrounds/lime.jpg)");

   $("body").css("background-image", "url(img/backgrounds/lime-gradient.png)");
   $("body").css("background-color", "rgb(189, 209, 88)");
  
   document.cookie = "theme=lime;max-age=31536000"; 
   return;
 }
//////////////////////////////////////////////////////////////////
Cheecha.setPepper = function(isHome)
{
  if(isHome)
    $("div#masterContainer").css("background-image", "url(img/backgrounds/home-salt-pepper.jpg)");
  else
    $("div#masterContainer").css("background-image", "url(img/backgrounds/salt-pepper.jpg)");

  $("body").css("background-image", "url(img/backgrounds/salt-pepper-gradient.png)");
  $("body").css("background-color", "rgb(175, 175, 175)");
  
  document.cookie = "theme=pepper;max-age=31536000";
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.setVinegar = function(isHome)
{
  if(isHome)
    $("div#masterContainer").css("background-image", "url(img/backgrounds/home-salt-vinegar.jpg)");
  else
    $("div#masterContainer").css("background-image", "url(img/backgrounds/salt-vinegar.jpg)");

  $("body").css("background-image", "url(img/backgrounds/salt-vinegar-gradient.png)");
  $("body").css("background-color", "rgb(110, 180, 206)");
  
  document.cookie = "theme=vinegar;max-age=31536000";
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.setGinger = function(isHome)
{
  if(isHome)
    $("div#masterContainer").css("background-image", "url(img/backgrounds/home-ginger.jpg)");
  else
    $("div#masterContainer").css("background-image", "url(img/backgrounds/ginger.jpg)");

  $("body").css("background-image", "url(img/backgrounds/ginger-gradient.png)");
  $("body").css("background-color", "rgb(215, 189, 44)");
  
  document.cookie = "theme=ginger;max-age=31536000";
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.imageToolTip = function(event)
{
  return;
}
//////////////////////////////////////////////////////////////////  
Cheecha.growProduct = function(event)
{
  $(this).animate({width: "86px", height: "139px", top: "-12px"}, "fast");
  $("div#toolTip").stop(true, true);
  $("div#toolTip").fadeIn("slow");
  $("div#toolTip").text($(this).attr("alt"));
  $("img#largeShowProduct")[0].src = this.src.replace("small", "large");
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.shrinkProduct = function(event)
{
  $(this).animate({width: "78px", height: "127px", top: "0px"}, "fast");
  $("div#toolTip").stop(true, true);
  $("div#toolTip").fadeOut("slow");
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.mouseInfo = function(event)
{
  $("div#toolTip").css({"left": event.pageX+14, "top": event.pageY});
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.cheechaSelect = function(event)
{
  var selectedItem = this.options[this.selectedIndex].value;

  if (selectedItem.length == 0)
    return;

  $("img#cheechaNutritionalImage")[0].src = "img/nutrition/" + selectedItem;
  Cheecha.updateNutritionalComparison();
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.otherSelect = function(event)
{
  var selectedItem = this.options[this.selectedIndex].value;

  if (selectedItem.length == 0)
    return;

  $("img#otherNutritionalImage")[0].src = "img/nutrition/" + selectedItem;
  Cheecha.updateNutritionalComparison();
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.updateNutritionalComparison = function()
{
  var cheechItem = $("select#cheechaSelect").get(0).options[$("select#cheechaSelect").get(0).selectedIndex].value;
  var otherItem  = $("select#otherSelect").get(0).options[$("select#otherSelect").get(0).selectedIndex].value;
  var directory = "";

  if ((cheechItem.length == 0) ||
      (otherItem.length == 0))
    return;

  if(cheechItem.search(/krackles/i) != -1)
    directory = "vs-krackles/";
  else if(cheechItem.search(/puffs/i) != -1)
    directory = "vs-puffs/";
  
  $("img#comparisonResult").get(0).src = "img/nutrition/" + directory + otherItem;  
  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.setClickMode = function()
{
  $("map#canadianImageMap area").unbind("mouseleave mouseenter click").bind("click", Cheecha.hideLists);
  
  $("area#Alberta").bind("click", function(){$("ul#albertaList").show();});
  $("area#BritishColumbia").bind("click", function(){$("ul#britishColumbiaList").show();});
  $("area#Saskatchewan").bind("click", function(){$("ul#saskatchewanList").show();});
  $("area#Manitoba").bind("click", function(){$("ul#manitobaList").show();});
  $("area#Ontario").bind("click", function(){$("ul#ontarioList").show();});
  $("area#Quebec").bind("click", function(){$("ul#quebecList").show();});
  $("area#NovaScotia").bind("click", function(){$("ul#novaScotiaList").show();});
  $("area#NewBrunswick").bind("click", function(){$("ul#newBrunswickList").show();});
  $("area#NewfoundLand").bind("click", function(){$("ul#newfoundlandList").show();});
  $("area#Pei").bind("click", function(){$("ul#peiList").show();});

  return;
}
//////////////////////////////////////////////////////////////////
Cheecha.hideLists = function()
{
  $("ul#albertaList").hide();
  $("ul#britishColumbiaList").hide();
  $("ul#saskatchewanList").hide();
  $("ul#manitobaList").hide();
  $("ul#ontarioList").hide();
  $("ul#quebecList").hide();
  $("ul#novaScotiaList").hide();
  $("ul#newBrunswickList").hide();
  $("ul#newfoundlandList").hide();
  $("ul#peiList").hide();
  return;
}
//////////////////////////////////////////////////////////////////  
Cheecha.start = function()
{
  var strCookies = document.cookie;
  var iPos = strCookies.indexOf("theme=");
  
  if(undefined == Cheecha.home)
    Cheecha.home = false;
  
  if(iPos != -1)//if cookie set
  {
    var iStart = iPos +  6;
    var iEnd = strCookies.indexOf(";", iStart);
    if (iEnd == -1)
      iEnd = strCookies.length;
    
    var strValue = strCookies.substring(iStart, iEnd);
    
    switch(strValue)
    {
      case "original":
        Cheecha.setOriginal(Cheecha.home);
        break;
      case "pepper":
        Cheecha.setPepper(Cheecha.home);
        break;
      case "vinegar":
        Cheecha.setVinegar(Cheecha.home);
        break;
      case "lime":
      default:
        Cheecha.setLime(Cheecha.home);
        break;  
	  case "ginger":
		Cheecha.setGinger(Cheecha.home);
		break;
    }
  }
  //if no cookie set
  else
    Cheecha.setLime(Cheecha.home);
  

  $("img.showProduct").mouseover(Cheecha.growProduct);
  $("img.showProduct").bind("mouseleave", Cheecha.shrinkProduct);
  $("img.showProduct").mousemove(Cheecha.mouseInfo);

  $("div#originalThemeSelect").bind("click", function(){Cheecha.setOriginal(Cheecha.home);});
  $("div#limeThemeSelect").bind("click", function(){Cheecha.setLime(Cheecha.home);});
  $("div#saltAndPepperThemeSelect").bind("click", function(){Cheecha.setPepper(Cheecha.home);});
  $("div#saltAndVinegarThemeSelect").bind("click", function(){Cheecha.setVinegar(Cheecha.home);});
  $("div#gingerThemeSelect").bind("click", function(){Cheecha.setGinger(Cheecha.home);});

  $("map#canadianImageMap area").click(Cheecha.setClickMode);


  $("area#Alberta").bind("mouseenter", function(){$("ul#albertaList").show();});
  $("area#Alberta").bind("mouseleave", function(){$("ul#albertaList").hide();});

  $("area#BritishColumbia").bind("mouseenter", function(){$("ul#britishColumbiaList").show();});
  $("area#BritishColumbia").bind("mouseleave", function(){$("ul#britishColumbiaList").hide();});

  $("area#Saskatchewan").bind("mouseenter", function(){$("ul#saskatchewanList").show();});
  $("area#Saskatchewan").bind("mouseleave", function(){$("ul#saskatchewanList").hide();});

  $("area#Manitoba").bind("mouseenter", function(){$("ul#manitobaList").show();});
  $("area#Manitoba").bind("mouseleave", function(){$("ul#manitobaList").hide();});

  $("area#Ontario").bind("mouseenter", function(){$("ul#ontarioList").show();});
  $("area#Ontario").bind("mouseleave", function(){$("ul#ontarioList").hide();});

  $("area#Quebec").bind("mouseenter", function(){$("ul#quebecList").show();});
  $("area#Quebec").bind("mouseleave", function(){$("ul#quebecList").hide();});

  $("area#NovaScotia").bind("mouseenter", function(){$("ul#novaScotiaList").show();});
  $("area#NovaScotia").bind("mouseleave", function(){$("ul#novaScotiaList").hide();});

  $("area#NewBrunswick").bind("mouseenter", function(){$("ul#newBrunswickList").show();});
  $("area#NewBrunswick").bind("mouseleave", function(){$("ul#newBrunswickList").hide();});

  $("area#NewfoundLand").bind("mouseenter", function(){$("ul#newfoundlandList").show();});
  $("area#NewfoundLand").bind("mouseleave", function(){$("ul#newfoundlandList").hide();});

  $("area#Pei").bind("mouseenter", function(){$("ul#peiList").show();});
  $("area#Pei").bind("mouseleave", function(){$("ul#peiList").hide();});      

  $("select#cheechaSelect").change(Cheecha.cheechaSelect);
  $("select#otherSelect").change(Cheecha.otherSelect);

  $("div#homeLinkContainer a").tooltip({showURL: false});

  $("div#themeSelectContainer div").tooltip();

  return;
}
//////////////////////////////////////////////////////////////////
$(document).ready(Cheecha.start);
