/**
 * @version $Id: jquery.bb.js 44063 2010-02-08 18:18:58Z a.chuprina $
 */

(function($){

var IE = $.browser.msie,
	IE6 = IE && $.browser.version < 7,
	IE7 = !IE6 && IE && $.browser.version < 8,
	OPERA = !IE && $.browser.opera,
	BB_ELMS = {
		bg: 'jbb-bg',
		lt: 'jbb-c jbb-lt',
		t:  'jbb-ln jbb-h jbb-t',
		rt: 'jbb-c jbb-rt',
		r:  'jbb-ln jbb-v jbb-r',
		rb: 'jbb-c jbb-rb',
		b:  'jbb-ln jbb-h jbb-b',
		lb: 'jbb-c jbb-lb',
		l:  'jbb-ln jbb-v jbb-l'
	};

if (IE && document.documentMode && document.documentMode > 7) {
	IE7 = false;
	IE8 = true;
}
	
//
function createElm(tag) {
	return document.createElement(tag);
}

//
function initialize()
{
	initialize = function(){};

	var style = [
		'<style type="text/css">',
		'.jbb {position: relative; z-index: 1;}',
		'.jbb-inline {display: inline-block;}',
		'.jbb-box {position: absolute; left: 0; top: 0; display: block; width: 100%; height: 100%; z-index: -1;}',
		'.jbb-box span {display: block; position: absolute;}',
		'.jbb-t {top: 0; background-position: top; background-repeat: repeat-x;}',
		'.jbb-r {right: 0; background-position: right; background-repeat: repeat-y;}',
		'.jbb-b {bottom: 0; background-position: bottom; background-repeat: repeat-x;}',
		'.jbb-l {left: 0; background-position: left; background-repeat: repeat-y;}',
		'.jbb-lt {left: 0; top: 0; background-position: left top;}',
		'.jbb-rt {right: 0; top: 0; background-position: right top;}',
		'.jbb-rb {right: 0; bottom: 0; background-position: right bottom;}',
		'.jbb-lb {left: 0; bottom: 0; background-position: left bottom;}',
		'.jbb-over {z-index: 2; width: 0; height: 0;}',
		'.jbb-over .jbb-bg {display: none;}'
	];

	if (IE7) {
		style.push(
			'.jbb {zoom: 1;}',
			'.jbb-c, .jbb-ln {width: 0; height: 0;}'
		);
	}
	else if (IE6) {
		style.push(
			'.jbb {zoom: 1;}',
			'.jbb-box {line-height: 0; font-size: 0; width: auto; height: auto;}',
			'.jbb-box * {line-height: 0; font-size: 0; overflow: hidden;}',
			'.jbb-c, .jbb-ln {width: 0; height: 0;}'
		);
	}

	style.push('</style>');

	$('head').prepend(style.join(''));
}


//
function draw(elm, config)
{
	if (elm.__jbb) {
		update(elm);
		return;
	}

	config = config || {};
	
	var jbb = elm.__jbb = {}, i, $elm = $(elm);

	jbb.container = $elm.addClass('jbb');

	jbb.inline = jbb.container.css('display').indexOf('inline') === 0;
	
	if (jbb.inline) {
		$elm.addClass('jbb-inline');
	}

	jbb.box = $(createElm('span')).addClass('jbb-box');
	if (config.over) {
		jbb.over = true;
		jbb.box.appendTo(jbb.container).addClass('jbb-over');
	} else
		jbb.box.prependTo(jbb.container);
		
	if (config.cls) jbb.box.addClass(config.cls);
	if (config.id) jbb.box.attr('id', config.id);
	
	for (i in BB_ELMS) jbb[i] = $(createElm('span')).addClass(BB_ELMS[i]).appendTo(jbb.box);

	update(elm);

	if (IE6 || OPERA) setInterval(function(){ update(elm); }, 100);
}

//
function update(elm)
{
	var i, jbb = elm.__jbb, s = {};
	
	for (i in BB_ELMS) {
		s[i+'w'] = jbb[i].width();
		s[i+'h'] = jbb[i].height();
	}
	
	if (jbb.over || IE6 || IE7 || OPERA) {
		var w = jbb.container.innerWidth(),
			h = jbb.container.innerHeight();
			test = jbb.w === w && jbb.h === h;

		if (test) for (i in BB_ELMS) {
			test = jbb[i+'w'] === s[i+'w'] && jbb[i+'h'] === s[i+'h'];
			if (!test) break;
		}
			
		if (test) return;

		if (!jbb.over) jbb.box.css({width: w, height: h});
		
		var ltmw = (parseInt(jbb.lt.css('marginLeft')) || 0),
			rtmw = (parseInt(jbb.rt.css('marginRight')) || 0),
			lbmw = (parseInt(jbb.lb.css('marginLeft')) || 0),
			rbmw = (parseInt(jbb.rb.css('marginRight')) || 0),
			ltmh = (parseInt(jbb.lt.css('marginTop')) || 0),
			rtmh = (parseInt(jbb.rt.css('marginTop')) || 0),
			lbmh = (parseInt(jbb.lb.css('marginBottom')) || 0),
			rbmh = (parseInt(jbb.rb.css('marginBottom')) || 0),
			tm = (parseInt(jbb.t.css('marginTop')) || 0),
			rm = (parseInt(jbb.r.css('marginRight')) || 0),
			bm = (parseInt(jbb.b.css('marginBottom')) || 0),
			lm = (parseInt(jbb.l.css('marginLeft')) || 0),
			tmp;

		jbb.t.css({left: s.ltw + ltmw, width: w - s.ltw - s.rtw - ltmw - rtmw});
		tmp = h - s.rth - s.rbh - rtmh - rbmh;
		if (tmp < 0) tmp = 0;
		jbb.r.css({left: w - s.rw - rm, top: s.rth, height: tmp});
		
		jbb.b.css({top: h - s.bh - bm, left: s.lbw + lbmw, width: w - s.lbw - s.rbw - lbmw - rbmw});
		tmp = h - s.lth - s.lbh - ltmh - lbmh;
		if (tmp < 0) tmp = 0;
		jbb.l.css({top: s.lth + ltmh, height: tmp});

		jbb.rt.css({left: w - s.rtw - rtmw});
		jbb.rb.css({top: h - s.rbh - rbmh, left: w - s.rbw - rbmw});
		
		jbb.lb.css({top: h - s.lbh - lbmh});
		
		jbb.bg.css({
			left: s.lw + lm, top: s.th + tm,
			width: w - s.lw - s.rw - lm - rm,
			height: h - s.th - s.bh - tm - bm
		});

		jbb.w = w; jbb.h = h;
		for (i in BB_ELMS) {
			jbb[i+'w'] = s[i+'w'];
			jbb[i+'h'] = s[i+'h'];
			
			
			if($.png) jbb[i].png();
		}

	} else {
		var ltmw = (parseInt(jbb.lt.css('marginLeft')) || 0),
			rtmw = (parseInt(jbb.rt.css('marginRight')) || 0),
			lbmw = (parseInt(jbb.lb.css('marginLeft')) || 0),
			rbmw = (parseInt(jbb.rb.css('marginRight')) || 0),
			ltmh = (parseInt(jbb.lt.css('marginTop')) || 0),
			rtmh = (parseInt(jbb.rt.css('marginTop')) || 0),
			lbmh = (parseInt(jbb.lb.css('marginBottom')) || 0),
			rbmh = (parseInt(jbb.rb.css('marginBottom')) || 0),
			tm = (parseInt(jbb.t.css('marginTop')) || 0),
			rm = (parseInt(jbb.r.css('marginRight')) || 0),
			bm = (parseInt(jbb.b.css('marginBottom')) || 0),
			lm = (parseInt(jbb.l.css('marginLeft')) || 0);
		
		jbb.t.css({left: s.ltw + ltmw, right: s.rtw + rtmw});
		jbb.r.css({top: s.rth + rtmh, bottom: s.rbh + rbmh});
		jbb.b.css({left: s.lbw + lbmw , right: s.rbw + rbmw});
		jbb.l.css({top: s.lth + ltmh, bottom: s.lbh + lbmh});
		jbb.bg.css({left: s.lw + lm, top: s.th + tm, right: s.rw + rm, bottom: s.bh + bm});
		
	}
}

//
$.fn.bb = function(config)
{
	initialize();
	for (var i = 0, l = this.length; i < l; i++) draw(this[i], config);
	return this;
};

})(jQuery);