if (typeof $U == "undefined") var $U = {

	/*
	 * shows an error message
	 */
	errMsg: function(t, d) {
		if (d) {
			$("#err_link").show();
			$("#err_details").hide();
			$("#err_details i").html(d);
		} else {
			$("#err_link").hide();
			$("#err_details").hide();
		}
		$("#err_msg").html(t);
		$("#err_dlg").dialog('open');
	},

    /*
    * shows a pop up window with a message
    */
    okMsg: function(t,callback, title) {
        $("#ok_msg").html(t);
        if (callback){
            $("#ok_dlg").dialog('option', 'buttons',{
                "Ok": function(){ 
                    $('#ok_dlg').dialog('close'); 
                    //callback(); 
                }
            } );
            $("#ok_dlg").bind('dialogclose', function(event) {
                    $("#ok_dlg").unbind('dialogclose');
                    callback();
            });		
        }
        if (title){
            $('#ok_dlg').dialog('option', 'title', title);
        }
        $("#ok_dlg").dialog('open');
    },

    /*
    * shows a pop up window with a message and two buttons, ok and cancel
    */
    dlgMsg: function(t, callback) {
        $("#dlg_msg").html(t);
        $('#dlg_dlg').dialog('option', 'buttons',
            {
                "No": function() {$('#dlg_dlg').dialog('close');callback(false);},
                "Yes": function() {$('#dlg_dlg').dialog('close');callback(true);}
            } );
        $("#dlg_dlg").dialog('open');
    },

    playVideo: function(v) {
        $f("video_player", "flowplayer/flowplayer-3.1.4.swf",
            {
                clip:  {
                    url: v,
                    autoPlay: false,
                    autoBuffering: false
                }
            }
        );
        $("#player_dlg").dialog('open');
    },

    ajaxDialog: function(controller, title, width, load_callback, buttons) {
        $("#ajax_dlg").empty();
        $("#ajax_dlg").load($G.CI_BASE + "index.php/admin/" + controller, null, load_callback);
        $('#ajax_dlg').dialog('option', 'width', width);
        $('#ajax_dlg').dialog('option', 'height', 'auto');
        if (buttons) {
            $('#ajax_dlg').dialog('option', 'buttons', buttons);
        }
        $('#ajax_dlg').dialog('option', 'title', title);
        $('#ajax_dlg').dialog('option', 'position', ['center', 35]);
        $("#ajax_dlg").dialog('open');

        //this is a fix for the ajax_dlg in order to show the Loading Dialog
        if ($("#loading_dlg").dialog('isOpen')) {
            //$Loading.MoveToTop():
            $("#loading_dlg").dialog( 'close' );
            $("#loading_dlg").dialog( 'open' );
            $("#loading_dlg").dialog( 'moveToTop' );
        } else { }
    },

    closeAjaxDialog: function() {
        $('#ajax_dlg').dialog('close');
    },

    /* Moved to dpicker.js
     *domainPicker: function(saveCallback) {
        $U.ajaxDialog(
            'domain_picker',
            'Domain Picker',
            450,
            function () { initDomainPicker() },
            { "Save": function() { saveDomainPicker(saveCallback); } }
        );
    },*/

	sectionWarning: function(t) {
		$("#section_warning span").html(t);
		$("#section_warning").slideDown();
	},

	sectionWarningAppend: function(t) {
		$("#section_warning span").append(t);
		$("#section_warning").show();
	},

	sectionWarningHide: function() {
		$("#section_warning").hide();
	},

	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 ca = document.cookie.split(';');
		for (var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') {
				c = c.substring(1,c.length);
			}
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return null;
	},

	eraseCookie: function(name) {
		$U.createCookie(name, "", -1);
	},

    getImgPath: function(img) {
        img = $U.trim(img);
        if (img) {
            return img.replace(/\\/g, '/');
        } else {
            return 'css/images/placeholder.jpg';
        }
    },

    trim: function(str) {
        if (!str || typeof str != 'string') return null;
        return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
    },

    /*
     * a date from doctrine will always be like this: 2009-11-06 00:36:23
     * this function will format that so that it looks like: 09/06/11 00:36:23
     */
    dateFormat: function(date, format, return_only_date) {
        date_parts = date.split(' ');
        var dd = $.datepicker.parseDate('yy-mm-dd', date_parts[0]);
        if (!format) {
            format = 'yy/d/m';
        }
        var jd = $.datepicker.formatDate(format, dd);
        if (return_only_date) {
            return jd;
        } else {
            return jd + ' ' + date_parts[1];
        }
        
    },

    domainReportDetails: function(domains, period) {
        $U.ajaxDialog(
            'domain_report_detail',
            'Domain, Detailed Report',
            500,
            function() {initDomainDetailReport(domains, period)},
            {"Close": function() {$('#ajax_dlg').dialog('close');}}
        );
    },

    onEnter: function(e, callback) {
        $(e).keydown(function(e) {
            if (e.which == 13) {
                 callback();
            }
        });
    },

    pickDomain: function(new_domain, callback) {
        $Client.req(
            "Auth",
            "authNewDomain",
            {new_domain: new_domain},
            function(data) {
                $U.createCookie('sDomainPick', new_domain, 0);
                $U.okMsg($Lang.tr("You're now operating over the picked domain."));
                $Auth.showSessionData(callback);
            }
        );
    },

    getIconLink: function(icon, title, css_classes, onclick) {
        if (onclick != null && onclick != '') {
            icon_html = '<a class="' + css_classes + '"name="#" title="' + title + '" onclick="' + onclick + '"><span class="fg-icon-' + icon + '"/></a>';
        } else {
            icon_html = '<a class="' + css_classes + '"name="#" title="' + title + '"><span class="fg-icon-' + icon + '"/></a>';
        }
        
        return icon_html;
    },

    clone: function (object)
    {
        var ClonedObject = function(){};
        ClonedObject.prototype = object;
        return new ClonedObject;
    },
    /*
     * returns substring of text if it's lenght is greater than max
     * it adds '...' at the end if the return is not the same text
     * */
    snippet: function (text, max){
        var str = '';
        if (text.length > max){
                str = text.substring(0,max);
                if (str.charAt(max-1)== ' '){
                    str = str +'...';
                }else{
                    str = str +' ...';
                }
        }else{
            str = text;
        }
        return str;
    },

    checkUnreadMsg : function (){
        if ($Auth.valid() && $Auth.granted($G.MODULE_LIGHTPAD, $G.PRIVILEGE_ALL)){
          $Client.hiddenReq(
            "Message",
            "getUnreadCount",
            {},
            function(data) {
                if (data<=0){
                    $(".session-data-messages").hide();
                }else{
                    var plural = '';
                    if (data >1){
                        plural = 's'
                    }
                    var text = $Lang.tr("You have @param1 unread message"+plural,{'@param1': $Lang.b(data)})
                    $(".session-data-messages span").html(text);
                    $(".session-data-messages").show();
                }
            }
        );
    }else{
        $(".session-data-messages").hide();
    }
    },
    
    getUrlVars : function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

}

