﻿
VOM = (function() {
    var progressBars = [],
        getAJAXmanager = function() { return Sys.WebForms.PageRequestManager.getInstance(); },
        canCallClicky = function() {
            /*ensure we aren't doing this on localhost or dev*/
            var location = document.location.href.toLowerCase();
            return location.indexOf('localhost') === -1 && location.indexOf('vom.dev') === -1;
        };

    return {
        skinPath: '',
        log: function(string) { if (window.console && window.console.log) window.console.log(string) },
        AJAX: {
            /*The initializeRequest method is raised before processing of the asynchronous request starts. You can use this event to cancel a postback.*/
            addInitializeRequest: function(f) { getAJAXmanager().add_initializeRequest(f) },
            /* Raised just before the asynchronous postback is sent to the server. Event data is passed to handlers as a BeginRequestEventArgs 
            * object. The object makes available the element that caused the postback and the underlying request object.*/
            addBeginRequest: function(f) { getAJAXmanager().add_beginRequest(f) },
            /* Raised after the response to the most recent asynchronous postback has been received but before any updates 
            *  to the page have been made. Event data is passed to handlers as a PageLoadingEventArgs object. The object makes 
            *  available information about what panels will be deleted and updated as a result of the most recent asynchronous postback. */
            addPageLoading: function(f) { getAJAXmanager().add_pageLoading(f) },
            /* Raised after page regions are updated after the most recent postback. Event data is passed to handlers as a 
            *  PageLoadedEventArgs object. The object makes available information about what panels were created or updated. 
            *  For synchronous postbacks, panels can only be created, but for asynchronous postbacks, panels can be both created and updated. */
            addPageLoaded: function(f) { getAJAXmanager().add_pageLoaded(f) },
            /* Raised when request processing is finished. Event data is passed to handlers as an EndRequestEventArgs object. 
            *  The object makes available information about errors that have occurred and whether the error was handled. 
            *  It also makes available the response object.*/
            addEndRequest: function(f) { getAJAXmanager().add_endRequest(f) },

            /* custom function that adds an ajax progress bar to list of ones to be optimized */
            addProgressBar: function(id) { progressBars[progressBars.length] = id; },

            /* Aborts the current MS AJAX request */
            stopRequest: function() { getAJAXmanager().abortPostBack(); }
        },
        addAJAXbar: function(id) { progressBars[progressBars.length] = id; },
        enhanceAJAXbars: function(jQuery, className) {
            return jQuery.each(progressBars, function() {
                var c = jQuery("#" + this).addClass(className || "progressbar"), window = jQuery(window);
                if (!c.length) return true; /* no progress bars*/

                var moveProgressbars = function() {
                    var position = { top: window.scrollTop() + ((window.height() - c.outerHeight()) / 2),
                        left: window.scrollLeft() + ((window.width() - c.outerWidth()) / 2)
                    };
                    if (c.is(":visible")) c.animate(position, 75);
                    else c.css(position);
                };
                window.resize(moveProgressbars).scroll(moveProgressbars);
                moveProgressbars();
            });
        },
        embedFlash: function(jQuery, element, options, params) {
            return element.flashembed(
						jQuery.extend({}, { src: this.skinPath + 'flash/BAD_player.swf', version: [9, 0], onFail: function() { jQuery(".fail", element).show() }, autoplay: true, wmode: "opaque" }, options),
						jQuery.extend({}, { themeColor: "F0AF17", /*mode: "overlay", scaleMode: "stretch",*/fontColor: "cccccc", autostart: "false" }, params)
						);
        },
        cleanupPages: function(jQuery) {
            jQuery("div.shell ul.mainNav li:last-child").css("border-right", "0");
            jQuery("div.shell .langNavContainer ul li select.NormalTextBox").remove();
        },
        clickyGoal: function(id) {
            if (clicky && clicky.goal && canCallClicky()) {
                /*this.log('Clicky Goal: ' + id);*/
                clicky.goal(id);
            }
        },
        clickyLog: function(href, title, type) {
            /*    
            * href: Required. The URL you want logged for this action. Outbound links need to start with http, all other actions 
            need to start with either a / or a #. If you just want to the log the URL as it is in the anchor tag, use the 
            value this.href. (this.href will only work within the HTML scope, not in Flash).
            * title: Optional. The text you want to show up next to the URL. Our standard tracking code uses the text of the 
            link itself but you may want to use something else.
            * type: Optional. The type of action you are logging. If left blank, it defaults to 'click'. Other options are 
            'download', 'outbound', and 'pageview'. 
            */
            if (clicky && clicky.log && canCallClicky()) {
                /*this.log('Clicky Log: ' + title);*/
                clicky.log(href, title, type);
            }
        }
    };
})();