﻿var uihelper = (function () {
    var config = {
        stringFormatPattern: /\{\d+\}/g
    }

    return {
        // example: var str = "test {0} ok"; uihelper.stringFormat(str, 'is'); 
        stringFormat: function (str) {
            var args = new Array();
            $.each(arguments, function (idx, value) {
                if (idx != 0) args[idx - 1] = value;
            });

            return str.replace(config.stringFormatPattern, function (capture) { return args[capture.match(/\d+/)]; });
        },

        // gets request parameter value from current url
        getRequestParameter: function (paramName) {
            var ret;
            paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + paramName + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(window.location.href);
            ret = results == null ? "" : results[1];
            return ret;
        },
        
        addBookmark: function(url, title) {
            if (window.sidebar) { // Mozilla Firefox Bookmark
                window.sidebar.addPanel(title, url, "");
            } else if (window.external) { // IE Favorite
                window.external.AddFavorite(url, title);
            }
            else if (window.opera && window.print) { // Opera Hotlist
                return true;
            }
        },
        
        formatMoney: function(val, c, d, t) {
            var n = val, 
                c = isNaN(c = Math.abs(c)) ? 2 : c, 
                d = d == undefined ? "," : d, 
                t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
                i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
                j = (j = i.length) > 3 ? j % 3 : 0;
                
            return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");    
        },
        
        removeDecimals: function(val) {
            var ret = val;        
            var index = val.indexOf(',');
            if (index != null && index != 'undefined') {
                ret = val.substring(0, index);
            }
            return ret;
        },

        trackDocumentGA: function(path) {
            _gaq.push(['_trackPageview', path]);
        },

        getFilePathFromOnClick: function(val) {
            var ret = val;
            var from = test.indexOf('/downloads/');
            return test.substring(from, test.length - 4);
        }
    }
} ());
