/* Declare a namespace for the site */
window.MFL = window.MFL || {};
MFL.Site = MFL.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function() {
	
	/**
		Internal Shorthand reference
	*/
	var $self = this,

		/**
			Primitives
		*/
		TRUE = true,
		FALSE = false,
		NULL = null,
		UNDEFINED,

		/**
			An internal flag to ensure we don't initialize this object more than once.
		*/
		initialized = FALSE;
	
	/**
	 * The initialize method
	 * Initializes other common scripts and sets up some global constants.
	 */
	$self.initialize = function(){
		
		if (initialized) {
			return;
		}

		$self.HomeNav.initialize();
		$self.ExternalLinks.initialize();
		$self.SiteMapHidePosts.initialize();
		$self.HideLBClose.initialize();
		$self.PageNav.initialize();
		
		initialized = TRUE;
		
	};

	$self.HomeNav = function() {

		/**
			An internal flag to ensure we don't initialize this object more than once.
		*/

			var $scope,
				$link,
				$children,

			vars = {
				scopeID			:	"nav",
				linkClass		:	"page_item",
				childrenClass	:	"children"
			},

			setup = function() {

						$children.hide();

						$link.hoverIntent({
							sensitivity:3,
							interval: 400,
							over:
							function() {

								var checkElement = $(this).next();
								if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
									return false;
								}
								if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
									$('li.page_item ul:visible').slideUp('normal').hide();
									checkElement.slideDown('normal').show();
									return false;
								}
							},
							timeout: 500,
							out: function() {}
							}
						);
			};

		return {

			initialize : function(){

				$scope			= $("#" + vars.scopeID);

				if ($scope.length === 0) {
					return;
				}

				$link			= $scope.find("." + vars.linkClass + " > a");
				$children		= $scope.find("." + vars.childrenClass);

				setup();

			}

		};
		
	}();

	$self.PageNav = function() {

		/**
			An internal flag to ensure we don't initialize this object more than once.
		*/

			var $scope,
				$link,
				$child,
				$children,
				$currentPage,
				$parent,
				$currentParent,
				$mainNavLinks,

			vars = {
				scopeID				:	"logo-and-nav",
				linkClass			:	"ul.pagenav li > a",
				childClass			:	"child",
				childrenClass		:	"ul.pagenav li ul",
				currentPageClass	:	"current_page_item",
				parentClass			:	"parent",
				currentParentClass	:	"current_page_parent",
				mainNavClass		:	"ul.pagenav > li"
			},

			setup = function() {

				if($currentPage.parent().hasClass("children")){
					$currentPage.parent().parent().parent().parent().addClass("parent on");
				} else if($currentPage.parent().hasClass("child")){
					$currentPage.parent().parent().addClass("parent on");
				}

				//hides all list items on load
				$children.hide();

				$parent			= $scope.find("." + vars.parentClass);

				//determinse current parent section and set global variable 
				var currParent = $(vars.mainNavClass).index($parent);

				//show current section subnav
				if($parent.length === 1){
					$parent.find(" > ul").show();
				}

				//show current subsection
				if($currentParent.length === 1){
					$currentParent.parent().show();
				}

				$currentPage.parent().show();
				$currentPage.find(" > ul").show();
				$currentPage.parent().parent().show();

				$mainNavLinks.hoverIntent({
					sensitivity:3,
					interval: 400,
					over:
					function() {

						var cnt = $mainNavLinks.length;
						var currItem = $(vars.mainNavClass).index($(this));
						var currChild = $(vars.mainNavClass)
						var children = $mainNavLinks.find("." + vars.childClass);
						var hasChild = $(this).find("." + vars.childClass);

						$mainNavLinks.each(
							function(i){
								var  indx = i;

								//checks if this is the section parent and if so do nothing
								if((currItem == currParent) || (hasChild == 0)){
									return false;
								}

								//opens item being hovered over...
								if(indx == currItem){
									$(this).find("." + vars.childClass).slideDown('normal').show();
								//...and closes all the rest except for the section parent.
								} else if((indx != currItem) && (indx != currParent)){
									$(this).find("." + vars.childClass).slideUp('normal').hide();
								}

							}
						);

					},
					timeout: 500,
					out: 
						function() {

						}
					}
				);

			};

		return {

			initialize : function(){

				$scope			= $("#" + vars.scopeID);

				if ($scope.length === 0) {
					return;
				}

				$link			= $scope.find(vars.linkClass);
				$child			= $scope.find("ul." + vars.childClass);
				$children		= $scope.find(vars.childrenClass);
				$currentPage	= $scope.find("." + vars.currentPageClass);
				$currentParent	= $scope.find("." + vars.currentParentClass);
				$mainNavLinks	= $scope.find(vars.mainNavClass);

				if($mainNavLinks.length === 0 ){
					return;
				}

				setup();

			}

		};
		
	}();

	$self.ExternalLinks = function() {

		/**
			An internal flag to ensure we don't initialize this object more than once.
		*/

			var $scope,
				$link,

			vars = {
				scopeID			:	"wrapper",
				linkRel			:	"external"
			},

			setup = function() {

				$link.bind("click",
					function(){
						$(this).attr("target","_blank");
					}
				);

			};

		return {

			initialize : function(){

				$scope			= $("#" + vars.scopeID);

				if ($scope.length === 0) {
					return;
				}

				$link	= $scope.find("a[rel=\"" + vars.linkRel + "\"]");

				setup();

			}

		};
		
	}();

	$self.SiteMapHidePosts = function() {

		/**
			An internal flag to ensure we don't initialize this object more than once.
		*/

			var $scope,
				$headers,

			vars = {
				scopeID			:	"ddsg-wrapper",
				headers			:	"h2"
			},

			setup = function() {

				$headers.each(
					function(){

						hideMe = $(this).html();

						if(hideMe == "Posts"){
							$(this).hide();
							$(this).next().hide();
						}

					}	

				);				

			};

		return {

			initialize : function(){

				$scope			= $("." + vars.scopeID);

				if ($scope.length === 0) {
					return;
				}

				$headers	= $scope.find(vars.headers);

				setup();

			}

		};
		
	}();

	$self.HideLBClose = function() {

		/**
			An internal flag to ensure we don't initialize this object more than once.
		*/

			var $scope,
				$lb,
				$close,

			vars = {
				scopeID			:	"gallery",
				lbID			:	"lbIframe",
				closeClass		:	"closeLink"
			},

			setup = function() {

				if(($lb.length == 0) && ($close.length != 0)){

					$close.hide();

				}


			};

		return {

			initialize : function(){

				$scope	= $("#" + vars.scopeID);

				if ($scope.length === 0) {
					return;
				}

				$lb			= $scope.find("#" + vars.lbID);
				$close		= $scope.find("." + vars.closeClass);

				setup();

			}

		};
		
	}();

	return $self;

}).call(MFL.Site);

/**
	Fire up the global initialization.
*/
$(document).ready(MFL.Site.initialize);
