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

$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});


function addFooterHandlers() {
 $('a#client_login', this).click(function() {
     $("#footer_content").load(this.href, addFooterHandlers);
     return false;
  });

$('a#client_log_in', this).click(function() {
     $("#footer_content").load(this.href, addFooterHandlers);
     return false;
  });
    
  $('#login_form').ajaxForm({target:'#footer_content', success: addFooterHandlers});
  
};

function addNavHandlers() {
  if(animate_portfolio) {
    $(".portfolio_items.hidden").show().slideUp("slow");
    $(".portfolio_items :not(.portfolio_items.hidden)").hide().slideDown("slow");
  }
  
}

function addPanelHandlers() {
  $(".rollover img,").hover(
   function()
   {
    this.src = this.src.replace("_up","_down");
   },
   function()
   {
    this.src = this.src.replace("_down","_up");
   }
  );
}

replace_ids = function(s){
  var new_id = new Date().getTime();
  return s.replace(/NEW_RECORD/g, new_id);
}
/*
'.add_nested_item': function(e){
  el = Event.findElement(e);
  template = eval(el.href.replace(/.*#/, ''))
  $(el.rel).insert({     
    bottom: replace_ids(template)
  });
*/
function addFormHandlers() {
  $('.add_nested_item').click(function(){
      template = eval(this.href.replace(/.*#/, ''))
      $("#"+this.rel).append(replace_ids(template));
      reorderImages();
  });
  
  $('#images').sortable({
    tolerance:'pointer', 
    handle:'img', 
    items:'.sortable', 
    containment:'#images_holder', 
    axis:'y', 
    update: reorderImages
  });
}
function reorderImages() {
  $('#images .sortable').each(function(i,e){
    $('.position_holder', e).attr("value", i+1);
  })
}
function init() {
  $(".form_button").corners();
  $('#wrapper').supersleight({shim: '/images/x.gif'});
  //$('#bottom_gear').supersleight({shim: '/images/x.gif'});
  addFormHandlers()
}

$(document).ready(addFooterHandlers);
$(document).ready(addNavHandlers);
$(document).ready(addPanelHandlers);
$(document).ready(init);


