﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery */

//  
//  Author: Kevin Burkitt
//  Date:   2010-02-25
//  
//  CC Helpers
//
cc.helpers = function() {
    return {
        addQSParam: function(url, param) {
            if (url.indexOf('?') < 1) {
                url = url + '?' + param;
            }
            else {
                url = url + '&' + param;
            }

            return url;
        },

        getPageTitle: function() {
            var title = document.title;
            if (jQuery('#featuredTitle').html() !== null) {
                title = jQuery('#featuredTitle').html();
            } else if (jQuery('#blogTitle').html() !== null) {
                title = jQuery('#blogTitle').html();
            } else if (jQuery('#profileName').html() !== null) {
                title = jQuery('#profileName').html();
            }
            return title;
        },

        nl2br: function(text) {
            text = escape(text);

            var re_nlchar = '';
            if (text.indexOf('%0D%0A') > -1) {
                re_nlchar = /%0D%0A/g;
            } else if (text.indexOf('%0A') > -1) {
                re_nlchar = /%0A/g;
            } else if (text.indexOf('%0D') > -1) {
                re_nlchar = /%0D/g;
            } else {
                return unescape(text);
            }

            return unescape(text.replace(re_nlchar, '<br />'));
        },

        encodeQsParam: function(url) {
            var encodedInputString = escape(url);
            encodedInputString = encodedInputString.replace(/\+/g, "%2B");
            encodedInputString = encodedInputString.replace(/\//g, "%2F");
            return encodedInputString;
        },

        getQueryStringAsObject: function(queryString) {
            var objURL = {};

            if (typeof queryString === 'undefined') {
                queryString = window.location.search;
            }
            queryString.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"),
                function($0, $1, $2, $3) {
                    objURL[$1] = unescape($3).replace(/\+/g, " ");
                }
            );

            return objURL;
        },

        getQueryStringParam: function(param) {
            var objURL = cc.helpers.getQueryStringAsObject();

            for (var strKey in objURL) {
                if (strKey === param) {
                    return objURL[strKey];
                }
            }

            return '';
        },

        stripParamFromQs: function(param) {
            var objURL = cc.helpers.getQueryStringAsObject();
            var delim = '?';
            var qs = '';
            for (var strKey in objURL) {
                if (strKey !== param) {
                    qs += delim + strKey + '=' + cc.helpers.encodeQsParam(objURL[strKey]);
                    delim = '&';
                }
            }

            return window.location.href.split("?")[0] + qs;
        },

        addOrReplaceQsParam: function(url, param, paramValue) {
            var i = url.indexOf('?');
            if (i < 0) {
                // no Querystring
                return url + '?' + param + '=' + paramValue;
            } else {
                var urlRoot = url.split("?")[0];
                var qs = url.substr(i, url.length - i);

                // Remove any existing 'cct' parameter
                var objURL = cc.helpers.getQueryStringAsObject(qs);
                var delim = '?';
                qs = '';
                for (var strKey in objURL) {
                    if (strKey !== param) {
                        qs += delim + strKey + '=' + cc.helpers.encodeQsParam(objURL[strKey]);
                        delim = '&';
                    }
                }

                // Add fresh 'cct'
                return cc.helpers.addQSParam(urlRoot + qs, param + '=' + paramValue);
            }
        },

        addAjaxNoCacheParam: function(url) {
            return cc.helpers.addOrReplaceQsParam(url, 't', ''+(new Date().getTime())+'');
        },

        /*
        *  Error Message ( ?emsg=My+Error+Message )
        * 
        *  When a user is redirected to any site url with the querystring param: ?emsg=.... display 
        *  the value as an error message
        *  
        *  - Used in the payment process - after attempting (and in this case failing) to make a payment 
        *  on a remote (e.g. clickandbuy.com) page, the users is returned to their starting page on 
        *  the site and an error is displayed
        */
        queryStringErrorMessage: function() {
            var msg = cc.helpers.getQueryStringParam("emsg");
            if (typeof msg !== 'undefined' && msg !== '') {
                cc.ui.showModalErrorMessage(msg);
            }
        }
    };
} ();

