﻿// Sample Usage:
//$(document).ready(function () {
//    $('body').topbar({
//        'message': 'This is your message, type your message here.',
//        'linkText': 'Your link here',
//        'linkUrl': 'http://google.com',
//        'displayOpenButton': true,
//        'displayCloseButton': true,
//        'delayToDisplay': 1000
//    });
//});


(function ($) {
    $.topbar = function (el, options) {
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("topbar", base);

        base.init = function () {
            base.options = $.extend({}, $.topbar.defaultOptions, options);

            // Initialization Code HERE

            // Quit if it already active.
            if ($('#topbar').length > 0)
                return this;

            $('body').prepend('<div id="topbar"><div class="message">' + base.options.message + '<a id="call_to_action" href="' + base.options.linkUrl + '">' + base.options.linkText + '</a></div></div><div id="bar_handle_close">close</div><div id="bar_handle_open">open</div>');

            // Initialize the right display style for the open button.
            if (base.options.displayOpenButton)
                $('bar_handle_open').css('display', 'block');

            $('#bar_handle_close').click(function () {
                
                $('#bar_handle_close').slideUp('slow');

                $('#topbar').slideUp('slow');

                if (base.options.displayOpenButton)
                    $('#bar_handle_open').slideDown('slow');
            });

            $('#bar_handle_open').click(function () {

                $('#topbar').slideDown('slow');

                if (base.options.displayCloseButton)
                    $('#bar_handle_close').slideDown('slow');

                $('#bar_handle_open').slideUp('slow');
            });

            window.setTimeout(function () {
                $('#topbar').slideDown('slow');

                if (base.options.displayCloseButton)
                    $('#bar_handle_close').slideDown('slow');

                $('#bar_handle_open').slideUp('slow');
            }, base.options.delayToDisplay);  // delay to display the bar after loading.

            // Initialization Code HERE
        };

        // Sample function here
        //base.functionName= function(parameter){

        //};





        // Run initializer
        base.init();
    };

    // Public function
    $.topbar.Deactivate = function () {
        $('#topbar').remove();
    };


    $.topbar.defaultOptions = {
        'message': 'This is your message, type your message here.',
        'linkText': 'Your link here',
        'linkUrl': 'http://google.com',
        'displayOpenButton': false,
        'displayCloseButton': false,
        'delayToDisplay': 1500
    };

    $.fn.topbar = function (options) {
        return this.each(function () {
            (new $.topbar(this, options));
        });
    };

})(jQuery);
