/* $Id: admin_devel.js,v 1.2 2010/03/12 22:54:41 sun Exp $ */
(function($) {

/**
 * jQuery debugging helper.
 *
 * Invented for Dreditor.
 *
 * @usage
 *   $.debug(var [, name]);
 *   $variable.debug( [name] );
 */
jQuery.extend({
  debug: function () {
    // Setup debug storage in global window. We want to look into it.
    window.debug = window.debug || [];

    args = jQuery.makeArray(arguments);
    // Determine data source; this is an object for $variable.debug().
    // Also determine the identifier to store data with.
    if (typeof this == 'object') {
      var name = (args.length ? args[0] : window.debug.length);
      var data = this;
    }
    else {
      var name = (args.length > 1 ? args.pop() : window.debug.length);
      var data = args[0];
    }
    // Store data.
    window.debug[name] = data;
    // Dump data into Firebug console.
    if (typeof console != 'undefined') {
      console.log(name, data);
    }
    return this;
  }
});
// @todo Is this the right way?
jQuery.fn.debug = jQuery.debug;

})(jQuery);
;
(function ($) {

  Drupal.behaviors.au = {
    attach: function (context, settings) {
      
      //behavior of the intro block link
      $("#block-au-welcome a.control").click(function() {
        //set the "no show" cookie if needed
        if ( $(this).hasClass("closed") ) {
          //it's being manually opened. kill the cookie
          setCookie("auwelcome","",0);
        } else {
          //it's been closed. set the cookie
          setCookie("auwelcome","never",5000); //5000 days is more or less "forever"
        }
        curr_text = $(this).html();
        $(this).delay(600).toggleClass("closed").html( $(this).attr("data-text") ).attr('data-text',curr_text);
        $("#block-au-welcome .lower").slideToggle();
      });
      
      //intro block cookie behaviors
      var cookie_val = getCookie("auwelcome");
      if (!cookie_val) {
        //cookie doesn't exist yet. Show the content, then reset the cookie as a counter
        $("#block-au-welcome a.control.closed").click();
        setCookie("auwelcome",1,90);  //counter only lasts 3 months
      } else if (cookie_val == "never") {
        //do nothing to the cookie
      } else if (cookie_val >= 30) {
        //they've visited more than 30 pages in last 90 days. Assume they want it closed now...
        setCookie("auwelcome","never",5000);
      } else {
        //they've been here before. Show the content and increment cookie counter
        $("#block-au-welcome a.control.closed").click();
        setCookie("auwelcome", (cookie_val*1)+1,30);  //cast as int
      }
      
      //signup form label hiding
      $("#block-au-takeaction label").click( function() {
        var t = $(this),
            inputId = t.attr('for'),
            input = $('#'+inputId);
        if(input.length && input.attr('type') in {'text': '', 'email': '', 'search': '', 'url': ''}) {
          $(this).fadeOut('fast');    
        }
      });
      $("#block-au-takeaction input.form-text").focus( function() {
        $(this).prev("label").fadeOut('fast');
      } ).blur( function() {
        if ( $(this).val() == "") {
          $(this).prev('label').fadeIn('fast');
        }
      }).each(function(){
        if ( $(this).val() == "") {
          $(this).prev('label').fadeIn('fast');
        }
      });
    }
  };

}(jQuery));

//helper functions
function setCookie(c_name,value,exdays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  if (exdays == 0) exdate.setDate(exdate.getDate() - 10); //set a date in the past
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++) {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name) {
      return unescape(y);
    } 
  }
  return false;
}
;

