$(document).ready(function(){
  // Instantiate Cufon
  Cufon.replace('#community-login a', { fontFamily: 'Knockout54', fontSize: '9px' });
  Cufon.replace('#community-login span', { fontFamily: 'Knockout34', fontSize: '10px' });
  Cufon.replace('.button-submit,.download,#find-a-school-module #submit-btn', { fontFamily: 'Knockout54', fontSize: '11px' });
  Cufon.replace('.download-btn', { fontFamily: 'Knockout54', fontSize: '10px' });
  Cufon.replace('h3', { fontFamily: 'Knockout27', fontSize: '28px' });
  Cufon.replace('h2:not(#article h2)', { fontFamily: 'Knockout27', fontSize: '36px' });
  Cufon.replace('#header h2', { fontFamily: 'Knockout34', fontSize: '13px' });
  Cufon.replace('#subscribe h4', { fontFamily: 'Knockout27', fontSize: '28px' });
  Cufon.replace('h5, .head h2', { fontFamily: 'Knockout29', fontSize: '38px' });
  Cufon.replace('.head h2 span', { fontFamily: 'Knockout27', fontSize: '30px' });
  setTimeout(function(){
    Cufon.replace('.large-number', { fontFamily: 'Knockout46', fontSize: '100px' });
    Cufon.replace('.small-number', { fontFamily: 'Knockout46', fontSize: '78px' });
    
    Cufon.replace('#vision-and-mission-module dt, #vision-and-mission-module dd', { fontFamily: 'Knockout31'});
    Cufon.replace('#blog-module h4', { fontFamily: 'Knockout31'});
    
    Cufon.replace('.large-text', { fontFamily: 'Knockout31', fontSize: '13px' });
    Cufon.replace('.small-text', { fontFamily: 'Knockout31', fontSize: '9.5px' });
  },100);
  
  // Open external links in a new window
  $("a[href^='http']").attr('target','_blank');
  
  // Share this page rollover
  var over = false;
  function hide(){setTimeout(function(){if(over === false){$(".share-options").hide();}},40);}
  $(".share-this").mouseenter(function(){$(this).next(".share-options").show();}).mouseleave(hide);
  $(".share-options").mouseenter(function(){over = true;}).mouseleave(function(){hide();over = false;  });
  // Share this page icon functions
  $(".fb-share").click(function(){
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),' sharer', 'toolbar=0, status=0, width=625, height=400');
    return false;
  });
  $(".tw-share").click(function(){
    u=location.href;
    t=document.title;
    window.open('http://twitter.com/home?status=Currently reading '+encodeURIComponent(u),' sharer', 'toolbar=0, status=0, scrollbars=1, width=810, height=450');
    return false;
  });
  $(".pr-share").click(function(){
    window.print();
    return false;
  });
  $(".em-share").click(function(){
    var item = "page";
    if($("body").attr("id") == "news") { item="article"; }
    $(this).attr("href","mailto:?&subject=Check out this " + item + " from NC New Schools Project&body=" + location.href);
  });
  
  // Add First and Last classes
  $("ul").each(function(){$(this).find("li:last").addClass("last");});
  $("#subNavigation li:first, .secondary-subnav li:first, .news-list li:first, .search-list li:first").addClass("first");
  $(".boxed-list").each(function(){
    $(".boxed-list").find("li:even").addClass("left");
    $(".boxed-list").find("li:odd").addClass("right");
    $(".boxed-list").find("li:even:last").addClass("last");
    if($(".boxed-list").find("li:last").hasClass("right")){
      $(".boxed-list").find("li:odd:last").addClass("last");
    }
  });
  $(".boxed-list .left").each(function(){
    var lH = $(this).height();
    var rH = $(this).next(".right").height();
    if(lH < rH) {$(this).height(rH);}
    if(lH > rH) {$(this).next(".right").height(lH);}
  });
  
  // Instantiate watermark
  $('#search-query').watermark('SEARCH', {className: 'watermark'}, {useNative: true});
  $('#subscribe-email').watermark('YOUR EMAIL', {className: 'watermark'}, {useNative: true});
  $('#payPalForm #amount').watermark('ex: $50.00', {className: 'watermark'}, {useNative: true});
  
  // Community Login rollover state changes
  $("#community-login a").mouseenter(function(){
    $(this).css("color","#FFC425");Cufon.replace(this, { fontFamily: 'Knockout54', fontSize: '9px' });
  }).mouseleave(function(){
    $(this).css("color","#53A4C4");Cufon.replace(this, { fontFamily: 'Knockout54', fontSize: '9px' });
  });
  $(".download-btn").mouseenter(function(){
    $(this).css("backgroundColor","#777777");Cufon.replace(this, { fontFamily: 'Knockout54', fontSize: '10px' });
  }).mouseleave(function(){
    $(this).css("backgroundColor","#BABABA");Cufon.replace(this, { fontFamily: 'Knockout54', fontSize: '10px' });
  });
  $(".download").mouseenter(function(){
    $(this).css("backgroundColor","#777777");Cufon.replace(this, { fontFamily: 'Knockout54', fontSize: '11px' });
  }).mouseleave(function(){
    $(this).css("backgroundColor","#BABABA");Cufon.replace(this, { fontFamily: 'Knockout54', fontSize: '11px' });
  });
  
  // Navigation functionality
  $('#navigation > li > a').append('<span class="hover"></span>');
  // $("#navigation > li > a").click(function(){if($(this).attr("id") == "active"){return false;}});
  var navName;var speed = 300;
  function showSubnav () {
    navName = $(this).attr("id").replace("nav", "subNav");
    $("#"+navName).stop(true,true).slideToggle(speed, 'easeInOutQuad');
    if($("a", this).attr("id") != "active"){$(".hover", this).stop(true,true).animate({"opacity":1},speed);}
  }
  function hideSubnav () {
    navName = $(this).attr("id").replace("nav", "subNav");
    $("#"+navName).stop(true,true).slideToggle(speed, 'easeInOutQuad');
    if($("a", this).attr("id") != "active"){$(".hover", this).stop(true,true).animate({"opacity":0},speed);}
  }
  $("#navigation > li").hoverIntent({
    sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
    interval: 100,   // number = milliseconds of polling interval
    over: showSubnav,  // function = onMouseOver callback (required)
    timeout: 10,   // number = milliseconds delay before onMouseOut function call
    out: hideSubnav    // function = onMouseOut callback (required)
  });
  
  if($("#secondary-column").has("#partners-module")) {
    var partners = $("#partners-module");
    var slider = partners.find(".slider");
    var first = slider.find(".image:first");
    first.clone().appendTo(slider);
    var plength = slider.find(".image").length;
    var slidewidth = plength*234;
    slider.width(slidewidth);
    var slideleft = 0;
    var slidedelay = 5000;
    var slidespeed = 1000;
    function moveslider() {
      setTimeout(function(){
        slideleft = slideleft - 234;
        if(slideleft == -slidewidth){
          slideleft = 0;
          slider.css('left',slideleft);
        }
        slider.animate({'left':slideleft},slidespeed);
        moveslider();
      },slidedelay);
    }
    moveslider();
  }
  
  $("#search-form .button-submit").click(function(){
    $("#search-form").submit();
  });
    
  $("#find-a-school-module #submit-btn").click(function(){
    var countyname = $('#county :selected').text().toLowerCase().replace(/ /g,"-").replace(",","");
    window.location.replace("http://"+window.location.hostname+"/our-schools/find-a-school/"+countyname+"/");
    return false;
  });
  $("#lb-container:not(.close)").click(function(event){
    event.stopPropagation();
  });
  $("#lightbox").click(function(){
    $("#lightbox").hide();
  });
  $("#newsletter-form .button-submit").click(function(){
    $("#newsletter-form").submit();
  });

  $("#school_district").change(districtChange);
  function districtChange(){
    var districtName = $('#school_district :selected').attr("class");
    window.location.replace("/our-results/school-comparisons/"+districtName);
  }

});

$(window).load(function () {
  $(".crop").each(function(){
    var originalW = $(this).find("img").width();
    var originalH = $(this).find("img").height();
    var percent = 150/originalH;
    var newW = originalW * percent;
    var newMargin = (newW - 205)/-2;
    if($.browser.msie && $.browser.version < 8){
      $(this).find("img").css({"marginLeft": newMargin, "width": newW, "height": 150});
    } else {
      $(this).find("img").css({"marginLeft": newMargin, "width": newW});
    }
  });
  $(".fit").each(function(){
    var img = $(this).find('img');
    var originalW = img.width();
    var originalH = img.height();
    
    var newW = 205;
    var newH = originalH * (newW / originalW);
    
    var newMargin = (newH - 150)/-2;
    img.css({"marginTop": newMargin, "width": newW, "height": newH});
  });
});
