/**
 * Functions for show / hiding the main intro content
 * @author mjames
 * @requires jquery.1.2.6
 * @optional NewMind.core.pngfix, JQuer.Cookies
 */

(function() {

    var NewMind = window.NewMind //namespace

    NewMind.registerNameSpace("NewMind.platform");
    NewMind.registerNameSpace("NewMind.env");


    /**
    * Show Hide Content Functionality - allows up to three different methods of "closing" the content
    */
    NewMind.platform.introContent = function() {
        var contentSelector = ""; //global var for content selector - content selector as it could be 1 or more container
        var cookiename = null;
        //custom events
        var preanimateevent = new NewMind.CustomEvent(); // custom pre animate event
        var postanimateevent = new NewMind.CustomEvent(); // custom post animate event
        NewMind.env.introcontent = {}; //environmental variables


        /**
        * Perform the show / hide of the Content
        */
        var showhide = function() {
            preanimateevent.fire();
            $(contentSelector).each(function() {
                var obj = $(this);
                var isVisible = obj.is(':visible');

                if (isVisible) {
                    obj.slideUp();
                    $('a.showhide').html('Show Intro <img alt="+" src="/images/icons_large/plus_button.gif">');
                    setCookie();
                }
                else {
                    obj.slideDown();
                    $('a.showhide').html('Hide Intro <img alt="-" src="/images/icons_large/minus_button.gif">');
                }
            });
            postanimateevent.fire();
            return false;
        };

        var setCookie = function() {
            if (cookiename !== null && typeof ($.cookie) !== 'undefined') {
                $.cookie(cookiename, "closed");
            }
        };

        /**
        * Attach page turn graphic and event
        * @param {Object} sContainerSelector container to place page turn graphic in
        */
        var attachpageturn = function(sContainerSelector) {
            $('<a href="#" class="pageturn"><img alt="close" src="/images/icons_large/pageturn.png"></a>').appendTo(sContainerSelector).click(showhide);
        };

        /**
        * Attach Show Hide Graphic and event
        * @param {Object} sContainerSelector container to place show hide graphic in
        */
        var attachshowhide = function(sContainerSelector) {
            $('<a href="#" class="showhide">Hide Intro <img alt="-" src="/images/icons_large/minus_button.gif"></a>').appendTo(sContainerSelector).click(showhide);
        };

        /**
        * Attach Close Button Graphic and event
        * @param {Object} sContainerSelector container to place close graphic in
        */
        var attachclose = function(sContainerSelector) {
            $('<a href="#" class="pageclose"><img alt="X" src="/images/icons_large/close.gif"></a>').appendTo(sContainerSelector).click(showhide);
        };

        var setTimer = function(args) {
            if ('autoclose' in args) {
                NewMind.env.introcontent.autoclosetimer = setTimeout(function() {
                    showhide();
                    setCookie();
                }, args.autoclose);
            }
        };

        return {
            /**
            * setup function
            * @param {Object} args setup arguments
            */
            init: function(args) {
                contentSelector = args.contentSelector;
                if ('closecontainer' in args) {
                    attachclose(args.closecontainer);
                }
                if ('showhidecontainer' in args) {
                    attachshowhide(args.showhidecontainer);
                }
                if ('pageturncontainer' in args) {
                    attachpageturn(args.pageturncontainer);
                }
                if ('cookiename' in args) {
                    cookiename = args.cookiename;
                    if (typeof ($.cookie) !== 'undefined') {
                        if ($.cookie(cookiename) === 'closed') {
                            showhide();
                        }
                        else {
                            setTimer(args);
                        }
                    }
                }
                else {
                    setTimer(args);
                }

            },

            /**
            * Wraps up content elements in one wrapper, i.e if you dont already have a wrapper insert one with JS
            * @param {Object} args wrapper arguments: contentSelector - expression to get to elements to wrap, wrapperClass - optional class name to append to wrapper div
            */
            wrapupElements: function(args) {
                var className = 'wrapperClass';
                if (args.wrapperClass !== undefined) {
                    className = args.wrapperClass;
                }
                $(args.contentSelector).wrapAll('<div class="' + className + '"></div>');
            },

            /**
            * Add Custom Events to fire upon show/hide
            * @param {Object} sEvent function to fire on event
            * @param {Object} bPreAnimation - fire function pre or post show / hide
            */
            addcustomevent: function(sEvent, bPreAnimation) {
                if (bPreAnimation) {
                    preanimateevent.subscribe(sEvent);
                }
                else {
                    postanimateevent.subscribe(sEvent);
                }
            }


        };

    } ();



})();
