// ==UserScript==
// @name           My City messenger 0.8
// @namespace      http://urosevic.net/greasemonkey
// @description    Compose predefined or custom bulletin message on visit to minicity.in city with usage of data from city stats (%city%, %state%, %rank%, %bank%, %pop%, %sig%, %br%, %e%, %inc%, %incf% and %incd%)
// @include        http://minicity.in/index.php?city=*
// @include        http://minicity.in/receive-money.php
// ==/UserScript==

/*
Author: Aleksandar Urošević, urke@users.sourceforge.net
License: GNU GPLv3
Version: 0.8
ChangeLog:
  * 0.8 (29.05.2008): enable recording daily income ($inc%, %incf% and %incd%) and new message type - Income report
  * 0.7 (27.05.2008): enabled menu on self city and setting name of self city
	* 0.6 (26.05.2008): added variables %e% (EUR) and %br% (line break) + do replaces globally
	* 0.5 (25.05.2008): added variables for custom message (%pop%, %bank%, %city%, %state%, %rank% and %sig%)
	* 0.4 (24.05.2008): added resource and city info (city name, state, bank and population) + first public release
	* 0.3 (20.05.2008): added message for HYDRA i Yarold
	* 0.2 (19.05.2008): added popup menu with basic predefined text options and custom text
	* 0.1 (18.05.2008): initial release, static text without interactive change
*/

var mycity = GM_getValue("mcm_mycity", "alexandria");
var cityurl = document.location.toString();

function mycitymsg()
{
if ( cityurl.match("city=") && !cityurl.match(mycity) ) {
	var mcm_mode = GM_getValue("mcm_mode", "standard");
	
	switch (mcm_mode)
	{
	case "friends":
		mcm_text = "Mission: Friendly cities visit wave.\nAccomplished by: %sig% :D";
		break;
	case "income":
		mcm_text = "My dear friend %city% from %state%,%br%%br%You earned %incf% %e% (of my %inc% %e%) for %incd% from me! :D%br%%br%-- %sig%";
		break;
	case "top100":
		mcm_text = "Mission: TOP100 cities visit wave.\nYour current state: %bank%€ in bank and %pop% residers.\nDone by: %sig% :)";
		break;
	case "new":
		mcm_text = "Mission: New cities visit wave.\nDone by: %sig% :)";
		break;
	case "squad":
		mcm_text = "Mission: CMC Forum Click Squad visit wave.\nAccomplished by: %sig% :)";
		break;
	case "hydra":
		mcm_text = "Mission: Hydra System visit wave.\nAccomplished by: %sig% :)";
		break;
	case "yarold":
		mcm_text = "Mission: Yarold's SWLE visit wave.\nAccomplished by: %sig% :)";
		break;
	case "custom":
		mcm_text = GM_getValue("mcm_custom", "I bring new resider to your city %city%. Visit me back :) --%sig%");
		break;
	default: // standard
		mcm_text = "Your city %city% from %state% (currently on place #%rank%) has visited by %sig%. Your city now have %bank%€ in bank and %pop% residers.\n\nBe my guest and visit me back :D";
	}

	document.getElementById('data5').innerHTML = mcm_vars(mcm_text);
	document.location = document.location + "#commentEditor";
} // nije moj grad
}

function mcm_setcustom()
{
	var tmp = GM_getValue("mcm_custom", "I bring new resider to your city. Visit me back :)");
	tmp = prompt("Please enter your custom text, or change old.", tmp);
	GM_setValue("mcm_custom", tmp);
	GM_setValue("mcm_mode", "custom");
	mycitymsg();
}

function mcm_setsignature()
{
	var tmp = GM_getValue("mcm_sig", "Aleksandar, mayor of city Alexandria");
	tmp = prompt("Please enter your new signature text, or change old.", tmp);
	GM_setValue("mcm_sig", tmp);
	mycitymsg();	
}

function mcm_setmycity()
{
	var tmp = GM_getValue("mcm_mycity", "alexandria");
	tmp = prompt("Please enter name of your own city.", tmp);
	GM_setValue("mcm_mycity", tmp);
}

