if (typeof GOREFIT === "undefined" || !GOREFIT) {
    var GOREFIT = {};
}

if (typeof GOREFIT.widget === "undefined" || !GOREFIT.widget) {
        GOREFIT.widget = {};
}

if (typeof GOREFIT.util === "undefined" || !GOREFIT.util) {
        GOREFIT.util = {};
}

/**
 * Creates a horizontal navigation menu with one level of horizontal submenu.
 * @constructor
 * @param {String|HTMLElement} navEl  The ID of or reference to the navigation
 *                                    element
 * @param {Object} userConfig  An object literal containing override values for
 *                             HorizontalNav's default settings
 * @requires YAHOO.util.Dom
 * @requires YAHOO.util.Event
 */
GOREFIT.widget.HorizontalNav = function (navEl, userConfig) {
    if (navEl) {
        this.navEl = YAHOO.util.Dom.get(navEl);
        this.id = this.navEl.id;
        this.subMenus = [];
        this.timeoutId = null;
        
        // Default settings
        this.cfg = {
            showDelay: 300,
            hideDelay: 300
        }
        
        // Override defaults with user configuration
        for (var key in userConfig) {
            if (userConfig.hasOwnProperty(key)) {
                this.cfg[key] = userConfig[key];
            }
        }
        
        // Get root UL node for navigation
        if (this.navEl.nodeName.toLowerCase() !== "ul") {
            this.navEl = this.navEl.getElementsByTagName("ul")[0];
        }
        
        // Get first level navigation items
        this.firstLevel = YAHOO.util.Dom.getChildrenBy(this.navEl, function (el) {
            var firstChild = YAHOO.util.Dom.getFirstChild(el);
            if (firstChild && firstChild.nodeName.toLowerCase() === "a") {
                return true;
            }
            return false;
        });
        
        this.bindEvents();
    }
};

GOREFIT.widget.HorizontalNav.prototype.bindEvents = function () {
    // Hide submenus when mouse leaves nav
    YAHOO.util.Event.on(this.navEl, "mouseout", function (e) {
        e = e || event;
        var target = YAHOO.util.Event.getTarget(e);
        if (target.nodeName.toLowerCase() !== "input") { // Protect against strange DOM element access error
            var relatedTarget = YAHOO.util.Event.getRelatedTarget(e);
        }
        if (relatedTarget) {
            var isAncestor = YAHOO.util.Dom.isAncestor(this.navEl, relatedTarget);
            if (!isAncestor) {
                this.hideSubMenus();
            }
        } else {
            this.hideSubMenus();
        }
    }, this, true);
    
    // Show submenu for first level item that's being hovered (hide all other submenus)
    for (var i = 0; this.firstLevel[i]; i += 1) {
        YAHOO.util.Event.on(this.firstLevel[i], "mouseover", function () {
            // Precalculate subMenu (Module pattern)
            var lastChild = YAHOO.util.Dom.getLastChild(this.firstLevel[i]);
            if (lastChild.nodeName.toLowerCase() === "div") {
                var subMenu = lastChild;
                this.subMenus.push(subMenu);
            }
            // Return actual function to execute on each mouseover
            return function (e, navObj) {
                e = e || event;
                navObj.showSubMenu(subMenu);
            }
        }.call(this), this); // use .call() in order to set scope of anon function to nav object (and not lose "this" reference
    }
};

GOREFIT.widget.HorizontalNav.prototype.showSubMenu = function (subMenu) {
    clearTimeout(this.timeoutId);
    var navObj = this;
    this.timeoutId = setTimeout(function () {
        if (subMenu) {
            navObj.hideSubMenus(subMenu);
            subMenu.style.display = "block";
            YAHOO.util.Dom.addClass(YAHOO.util.Dom.getPreviousSibling(subMenu), "active");
        } else {
            navObj.hideSubMenus(true);
        }
    }, this.cfg.showDelay);
};

GOREFIT.widget.HorizontalNav.prototype.hideSubMenus = function (subMenu) {
    var navObj = this;
    function doHide() {
        for (var i = 0; navObj.subMenus[i]; i += 1) {
            if (navObj.subMenus[i] !== subMenu) {
                navObj.subMenus[i].style.display = "none";
                YAHOO.util.Dom.removeClass(YAHOO.util.Dom.getPreviousSibling(navObj.subMenus[i]), "active");
            }
        }
    }
    
    if (subMenu) {
        doHide();
    } else {
        setTimeout(doHide, navObj.cfg.hideDelay);
    }
};

GOREFIT.widget.HorizontalNav.prototype.toString = function () {
    return "HorizontalNav#" + this.id;
};

GOREFIT.util.makeHorizontalList = function (containerEl) {
	var el = YAHOO.util.Dom.get(containerEl), 
	    ul, 
	    items,
	    totalWidth = 0;
	if (el) {
		if (el.nodeName.toLowerCase() === "ul") {
			ul = el;
		} else {
			ul = el.getElementsByTagName("ul")[0];
		}
		items = YAHOO.util.Dom.getChildren(ul);
		for (var i = 0; items[i]; i += 1) {
			totalWidth += items[i].offsetWidth;
		}
		ul.style.width = totalWidth + "px";
		ul.className ="toprint";
		el.style.visibility = "visible";			
	}	
};


(function($){

	// Automatic Height Resizing for the homepage flash intro
	GOREFIT.doResize = function() {
//		$("#contents").css("height", "auto");
		var $resizedElems = $(".banners, #presentation, #flashmovie"),
			windowHeight = $(window).height(),
			lowerBound = 420,
			higherBound = 630,
			offset = 160,
			newHeight;
		newHeight = windowHeight - offset;
		newHeight = (newHeight > higherBound) ? higherBound : newHeight;
		newHeight = (newHeight < lowerBound) ? lowerBound : newHeight;
		$resizedElems.height(newHeight);
	};

})(jQuery);

if (typeof(jQuery126) !== "undefined") {
	(function($){
	
		GOREFIT.startCarousel = function() {
			$('#slide_holder').agile_carousel({
				first_last_buttons: "no",
				hover_next_prev_buttons: "no",
				next_prev_buttons: "no",
				path_to_slides: "agile_carousel_slides",
				php_doc_location: "/resources/fit/gorebikewear/js/carousel/slides.js",
				pause_button: "no",
				slide_buttons: "yes",
				slide_captions: "", 
				slide_directory: "agile_carousel_slides", // must contain the phrase "agile_carousel"
				slide_links: "",
				target_attributes: "",
				slide_number_display: "no",
				timer_length: "6000",
				transition_duration: 1000,
				transition_easing: "easeOutQuad", //easeOutQuad, easeInQuad, easeinBack, easeInOutQuint, easeInSine
				transition_type: "carousel",  //could be: carousel, fade, no_transition_effect, scroll_right 
				water_mark: "no",
				slide_doctype: "xhtml",
				remove_content: "no"
			});
		};

	})(jQuery126);
};

