var currentGalleryImage = 1,
nextGalleryImage = 1,
superfishSettings = {
  speed: "fast",
  dropShadows: false
};

if (document.location.toString().match("www.columbia.edu")) {
  document.getElementsByTagName("html")[0].className += " www";
}

function searchboxInit(label) {
  label = (label) ? label : $("#cu-search label:eq(0)").text();
  $("#cu-search-box").val(label);

  $("#cu-search-box").unbind('focus click blur').bind('focus click', function() {
    if ($("#cu-search-box").val() == label) {
      $("#cu-search-box").val("");
    }
  }).bind('blur', function() {
    if ($("#cu-search-box").val() == "") {
      $("#cu-search-box").val(label);
    }
  });
  
  $("#cu-search").unbind('submit').bind('submit', function(e) {
	  value = $("#cu-search-box").val();
		if (value == "" || value == label) {
  	  e.preventDefault();
		}
  });
}

$(document).ready(function(){
  if (typeof Cufon == 'function') {
    Cufon.replace('#content h1.title');
    Cufon.now();
  }

  if (!$("body.section-node-add, body.section-node-edit").length) {
    document.domain = 'columbia.edu';
  }

  /* Better input textfields */
  $("input.form-text, input.style1").bind("focus", function() {
    $(this).addClass("focus");
  }).bind("blur", function() {
    $(this).removeClass("focus");
  });

  /* Anchor smooth scrolling */
  currentPage = location.href.split("/").pop();
  if(typeof $().live == 'function') {
  	$("a[href^='#'], a[href^='"+ currentPage +"#']").live("click", function(event) {
  		event.preventDefault();
  		var url_parts = this.href.split("#");
  
  		if (target = url_parts[1]) {
  		  if ($("#" + target + ", a[name='"+ target +"']").length == 0) {
  		    return false;
  		  }
    		var target_offset = $("#" + target + ", a[name='"+ target +"']").offset().top;
  		}
  		else {
    		var target_offset = 0;
  		}
  		
  		$('html, body').animate({scrollTop:target_offset}, 200, "swing");
  	});
  }

  searchboxInit();

  /* Google Suggest */
  if (document.domain == 'columbia.edu') {
    var cache = {};

    $("#cu-search-box").autocomplete({
      appendTo: $("#cu-search"),
      source: function(request, response) {
        if ( request.term in cache ) {
          response( cache[ request.term ] );
          return;
        }

        var result = [],
        i = 0;

        LAMP.$.ajax({
          url: "http://webservices.lamp.columbia.edu/cu_scripts/suggest-query-0.php",
          dataType: "json",
          data: request,
          success: function( data ) {
            for (j in data) {
              result[i] = data[j];
              i++;
            }
            cache[ request.term ] = result;
            response( result );
          }
        });
      },
  		minLength: 2,
  		open: function(event, ui) {
  		  $("ul.ui-autocomplete:not('.displayed')").addClass("displayed").hide().show("fast");
  		},
  		close: function(event, ui) {
  		  $("ul.ui-autocomplete").removeClass("displayed");
  		},
  		select: function(event, ui) {
  		  $("#cu-search-box").val(ui.item.value);
  		  $("#cu-search").trigger("submit");
  		}
    }).keyup(function(e) {
      if (e.keyCode == 13) {
        $("#cu-search").trigger("submit");
      }
    });
  }

  $("#content-area .faq-content li").each(function() {
    if ($(this).html().match(/^<a/)) {
      $(this).addClass('raquo').find("a:eq(0)").prepend("&raquo; ");
    }
    else {
      $(this).prepend("&raquo; ").addClass('raquo');
    }
  });

  $("#block-menu_block-1 ul.menu").superfish();
  $("#block-menu_block-1 li.expanded > span a").filter(function() {
    homeUrls = ['/', 'index.html', 'http://www.columbia.edu/index.html'];
    if (!jQuery.inArray($(this).attr("href"), homeUrls)) {
      return true;
    }
  }).bind("click", function(e) {
    e.preventDefault();
    $(this).parents("li.expanded:eq(0)").hideSuperfishUl();
  });
  $("#navigation ul.menu").superfish();

  if ($("#extra-navigation .block-menu_block").length) {
    $("#extra-navigation .block-menu_block div li.expanded").hoverIntent(function() {
      $(this).addClass("sfHover").find("ul:eq(0)").fadeIn("fast");
    }, function() {
      $(this).not(".active-trail .active-trail").removeClass("sfHover").find("ul:eq(0)").hide();
    });
  }  
});
