//----------------------
// toggle ribbon from copyright symbol

//----------------------

function toggle(element) {
	if (document.getElementById(element).style.display == "block"){
	document.getElementById(element).style.display = "none"	} else {
	document.getElementById(element).style.display = "block"
	}
}


//----------------------
// slide navigation
//----------------------

//pause before animation
$.fn.pause = function(duration) {
	$(this).stop().animate({ dummy: 1 }, duration);
	return this;
};

function mouseleft() {
	$("#nav-bar").triggerHandler("mouseleave");
}

//slide open navigation on over (add focus someday)
$(document).ready(function(){
	var selectedInput = null;
	
	$("#query").focus(function() {
		selectedInput = this;
	});
	$("#query").blur(function() {
		selectedInput = null;
	});
	$("#nav-bar").mouseenter(
		function () {
			$("#query").unbind("blur", mouseleft);
			$(this).stop().pause(60).animate({ height:"207px" }, 400, "easeOutQuart" );
		}).mouseleave(
		function () {
			$("#query").bind("blur", mouseleft);
			if (selectedInput == null) {
				$(this).stop().pause(60).animate({ height:"41px" }, 400, "easeOutQuart" );
			}
		}
	);
});

//slide open navigation on over (add focus someday)
/*
$(document).ready(function(){
	$("#nav-bar").hover(
		function () {
			$(this).stop().pause(60).animate({ height:"207px" }, 400, "easeOutQuart" );
		},
		function () {
			$(this).stop().pause(60).animate({ height:"41px" }, 400, "easeOutQuart" );
		}
	);
});
*/


//----------------------
// navigte sub features
//----------------------

$.fn.reverse = [].reverse;

//prep more/previous buttons
$(document).ready(function() {
	disableCheck();
	var randomSeed = new Date().getTime(); //defeat IE caching of $.load function
	
	var loadPane = function(pane, dir) {
		var nextURL = base_uri + "/" + pane + "/";
		var items = $(".item");
		var fadeout_rev = (dir == 'right' ? false : true);
		fadeOutOld(items, fadeout_rev, nextURL + "?cachebust=" + randomSeed);
		switch(dir) {
		  case 'left':
		    pageTracker._trackEvent('BUHomepage', 'scroll-left', nextURL);
		    break;
		  case 'right':
		    pageTracker._trackEvent('BUHomepage', 'scroll-right', nextURL);
		    break;
		  case 'land':
		    pageTracker._trackEvent('BUHomepage', 'land-pane', nextURL);
		    break;
	  }
		window.location.hash = '#' + pane;
  };
	
	$("#more:not(.disabled)").live("click", function(event) {
		event.preventDefault();
		var urlPieces = (event.target.href).split("/");
		loadPane(urlPieces[urlPieces.length - 2], 'right');
	});
	
	$("#previous:not(.disabled)").live("click", function(event) {
		event.preventDefault();
		var urlPieces = (event.target.href).split("/");
		loadPane(urlPieces[urlPieces.length - 2], 'left');
	});
	
	//legacy code - ensures that if the disabled arrows are visible they still cannot be clicked.
	$("#more.disabled, #previous.disabled").live("click", function(event) {
		event.preventDefault();
	});
	
	// track feature clicks as events
	$('.hp_feature_link').live("click", function() {
	  pageTracker._trackEvent('BUHomepage', 'feature-click', $(this).attr('href'));
  });
  
  // if there's a hash in the url when they land (presumably from the back button), take them to that pane
  if (window.location.hash != '') {
    var pane = window.location.hash.substr(1, 1);
    if ((pane != '') && (! isNaN(parseInt(pane)))) {
      loadPane(pane, 'land');
    }
  }
});

//test if we need to disable an arrow
function disableCheck() {
	var moreHref = $("#more").attr("href");
	var prevHref = $("#previous").attr("href");
	
	//cannot test simply for "#" as the href because IE resolves that to the full current path with a pound at the end when loaded in dynamically.
	if (moreHref && moreHref.indexOf("#") === moreHref.length - 1) {
		$("#more").addClass("disabled");
	}
	else if (prevHref && prevHref.indexOf("#") === prevHref.length - 1) {
		$("#previous").addClass("disabled");
	}
}

//fade in new items
function fadeInNew(items, rev) {
	items.animate({opacity:"0"}, 0);
	disableCheck();
	
	if (rev) {
		items.reverse();
	}
	
	items.each(function(i) {
		var passthru = this;
		window.setTimeout(function() {
			$(passthru).animate({opacity:"1"}, 60);
		}, 60*i);
	});
}

//fade out old items, load in new items from next page and call fadeInNew
function fadeOutOld(items, rev, nextURL) {
	var itemCount = items.length;
	
	if (rev) {
		items.reverse();
	}
	
	items.each(function(i) {
		var passthru = this;
		var alreadyLoaded = false;
		window.setTimeout(function() {
			$(passthru).animate({opacity:"0"}, 60, function() {
				if (i == itemCount - 1) {
					window.setTimeout(function() {
						if (!alreadyLoaded) {
							$("#browse").addClass("loading");
						}
					}, 50);
					
					$("#browse").html("<!-- clear -->");
					$.get(nextURL, function(data) {
						$("#browse").append($(".item", data)).append($("#controls", data));
						alreadyLoaded = true;
						$("#browse").removeClass("loading");
						fadeInNew($(".item"), rev);
					}, 'html');
					
					/*
					$("#browse").load(nextURL + " .item, #controls", function() {
						alreadyLoaded = true;
						$("#browse").removeClass("loading");
						fadeInNew($(".item"), rev);
					});
					*/
				}
			});
		}, 60*i);
	});
}
