/* 

 * NOTE: This plugin depends on jQuery.  Download jQuery at www.jquery.com
 *
 */


/*
SCRATCH PAD


*/
(function(jQuery)
{
	var self = null;
	jQuery.fn.hideAway = function() {
		return this.each(function() {
			new jQuery.hideAway(this);
		});
	};
	
	jQuery.hideAway = function (e) {
		this.menuBar = $(e).children('.control');
		this.menuPanel = $(e).children('.menupanel');
		this.init();
	};
	
	jQuery.hideAway.fn = jQuery.hideAway.prototype = {
		hideAway: '0.0.1'
	};
	
	jQuery.hideAway.fn.extend = jQuery.hideAway.extend = jQuery.extend;
	jQuery.hideAway.fn.extend({
		init: function() {
			this.enabled = null;
			_self = this;
			$(window).resize(function() {
				_self.resize();
			});
			var panel = this.menuPanel;
			var bar = this.menuBar;
			this.menuBar.contents().wrap('<a href="javascript:">').parent().append('<span>-</span>');
			this.menuBar.closeMenu = function(){
				this.opened = false;
				this.children('a').children('span').text('+');
				panel.slideUp();
			};
				
			this.menuBar.openMenu = function(){
				this.opened = true;
				this.children('a').children('span').text('-');
				panel.slideDown();
			};
			
			this.menuBar.children('a').click(function(){
				if (bar.opened){
					bar.closeMenu();
				} else {
					bar.openMenu();
				}
			});
			_self.resize();
			
			
		},
		resize: function(){
			if (matchMedia("only screen and (max-width: 480px)").matches) {
				if (this.enabled != true){
					this.enable();
				}
			} else {
				if (this.enabled != false){
					this.disable();
				}
			}
		},
		disable: function() {
			this.enabled = false;
			this.menuBar.hide();
			this.menuBar.openMenu();
		},
		enable: function() {
			this.enabled = true;
			this.menuBar.show();
			this.menuBar.closeMenu();
		}
	});
})(jQuery);
