var PARENTSCONNECT = PARENTSCONNECT?PARENTSCONNECT:{}; 

(function($, PARENTSCONNECT){
	PARENTSCONNECT.globalNavigation = function(menu, full){
	  var glbNav        = menu.globalNavItems;
	  var mainMenuBlock = $('.global-navigation-menu');
	  var topMenu       = $('.main-navigation-category-ex');
	  var wideTarget    = $('.nav-submenu-row');
	  var exHTML        = $('.submenu-ex').html();
	  var builtMenu     = '';
	  var ex            = '.submenu-ex';
	  var storedMenus   = new Array();
	  
	  /**
	  * initialize menu
	  */
	  function init(full){
		// render top level navigation
		if(full) renderMenuTopMenu();
		
	    // pre-render json data 
	    $('.main-navigation-category').each( function(){
	    	renderMenuSubMenu($(this));
	    });
	    
	    // bind mouse events 
	    bindEvents();
	  }
	  
	  function renderMenuTopMenu(){
		var last  = "global-navigation-gbr";
		
		for( i=0; i<glbNav.length; i++ ){
			
		  // build top level menu clone and append to top-navigation
		  var topMenuCl = topMenu.last().clone().attr(
			    'navItem', 
			    i
			  ).find(
				'.nav-opt'
			  ).attr(
				'href', 
				buildNavId(glbNav[i].url, "top-nav")
			  ).html(
				glbNav[i].title
			  ).parent().parent().removeClass(
				'hide'
			  ).removeClass(
				'main-navigation-category-ex'
			  ).addClass(
				'main-navigation-category'
			  );
		  	
		    // apply first option style 
		  	if( i == (glbNav.length-1) ){
				topMenuCl.addClass(last);
			}
		  	
			mainMenuBlock.find('.top-navigation').append(topMenuCl);
		}
	  }
	  
	  /**
	  * build DOM menu and store uri encoded version in array
	  */
	  function renderMenuSubMenu(nav){
	    var index   = nav.attr('navItem');
	    var subMenu = glbNav[index].navItems;
	    
	    if( subMenu.length > 1 ){ /* Build Wide Menu */
	      for( i=0; i<subMenu.length; i++ ){
	    	if( subMenu[i].visible==null || subMenu[i].visible==true ){
	    	  buildSubMenu( nav, subMenu[i] );
	    	  // append a clean template 
	    	  if(i<(subMenu.length-1)) $(ex).append(exHTML);
	    	}
	      }
	      
	      /* store menu html */
	      storedMenus[index] = new Array('wide', encodeURI($(ex).html()));
	      
	      // reset example space
	      $(ex).html(exHTML);;
	      
	    } else { 
	      /* 
	       * Build Single Menu 
	      */
	      if( subMenu.length > 0 && ( subMenu[0].visible==null || subMenu[0].visible==true ) ){
	        buildSubMenu( nav, subMenu[0] );
	        
	        // store menu html
	        storedMenus[index] = new Array('single', encodeURI($(ex).html()));
	        
	        // reset example space 
	        $(ex).html(exHTML);
	      } else {
	    	  storedMenus[index] = new Array('single', null);
	      }
	    }
	  }
	  
	  /**
	  * build sub navigation DOM
	  */
	  function buildSubMenu( target, submenu ){
	    // target current menu
	    curMenu = $(ex).find('.submenu:last');
	
	    // apply catagory/image/url props 
	    curMenu.find('.nav-opt-image img').attr('src', ('http://images.parentsconnect.com' + submenu.image)).parent().attr('href', buildNavId(submenu.url, "sub-nav"));
	    
	    // sponsor image if one exists 
	    if ( submenu.sponsorImageUrl !== null ){
	    	curMenu.find('.nav-opt-sponsor img').attr('src', ('http://www.parentsconnect.com' + submenu.sponsorImageUrl)).parent().attr('href', buildNavId(submenu.url, "sub-nav"));
	    	curMenu.find('.nav-opt-sponsor').show();
	    }
	    
	    // add menu header 
	    curMenu.find('.menu-link-first a').attr('href', buildNavId(submenu.url,"sub-nav")).html(submenu.title);
	    
	    // add Options 
	    for( j=0; j<submenu.navItems.length; j++ ){
	      if( !j ){
	        curMenu.find('.menu-link a').attr('href', buildNavId(submenu.navItems[j].url,"sub-nav")).html(submenu.navItems[j].title.replace('q&a', 'q & a'));
	      } else {
	        var opt = curMenu.find('.menu-link:last').clone().find('a').attr('href', buildNavId(submenu.navItems[j].url,"sub-nav")).html(submenu.navItems[j].title).parent();
	        curMenu.find('.nav-opt-submenu').append(opt);
	      }
	    }
	  }
	  
	  /**
	  * bind mouse enter/leave events for main menu items
	  */
	  function bindEvents(){
	    $('.main-navigation-category').mouseenter(function(){
	      var index = $(this).attr('navItem');
	      
	      if ( storedMenus[index][0] == 'wide' ){ 
	    	/* 
	    	 * menu is wide 
	    	*/
	        $('.main-navigation-category').removeClass('hover-opt');
	        $(this).addClass('hover-opt');
	        
	        /* 
	         * populate menu 
	        */
	        wideTarget.addClass('hover-opt').find(
	          '.nav-opt-submenuWrap'
	        ).removeClass('hide').html( decodeURI(storedMenus[index][1]) );
	        
	      } else {
	    	/* 
	    	 * single menu 
	    	*/
	        wideTarget.removeClass('hover-opt');
	        $('.main-navigation-category').removeClass('hover-opt');
	        if( storedMenus[index][1] != null ){
	        	$(this).addClass('hover-opt').find('.nav-opt-submenuWrap').removeClass('hide').html( decodeURI(storedMenus[index][1]) );
	        } else {
	        	$(this).addClass('hover-opt');
	        }
	      }
	      
	    }).mouseleave(function(){
	      var index = $(this).attr('navItem');
	      if ( storedMenus[index][0] != 'wide' ){ /* menu is single */
	    	  $(this).addClass('hover-opt').find('.nav-opt-submenuWrap').addClass('hide');
	      }
	    });
	    
	    /* 
	     * bind mouse leave
	     */
		wideTarget.mouseleave( function(){
		  $(this).removeClass('hover-opt');
		});
		  
		mainMenuBlock.mouseleave( function(){
		  $('.main-navigation-category').removeClass('hover-opt');
		  wideTarget.removeClass('hover-opt');
		});
	  }
	  
	  /**
	   * build navid param
	   */
	  function buildNavId(targetUrl, navid) {
		  if (typeof targetUrl !== 'undefined'){
			  if (targetUrl.indexOf('?') === -1){
				  return targetUrl + "?navid=" + navid;
			  } else {
				  return targetUrl + "&navid=" + navid;
			  }
		  }
	  }
	  
	  
	  
	  /**
	  * initialize global navigation
	  */
	  init(full);
	}
})(jQuery, PARENTSCONNECT);

/**
 *show the 'bumper' & warning link if the user is coming from nick.com
 */
$.safeDocumentReady.add(function() {
	if ( document.referrer != "" && document.referrer.indexOf(".nick.com") !== -1 ) {
		if ( document.referrer.indexOf("bumperFrameset") == -1 ) { // linked directly from nick.com; without a bumper
			var bumperDiv = '<div id="nickBumper" style="background: #FFF; height: 179px; padding-bottom: 5px; text-align: center;"><img src="/assets/header/bumper_generic_new_d2.jpg" style="margin: 0 auto 0 auto;" width="900" height="179" /></div>';
			$(".brand-mamabar").before( bumperDiv );
	
			mtvn.btg.util.Cookie.set( 'fromNick', true, { path: '/', domain: '.parentsconnect.com' }); // write cookie value
	
			setTimeout( function() { $("#nickBumper").animate( {height: 0}, function() { $("#nickBumper").remove(); } ); }, 5000 );
		}
	}
});

