// ==UserScript==
// @name		Ikariam Resource Colorizer
// @namespace  	 	kkhweb.com
// @author		kevinhill
// @version             0.2
// @description		Displays resources in different colors depending on if it is safe or not. Green is safe and Red is Pillageable.
// @homepage            http://kkhweb.com/web_share/greasemonkey/ResourceColorizer/
// @include		http://*.ikariam.*/*
// @exclude             http://www.ika-world.com/*
// @exclude             http://ikariamap.com/*
// @exclude             http://board.ikariam.org/*
// @require     	http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==

//===============================================================
//                         Version Log
// ==============================================================
//  Version 0.1:
// - Completed initial script
//
//  Version 0.2:
// - Condensed functions and variables into an object with methods
// - Added Saving of Secure Amount with GM_setValue so colorizing
//   is independant of the wharehouse page call
//
//  Version 0.3:
// - Complete rewrite of the code, cleaner and faster
// @TODO update secure amount with page call in background
//
//
//===============================================================

(function() {

function init() {
    GM_log('Initializing');
    GM_setValue('server', 0);
    GM_setValue('cityID', 0);
    GM_setValue('secureAmount', 0);
    GM_setValue('initDone', true);
    alert('Please visit your wharehouse to set current Secure Amount.');
}

function run() {
    GM_log('Running');
    GM_addStyle(".insecureRes {color: #FF0000; font-weight: bold;} .insecureRes span {font-size:smaller;}");
    GM_addStyle(".secureRes {color: #4AC948; font-weight: bold;}");

    if(GM_getValue('server') == 0) {
        var server = top.location.host;
        GM_setValue('server', server);
    }

    if(GM_getValue('cityID') == 0) {
        var cityID = $('#citySelect > option:selected').attr('value');
        GM_setValue('cityID', cityID);
    }

    if(GM_getValue('secureAmount') == 0) {
        var secure = getSecure();
        GM_setValue('secureAmount', secure);
    }

colorize();

}

function getSecure() {
    return parseInt($('#mainview').find('.secure:first').text());
}

function getNumber(number) {
    return parseInt(number.replace(/\,/g,''));
}

function colorize() {
    var secureAmount = GM_getValue('secureAmount');

    var woodSpan = $('#value_wood');
    var wineSpan = $('#value_wine');
    var marbleSpan = $('#value_marble');
    var crystalSpan = $('#value_crystal');
    var sulfurSpan = $('#value_sulfur');

    var woodAmount = getNumber(woodSpan.text());
    var wineAmount = getNumber(wineSpan.text());
    var marbleAmount = getNumber(marbleSpan.text());
    var crystalAmount = getNumber(crystalSpan.text());
    var sulfurAmount = getNumber(sulfurSpan.text());

 //Wood Colorize
    if(woodAmount <= secureAmount) {
        woodSpan.addClass('secureRes');
    }
    if(woodAmount > secureAmount) {
        woodSpan.addClass('insecureRes');
    }

//Wine Colorize
    if(wineAmount <= secureAmount) {
        wineSpan.addClass('secureRes');
    }
    if(wineAmount > secureAmount) {
        wineSpan.addClass('insecureRes');
    }

//Marble Colorize
    if(marbleAmount <= secureAmount) {
        marbleSpan.addClass('secureRes');
    }
    if(marbleAmount > secureAmount) {
        marbleSpan.addClass('insecureRes');
    }

//crystal Colorize
    if(crystalAmount <= secureAmount) {
        crystalSpan.addClass('secureRes');
    }
    if(crystalAmount > secureAmount) {
        crystalSpan.addClass('insecureRes');
    }

//Sulfur Colorize
    if(sulfurAmount <= secureAmount) {
            sulfurSpan.addClass('secureRes');
    }
    if(sulfurAmount > secureAmount) {
            sulfurSpan.addClass('insecureRes');
    }
}

if(GM_getValue('initDone') != true) {
    init();
} else {
    run();
}

})();//KEEP