Core.settings({site:'bulletin',plugins:'/bulletin/js/plugins'});

(function($){

var Bulletin = {
     ondomready:function() {
         Bulletin.links();
         Bulletin.topnav();
         Bulletin.toc();
         Bulletin.max10();
         Bulletin.pastissues();
         Bulletin.loginform();
         Bulletin.tooltips();
         Bulletin.carousel();
         Bulletin.lightbox();         
         //Bulletin.sendtofriend();
         Core.email_obfuscation();
         $("#flipper").coreRotator();
         Alumni.autofill_forms();
         $("body").addClass('jsenabled');
         Bulletin.sharecssfixes();
         $(".sharethis").each(function(){  Core.sharethis(this);}  );        
         Bulletin.set_input("economy, Jones","class-search-name");
         Bulletin.set_input("economy, crisis","past-issue-keywords");
         Bulletin.set_input("2008 or AMP07","class-search-class");
         Bulletin.set_input("jsmith@mba1967.hbs.edu","login-username");

         Bulletin.expandable_uls();
         Bulletin.classnotes_search();
         Bulletin.usertools();
         Bulletin.articletools();
         $("#rotator1").coreRotator({random:1});
         $("#rotator2").coreRotator({random:1});
         $("#loader").addClass('loaded');
         $("hr").replaceWith('<div class="hr"></div>');
         $("img#dyk").replaceWith('<img src="/images/site/text-did-you-know-bull.gif" alt="Did You Know?" height="23" width="220" />');
         $("a[href$=.jpg],a[href$=.png],.textpop").fancybox();
         Bulletin.fancyvideo();
    },
    
    sharecssfixes: function(){
    	/*rqd cleaning up some spacing issues for sharethis toolbar aug 2011*/
        $("#content h3:first").css("margin-top", "0px");
    }, 	
     
     fancyvideo: function() {  	
     	$(".video").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 385,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});

		return false;
		});
   },
   
    links: function() {   
           $("#content a").each(function() {
              // comment add .ext class
              $a = $(this);
              if (this.href && !/mailto/.test(this.href)) {
                 if (! /hbs.edu/.test(this.href)) {$a.addClass("ext");}
                 if (/video2.harvard.edu/.test(this.href)) {$a.removeClass("ext");}
                 if (/hbsp.harvard.edu/.test(this.href)) {$a.removeClass("ext");}
		         if (/harvardbusiness.org/.test(this.href)) {$a.removeClass("ext");}
		         if (/hbr.org/.test(this.href)) {$a.removeClass("ext");} 		         
                 if (/javascript:/.test(this.href)) {$a.removeClass("ext");}
                 if ( /.pdf$/.test(this.href)) {$a.addClass('pdf')}
              }
              
              if ($a.hasClass('ext')) {
                 if ($a.hasClass('noext')) {
                   $a.removeClass('ext');
                 }
                 $a.addClass('new_window');
              }
              
              if ($a.hasClass("new_window")){
                  $(this).attr('target','_blank'); 
                  //$(this).click(function(){
                  //    Core.new_window(this);
                  //    return false;
                  //});
              }
          });
    },

   lightbox: function() {
          $("a.lightbox").click(function() {
              var a = this;          
              //if (!/\/videos\//.test(this.href)) {
              //   return;
              //}              
              //Alumni.getScript("/framework/js/plugins/fancybox/jquery.fancybox-1.3.1.pack.js",function(){   
   		   $.ajax({
   			      type: 'GET',
   			      url: a.href,
   			      cache: true,
   			      success: function(html) {
   			         //alert(html);
				 //var $content = $(html).find("#content>.inner");
				 //$("body").append('<div id=lightboxtmp></tmp>');
				 //$("#lightboxtmp").css("visibility","hidden").append($content);
				 //Alumni.rendervideos();
				 //Alumni.renderVideos(true);
				 //Core.lightbox($("#lightboxtmp").html());
				 Core.lightbox(html);
				 //$("#lightboxtmp").remove();
   			      },
   			      error: function(req,text,error) {
   				 alert("error "+error);
   			      },
   			      dataType: 'html',
   			      data: null
   		      });
              //})
              
              return false;
          });
    },
   
    
   set_input: function(text,id) {  
          input = document.getElementById(id);
          if (!input) return false;
          if ($(input).val() == text) {
             $(input).css("color","#9d9d9d").addClass("default");
          }
          input.onfocus = function() {
             if (this.value == text) {this.value = "";$(this).css("color","black").removeClass("default")}
          }
          input.onblur = function() {
             if (this.value == "") {this.value = text;$(this).css("color","#9d9d9d").addClass("default")}
          }
   },
    
   expandable_uls: function() {
       $("#content .expandable").not("table").each(function() {
           var lis = $(this).children()
           if (lis.length == 0) {return}
           $(lis[0]).addClass("first");
           $(lis[lis.size()-1]).addClass("last");
           lis.each(function() {
              var li = $(this)
              li.addClass("closed");
              var tog = $("a:first",this)
              tog.addClass("toggler");
              tog.click(function() {
                  li.toggleClass("closed");
                  li.toggleClass("open");
                  return false;
              });
           });
       });
   },   
    
    classnotes_search: function(){
       $("#class-notes-search").submit(function(){
          $("input[class=default]",this).val("");
       });
    
    },

