google.load("jquery", "1.4");
google.load("jqueryui", "1.8");


var $;

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-15073882-3']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
});


function ContentExposer(content, container, button) {
  this.content = content;
  this.container = container;
  this.mode = "fixed";
  this.fixedHeight = container.height();
  this.fullHeight = content.outerHeight(true) + 10;
  this.button = button;

  //Setter for mode with some associated functionality
  this.setMode = function(mode) {
    this.mode = mode;
    if (mode == 'full') {
      $.cookie("expose_mode", "full", {expires: 7});
      this.container.children().css('overflow', 'hidden');
      this.button.text('Collapse');
    }
    else {
      $.cookie("expose_mode", "fixed", { expires: 7});
      this.button.text('Show all content');
      this.container.children().css('overflow', 'auto');
    }   
  }


  this.senseMode = function() {
    if (Math.abs(this.container.height() - this.fullHeight) <= 3) {
      this.setMode('full');      
    }
    else {
      this.setMode('fixed');
    }
  }

  this._goFull = function() {
      this.container.animate({
	'height': this.fullHeight + 10
	}, 450, 'easeInOutBack');
      this.setMode('full');
  }
  this._goFixed = function() {
    this.container.animate(
    {
      'height': this.fixedHeight
	}, 450, 'easeInOutBack');
    this.setMode('fixed');
  }

  this.displayMode = function(mode) {
    if (mode == 'full') {
      this._goFull();
    }
    else {
      this._goFixed();
    }
  }


  
  this.toggle = function() {
    if (this.mode == "fixed") {
      this.displayMode('full');
    }
    else {
      this.displayMode('fixed');
    }
  }

  var expose_mode = $.cookie("expose_mode");
  this.displayMode(expose_mode);

}



function expandPane() {
  toggler.toggle();
}


function makeContentExposable() {
  var content_height = $("#page_content").height();

  if (content_height > $("#content_pane").height()) {


     var btn_div = $(document.createElement('div'));
     btn_div.attr('id', 'expand_button');

     var btn = $(document.createElement('a'));
     btn.text('Show all content');
     toggler = new ContentExposer($("#page_content"), $("#content_frame"), btn);
     btn.click(expandPane);
     btn_div.append(btn);
      
     $("#content").after(btn_div);
  $('#content_frame').resizable({
     handles: 's',
     minHeight: toggler.fixedHeight,
     maxHeight: toggler.fullHeight
  })
      .resize(function() {
      toggler.senseMode();
	});
  $("#content_frame .ui-resizable-handle").text('Drag to resize');
  }
}

google.setOnLoadCallback(function() {

    $ = jQuery;     


    //Pasted jQuery cookie plugin
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

	jQuery(".date_widget").datepicker();
	makeContentExposable();

  });

