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

function mark_for_destroy(element) {
	$(element).next('.should_destroy').value = 1;
	//$(element).up('.image').hide();
	$(element).up('.product').hide();
}

function remove_field(element, item) {
  element.up(item).remove();
}

// READ THE COOKIE AND READ THEN LOGIN THEN SET SWITCH THE USER_BAR DIVS
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length)).replace("+"," ");
  }
  return null;
}

function handle_cached_user() {
	var login_cookie = readCookie('login');
  var logged_in = $('logged_in');
  var logged_out = $('logged_out');
  if(login_cookie == null) {
    logged_in.style.display = 'none';
    logged_out.style.display = 'block';
  } else {
    handle_user_partial();
    logged_out.style.display = 'none';
    logged_in.style.display = 'block';
  }
}

function handle_user_partial() {
  document.getElementById('profile_link').href = '/cooks/' + readCookie('label');
  document.getElementById('user_short_name').firstChild.nodeValue=readCookie('login');
}

replace_ids = function(s){
  var new_id = new Date().getTime();
  return s.replace(/NEW_RECORD/g, new_id);
}

// Hides the recipe submit button and replace it with a note asking the user to please wait
function recipe_submit_please_wait() {
	$("recipe-submit-button").style.display  = "none";
	$("recipe-submit-waiting").style.display = "block";
}

// Hides the comment submit button and replaces it with a note asking the user to please wait
function comment_submit_please_wait(submit_id, wait_id){
	$(submit_id).style.display  = "none";
	$(wait_id).style.display = "block";
}

// Restores the comment submit button and hides the please wait message so that the user can add another comment
function comment_submit_stop_waiting(){
	$$('.comment-submit-button').each(function(x){ x.style.display = 'block';});
	$$(".comment-waiting").each(function(x){ x.style.display = 'none';});
}

function toggleEmailForm(){
	$('email_me').toggleClassName('focus');
	$('email_user_container').toggle();
	new Effect.Move('email_user_container', { x: 141, y: 280, mode: 'absolute',duration:0.2 });
	$('email_me').blur();
	return false;
}

function selectTab(moduleName, tabClassName){
	// remove selected from all tabs
  $$("#" + moduleName + "-tabs li").each(function(x){
	  x.removeClassName('selected');
  });
  // remove selected from all content boxes
  $$("." + moduleName + " .tab-content").each(function(x){
	  x.removeClassName('selected');
  });
  // turn on the selected tab and content box
  $$("." + tabClassName).each(function(x){
	  x.addClassName('selected');
  });
}

//var myrules = {
//  '.remove': function(e){
//    el = Event.findElement(e);
//    target = el.href.replace(/.*#/, '.')
//    el.up(target).hide();
//    if(hidden_input = el.previous("input[type=hidden]")) hidden_input.value = '1'
//  },
//  '.add_nested_item': function(e){
//    el = Event.findElement(e);
//    template = eval(el.href.replace(/.*#/, ''))
//    $(el.rel).insert({
//      bottom: replace_ids(template)
//    });
//  }
//};

/*
document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
    var img = new Image
    img.src = '/images/spinner.gif'

    function createSpinner() {
      new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }
})
*/

//Event.observe(window, 'load', function(){
//  $('content').delegate('click', myrules);
//});



