$(document).ready(function(){
  $("#new_user_session input:visible:enabled:first").focus();
  set_asset_check();
  $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
  $("#sub4nav").tabs({
    idPrefix: 'page_',
    spinner: null,
    load: function(event, ui) {
      p = $(ui.tab).attr("rel").split("_");
      page_id = p[p.length-1];
      $.ajax({dataType:'script', type:'get', url:pages_path + "/" + page_id + '/extra.js'});
      return false;
    }
  });
  set_tree($("#page_index ul:first li:first ul:first"));
  $('#illustration').cycle({ 
    fx:      'fade', 
    speed:    3000, 
    timeout:  3000 
  });

	$("#slider").easySlider({
	  prevText: '<span>prev</span>',
	  nextText: '<span>next</span>',
	  speed: 300
	});

});

function set_asset_check()
{
  $('#assets input:checkbox').each(function(){
    $(this).change(function(){
      $(this).attr("value", $(this).attr("checked")); 
      the_form = $(this).parents('.asset_form:first');
      css_class = "";
      
      if ($(this).hasClass("asset_visible")) {
        css_class = "asset_form_invisible";
        if (!$(this).attr("checked")){
          the_form.addClass(css_class);
        } else {
          the_form.removeClass(css_class);
        }
      }
      //I can't use elseif :-P
      if ($(this).hasClass("asset_remove_check")){
        css_class = "asset_form_removed";
        if (!$(this).attr("checked")){
          the_form.removeClass(css_class);
          the_form.children("input").enable();
          the_form.children("select").enable();
          the_form.children("textarea").enable();        
        } else {
          the_form.addClass(css_class);
          the_form.children("input").disable();
          the_form.children("select").disable();
          the_form.children("textarea").disable();
          $(this).enable();
        }
      }
    })
  });
}

function set_tree(ele)
{
  if(ele.size() > 0) {
    ele.quickTree();
    //bd TODO show a node
    url = window.location.href;
    pos = url .indexOf("#");
    if(pos > 0) {
      jump_to = url.substr(pos);
      $(jump_to).parents('li').children("span.expand").toggleClass('contract').nextAll('ul').slideDown();
      $(jump_to).pulse({backgroundColors: ['#ffffff','#dedede'], duration: 3000});
    }
  }
}
