/*
 * OUI (Other User Interface) - jQuery components
 *
 * Copyright (c) 2009 Raphaël BENITTE
 *
 * Project home:
 *   http://www.undes1gn.org/
 *
 */

(function($) {
	
	$.extend($, {
		oui: {
			scrollbarWidth: 19, // value used in various scrollable components
			higherDepth: 1000, // depth stored for all components
			getHigherDepth: function() {
				$.oui.higherDepth++;
				return $.oui.higherDepth;
			},
			bringFront: function(component) {
				if ( component.depth ) {
					if ( component.depth < $.oui.higherDepth ) return $.oui.getHigherDepth();
					else return $.oui.higherDepth;
				} else {
					return 0;
				};
			},
			KEY: {
				UP: 38,
				DOWN: 40,
				DEL: 46,
				TAB: 9,
				RETURN: 13,
				ESC: 27,
				COMMA: 188,
				PAGEUP: 33,
				PAGEDOWN: 34,
				BACKSPACE: 8
			},
			
			createCookie: function( name, value, days ) {
				if (days) {
					var date = new Date();
					date.setTime( date.getTime() + (days*24*60*60*1000) );
					var expires = "; expires=" + date.toGMTString();
				}
				else var expires = "";
				document.cookie = name + "=" + value + expires + "; path=/";
			},

			readCookie: function( name ) {
				var nameEQ = name + "=";
				var cookiesArray = document.cookie.split(';');
				for ( var i=0; i < cookiesArray.length; i ++ ) {
					var cookie = cookiesArray[i];
					while ( cookie.charAt(0) == ' ' ) {
						cookie = cookie.substring(1, cookie.length);
					}
					if ( cookie.indexOf(nameEQ) == 0 ) {
						return cookie.substring( nameEQ.length, cookie.length );
					}
				}
				return null;
			},
			
			destroyCookie: function(name) {
				$.oui.createCookie(name,"",-1);
			},			
			
			findValInClass : function(elem, prefix) {
				var classes = $(elem).attr('class');

				if ( classes != '' )
				{
					var regex = new RegExp( prefix + "(\\d+)" );
					var matches = classes.match(regex);
					if ( matches != null ) return matches[1];
				}
				return false;
			},

			roundCorners : function( mode, radius ) {
			
				var mozCmds = {'all':'-moz-border-radius','tl':'-moz-border-radius-topleft','tr':'-moz-border-radius-topright','br':'-moz-border-radius-bottomright','bl':'-moz-border-radius-bottomleft'};
				var wkCmds = {'all':'-webkit-border-radius','tl':'-webkit-border-top-left-radius','tr':'-webkit-border-top-right-radius','br':'-webkit-border-bottom-right-radius','bl':'-webkit-border-bottom-left-radius'};

				cssProps = {};
				radius = radius == false ? 0 : radius;

				switch ( mode )
				{
					case 'all' :
						cssProps[mozCmds.all] = radius + 'px';
						cssProps[wkCmds.all] = radius + 'px';
						break;
					case 'top' :
						cssProps[mozCmds.tl] = radius + 'px'; cssProps[mozCmds.tr] = radius + 'px';
						cssProps[wkCmds.tl] = radius + 'px'; cssProps[wkCmds.tr] = radius + 'px';
						break;
					case 'tr' :
						cssProps[mozCmds.tr] = radius + 'px';
						cssProps[wkCmds.tr] = radius + 'px';
						break;
					case 'right' :
						cssProps[mozCmds.tr] = radius + 'px'; cssProps[mozCmds.br] = radius + 'px';
						cssProps[wkCmds.tr] = radius + 'px'; cssProps[wkCmds.br] = radius + 'px';
						break;
					case 'br' :
						cssProps[mozCmds.br] = radius + 'px';
						cssProps[wkCmds.br] = radius + 'px';
						break;
					case 'bottom' :
						cssProps[mozCmds.br] = radius + 'px'; cssProps[mozCmds.bl] = radius + 'px';
						cssProps[wkCmds.br] = radius + 'px'; cssProps[wkCmds.bl] = radius + 'px';
						break;
					case 'bl' :
						cssProps[mozCmds.bl] = radius + 'px';
						cssProps[wkCmds.bl] = radius + 'px';
						break;
					case 'left' :
						cssProps[mozCmds.tl] = radius + 'px'; cssProps[mozCmds.bl] = radius + 'px';
						cssProps[wkCmds.tl] = radius + 'px'; cssProps[wkCmds.bl] = radius + 'px';
						break;
					case 'tl' :
						cssProps[mozCmds.tl] = radius + 'px';
						cssProps[wkCmds.tl] = radius + 'px';
						break;
					default :
						break;
				}
				return this.css(cssProps);
			}
		}
	});

	$.fn.extend({
		ouiRoundCorners: $.oui.roundCorners,
		ouiRC: $.oui.roundCorners/*,
		
		ouiSizeClone: function(ref, options) {
			
			var settings = {
	            affect: 'both',
				xSub: 0,
				ySub: 0,
				xAdd: 0,
				yAdd: 0
	        };

	        settings = $.extend( {}, settings, options || {} );
			
			var ref = $(ref);
			if( ref.size() > 0 ) {
				var ref = $(ref).eq(0); // works only on single reference
				var refPad = $(ref).ouiGetPadding();
				var refBorder = $(ref).ouiGetBorderWidth();
				var refHeight = $(ref).innerHeight();
				var refWidth  = $(ref).innerWidth();
				ref.ouiGetBorderWidth();
				return $(this).each(function() {
					var selfPad    = $(this).ouiGetPadding();
					var selfBorder = $(this).ouiGetBorderWidth();
					
					var newWidth  = refWidth  - selfPad.x - selfBorder.x - settings.xSub + settings.xAdd;
					var newHeight = refHeight - selfPad.y - selfBorder.y - settings.ySub + settings.yAdd;
					
					switch ( settings.affect ) {
						case 'width' :
							$(this).css('width', newWidth);
							break;
							
						case 'height' :
							$(this).css('height', newHeight);
							break;
						
						case 'both' :
							$(this).css('width', newWidth);
							$(this).css('height', newHeight);
							break;
							
						default : break;
					}
				});
			} else {
				return $(this);
			}
		},
		
		ouiGetPadding: function() {
			var padding = {};
			topPad    = parseInt($(this).css('padding-top'));
			rightPad  = parseInt($(this).css('padding-right'));
			bottomPad = parseInt($(this).css('padding-bottom'));
			leftPad   = parseInt($(this).css('padding-left'));
			padding['top']    = topPad != NaN ? topPad : 0;
			padding['right']  = rightPad != NaN ? rightPad : 0;
			padding['bottom'] = bottomPad != NaN ? bottomPad : 0;
			padding['left']   = leftPad != NaN ? leftPad : 0;
			padding['x']      = rightPad + leftPad;
			padding['y']      = topPad + bottomPad;
			return padding;
		},
		
		ouiWidthNoPad: function() {
		},
		
		ouiGetBorderWidth: function() {
			var widths = {};
			topWidth    = parseInt($(this).css('border-top-width'));
			rightWidth  = parseInt($(this).css('border-right-width'));
			bottomWidth = parseInt($(this).css('border-bottom-width'));
			leftWidth   = parseInt($(this).css('border-left-width'));
			
			widths['top']    = topWidth    != NaN ? topWidth : 0;
			widths['right']  = rightWidth  != NaN ? rightWidth : 0;
			widths['bottom'] = bottomWidth != NaN ? bottomWidth : 0;
			widths['left']   = leftWidth   != NaN ? leftWidth : 0;
			widths['x']      = widths['right'] + widths['left'];
			widths['y']      = widths['top']   + widths['bottom'];
			
			return widths;
		}
		*/
	});

})(jQuery);
