/**
 * @author: Rhys Burnie, Icon.inc http://www.iconinc.com.au
 * @website: Cabots
 * 
 * Exposed Methods and Properties:
 * 
 * Cabots.Wizard.close(level)
 * level = number 1 or 2 or not supplied
 * if 1: if 2 open closes then closes 1 else closes 1
 * if 2: closes 2
 * if no argument: closes any open 2 first if both present
 */

(function ($) {
    Cabots = this.Cabots || {
        extract_anchor: function (url) {
            var split = url.split('#');
            if (split.length != 2) return false;
            return split[1] != '' ? split[1] : false;
        }
    };
    var LOG = Cabots.log || function () { }
		, Wizard = Cabots.Wizard = {}
		;

    Wizard.runtime = function () {
        LOG('Wizard runtime!');

        $(function () {

            var open = null
				, _interior_ = 'interior'
				, _exterior_ = 'exterior'
				, _hide_ = 'hide'
				, _show_ = 'show'
				, _open_ = 'open'
				, wizard = $('#wizard')
				, bar1 = $('.bar', wizard).addClass(_hide_)
				, bar2 = $('.bar2', wizard).addClass(_hide_)
				, interior = $('#nav_interior')
				, exterior = $('#nav_exterior')
				, project_toggles = $('#wizard .bar ul a')
				, project_toggle_parents = []
				, project_lists = []
				, empty_fn = function () { }
                , view_all = $('#step2_viewall')
				, show_project_list = function (el) {
				    $(project_lists).css('display', 'none');
				    $(el).css('display', 'block');
				    // Fix view all link
				    view_all.attr('href', '/products/' + $(el).attr('id') + '/' + $(el).attr('tag'));
				}
				, tabs = [interior[0], exterior[0]]
            /**
            * @param String - 'interior' || 'exterior'
            * 
            * If not supplied removes boths mode classes
            */
				, set_mode_class = function (mode) {
				    switch (mode) {
				        case _interior_: $(wizard).removeClass(_exterior_).addClass(_interior_);
				            break;
				        case _exterior_: $(wizard).removeClass(_interior_).addClass(_exterior_);
				            break;
				        default: $(wizard).removeClass(_interior_).removeClass(_exterior_);
				            break;
				    }
				}
				, autoclose_timer = null
				, autoclose_delay = 500
				, autoclose = function () {
				    autoclose_timer = setTimeout(Wizard.close, autoclose_delay)
				}
				, autoclose_cancel = function () {
				    clearTimeout(autoclose_timer);
				}
				, wizardTabClick = function () {
				    autoclose_cancel();
				    if ($(this).hasClass(_open_)) return;
				    wizard.show();
				    $(tabs).removeClass(_open_);
				    $(this).addClass(_open_);
				    set_mode_class($(this).attr('id').replace('nav_', ''));
				    if (!bar1.hasClass(_open_)) bar1.trigger(_show_);
				}
				;

            /**
            * Expose some methods publicly
            */
            Wizard.close = function (level) {
                clearTimeout(autoclose_timer);

                switch (level) {
                    case 2: if (bar2.hasClass(_open_)) {
                            bar2.trigger(_hide_);
                        }
                        break;
                    default: if (bar2.hasClass(_open_)) {
                            bar2.trigger(_hide_, [function () {
                                if (bar1.hasClass(_open_)) {
                                    bar1.trigger(_hide_, [function () {
                                        wizard.hide();
                                    } ]);
                                }
                            } ]);
                        } else if (bar1.hasClass(_open_)) {
                            bar1.trigger(_hide_, [function () {
                                wizard.hide();
                            } ]);
                        }
                        break;
                }
            };

            /**
            * Initial bar setup
            */
            $.each([bar1, bar2], function (i, bar) {
                bar.css('display', 'block');
                var height = $('.bar-inner', bar).outerHeight()
					;

                bar.height(0).removeClass(_hide_).bind({
                    show: function (e, callback) {
                        callback = callback || empty_fn;
                        $(this).animate({ height: height }, 80, callback).addClass(_open_);
                    }
					, hide: function (e, callback) {
					    callback = callback || empty_fn;
					    $(this).animate({ height: 0 }, 150, callback).removeClass(_open_);

					    /**
					    * remove open class of tabs or filters
					    */
					    $(this == bar1[0] ? tabs : project_toggle_parents).removeClass(_open_);
					}
                });

            });

            /**
            * The two main tabs - toggles visibility of 1st level filter items
            * and sets the wizards 'mode'
            */
            $.each(tabs, function (i, el) {
                el = $(el);
                if (el.hasClass(_open_)) {
                    open = el;
                    set_mode_class(el.attr('id').replace('nav_', ''));

                }

                el.bind({
                    mouseenter: wizardTabClick
					, mouseleave: autoclose
                });
            });

            bar1.bind({
                mouseenter: autoclose_cancel
				, mouseleave: autoclose
            });

            bar2.bind({
                mouseenter: autoclose_cancel
				, mouseleave: autoclose
            });

            /**
            * Sub item filters - toggles visibility of filtered 2nd level items
            */
            $.each(project_toggles, function (i, el) {
                //var id = Cabots.extract_anchor(el.href);
                var id = $(el).attr('projectid');
                if (id) {
                    var content = $('#' + id, wizard);
                    if (content.length == 1) {
                        var parent = $(el).parent('li');
                        project_lists.push(content[0]);
                        project_toggle_parents.push(parent[0]);

                        $(el).mouseenter(function (e) {
                            e.preventDefault();
                            if (parent.hasClass(_open_)) return;
                            show_project_list(content[0]);
                            $(project_toggle_parents).removeClass(_open_);
                            parent.addClass(_open_);
                            if (!bar2.hasClass(_open_)) bar2.trigger(_show_);
                        });

                        if (parent.hasClass(_open_)) show_project_list(content[0]);

                        $('li', content).css('cursor', 'pointer').click(function () {
                            var a = $('a', this).eq(0);
                            LOG(a.attr('href'));
                        });
                    }
                }
            });

        });
    }

})(jQuery);
