/*
 * 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
            },
        
            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;
            },
            
            viewportInfo: function() {
                var infos = {
                    w:  $(window).width(),
                    h:  $(window).height(),
                    sl: $(document).scrollLeft(),
                    st: $(document).scrollTop() 
                };
                return infos;
            },

			Cookie: {
				create: function(key, value, days) {
					var expires;
					if (days) {
						var date = new Date();
						date.setTime(date.getTime()+(days*24*60*60*1000));
						expires = '; expires=' + date.toGMTString();
					} else {
						expires = '';
					}
					document.cookie = key + '=' + value + expires + '; path=/';
				},
				read: function(key) {
					key = key + "=";
					var cookiesArray = document.cookie.split(';');
					for (var i = 0; i < cookiesArray.length; i++) {
						var currentCookie = cookiesArray[i];
						while (currentCookie.charAt(0) === ' ') {
							currentCookie = currentCookie.substring(1, currentCookie.length);
						}  
						if (currentCookie.indexOf(key) === 0) {
							return currentCookie.substring(key.length, currentCookie.length);
						}
					}
					return null;
				},
				clear: function(key) {
					$.Oui.Cookie.create(key, '', -1);
				}
			}
        }
    });
    
    $.fn.extend({
        
        ouiGetSizes: function () {
            
            var sizes = {};
            
            var target = this;
            var cloned = false;
			var clone;
            
            // if element is hidden, clone it and pput out of screen
            if (this.is(':hidden')) {
                clone = this.clone();
                clone.show();
                clone.css({
                    position: 'absolute',
                    left: -9999,
                    top:  0
                });
                clone.appendTo('body');
                target = clone;
                cloned = true;
            }
            
            sizes.width       = target.width();
            sizes.height      = target.height();
            sizes.totalWidth  = target.outerWidth();
            sizes.totalHeight = target.outerHeight();
            
            if (cloned) {
                clone.remove();
            }
            
            return sizes;
        },
        
		preventSelect: function(flag) {
			
			var prevent;
			
			if (flag === null) {
				prevent = true;
			} else {
				prevent = flag;
			}

			if (prevent) {
				return this.each(function(){
					if ($.browser.msie || $.browser.safari) {
						$(this).bind('selectstart', function(){
							return false;
						});
					} else if ($.browser.mozilla) {
						$(this).css('MozUserSelect','none');
						$('body').trigger('focus');
					} else if ($.browser.opera) {
						$(this).bind('mousedown', function(){
							return false;
						});
					} else {
						$(this).attr('unselectable','on');
					}
				});
			} else {
				return this.each(function(){
					if ($.browser.msie || $.browser.safari) {
						$(this).unbind('selectstart');
					} else if ($.browser.mozilla) {
						$(this).css('MozUserSelect','inherit');
					} else if ($.browser.opera) {
						$(this).unbind('mousedown');
					} else {
						$(this).removeAttr('unselectable','on');
					}
				});
			}
		},

        fixPngBg: function () {
            return this.each(function() {
                var bgImg = $(this).css('backgroundImage');
                if (bgImg.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
                    var image = RegExp.$1;
                    $(this).css({
                        'backgroundImage': 'none',
                        'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
                    }).each(function() {
                        var position = $(this).css('position');
                        if (position != 'absolute' && position != 'relative') {
							$(this).css('position', 'relative');
						}
                    }).css('zoom', 1);
                }
            });
        }
    });

})(jQuery);