// Carousel Code
   autorotate: 1,
   carousel: function() {
        if (!document.getElementById("carousel")) return;
        $("#play").hide();
        
        var low = 0;
        var high = $("#carousel > div").size() - 1;
        //var current = Math.floor(Math.random() * ( 1 + high - low ) + low );
        var current = 0;
        
        function showdiv(num) {
           console.info('showing',num);
           $("#carousel>div").each(function(i) {
              i == num ? $(this).show() : $(this).hide()
           });
           
           $("#carousel ul#nav-carousel li").each(function(i) {
  	    i == num ? $(this).addClass('on') : $(this).removeClass('on')
           });
        }
      
        $("#carousel ul#nav-carousel li").each(function(i) {
          $(this).click(function(){current = i;showdiv(i);Bulletin.autorotate = 0;return false;});
        });
        
        
        $("#prevnext-prev").click(function() {
           Bulletin.autorotate = 0;
           current = current - 1;
           if (current < 0) { current = high }
           showdiv(current);
           return false;
        });
        
        $("#prevnext-next").click(function() {
           Bulletin.autorotate = 0;
           current = (current + 1) % (high + 1);
           showdiv(current);
           return false;
        });

        $("#play").click(function() {
		Bulletin.autorotate = 1;		
		rotate();
           	$(this).hide();
           	$("#pause").show();
		showdiv(current);
        });
        
        $("#pause").click(function() {
           Bulletin.autorotate = 0;
           showdiv(current);
           $(this).hide();
           $("#play").show();
           return false;
        });
      
        function rotate(){ 
           window.setTimeout(function(){
              if (!Bulletin.autorotate) return;
              current = (current + 1) % (high + 1);
              showdiv(current);
              rotate();
           },8000);
        }
      
        rotate();
        showdiv(current);
      
    },

    articletools: function() {
       $("#printthis").click(function(){window.print()});
       $("#sendtofriend").each(function(){
            var title = $("#content h3:first").text();
            $("#sendtofriend").attr('href',"mailto:?subject=Shared from the HBS Alumni Bulletin&body="+title+"%0A"+document.location.href)
       });
    },
    
    usertools: function(){
      $("#usertools-close").click(function(){
         $("#usertools").hide();
         $("#tools-arrow").show()
         return false;
      })
      $("#tools-nav,#login1").click(function(){
         $("#usertools").toggle();
         $("#tools-arrow").toggle()
         return false;
      })
    
    },
    
    pastissues: function() {
       $("body.pastissues #rotate li li").mouseover(function(){$(this).addClass("over")},
                                                    function(){$(this).removeClass("over")});
                                                    
       function showyear(a,yr) {
          $("#chooser-nav li.selected").removeClass('selected');
          $("#chooser-years li.selected").removeClass('selected');
          $(a).parent().addClass("selected");
          $(yr).addClass("selected");
       }
       
       $("#chooser-years div.year>ul>li").mouseover(function(){
          $("#chooser-years div.year>ul>li.on").removeClass("on");
          $(this).addClass("on");
       })
                                                    
       $("#chooser-nav li a").click(function(){
          showyear(this,this.hash);
          return false;
       });
    },
    
    toc: function() {
       $("#subnav ul.toc li:first").addClass('features');
       $("#subnav ul.toc li:first").find("a:first").css("display","none");
       //$("#subnav ul.toc li.features a").hide();
       $("#subnav ul.toc li[class!='features'] ul").hide()
       $("#subnav ul.toc>li").each(function(){
         var $li = $(this);

         $li.css({position:'relative'});
         if ($li.hasClass('features')) {
             $(">a",$li).hide();
             $("li a",$li).each(function(){
                var $t = $(this);
                //$t.append('&hellip; <span class="more"></span>');
                $t.append('');
             });
             //<a class="more" href="#">More</a>
             return;
         }
         if($("ul",$li).size() == 0) { return }
         $("a:first",$li).append(" &gt;&gt;&gt;");
         var $ul = $("ul:first",$li).eq(0);
         offset = $li.offset();
         //$ul.css({top:offset.top-$ul.height()+50,left:offset.left+60});
         $ul.css({top:-30-($ul.height()/2),left:72});
         
         $li.hover(function(){
                       var $li = $(this);
                       $("ul",$li).toggle()
                       $li.addClass("over");
                       },
                   function(){
                       var $li = $(this);
                       $("ul",$li).toggle()
                       $li.removeClass("over");
                   }
         )
       });
    },
    
    max10: function() {
       $("div#max4>div").each(function(x,i){
          if(x==3) {
            $(this).after('<p id="next10"><a class="more" href="#">See next (10) stories</a></p>');
            $("p#next10").click(function(){
               $("div#max4>div:hidden").show();
               $(this).hide();
               return false;
            });
          }
          if (x>3) {
            $(this).hide();
          }
       });
    
    },
    
   tooltips: function() {
        function findPos(obj)  {
            var curleft = curtop = 0;
            if (obj.offsetParent) {
                curleft = obj.offsetLeft
                curtop = obj.offsetTop
                while (obj = obj.offsetParent) {
                    curleft += obj.offsetLeft
                    curtop += obj.offsetTop
                }
            }
            return [curleft,curtop];
        }
        
        var loginHelp = document.getElementById('login-help');
        var loginCallout = document.getElementById('login-help-callout');
        if ( loginHelp ) {
            loginHelp.onmouseover = function() {
                loginCallout.style.left = findPos(this)[0] - 250 + "px";
                loginCallout.style.top =  findPos(this)[1] + 27 + "px";
                loginCallout.style.display = "block";
            }
            loginHelp.onmouseout = function() {
                loginCallout.style.display = "none";
            }
        }

        var toolsHelp = document.getElementById('tools-help');
        var toolsCallout = document.getElementById('tools-help-callout');

        if ( toolsHelp ) {
            
            toolsHelp.onmouseover = function() {
                if ($(document.body).hasClass('home')) {
                   toolsCallout.style.left = findPos(this)[0] + 0 + "px";
                } else {
                   toolsCallout.style.left = findPos(this)[0] - 250 + "px";
                }
                toolsCallout.style.top =  findPos(this)[1] + 27 + "px";
                toolsCallout.style.display = "block";
            }
            toolsHelp.onmouseout = function() {
                toolsCallout.style.display = "none";
            }
        }
   },
   
   loginform: function() {
      $("#loginform").submit(function(){ 
          var username = $("#login-username").val();
          username = jQuery.trim(username);
          
          var password = $("#login-password").val();
          password = jQuery.trim(password);
  
          var redir = document.location.href;
          
          // a redirect parameter is being passed to the /login/ page
          if (/\?http/.test(document.location.search)) {
             redir = document.location.search;
             redir = redir.replace(/^\?/,'');
          }
          if (redir.indexOf('/login/') == -1) {
             $("input[name='url']",this).val(redir);
          }

          if (/@hbs.edu/.test(username)) {
             $("#login-username").val(username);
          } else if (/.hbs.edu/.test(username)) {
             $("#login-username").val(username);
          } else if (/^[a-z]+$/.test(username)) {
             $("#login-username").val(username);
          } else {
             $("#login-username").val(username + ".hbs.edu");
          }

          
          
          
          return true;
      });
   },
    
    /*
     * Add an 'on' class to the top nav
     */
     
    topnav: function() {
        function sethere(id) {
            var a = document.getElementById(id+'-nav');
            if (!a){return};
            a.parentNode.className += ' on';
            a.className += ' on';
        }
        
        var loc = document.location.href;
        if (loc.indexOf('/bulletin/alumni-news/') > -1) { sethere('alumni-news'); } 
        else if (loc.indexOf('/faculty-news/') > -1) { sethere('faculty-news'); } 
        else if (loc.indexOf('/blog/') > -1) { sethere('editors-blogs'); }
        else if ($("body").hasClass("past-issues")) { sethere('past-issues'); } 
        else if (loc.indexOf('/about/') > -1) { sethere('about'); } 
        else if ($("body").hasClass("home")) { sethere('bulletinhome'); } 
        
    }

     
}


window.Bulletin = Bulletin;

$(document).ready(Bulletin.ondomready);

})(jQuery)






