﻿(function($) {


    ////////////////////////////////////////////////////////////
    //	PRIMARY NAVIGATION
    ////////////////////////////////////////////////////////////
    $.fn.geoPrimaryNavigation = function(options) {

        // Multiple Elements ? Regsiter all elements
        if (this.length > 1) {
            $(this).each(function() {
                $(this).geoPrimaryNavigation(options);
            });
            return this;
        }

        // Private Variables
        var defaults = {
            offsetTop: 6,
            offsetLeftColl: {}
        };
        var options = $.extend(defaults, options);
        var scope = this;
        var listObjs = $("li", this);
        var menuDelay = null;


        // Private Methods
        var init = function() {
            $.each(listObjs, function() {
                var id = $(this).attr("id");
                var m = $("div#" + id + "-menu");
                var l = $(this).position().left - options.offsetLeftColl[id];
                m.css({
                    "position": "absolute",
                    "top": $(this).position().top - options.offsetTop,
                    "left": l
                });

                //alignPanel( m );

                $(this).mouseenter(onListItemOver);
                m.mouseleave(onListMenuOut);
            });
        };

        var alignPanel = function(menu) {
            if (menu.hasClass("menu-align-right")) {
                var tab = $(menu).find(".primary-nav-menu-tab");
                var contents = $(menu).find(".primary-nav-menu-contents");
                tab.css({
                    "position": "absolute",
                    "right": "0"
                });
                contents.css({
                    "position": "absolute",
                    "left": -(menu.outerWidth() / 2)
                });
            }
        }

        var onListItemOver = function() {
            var cid = $(this).attr("id");
            var m;
            var active;
            clearInterval(menuDelay);

            $.each(listObjs, function(index) {
                id = $(this).attr("id");
                m = $("div#" + id + "-menu");
                if (cid === id) {
                    active = m;
                    //menuDelay = setTimeout(function() {
                    active.fadeIn(200);
                    //}, 350);
                }
                else {
                    m.trigger("mouseleave");
                }
            });
        }

        var onListMenuOut = function() {
            //clearInterval( menuDelay );

            var m = $("div#" + $(this).attr("id"));
            m.hide();
        }


        // Public Methods
        scope.initialize = function() {
            init();
            return scope;
        };

        // Support Chaining
        return scope.initialize();
    }


    ////////////////////////////////////////////////////////////
    //	Gallery Controller : 3 B'S
    ////////////////////////////////////////////////////////////
    $.fn.geoGalleryController = function(options) {

        // Multiple Elements ? Regsiter all elements
        if (this.length > 1) {
            $(this).each(function() {
                $(this).geoGalleryController(options);
            });
            return this;
        }

        // Private Variables
        var scope = this;
        var defaults = {
            collection0: null,
            collection1: null,
            collection2: null,
            collection0Class: null,
            collection1Class: null,
            collection2Class: null,
            time: 8000,
            mouseTarget: document,
            menuTargets: "",
            mouseTargetY: 470
        };
        var options = $.extend(defaults, options);
        var listObjs = $("li", scope);
        var isRunning = false;
        var intervalID;
        var currentIndex = 0;
        var nextIndex = -1;

        // Private Methods
        var init = function() {
        };
        var initListeners = function() {
            //$(options.mouseTarget).bind( 'mousemove', onUserMouseMove );
            $(options.mouseTarget).bind('mouseover', onUserMouseAction);
            $(options.mouseTarget).bind('mouseout', onUserMouseAction);
            /*$(options.mouseTarget).bind('mouseover', stop);*/

            if (options.menuTargets.length > 0) {
                $(options.menuTargets).bind('mouseover', onUserMenuAction);
                $(options.menuTargets).bind('mouseout', onUserMenuAction);
            }

            $(window).resize(onWindowResize);
            $(window).trigger('resize');
        };
        var onWindowResize = function(e) {
            var targetW = listObjs.width();
            var winW = $(window).width();
            var w;
            if (targetW > winW) {
                w = -((targetW - winW) / 2);
            }
            else {
                w = ((winW - targetW) / 2);
            }
            $.each(listObjs, function() {
                $(this).css("left", w + "px");
            });
        }

        var closeOpenMenus = function(e) {
            $('#primary-navigation-menus div.hidden').hide();
        }


        var onUserMouseAction = function(e) {
            /*alert("stop animatioon");*/
            if (isRunning) {
                /*alert("stop");*/
                stop();
            }
            else {
                /*alert("reset");*/
                reset();
            }
        }
        var onUserMenuAction = function(e) {
            if (isRunning) {
                stop();
                var c;
                var id = $(this).attr("id");
                $.each(options.menuTargets, function(index) {
                    c = "animation-target-" + (index + 1);
                    if (id == c) {
                        nextIndex = (index + 1);
                    }
                });
                next();
            }
            else {
                reset();
            }
        }

        var onTimedEvent = function(e) {
            next();
        };
        var next = function() {
            if (nextIndex == currentIndex) {
                return;
            }

            // HIDE
            options.collection1.filter(options.collection1Class + currentIndex).hide();
            options.collection0.filter(options.collection0Class + currentIndex).delay(600).fadeOut(400);

            var busLine = $("div .business-line-sprites");
            var oldClass = options.collection2[currentIndex];
            if (busLine.hasClass(oldClass))
                busLine.removeClass(oldClass)

            if (nextIndex == -1) {
                currentIndex++;
                if (currentIndex >= listObjs.length) {
                    currentIndex = 0;
                }
            }
            else {
                currentIndex = nextIndex;
                nextIndex = -1;
            }

            // SHOW
            options.collection0.filter(options.collection0Class + currentIndex).delay(500).fadeIn(600, function() {
                var e = options.collection1.filter(options.collection1Class + currentIndex);
                e.show();
                var newClass = options.collection2[currentIndex];
                if (newClass != "null")
                    busLine.addClass(newClass);
            });
        };
        var run = function() {
            isRunning = true;
            intervalID = setInterval(onTimedEvent, options.time);
        };
        var reset = function() {
            if (isRunning) {
                stop();
            }
            run();
        };
        var stop = function() {
            
            isRunning = false;
            clearInterval(intervalID);
        };


        // Public Methods
        scope.initialize = function() {
            init();
            initListeners();
            run();
            return scope;
        };

        // Support Chaining
        return scope.initialize();
    }
})(jQuery);

