/*
 * FBBorderLayout - a jQuery "border layout"
 * Version 0.3 - 5th August 2008
 *
 * Copyright (c) 2008 Fabrizio Balliano (http://fabrizioballiano.net).
 * Dual licensed under the GPL (http://www.gnu.org/licenses/gpl.html)
 * and MIT (http://www.opensource.org/licenses/mit-license.php) licenses.
 */

jQuery.FBBorderLayout = function (custom_config)
{
	
        var defaultConfig = {
		spacing: 5,
		north_collapsable: true,
		north_resizable: false,
		east_width: 200,
		east_collapsable: true,
		east_resizable: false,
		south_collapsable: true,
		south_resizable: false,
		west_width: 200,
		west_collapsable: true,
		west_resizable: false
	}
	
	var numCurCSS = function (object, property) {
		return parseInt(jQuery.curCSS(object[0], property, true), 10) || 0;
	}
	
	var collapseNorth = function () {
		$center.css("top", config.spacing);
		$east.css("top", config.spacing);
		$west.css("top", config.spacing);
		jQuery(".fbbl_north_collapser").css("top", 0);
		$north.css({
			zIndex:1000
		}).hide();
		apply();
	}
	
	var collapseEast = function () {
		$center.css("right", config.spacing);
		jQuery(".fbbl_east_collapser").css("right", 0);
		$east.css({
				zIndex: 1000
		}).hide();
		apply();
	}
	
	var collapseSouth = function () {
		$center.css("bottom", config.spacing);
		$east.css("bottom", config.spacing);
		$west.css("bottom", config.spacing);
		jQuery(".fbbl_south_collapser").css("bottom", 0);
		$south.css({
			zIndex:1000
		}).hide();
		apply();
	}
	
	var collapseWest = function () {
		$center.css("left", config.spacing);
		jQuery(".fbbl_west_collapser").css("left", 0);
		$west.css({
				zIndex: 1000
		}).hide();
		apply();
	}
	
	var expandNorth = function () {
		if ($north.is(':visible')) return;
		$north.show();
		apply();
	}
	
	var expandEast = function () {
		if ($east.is(':visible')) return;
		$east.show();
		apply();
	}
	
	var expandSouth = function () {
		if ($south.is(':visible')) return;
		$south.show();
		apply();
	}
	
	var expandWest = function () {
		if ($west.is(':visible')) return;
		$west.show();
		apply();
	}
	
	var apply = function () {
		center_top = config.spacing;
		center_right = config.spacing;
		center_bottom = config.spacing;
		center_left = config.spacing;
		
		$html.css({
			height: '100%'
		});
		
		$body.css({
			position: 'relative',
			overflow: 'hidden',
			height: '100%',
			margin: 0,
			padding: 0
		});
		
		if ($north.length) {
			if ($north.is(':visible')) {
				$north.css({
					zIndex: 0,
					width: 'auto'
				});
				center_top += $north.outerHeight();
			}
			if (config.north_collapsable) {
				jQuery('.fbbl_north_collapser').css({
					position: 'absolute',
					cursor: 'pointer',
					zIndex: 0,
					fontSize: '1px',
					width: 50,
					height: config.spacing,
					top: center_top - config.spacing,
					left: ($window.width()-50)/2
				}).appendTo($body).toggle(collapseNorth, expandNorth);
			}
		}
		
		if ($south.length) {
			if ($south.is(':visible')) {
				$south.css({
					position: 'absolute',
					zIndex: 0,
					top: 'auto',
					right: 0,
					bottom: 0,
					left: 0,
					width: 'auto'
				});
				center_bottom += $south.outerHeight();
				if (jQuery.browser.msie) $south.width('100%');
			}
			if (config.south_collapsable) {
				jQuery('.fbbl_south_collapser').css({
					position: 'absolute',
					cursor: 'pointer',
					zIndex: 0,
					fontSize: '1px',
					width: 50,
					height: config.spacing,
					bottom: center_bottom - config.spacing,
					left: ($window.width()-50)/2
				}).appendTo($body).toggle(collapseSouth, expandSouth);
			}
		}
	
		if ($west.length) {
			if ($west.is(':visible')) {
				$west.css({
					position: 'absolute',
					//overflow: 'auto , pix coment
					zIndex: 0,
					top: center_top,
					bottom: center_bottom,
					left: 0,
					width: config.west_width - numCurCSS($west, 'borderLeftWidth') - numCurCSS($west, 'borderRightWidth') - numCurCSS($west, 'paddingLeft') - numCurCSS($west, 'paddingRight'),
					height: 'auto'
				});
				if (jQuery.browser.msie) {
					$west.width(config.west_width).height($window.height() - center_top - center_bottom);
				} else if (jQuery.browser.opera) {
					$west.height($window.height() - center_top - center_bottom - numCurCSS($west, 'borderTopWidth') - numCurCSS($west, 'borderBottomWidth') - numCurCSS($west, 'paddingTop') - numCurCSS($west, 'paddingBottom'));
				}
				center_left += $west.outerWidth();
			}
			if (config.west_collapsable) {
				jQuery('.fbbl_west_collapser').css({
					position: 'absolute',
					cursor: 'pointer',
					zIndex: 0,
					width: config.spacing,
					height: 50,
					top: center_top + ($west.outerHeight()-50)/2,
					left: center_left - config.spacing
				}).appendTo($body).toggle(collapseWest, expandWest);
			}
		}
	
		if ($east.length) {
			if ($east.is(':visible')) {
				$east.css({
					position: 'absolute',
					overflow: 'auto',
					zIndex: 0,
					top: center_top,
					right: 0,
					bottom: center_bottom,
					left: 'auto',
					width: config.east_width - numCurCSS($east, 'borderLeftWidth') - numCurCSS($east, 'borderRightWidth') - numCurCSS($east, 'paddingLeft') - numCurCSS($east, 'paddingRight'),
					height: 'auto'
				});
				if (jQuery.browser.msie) {
					$east.width(config.east_width).css({
						left: $window.width() - $east.outerWidth(),
						height: $window.height() - center_top - center_bottom
					});
				} else if (jQuery.browser.opera) {
					$east.height($window.height() - center_top - center_bottom - numCurCSS($east, 'borderTopWidth') - numCurCSS($east, 'borderBottomWidth') - numCurCSS($east, 'paddingTop') - numCurCSS($east, 'paddingBottom'));
				}
				center_right += $east.outerWidth();
			}
			if (config.east_collapsable) {
				jQuery('.fbbl_east_collapser').css({
					position: 'absolute',
					cursor: 'pointer',
					zIndex: 0,
					width: config.spacing,
					height: 50,
					top: center_top + ($east.outerHeight()-50)/2,
					right: center_right - config.spacing
				}).appendTo($body).toggle(collapseEast, expandEast);
			}
		}
	
		if ($center.length) {
                   
			$center.css({
				position: 'absolute',
				zIndex: 0,
				top: center_top,
				right: center_right,
				bottom: center_bottom,
				left: center_left,
				overflow: 'auto'
			});
			if (jQuery.browser.msie) {
                              
				$center.width($window.width() - center_left - center_right);
				$center.height($window.height() - center_top - center_bottom);
			}
		}
                
	}
	
	var $window = jQuery(window);
	var $html = jQuery("html");
	var $body = jQuery("body");
	var config = jQuery.extend(defaultConfig, custom_config);
	var $north = jQuery("body > .fbbl_north");
	var $east = jQuery("body > .fbbl_east");
	var $south = jQuery("body > .fbbl_south");
	var $west = jQuery("body > .fbbl_west");
	var $center = jQuery("body > .fbbl_center");
	var center_top = config.spacing;
	var center_right = config.spacing;
	var center_bottom = config.spacing;
	var center_left = config.spacing;
	
	$window.resize(apply);
	if (config.north_collapsable && $north.length) jQuery("<div class='fbbl_north_collapser'></div>").appendTo($body);
	if (config.east_collapsable && $east.length) jQuery("<div class='fbbl_east_collapser'></div>").appendTo($body);
	if (config.south_collapsable && $south.length) jQuery("<div class='fbbl_south_collapser'></div>").appendTo($body);
	if (config.west_collapsable && $west.length) jQuery("<div class='fbbl_west_collapser'></div>").appendTo($body);
	apply();
	
	if (typeof jQuery.fn.resizable == "function") {
		if (config.north_resizable && $north.length) {
			$north.resizable({
				handles: 's',
				transparent: true,
				start: function () {
					$north.css('z-index', 1000);
				},
				stop: apply
			});
		}
		if (config.east_resizable && $east.length) {
			$east.resizable({
				handles: 'w',
				transparent: true,
				start: function () {
					$east.css('z-index', 1000);
				},
				stop: function() {
					config.east_width=$east.outerWidth();
					apply();
				}
			});
		}
		if (config.south_resizable && $south.length) {
			$south.resizable({
				handles: 'n',
				transparent: true,
				start: function () {
					$south.css('z-index', 1000);
				},
				stop: apply
			});
		}
		if (config.west_resizable && $west.length) {
			$west.resizable({
				handles: 'e',
				transparent: true,
				start: function () {
					$west.css('z-index', 1000);
				},
				stop: function() {
					config.west_width=$west.outerWidth();
					apply();
				}
			});
		}
	}
}