jQuery.fn.extend({
	toycode_setOuterRect: function( r) {
		return this.each( function() {
			var my = jQuery(this);
			my.css( {position:'absolute'});
			var dw = my.outerWidth() - my.width();
			var dh = my.outerHeight() - my.height();
			return my.css( {left:r.x, top:r.y}).width( r.w - dw).height( r.h - dh);
		});
	}
});
toycode = {};
toycode.breadcrumb = function(){
	jQuery('.breadcrumb li:gt(0)').prepend('&gt;');
};
toycode.getCookie = function(name) {
	name = encodeURIComponent( name);
	var list = document.cookie.replace(/\s/g,'').split(';');
	for( var i in list){
		var item = list[i].split('=');
		if( item[0] == name){
			return decodeURIComponent(item[1]);
		}
	}
	return undefined; 
};
toycode.setCookie = function(name,value) {
	document.cookie = encodeURIComponent( name) + '=' +encodeURIComponent(value) + ';' ;
};
toycode.getCookieBool = function(name) {
	var value = toycode.getCookie( name);
	if( value == 'false' || value == undefined || value == '0'){
		return false;
	}else{
		return true;
	}
};
toycode.setCookieBool = function(name,value) {
	if( value == true){
		toycode.setCookie( name, 'true');
	}else{
		toycode.setCookie( name, 'false');
	}
};
toycode.doLayout = function(on){
	var HEADER_H = 40;
	var FOOTER_H = 70;
	var MENU_W = 180;
	var MIN_CONTENTS_H = 90;
	var HANDLE_H = 10;
	var HANDLE_W = 8;
	var win = jQuery(window);
	var totalw = win.width();
	var totalh = win.height();
	if( totalh < HEADER_H + FOOTER_H + HANDLE_H*2 + MIN_CONTENTS_H){
		totalh = HEADER_H + FOOTER_H + HANDLE_H*2 + MIN_CONTENTS_H;
	}	
	var contentsh = totalh - HANDLE_H*2;
	var y = 0;
	if( toycode.headerOff){
		jQuery('#header').css( {display:'none'});
	}else{
		jQuery('#header').css( {display:'block'}).toycode_setOuterRect( {x:0, y:y, w:totalw, h:HEADER_H});
		y += HEADER_H;
		contentsh -= HEADER_H;
	}
	jQuery('#headerhandle').toycode_setOuterRect( {x:0, y:y, w:totalw, h:HANDLE_H}); 
	y += HANDLE_H;
	if( toycode.footerOff == false){
		contentsh -= FOOTER_H;
	}
	if( jQuery('#menu').length != 0){
		var x = 0;
		if( toycode.menuOff){
			jQuery('#menu').css( {display:'none'});
		}else{
			jQuery('#menu').css( {display:'block'}).toycode_setOuterRect( {x:x, y:y, w:MENU_W, h:contentsh}); 
			x += MENU_W;
		}
		jQuery('#menuhandle').toycode_setOuterRect( {x:x, y:y, w:HANDLE_W, h:contentsh});
		x += HANDLE_W;
		jQuery('#contents').toycode_setOuterRect( {x:x, y:y, w:(totalw-x), h:contentsh});
	}else{
		jQuery('#contents').toycode_setOuterRect( {x:0, y:y, w:totalw, h:contentsh});
	}
	y += contentsh;
	jQuery('#footerhandle').toycode_setOuterRect( {x:0, y:y, w:totalw, h:HANDLE_H}); 
	y += HANDLE_H;
	if( toycode.footerOff){
		jQuery('#footer').css( {display:'none'});
	}else{
		jQuery('#footer').css( {display:'block'}).toycode_setOuterRect( {x:0, y:y, w:totalw, h:FOOTER_H});
	}
};

toycode.headerOff = toycode.getCookieBool( 'headerOff');
toycode.menuOff = toycode.getCookieBool( 'menuOff');
toycode.footerOff = toycode.getCookieBool( 'footerOff'); 

toycode.initLayout = function(){
	var HANDLE_C = '#444';
	jQuery('body').css( {margin:0, border:0, padding:0, 'background-color':'#aaa'});
	jQuery('#header').after('<div id="headerhandle" />');
	jQuery('#headerhandle').css( {margin:0, border:0, padding:0, 'background-color':HANDLE_C});
	jQuery('#headerhandle').bind("click", function(){
		toycode.headerOff = ( toycode.headerOff == false);
		toycode.setCookieBool( 'headerOff', toycode.headerOff);
		toycode.doLayout();
	});
	if( jQuery('#menu').length != 0){
		jQuery('#menu').after('<div id="menuhandle" />');
		jQuery('#menuhandle').css( {margin:0, border:0, padding:0, 'background-color':HANDLE_C});
		jQuery('#menuhandle').bind("click", function(){
			toycode.menuOff = ( toycode.menuOff == false);
			toycode.setCookieBool( 'menuOff', toycode.menuOff);
			toycode.doLayout();
		});
	}
	jQuery('#footer').before('<div id="footerhandle" />');
	jQuery('#footerhandle').css( {margin:0, border:0, padding:0, 'background-color':HANDLE_C});
	jQuery('#footerhandle').bind("click", function(){
		toycode.footerOff = ( toycode.footerOff == false);
		toycode.setCookieBool( 'footerOff', toycode.footerOff);
		toycode.doLayout();
	});
	jQuery(window).resize( function(){
		toycode.doLayout();
	});
	toycode.doLayout();
};


