﻿

(function($){

var Framework = {
   init: function(){
       Framework.supernav();
       Framework.mainsearch();
       Framework.superfooter();
   },
   
   supernav: function(){
       $("#supernav-sitemap").click(function(){
          $("#supernav .sitemap").slideToggle(400);
          $("#supernav-sitemap").toggleClass("on");
          return false;
       });
       $("#supernav .close a").click(function(){
          $("#supernav .sitemap").slideToggle(400);
          $("#supernav-sitemap").toggleClass("on");
          return false;
       });
   },
      
   mainsearch: function() {
       $("#search_text").focus(function(){
            if ($(this).val() == 'SEARCH') { $(this).val('') }
       });
   },  
   
   superfooter: function(){
       
          // inject site-footer into the superfooter
          var footer = $("#site-footer #info").html() || $("#site-footer").html();
          $("#site-footer").remove();          
          $("#superfooter .infobar .inner").append(footer);
       
          function style(li) {
           $("li:last",li).addClass("last");

				$("ul.overlay", li).slideToggle();  				
                  $("ul.overlay",li).prepend('<a href="#" class="close"><img src="http://www.hbs.edu/shared/images/framework/2.0/icon.close.footer.gif" height="9" width="9" alt="Close" /></a>');
                  $("a.close",li).click(function(){
   					closeAll();
                    return false;
               })
          }
             
           function closeAll() {
				$(".overlay:not(.closed)").slideToggle(); 
	
	              $(".overlay:not(.closed)").toggleClass("closed");
	          }
             
          // opening the infobar with scroll
                
          function isBottom() {
			 /* adds a padding to tell you before it hits the bottom */
             var padding = 25;


              function getYOffset() {
                  var pageY;
                  if(typeof(window.pageYOffset)=='number') {
                     pageY=window.pageYOffset;
                  }
                  else {
                     pageY=document.documentElement.scrollTop;
                  }
                  return pageY;
              }
            
             return getYOffset() >= ((document.documentElement.scrollHeight - padding) - document.documentElement.clientHeight);
          }


          
          function toggleopen(){
	
	
             $("#superfooter .selector").not('.opened').each(function(){
				
	
                $("#superfooter .infobar").slideToggle(150,function(){
                    $("#superfooter .selector").toggleClass("opened");
                });
                
                
                 $(".footerspace").slideToggle(150,function(){ 
					//window.scrollTo(0, document.body.scrollHeight);
					$('html, body').animate({scrollTop: $(document).height()}, 100);
				 
					});
             });



		
          }
          
          function toggleclose(){
		
                $("#superfooter .selector.opened").each(function(){
				 $(".footerspace").slideToggle(150,function(){ });
	
                   $("#superfooter .infobar").slideToggle(150,function(){
                      $("#superfooter .selector").toggleClass("opened");
                   });
                });

					
          }
       
		/*
          Framework.lastbottom = true;          
          Framework.scrollInteveral = window.setInterval(function(){
             var isbottom = isBottom();
             if (isbottom && !Framework.lastbottom) {
                  // open up
                  toggleopen();
             }
             if (!isbottom && Framework.lastbottom) {
                 // closeup
                 toggleclose();
             }
             Framework.lastbottom = isbottom;
          },100);
		*/
				
          // opening the infobar with click
                
          $("#superfooter .selector").click(function(){
             toggleopen();

		});   
                
          $("#superfooter").click(
          function(){
             toggleclose();
          });   
                
          // Social Icons
               
          $("#superfooter").click(function(event){event.stopPropagation();})
                

		  $("#superfooter .opens-overlay-popup > a").click(function(event){
				var c = $(this).parent().find("ul");
				console.info(c);
				$(c).toggleClass("closed");
                $(c).prepend('<a href="#" class="close"><img src="http://www.hbs.edu/shared/images/framework/2.0/icon.close.footer.gif" height="9" width="9" alt="Close" /></a>');
				return false;	
		   });

          $("#superfooter .opens-overlay>a").click(function(event){
               var $p = $(this).parent();
               style($p);
               if ($(">ul",$p).hasClass("closed")) { 
                    //$("body").trigger("BodyClick"); 
                    closeAll();
               }
               $(">ul",$p).toggleClass("closed");
               event.stopPropagation();
               return false;
          });
                
          $("html").click(function(){
               closeAll();
               toggleclose();
          });
          
    }

}


$(document).ready(function(){Framework.init()});


})(jQuery);

window.query = {};
window.location.href.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function($0, $1, $2, $3) { if ($3) window.query[$1] = $3;}
);
