/*
	Any site-specific scripts you might have.
	Note that <html> innately gets a class of "no-js".
	This is to allow you to react to non-JS users.
	Recommend removing that and adding "js" as one of the first things your script does.
	Note that if you are using Modernizr, it already does this for you. :-)
*/

// remap jQuery to $

	$(document).ready(function(){
	
		/* ------ FLICKR ------ */
		
		/* http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/ */
				  
		$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=72157626277381391&nsid=60501698@N06&lang=en-us&format=json&jsoncallback=?", displayImages);
		
		function displayImages(data) {
		
		    // Start putting together the HTML string
		    var htmlString = "";
		    
		    // Custom Title
		        htmlString += '<h2>Pet Sitting</h2>';
		    
		    // Now start cycling through our array of Flickr photo details
		    $.each(data.items, function(i,item){
		    
		        var sourceSquare = (item.media.m);
		        
		        // Here's where we piece together the HTML
		        htmlString += '<li><a href="' + item.link + '" target="_blank">';
		        htmlString += '<img title="' + item.title + '" src="' + sourceSquare;
		        htmlString += '" alt="'; htmlString += item.title + '" />';
		        htmlString += '</a>';
		        htmlString += '<div class="picTitle">' + item.title + '</div></li>';
		        
		        if (i == 7) return false; /* limit the number of images (begins at 0) */
		    
		    });
		    
		    // Pop our HTML in the #images DIV
		    $('#images').html(htmlString);
		    
		    $('#images li:last').addClass('last');
		    
		    // Close down the JSON function call
		}
		
		/* InnerFade */
		
		$('.tweet').innerfade({
			speed: 1000,
			timeout: 6000,
			containerheight: '90px'
		});
		
		
		
		/* Income Calculator - written by Richard Goulstone 2011 */
		
		// Initialise

	    var initvals = [[10,40],[8,5],[10,2],[8,1],[0.4,20]]
    	
    	$(document).ready(function () {
      		income_calc = new IncomeCalculator(initvals);
    	});
		
	});
	
/* Income Calculator - Written by Richard Goulstone - Copyright Echidna 2011 */

function IncomeCalculator(initvals) { 
  this.populate(initvals);
  this.recalculate();
} 

/* Populate table with text fields and initial values */ 
IncomeCalculator.prototype.populate = function(initvals){
  var table = $("#calctable") 
  
  var i = 0; 
  //set the initial values and event handlers for the rate fields
  table.find('td.rate').each(
    function(){
    var input = document.createElement('input'); 
    input.type = 'text';
    input.value = initvals[i][0].toFixed(2);
    input.className = 'float';
    input.onchange = function() { IncomeCalculator.prototype.recalculate() }; 
    $(this).append(input);
    i++;
    }
  );
  
  i = 0;  
  //set the initial values and event handlers for the quantity fields
  table.find('td.quantity').each(
    function(index,domObject){
    var input = document.createElement('input'); 
    input.type = 'text';
    input.value = initvals[i][1];
    input.className = 'integer';
    input.onchange = function() { IncomeCalculator.prototype.recalculate() };    
    $(this).append(input);
    i++
    }
  );

}; 


/* Reset all input values and recalculate totals */

IncomeCalculator.prototype.reset = function(){
  $("#calctable").find('td.rate').children('input').each( function(){$(this).attr("value","0.00"); });
  $("#calctable").find('td.quantity').children('input').each( function(){ $(this).attr("value","0"); });
  IncomeCalculator.prototype.recalculate();
};


/* Read new values from table and recalculate totals */
IncomeCalculator.prototype.recalculate = function(){
  
  var total = 0;
  var total_add = 0;
  var total_sub = 0;
  //ref to recalcSection function to use inside each() clauses
  var func_recalcsection = IncomeCalculator.prototype.recalcSection;
  //add up the incoming   
  $("#calctable").children('tbody.add').each( function() {
    total_add += func_recalcsection($(this)); 
  });
  //add up the outgoing 
  $("#calctable").children('tbody.sub').each( function() {
    total_sub += func_recalcsection($(this)); 
  });
  //calculate grand total 
   total = total_add - total_sub;
  //update grand total text
   $("td.total").text("£ "+Math.round(total));
}; 

/* Recalculate totals for a tbody section */
IncomeCalculator.prototype.recalcSection = function(tbody){
  var total = 0;
  var multiplier = tbody.attr('data-multiplier') ? parseInt(tbody.attr('data-multiplier')) : 1;

  //loop through rows
  tbody.children('tr').has('td.rate+td.quantity,td.subtotal').each(
    function(){
      var rate = 0;
      var quantity = 0;
      //if this is an editable row  read values and increment total
      if ($(this).children().hasClass('rate') && $(this).children().hasClass('quantity'))
      {
        rate = parseFloat($(this).children('td.rate').children('input').attr('value'));
        quantity = parseInt($(this).children('td.quantity').children('input').attr('value'));
        if (!isNaN(rate*quantity)) total += rate*quantity;
      }
      //if this row has a subtotal update it
      if ($(this).children().hasClass('subtotal'))
      {
        $(this).children('td.subtotal').each( function(){ $(this).text("£ "+Math.round(total*multiplier)); });
      }
    }
  );
  return total*multiplier;
}; 




