/* CLEANING FUNCTIONS */
if (!CLEAN) {
    var CLEAN = {
        box: function (_box) {
            GENERAL.dimmer('hide');
            if ($(_box).length)
                $(_box).remove();
            else
                $('.box').remove();
        }
    };  //CLEAN
}

if (!GENERAL) {
    var GENERAL = {
        //action: show/hide (default is hide)
        dimmer: function (action) {
            var action = action || 'hide';

            switch (action) {
                case 'show':
                    if (!$('#overlay').length) {
                        var overlay = $('<div id="overlay"></div>');
                        overlay.css('opacity', 0);
                        overlay.height($(document).height());
                        $('body').addClass('overflow');
                        $('body').append(overlay);
                        overlay.fadeTo(200, 0.4);
                    }
                    break;
                case 'hide':
                    if ($('#overlay').length) {
                        $('body').removeClass('overflow');
                        $('#overlay').remove();
                    }
                    break;
            }
        }, //dimmer
		//show element (extends jquery)
        show: function (elm) {
            if (elm) {
                elm.show().css('visibility', 'visible');
            }
        },
        //centers an element on the screen
        //offset: {x:0,y:0}, will move the elm from center
        center: function (elm, offset) {
            var elm = $(elm);
            if (elm.length) {
                if (!offset)
                    var offset = { x: 0, y: 0 };
                elm.css('position', 'absolute');
                elm.css('top', (($(window).height() - elm.outerHeight()) / 2) + $(window).scrollTop() + offset.y + 'px');
                elm.css('left', (($(window).width() - elm.outerWidth()) / 2) + $(window).scrollLeft() + offset.x + 'px');
            }
        },
		format_msg: function(action,txt_to_fix){
			var action = action || 'encode';
			var _text = txt_to_fix || '';
			switch(action){
				case 'encode':
					_text = _text.replace(new RegExp('\\n','g'),'_nl_');
					_text = _text.replace(new RegExp('\\r','g'),'_brk_');
					_text = _text.replace(new RegExp('\&','g'),'_amp_');
					_text = _text.replace(new RegExp('\>','g'),'_arrow-r_');
					_text = _text.replace(new RegExp('\<','g'),'_arrow-l_');
					_text = _text.replace(new RegExp('\"','g'),'_dbl_');
					_text = _text.replace(new RegExp('\,','g'),'_comma_');
					_text = _text.replace(new RegExp('\'','g'),'_app_');
					_text = _text.replace(new RegExp('%','g'),'_per_');
				break;
				case 'decode':
					_text = _text.replace(new RegExp('_nl_','g'),'\n');
					_text = _text.replace(new RegExp('_brk_','g'),'\r');
					_text = _text.replace(new RegExp('_amp_','g'),'&');
					_text = _text.replace(new RegExp('_arrow-r_','g'),'>');
					_text = _text.replace(new RegExp('_arrow-l_','g'),'<');
					_text = _text.replace(new RegExp('_dbl_','g'),'"');
					_text = _text.replace(new RegExp('_comma_','g'),',');
					_text = _text.replace(new RegExp('_app_','g'),'\'');
					_text = _text.replace(new RegExp('_per_','g'),'%');
				break;
			}
			return _text;
		},
		str_to_JSON: function(txt){
			try{
				return eval('('+ txt + ')');
			}
			catch(e){
				return eval(txt);
			}
		},
		on_focus: function(obj,text,remove_grey){
			var obj = $(obj);
			var on_blur = function(){
				if (obj.val().toLowerCase()==text.toLowerCase() || obj.val().length==0){
					obj.css('color','#999999');
					obj.val(text);
				}
				obj.unbind('blur');
			};
			
			if (obj.hasClass('greytext'))
				obj.removeClass('greytext')
			if (obj.val().toLowerCase()==text.toLowerCase())
				obj.val('');
			obj.css('color','#000000');
			obj.bind('blur',on_blur);
		}
		
    }; //GENERAL
}

/* BOX */
if (!BOX){
    var BOX = {
        /*
        data: {id:'id', w:'width', h:'height', title:'title', html:'html', dd:'0/1', offset:{x,y}}
        data.id: id of the box, if not passed random UUID will be generated
        data.w: width of the box
        data.h: height of the box, (optional)
        data.title: title of the box
        data.html: html of the box, goes inside of mainbody
        data.dd: true/false, determines whether the box is drag-dropable, values: true/false, default is false, (optional)
        data.offset: true/false, {x:10,y:15} would offset the box by 10px to the right and by 15px down, (optional)
        data.fn: function that should execute when the window is closed (optional)
        */
        normal: function (data) {
            if (!data || !$(data).length)
                var data = { id: 'b-' + GENERAL.uuid(), w: 600, h: null, title: 'please note', html: '', dd: false, offset: { x: 0, y: 0 }, fn: null };

            var _div = $(data.id);
            if (!$(_div).length) { //create div if does not exist
                if (!data.id)
                    data.id = 'b-' + GENERAL.uuid();
                _div = $('<div id="' + data.id + '" class="box"></div>');
                $('body').append(_div);
            }
            var _html = '';
            _html += '<div class="box-bg"></div>';
            _html += '<div id="' + data.id + '-box-outer" class="box-outer">';
            _html += '<div id="' + data.id + '-box-inner" class="box-inner">';
            _html += '<div id="' + data.id + '-box-hd" class="box-hd ' + ((data.dd) ? 'movable' : '') + '"><span class="capitalize">' + data.title + '</span></div>';
            _html += '<div id="' + data.id + '-box-mainbody" class="box-mainbody clearfix">' + data.html + '</div>';
            _html += '</div>';
            _html += '<a id="' + data.id + '-box-boxcls" href="#" class="box-boxcls" title="close"></a></div>';
            _html += '</div>';
            _div.html(_html);
            //if height, set height of box-outer (making sure the box has white bg through-out)
            if (data.h != null)
                $('#'+data.id+'-box-outer').height(data.h - 20);
            //positioning
            if (data.offset == null)
                data.offset = { x: 0, y: 0 };
            _div.width(data.w);
            GENERAL.center(_div, data.offset); //will center the box
            //controls
            $('#'+data.id+'-box-boxcls').click(function (event) {
                event.preventDefault();
				if (data.fn && GENERAL.is_function(data.fn))
                    data.fn();
                CLEAN.box(_div);
            });
            if (data.dd)
                _div.draggable({ cursor: 'move', snap: $('#' + data.id + '-box-hd') });
            return _div;
        } //normal

    };    //BOX
}
