$(document).ready(function(){

  /* GALLERY */

  var pages = $("#gallery").attr("pages");

  var width = $("#gallery").width();

  if (width > 300){
    var speed = 1000;
  } else {
    var speed = "slow";
  }

  $("a#galfwd").click(function(){
  	if (!$(this).is(".active")){
  		return false;
  	}
	
  	$("#galcontrols a").removeClass("active");
	
  	var hideme = $(".galpage:visible");
  	var hideme_id = hideme.attr("id");
  	var l = hideme_id.length;
  	var hideme_index = hideme_id.substring((l - 1), l);					
  	var showme_index = parseInt(hideme_index) + 1;
  	var showme_id = "#galpage"+showme_index;
  	var showme = $(showme_id);

  	showme.show();

  	hideme.animate({
  		left: (0 - width)
  	}, speed,"",function(){
  		hideme.hide();
  		$("#galback").addClass("active");
  		if (hideme_index < (pages - 1)){
  			$("#galfwd").addClass("active");
  		}
  	});

  	showme.animate({
  		left: 0
  	}, speed);
	
  	return false;
  });

  $("a#galback").click(function(){
  	if (!$(this).is(".active")){
  		return false;
  	}

  	$("#galcontrols a").removeClass("active");

  	var hideme = $(".galpage:visible");
  	var hideme_id = hideme.attr("id");
  	var l = hideme_id.length;
  	var hideme_index = parseInt(hideme_id.substring((l - 1), l));
  	var showme_index = hideme_index - 1;
  	var showme_id = "#galpage"+showme_index;
  	var showme = $(showme_id);

  	showme.show();

  	hideme.animate({
  		left: width
  	}, speed,"",function(){
  		hideme.hide();
  		$("a#galfwd").addClass("active");
  		if (hideme_index > 2){
  			$("a#galback").addClass("active");								
  		}
  	});

  	showme.animate({
  		left: 0
  	}, speed);

  	return false;
  });

  $("a.galbtn").click(function(){
    if ($("#gallery").is(".being_changed")){
    	return false;
    }				

    $("#gallery").addClass("being_changed");

    var hideme = $(".slide:visible");
    var hideme_id = hideme.attr("id");  
    var this_id = $(this).attr("id");
    var l = this_id.length;
    var showme_index = parseInt(this_id.substring(6, l))
    var showme_id = "slide"+showme_index;

    if (showme_id != hideme_id){
      var showme = $("#"+showme_id);
      showme.fadeIn();
      hideme.fadeOut("slow", function(){
      	$("#gallery").removeClass("being_changed");		
        });
    } else {
    	$("#gallery").removeClass("being_changed");		      
    }
  
    return false;
  });


  /* EXPAND */

  $("a.reveal").click(function(){
  	$(this).parent(".body").children('.hidden').slideDown("slow");
  	$(this).hide();
  	$(this).parent(".body").children(".hide").show();
    return false;
  });

  $("a.hide").click(function(){
  	$(this).parent(".body").children('.hidden').slideUp("slow");
  	$(this).hide();
  	$(this).parent(".body").children(".reveal").show();
    return false;
  });
  
  /* NODETOOLS */

  $("#tool_share").mouseover(function(){
    $("#share_pulldown").show();
  });
  
  $("#tool_share").mouseout(function(){
    $("#share_pulldown").hide();
  });

  /* FONT SIZE */

  $("a.fontbtn").click(function(){
    var sizes = [];
    var heights = [];
    sizes[0]   = 11;
    heights[0] = 27;
    sizes[1]   = 13;
    heights[1] = 22;
    sizes[2]   = 15;
    heights[2] = 22;
    sizes[3]   = 17;
    heights[3] = 25;
    sizes[4]   = 19;
    heights[4] = 27;
    
    var changeme = $(".body");
    var currentsize = changeme.css("font-size");
    var size = parseInt(currentsize, 10);
    var tier = sizes.indexOf(size);

    var newTier;
    if (this.id == 'biggerfont'){
      newTier = tier + 1;
    } else if (this.id == 'smallerfont'){
      newTier = tier - 1;
    }
    if (newTier < 0) { newTier = 0; }
    if (newTier >  4) { newTier =  4; }

    changeme.css('font-size', sizes[newTier] + "px");
    changeme.css('line-height', heights[newTier] + "px");    
    return false;
  });


});
