/**
 * @author Aaron.Lisman
 */

(function($) {

    // extend jquery with the plugin
    $.fn.merckAccordian = function(options) {
        return new merckAccordian(this, options);
    }

    // constructor
    function merckAccordian(el, options) {
        this.Init(el);
    }

    // plugin methods and props
    $.extend(merckAccordian.prototype, {
        // props, collections		
        Panes: null,
        Tabs: null,
        TargetEl: null,
        Mode: "autoClose",
        //
        Init: function(el) {
            this.TargetEl = el;
            this.Items = this.TargetEl.children("li");
            this.Panes = this.TargetEl.children("li").children("div");
            this.Tabs = this.TargetEl.children("li").children("h4");
            this.SetStyles();
            this.SetHandlers();
            this.OpenDefault();
        },
        // function to set handlers
        SetHandlers: function() {
            //alert('inside set handlers');
            var self = this;
            this.Tabs.click(function() {
                // if its open, close it

                if ($(this).hasClass("module-merckAccordian-accordian-tab-open")) {
                    //alert('It is open');
                    self.ClosePane(this);
                } else { // open it!
                    //alert('it is closed');
                    self.OpenPane(this);
                }
            });
            //set hover states
            this.Tabs.hover(
				function() {
				    self.DetermineMode();
				    if (this.Mode != "openMode") {
				        $(this).toggleClass("merckAccordian-tab-over");
				    }
				},
				function() {
				    self.DetermineMode();
				    if (!this.Mode != "openMode") {
				        $(this).toggleClass("merckAccordian-tab-over");
				    }
				}
			);
        },
        // function to determine the mode
        DetermineMode: function() {
            // determine mode
            if ($(this.TargetEl).hasClass("leaveOpenMode")) {
                this.Mode = "leaveOpenMode";
            } else if
			($(this.TargetEl).hasClass("openMode")) {
                this.Mode = "openMode";
            } else {
                this.Mode = "autoCloseMode";
            }


        },
        // function to open the pane
        OpenPane: function(el) {
            //alert('O1')
            this.DetermineMode();
            //alert('O2')
            if (this.Mode in this.Openers) {
                //alert('O3')
                this.Openers[this.Mode].call(this, el);
            }
            //alert('Opening');
        },
        // function to close the pane
        ClosePane: function(el) {

            //alert('inside Close');
            var h4 = $(el);
            ////alert(h4.find("a").attr("title"));
            // h4.find("a").attr("title").replace("- open", "- closed");
            h4.removeClass("module-merckAccordian-accordian-tab-open").next("div").hide();
            try {
                h4.find("a").attr("title", h4.find("a").attr("title").replace("- open", "- closed"));
            }
            catch (e) {
            }

            // h4.find("a").attr("title").replace("- open", "- closed");
            //alert('Closing');

        },
        // function to set different styles
        SetStyles: function() {
            var self = this;

            // add style classes dynamically to avoid repetition in markup
            this.Items.addClass("module-merckAccordian-accordian-item");
            this.Tabs.addClass("module-merckAccordian-accordian");
            this.Tabs.find("a").addClass("accButton");

            // add padding where panes DO NOT contain nested modules
            this.Panes.not(":has(.module-merckTabs)").addClass("pane-padded");

            // these are the problem lines Jose
            this.Panes.prepend("<img src='includes/audio-video/swf/assets/images/accordian/icon-arrow-up.gif' class='accordianUpDownArrow' />");
            this.Panes.find('.accordianUpDownArrow').click(function() {
                //alert('inside set style');
                self.ClosePane($(this.parentNode.parentNode).children("h4").get(0));
            });



        },
        Openers: {
            leaveOpenMode: function(el) {
                this.GeneralOpener(el);
            },
            openMode: function(el) {
                // do nothing, they're already open
            },
            autoCloseMode: function(el) {
                this.CloseAll();
                this.GeneralOpener(el);
            }
        },
        GeneralOpener: function(el) {
            //alert('inside general opener');
            var h4 = $(el);

            var openFunc = function() {
                //alert('open function called');

                h4.toggleClass("module-merckAccordian-accordian-tab-open").next("div").toggle();
                //alert('Class toggled');
                try {
                    h4.find("a").attr("title", h4.find("a").attr("title").replace("- closed", "- open"));
                }
                catch (e) {
                }


            }
            // to fix ie6 background behavior bug, set a small timeout
            setTimeout(openFunc, 100);

        },
        // function to close all open tabs
        CloseAll: function() {
            var self = this;
            this.Tabs.each(function() { self.ClosePane(this); })
        },
        // function to open default tab
        OpenDefault: function() {
            var el = this.Items.filter("[open=true]").get(0);

            var self = this;
            this.Items.filter("[open=true]").children("h4").each(function() {
                self.GeneralOpener($(this).get(0));
            });

        }
    });

})(jQuery);
