"use strict";
/*globals $, jQuery, window, document, pageTracker */
var DUMAS = (function () {

    var dumas = {},

    /**********************************************************************
     * Minimal JSP / JS / Singleton synch code.  Separate if it gets big
     **********************************************************************/
    initCallbacks = [],
    contextCallbacks = {},
    contextInitiliazed;

    function initContext(context) {
        if (contextCallbacks[context] && contextCallbacks[context].length) {
            jQuery.each(contextCallbacks[context], function () {
                this(dumas[context]);
            });
        }
    }
    
    function initAllContext() {

        jQuery.each(initCallbacks, function () {
            this();
        });

        jQuery.each(contextCallbacks, function (context) {
            initContext(context);
        });
        contextInitiliazed = true;
    }

    function newContextData(context, data) {
        if (context) {
            dumas[context] = data;
        } else {
            jQuery.extend(dumas, data);
        }

        if (contextInitiliazed) {
            initContext(context);
        }
    }

    function addInitCallback(callback) {
        initCallbacks.push(callback);
        return dumas;
    }

    function addContextCallback(context, callback) {
        var ctx = contextCallbacks[context];

        if (!ctx) {
            ctx = contextCallbacks[context] = [];
        }
        
        ctx.push(callback);

        if (contextInitiliazed) {
            callback(dumas[context]);
        }
        
        return dumas;
    }

    jQuery.extend(dumas, {
        init: addInitCallback,
        context: addContextCallback,

        // JSP calls below:
        _initAllContext: initAllContext,
        _ctxData: newContextData
    });


    /*************************************
     * String prototype extension
     *************************************/
    jQuery.extend(String.prototype, {

        constrain: function (len) {
            return this.length < len ? this : this.substring(0, len - 3) + '...';
        },
        trim: function () {
            return jQuery.trim(this);
        }
    });

    /*************************************
     * jQuery collection base extension
     *************************************/
    jQuery.fn.extend({
        serializeObj: function () {
            var val, build = [], self = this.get(0);

            for (val in self) {
                if (Object.hasOwnProperty.call(self, val)) {
                    build.push([val, '=', self[val], '&'].join(''));
                }
            }
    
            return build.join('');
        },
        setClass : function (test, classOne, classTwo) {
            this[test ? 'addClass' : 'removeClass'](classOne);
            return (classTwo) ? this[!test ? 'addClass' : 'removeClass'](classTwo) : this;
        },
        deserialize : function (attrName) {
            return jQuery.deserialize(this.attr(attrName || 'href'));
        },
        updateQueryString: function (callback, attrName) {
            return this.each(
                function () {
                    var jq = $(this),
                        href,
                        baseUrl,
                        params,
                        qsIndex;

                    attrName = attrName || 'href';

                    href = jq.attr(attrName);
					if (href) {
						qsIndex = href.indexOf('?');
						baseUrl = (qsIndex >= 0) ? href.substring(0, qsIndex + 1) : href;
						params = jQuery.deserialize(href);

						callback(params, jq, baseUrl);

						jq.attr(attrName, baseUrl + jQuery.param(params));
					}
                }
            );
        },
        setVisible: function (visible) {
            return this.css('visibility', visible ? 'visible' : 'hidden');
        }

    });


    /*************************************
     * jQuery object base extension
     *************************************/
    jQuery.extend({

        deserialize : function (url) {
            var result = {}, params, param, index, i;
            index = url.indexOf('?');
            if (index >= 0) {
                params = url.substring(index + 1).split('&');
                for (i = 0; i < params.length;) {
                    param = params[i].split('=');
                    result[param[0]] = param[1];
                    i = i + 1;
                }
            }
            return result;
        }
    });

    function googleEventsForSearch() {
        var choice, searchForm = $('#search form:not(.ga_search)');
        searchForm
        .bind('submit.googleEvents', function () {
			choice = $('#myselectbox_container .selected').text() === '' ? 'Search All Music' : $('#myselectbox_container .selected').text();
			pageTracker._trackEvent('Search', 'Submit', choice);
        })
		.addClass('ga_search');
    }
    
    function initLoyaltyBox() {
        var  dalRight = DUMAS.loyalty ? DUMAS.loyalty.dalRight : false;
        if (dalRight) {
            $('#loyaltyDialog').jqm({
                ajax :	'/remote/loyalty.html',
                closeClass : 'jqmClose',
                overlay : 50,
                target : $('.fancyBoxContent'),
                toTop : 'true',
                onShow : function (h) {
                    h.w.css('top', DUMAS.scrollTop() + 40 + "px")
                        .css('left', (DUMAS.clientWidth() / 2) - (h.w.width() / 2) + "px")
                        .fadeIn('normal');
                }
            }).jqmShow();
        }
    }

    $(function () {
        googleEventsForSearch();
        initLoyaltyBox();
    });

    return dumas;
}());

/*
* gives inner module functions a specific global name to expose themselves to be unit-tested 
*/
var QUnit = {};