Ssieth / Fallen London Improver

// ==UserScript==
// @name        Fallen London Improver
// @namespace   fallen_london_improver.ssieth.co.uk
// @description Add Functionality for Fallen London
// @require		http://sprefs.ssieth.co.uk/mutation-summary/src/mutation-summary.js
// @include     http://fallenlondon.storynexus.com/*
// @version     1.0.1
// @grant       none
// ==/UserScript==

// Create an observer
var observerProgress = new MutationSummary({
  callback: handleProgressUpdate,
  queries: [{ element: 'img[alt="Rank"]', elementAttributes: 'width' }]
});	

/* 
This fires when the style element of the li.shop-item elements changes (including when those elements are added to the page 
It is used to maintain totals/subtotals of owned items in the bazaar.
*/
var observerShopItems = new MutationSummary({
  callback: handleItemUpdate,
  queries: [{ element: 'li.shop-item', elementAttributes: 'style' }]
});	

/* 
Show the percentage progress on stats and 
*/
function addPercProgress() {
	$("p.progress span.rank img").each(function() {
		var $this = $(this);
		var intWidth;
		var intParentWidth;
		var fltPerc;
		var strID; 
		strID = $this.attr("id");
		intWidth = $this.width();
		intParentWidth = $this.parent().width();
		fltPerc = (intWidth/intParentWidth) * 100;
		if ($('#perc-' + strID).length) {
			$('#perc-' + strID).text(fltPerc.toFixed(2) + "%");
		} else {
			$(this).parent().parent().append("<span id='perc-" + strID + "' style='color: green;' >" + fltPerc.toFixed(2) + "% </span>");
		}
	});
}

function handleProgressUpdate(summaries) {
  var elementSummary = summaries[0];
  var $e;
  addPercProgress();
}

function totalItems(){
	console.log("Fire totalItems");
	var $main = $('div#mainContentViaAjax');
	var strName = "";
	var strQty = "";
	var strVal = "";
	var strTotal = "";
	var $this;
	var fltTotal = 0.0;
	var fltGrandTotal = 0.0;
	var fltPageTotal = 0.0;
	$main.find("section.shop-items li.shop-item").each(function() {
		$this = $(this);
		strName = $this.find("h6").text().trim();
		if ($this.find("div.item-quantity").length) {
			strQty = $this.find("div.item-quantity").text().trim();
		} else {
			strQty = "0";
		}
		strVal = $this.find("span.price").text().trim();
		if ($this.text().contains("x Cryptic Clue")) {
			strVal = "0.5";
		}
		fltTotal = (parseFloat(strVal) * parseInt(strQty));
		strTotal = fltTotal.toFixed(2);
		fltGrandTotal += fltTotal;
		if ($this.css('display') != 'none') {
			fltPageTotal += fltTotal;
		}
		if ($this.find('span.total').length <= 0) {
			$this.append("<span style='font-weight:normal;' class='total price currency-echo'><span>");
		}
		$this.find('span.total').html(strTotal + " total");
	});
	/* Do page / grand totals */
	if ($main.find("div#grandTotal").length <= 0) {
		$main.find("section.shop-items").append("<div id='grandTotal'></div>");
	}
	$main.find("div#grandTotal").html("Page Total: <span class='pageTotal price currency-echo'>" + fltPageTotal.toFixed(2) + "</span>, Grand Total: <span class='grandTotal price currency-echo'>" + fltGrandTotal.toFixed(2) + "</span>");
}

function handleItemUpdate(summaries) {
	console.log("Fired item");
  var elementSummary = summaries[0];
  var $e;
  totalItems();
}