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


$(function(){
  $('ul.top-menu > li').has('ul').hover(
    function(){
      $(this).children('ul').slideDown(200);
    },
    function(){
      $(this).children('ul').slideUp(300);
    }
  );

  hopville = new Hopville();
  hopville.activateRecipe();

  calculator = new Hopville.Calculator();
  if (isBeerCalculus) {
    calculator.activate();
  }
  // link_to_remote(edit_button, {:url => {:controller => "calculator", :action => "ferment_manager", :recipe_id => @recipe.id}, :update => "ferment-switcheroo"})

  $('.teaser-text').live("focus", function(){
    $(this).val("").removeClass('teaser-text');
  });

  $('.defaulted').live("focus", function() {
    var title = $(this).attr('title');
    if ($(this).val() == title) {
      $(this).val("");
    };
  });

  $('a.new-window').click(function(){
      window.open(this.href);
      return false;
  });
});

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
        } else {
            begin += 2;
            var end = document.cookie.indexOf(";", begin);
            if (end == -1){
                end = dc.length;
            }
        }
    return unescape(dc.substring(begin + prefix.length, end));
};

function setCookie(name, value, expires, domain) {
	var curCookie = name + "=" + escape(value) +
              ((expires) ? "; expires=" + expires.toGMTString() : "") +
              "; path=/" +
              ((domain) ? "; domain=" + domain : "");
      document.cookie = curCookie;
};

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" + 
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};

var hopAlphas = new Array;

function updateAlpha(that){
   document.getElementById('hop-new-alpha').selectedIndex = (hopAlphas[that.selectedIndex] * 10 - 10);
}


function switchHopTimeOptions(usage) {
    if (usage == 'dry hop') {
        $('#hop-days').show();
        $('#hop-minutes').hide();
				$('#hop-minutes').disabled = true;
		} else if (usage == 'first wort') {
      $('#hop-days').hide();
      $('#hop-minutes').show();
			lastOptionIndex = $('#hop-minutes').children().length - 1;
			$('hop-minutes').selectedIndex = lastOptionIndex;
			$('hop-minutes').disabled = true;
    } else {
        $('#hop-days').hide();
				$('#hop-minutes').disabled = false;
        $('#hop-minutes').show();
    }
}

function switchMiscTimeUnit(rowID, select) {
	if ((select.value == 'mash') || (select.value == 'boil')) {
		value = 'mins'
	} else {
		value = 'days'
	}
	$('#misc-' + rowID + '-time-unit').html(value);
};



function checkHopAmount() {
  if (document.getElementById('new-hop-ounces').selectedIndex == 0) {
    alert("Please select the amount for your hop addition - currently you are adding a zero amount.");
		return false;
  }
}

	
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id)
{      
       // cancel close timer
       mcancelclosetime();

       // close old layer
       if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

       // get new layer and show it
       ddmenuitem = document.getElementById(id);
       ddmenuitem.style.visibility = 'visible';
}
// close showed layer
function mclose()
{
       if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
       closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
       if(closetimer)
       {
               window.clearTimeout(closetimer);
               closetimer = null;
       }
}

// close layer when click-out
document.onclick = mclose; 

var searchState = Array;
searchState['malt'] = false;
searchState['hops'] = false;
searchState['yeast'] = false;
searchState['misc'] = false;

function toggleAjaxSearch(mode) {
  if (searchState[mode]) {
    $('#' + mode + '-search').hide();
    $('#' + mode + '-search-off').hide();
    $('#' + mode + '-search-on').show();
    $('new-' + mode + '-name').value = '';
    searchState[mode] = false;
  } else {
    $('#' + mode + '-search').show();
    $('#' + mode + '-search-on').hide();
    $('#' + mode + '-search-off').show();  
    searchState[mode] = true;
  }
}

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

function selectAjaxIngredient(mode, ingredient_id, select_element_id) {
  var selectElement = document.getElementById(select_element_id);
  selectElement.value = ingredient_id;
  toggleAjaxSearch(mode);
}

