function coffeeClickHandler()
{
    var chunk = this.id.split("_");
    var i;
    if(chunk[0].strlen == 2)
    {
        i = chunk[0].substr(1,1);
     }
    else
    {
        i = chunk[0].substr(1,2);
     }
    if(this.id.indexOf("_")<0) // hey its a root element of the coffee window
    {
        turnOff();
        CoffeeWindows.show("c"+i);
		document.getElementById("i"+i+"_1").src = "/images/explore/details_on.png";
		document.getElementById("i"+i+"_2").src = "/images/explore/reviews.png";
		document.getElementById("c"+i+"_1").style.display = "block";
		document.getElementById("c"+i+"_2").style.display = "none";
        setTimeout("turnSubDefaultOn()",500);
    }
    else // its a sub element
    {
		turnSubOff();
		if(this.id.indexOf("_1")>-1)
		{
			document.getElementById("i"+i+"_1").src = "/images/explore/details_on.png";
			document.getElementById("i"+i+"_2").src = "/images/explore/reviews.png";
			document.getElementById("c"+i+"_1").style.display = "block";
			document.getElementById("c"+i+"_2").style.display = "none";
		}
		else
		{
			document.getElementById("i"+i+"_2").src = "/images/explore/reviews_on.png";
			document.getElementById("i"+i+"_1").src = "/images/explore/details.png";
			document.getElementById("c"+i+"_1").style.display = "none";
			document.getElementById("c"+i+"_2").style.display = "block";
		}
    }
}
function turnOff()
{
    var id;
    //alert("in turn off");
    for(var i = 1; i < 19; i++)
    {
      id = "c"+i;
      if($(id).style.dispay=="block") $(id).style.display = "none";
    }
}
function turnSubOff()
{
	var id;
    var sub_id;
    for(var i = 1; i < 19; i++)
    {
    	id = "c"+i;
    	sub_id = id+"_1";
    	sub_id2 = id+"_2";
	    if($(sub_id).style.display == "block")
	    {
  			document.getElementById(sub_id).style.display = "none";
  			document.getElementById(sub_id2).style.display = "none";
	    }
    }
}
window.addEvent('domready', function()
{
  CoffeeDetails.init($('CoffeeDetailsForm'));
  Utility.turnOff("loadingIcon");
  for(var i=1;i<19;i++)
  {
      eval("$('t"+i+"').onclick=coffeeClickHandler");
      eval("$('c"+i+"_1').onclick=coffeeClickHandler");
      eval("$('i"+i+"_1').onclick=coffeeClickHandler");
      eval("$('c"+i+"_2').onclick=coffeeClickHandler");
      eval("$('i"+i+"_2').onclick=coffeeClickHandler");
  }
});
function turnSubDefaultOn()
{
    var id;
    var sub_id;
    for(var i = 1; i < 19; i++)
    {
    	id = "c"+i;
    	sub_id = id+"_1";
    	sub_id2 = id+"_2";
    		document.getElementById(sub_id).style.display = "block";
    		document.getElementById(sub_id2).style.display = "none";
    }
}

var CoffeeDetails =
{
    myVal:null,
    curform:null,
    init:function(curform)
    {
    	this.curform = curform;
    },
	step1:function()
    {
        this.myVal = new Validate("ValidationSummary");
        this.myVal.isEmpty("name","NameError","Please enter your name");
        this.myVal.isEmpty("city","CityError","Please enter the city you live in");
        this.myVal.isEmpty("state","StateError","Please enter your State/Province");
 		this.myVal.isEmpty("country","CountryError","Please enter your Country");
 		this.myVal.isEmail("email","EmailError","Please enter a valid Email Address");
 		this.myVal.isEmpty("story","StoryError","Please enter Your Peet&#8217;s Story");
 		this.myVal.isEmpty("story","StoryError","Please enter Your Peet&#8217;s Story");
        //track('cmCreateConversionEventTag("Coffee Comment",1)');

		// FINAL STEP
		if(this.myVal.isValid())
        {

            //this.curform.submit();
            Utility.turnOnInline("loadingIcon");

            var serializer = new Serializer();
            serializer.init(["name", "city", "state", "country", "email", "drinking", "story"]);
            var URL = "/callback/CoffeeDetails/?i=i"+ serializer.yield();
			URL = encodeURI(URL);

			var myAjax = new Ajax(URL,
			{
    			method: 'get',
    			onSuccess: function(responseText)
    			{
                    if(responseText == "sent")
			    	{
                        $("responseMessage").innerHTML = "Thank you for your submission. It will be reviewed by an Administrator.";
                        Utility.turnOff("loadingIcon");
                        track('cmCreateConversionEventTag("Coffee Comment","2","Site Events","0")');
			    	}
			    	else
			    	{
                        $("responseMessage").innerHTML = "An error has occured.";
                        Utility.turnOff("loadingIcon");
			    	}
			    	Utility.turnOn("responseMessage");
    			},
    			onFailure: function(sender,responseText)
    			{
    				//alert('failed request');
    				$("responseMessage").innerHTML = "An error has occured.";
                    Utility.turnOff("loadingIcon");
    			}
			}).request();
        }
        else
        {
            this.myVal.yield(false);
            return false;
        }
    }
}
