(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.4') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js");
    script_tag.onload = scriptLoadHandler;
    script_tag.onreadystatechange = function () { // Same thing but for IE
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    };
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
	
    main(); 
}

function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/number_format
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // +      input by: Amirouche
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) { 
        var css_link = $("<link>", { 
			rel: "stylesheet", 
			type: "text/css", 
			href: "http://www.perceptiom.com/gemvara/gemvara.css?timestamp=" + Math.floor(Math.random()*10100)
		});
		css_link.appendTo('head');
		
		$("#gemvara-widget").html("<h2>Engagement Ring Price Calculator</h2><p>Use our simple calculator to determine your suggested engagement ring price!</p><ul><li><label for='gemvara-widget-income'>Income:</label><input type='text' name='gemvara-widget-income' id='gemvara-widget-income'></li><li><label for='gemvara-widget-time'>Frequency:</label><select name='gemvara-widget-time' id='gemvara-widget-time'><option value='monthly'>Monthly</option><option value='yearly'>Yearly</option></select></li><li><label for='gemvara-widget-result'>Suggested Price:</label><span id='gemvara-widget-result'></span></li></ul><input type='submit' name='gemvara-widget-submit' id='gemvara-widget-submit' value='Calculate!'><a href='http://www.gemvara.com' target='_blank'>Powered by Gemvara</a>");
		
		$("#gemvara-widget-submit").click(function() {
			var raw_income = $("#gemvara-widget-income").val();
			var income = Number(raw_income.replace(/[^0-9\.]+/g,""));
			var income = Math.round(income).toFixed(2);
		
			if ($("#gemvara-widget-time").val() == "monthly") {
				var cost = Math.round(income * 2).toFixed(2)
			} else {
				var cost = Math.round(income * (1/6)).toFixed(2);
			}
			$("#gemvara-widget-result").html("$" + number_format(cost,2,".",","));
			return false;
		});
		
    });
}

})(); // We call our anonymous function immediately





















