/*
*  JESS - Javascript-Enhanced Style Sheets
*  Version 0.1b (BETA), 2009-12-05
*  Copyright (c) 2009, Petr Stanicek, http://pixy.cz; pixy@pixy.cz ("the author")
*
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*
*  This software is provided by the author "as is" and any express or implied
*  warranties, including, but not limited to, the implied warranties or
*  merchantability and fitness for a particular purpose are disclaimed.
*  In no event shall the author be liable for any direct, indirect, incidental,
*  special, exemplary, or consequential damages (including, but not limited to,
*  procurement of substitute goods or services; loss of use, data, or profits;
*  or business interruption) however caused and on any theory of liability,
*  whether in contract, strict liability, or tort (including negligence or
*  otherwise) arising in any way out of the use of this software, even if
*  advised of the possibility of such damage.
*/


// jQuery is required and must be loaded before this code

var JE$$ = function() {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PRIVATE:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~

	if (!jQuery) return;
	if ($!=jQuery) var $ = jQuery;

var v, oldIE, Workers = {};

	v = parseInt($.browser.version,10);
	oldIE = $.browser.msie && v<7;

// class Worker
function Worker(parentClass,applyHandler,defaultDef) {
	this.apply = applyHandler;
	this.applyAll = function() {
		for (var id in this.List) if (id!='default') this.apply(id);
		}
	this.add = function (options) {
		this.List[options.id] = options;
		}
// init
	this.ParentClass = parentClass;
	this.List = {};
	this.add(defaultDef);
	}



// Worker: STYLEDBOX

Workers.styledBox = new Worker('JESS-styledbox', function(id,$elm) {

	var i, j, l, cn, e, minw, minh, options, optionsOK, style, order, that = this;
	if (!$elm) $elm = $('.' + this.ParentClass + '-' + id);
	if ($elm.length==0) return;

	options = this.List[id] || this.List['default'];
	optionsOK = !!options.img && !!options.width;
  
	if (optionsOK && !oldIE) {
		$elm.each( function() {
			var $e = $(this);
			$e.addClass(that.ParentClass);
			if ($e.css('position')=='static') $e.css({'position': 'relative'});
			if (options.padding) $e.css({ 'padding' : options.padding });
			minw = (options.minWidth || 0) - parseInt($e.css('padding-left'),10) - parseInt($e.css('padding-right'),10);
			if (minw<0) minw = 0;
			minh = (options.minHeight || 0) - parseInt($e.css('padding-top'),10) - parseInt($e.css('padding-bottom'),10);
			if (minh<0) minh = 0;
			$e.css({
				'border' : 'none',
				'background' : 'transparent',
				'min-width' : minw + 'px',
				'min-height' : minh + 'px'
				});
			order = options.order || that.List['default'].order;
      var zIndex = - 1 - $('[class*=' + that.ParentClass + '-]:not([class*=' + that.ParentClass + '-x-])', $e).length;
			for (i=0,l=order.length;i<l;i++) {
				j = order[i]-1;
				cn = that.ParentClass + '-x-' + j;
				if ($e.children('.'+cn).length==0) {
					e = createBox('DIV',{ className : cn });
					$e.append(e);
					}
				style = {
					'position' : 'absolute',
					'z-index' : zIndex,
					'background' : 'url(' + options.img + '/' + (j+1) + '.' + options.ext + ') 0 0 repeat'
					}
				switch (j) {
					case 0 :
						style.left = 0;
						style.right = 'auto';
						style.top = 0;
						style.bottom = 'auto';
						style.width = options.width.left+'px';
						style.height = options.width.top+'px';
						style.backgroundPosition = 'top left';
						break;
					case 1 :
						style.left = options.width.left+'px';
						style.right = options.width.right+'px';
						style.top = 0;
						style.bottom = 'auto';
						style.width = 'auto';
						style.height = options.width.top+'px';
						style.backgroundPosition = 'top left';
						break;
					case 2 :
						style.left = 'auto';
						style.right = 0;
						style.top = 0;
						style.bottom = 'auto';
						style.width = options.width.right+'px';
						style.height = options.width.top+'px';
						style.backgroundPosition = 'top right';
						break;

					case 3 :
						style.left = 0;
						style.right = 'auto';
						style.top = options.width.top+'px';
						style.bottom = options.width.bottom+'px';
						style.width = options.width.left+'px';
						style.height = 'auto';
						style.backgroundPosition = 'top left';
						break;
					case 4 :
						style.left = options.width.left+'px';
						style.right = options.width.right+'px';
						style.top = options.width.top+'px';
						style.bottom = options.width.bottom+'px';
						style.width = 'auto';
						style.height = 'auto';
						style.backgroundPosition = 'top left';
						break;
					case 5 :
						style.left = 'auto';
						style.right = 0;
						style.top = options.width.top+'px';
						style.bottom = options.width.bottom+'px';
						style.width = options.width.right+'px';
						style.height = 'auto';
						style.backgroundPosition = 'top right';
						break;

					case 6 :
						style.left = 0;
						style.right = 'auto';
						style.top = 'auto';
						style.bottom = 0;
						style.width = options.width.left+'px';
						style.height = options.width.bottom+'px';
						style.backgroundPosition = 'bottom left';
						break;
					case 7 :
						style.left = options.width.left+'px';
						style.right = options.width.right+'px';
						style.top = 'auto';
						style.bottom = 0;
						style.width = 'auto';
						style.height = options.width.bottom+'px';
						style.backgroundPosition = 'bottom left';
						break;
					case 8 :
						style.left = 'auto';
						style.right = 0;
						style.top = 'auto';
						style.bottom = 0;
						style.width = options.width.right+'px';
						style.height = options.width.bottom+'px';
						style.backgroundPosition = 'bottom right';
						break;
					}
				$e.children('.'+cn).css(style);
				}
			});
		}
	else {
		$elm.css({ 'border' : options.border, 'background' : options.bgColor });
		}
	}, {
	// default style
		id : 'default',
		border : '1px solid black',
		bgColor : 'transparent',
		img : null,
		ext : 'png',
		width : { top:1, right:1, bottom:1, left:1 },
		minWidth : 0,
		minHeight : 0,
		order : [1,2,3,4,5,6,7,8,9]
	});



/*
// Worker: STYLEDBUTTON

Workers.styledButton = new Worker('JESS-styledbutton-', function(id,$elm) {
	}, {
	// default style
	// ...
	});
*/



// Utils

	function createBox(tag,attrs) {
		var a, e = document.createElement(tag);
		if (attrs) for (a in attrs) e[a] = attrs[a];
		return e;
		}

// jQuery onReady

	jQuery(function(){
		JE$$.apply('styledBox');
		});


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 	PUBLIC:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~

	return {

		styledBox : function (options) {
			if (!!!options.id || options.id==='default') return;
			Workers.styledBox.add(options);
			},

		styledBoxReplace : function (selector,oldStyle,newStyle) {
			var $elm, classPrefix = Workers.styledBox.ParentClass;
			if (oldStyle) {
				$elm = $(selector + ' .'+classPrefix+'-'+oldStyle);
				$elm.removeClass(classPrefix+'-'+oldStyle);
				}
			else {
				$elm = $(selector + ' .'+classPrefix);
				}
			$elm.addClass(classPrefix+'-'+newStyle);
			Workers.styledBox.apply(newStyle,$elm);
			},

		apply : function (workerId,styleId) {
			if (styleId) Workers[workerId].apply(styleId);
			else Workers[workerId].applyAll();
			}

		}

	}();

// JESS end