function mcm_vars(msg)
{
	// %pop%, %bank%, %city%, %state% and %rank% and %sig%
	var pinfo = document.getElementById('placeInfo');
	var city = pinfo.getElementsByTagName('h2')[0].innerHTML.split(": ")[1].replace(/\t/g, "");
	var state = pinfo.getElementsByTagName('h3')[0].innerHTML.split(": ")[1].replace(/\t/g, "");
	var rank = document.getElementById('rank').getElementsByTagName('strong')[0].innerHTML;
	var cinfo = document.getElementById('cityInfo').getElementsByTagName('li');
 	var bank = cinfo[0].getElementsByTagName('span')[0].innerHTML.replace(/(\t|\n|€)/g, "");
	var pop = cinfo[5].getElementsByTagName('span')[0].innerHTML.replace(/(\t|\n)/g, "");
	var sig = GM_getValue("mcm_sig", "Aleksandar, mayor of city Alexandria");
	var income = GM_getValue("mcm_income", "Not jet income!");
	var income_friends = GM_getValue("mcm_income_friends", "Not jet income for friends!");
	var income_date = GM_getValue("mcm_income_date", "No income date jet!");

	// replace all variables
	msg = msg.replace(/%pop%/g, pop);
	msg = msg.replace(/%bank%/g, bank);
	msg = msg.replace(/%city%/g, city);
	msg = msg.replace(/%state%/g, state);
	msg = msg.replace(/%rank%/g, rank);
	msg = msg.replace(/%sig%/g, sig);
	msg = msg.replace(/%e%/g, "€");
	msg = msg.replace(/%br%/g, "\n");
	// keshiranje
	msg = msg.replace(/%inc%/g, income);
	msg = msg.replace(/%incf%/g, income_friends);
	msg = msg.replace(/%incd%/g, income_date);
	return msg;
}

GM_registerMenuCommand( "Standard", function() { GM_setValue("mcm_mode", "standard"); mycitymsg(); } );
GM_registerMenuCommand( "Friends", function() { GM_setValue("mcm_mode", "friends"); mycitymsg(); } );
GM_registerMenuCommand( "Income report", function() { GM_setValue("mcm_mode", "income"); mycitymsg(); } );
GM_registerMenuCommand( "TOP100", function() { GM_setValue("mcm_mode", "top100"); mycitymsg(); } );
GM_registerMenuCommand( "New cities", function() { GM_setValue("mcm_mode", "new"); mycitymsg(); } );
GM_registerMenuCommand( "CMC Squad", function() { GM_setValue("mcm_mode", "squad"); mycitymsg(); } );
GM_registerMenuCommand( "Hydra", function() { GM_setValue("mcm_mode", "hydra"); mycitymsg(); } );
GM_registerMenuCommand( "Yarold's SWLE", function() { GM_setValue("mcm_mode", "yarold"); mycitymsg(); } );
GM_registerMenuCommand( "Custom", function() { GM_setValue("mcm_mode", "custom"); mycitymsg(); } );
GM_registerMenuCommand( "Set signature", function() { mcm_setsignature(); } );
GM_registerMenuCommand( "Set custom message", function() { mcm_setcustom(); } );
GM_registerMenuCommand( "Setup self city name", function() { mcm_setmycity(); } );

if ( document.location.toString().match("receive-money") ) {
	// posecena stranica za isplatu
	var isplata = document.getElementsByTagName("p")[0].innerHTML.toString();
	if ( isplata.match("Your friends got") ) {
		// ako je ostvaren prihod, izvadi vrednosti
		sebi = isplata.split(" ")[4];
		prijateljima = isplata.split("got ")[1].split(" ")[0];
		var datum = new Date();
		datum = (datum.getDate()) + "/" + (datum.getMonth() + 1);
		// upisi vrednosti u GM registar
		GM_setValue("mcm_income", sebi);
		GM_setValue("mcm_income_friends", prijateljima);
		GM_setValue("mcm_income_date", datum);
	}
} else {
	mycitymsg();
}

