// ==UserScript==
// @name	Ipernity Refer Comment
// @namespace	http://www.consulentionline.org/
// @description Auto comment the place where you come from
// @version        0.92
// @identifier	http://www.consulentionline.org/js/ipernityrefercomment.user.js
// @date           2008-07-26
// @creator        Roberto Ballerini, based on Flickr Refer Comment by Pierre Andrews (mortimer.pa@free.fr)
// @include http://*ipernity.com/doc/*/*
// @include http://*ipernity.com/blog/*/*
// @include http://*ipernity.com/group/*/*
// @exclude http://*ipernity.com/doc/*/organize
// ==/UserScript==

// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// --------------------------------------------------------------------
// Copyright (C) 2007 Roberto Ballerini, (C) 2006 Pierre Andrews
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// The GNU General Public License is available by visiting
//   http://www.gnu.org/copyleft/gpl.html
// or by writing to
//   Free Software Foundation, Inc.
//   51 Franklin Street, Fifth Floor
//   Boston, MA  02110-1301
//   USA


(function () {

	//update information
	var SCRIPT = {
		name: "Ipernity Refer Comment",
		namespace: "http://www.consulentionline.org/",
		description: "auto comment the place where you come from",
		identifier: "http://www.consulentionline.org/js/ipernityrefercomment.user.js",
		version: "0.92",								// version
		date: (new Date(2008, 07, 26))		// update date
		.valueOf()
	};



	var IpernityReferComment = function() {this.init();}

	IpernityReferComment.prototype = {

		init: function() {
			if (document.referrer=="") return;
			var referrer = document.referrer;
			//alert(referrer);
			// ADD 0.9: home page resolution
			if (referrer == "http://www.ipernity.com/" || referrer == "http://ipernity.com/" ||  referrer == "http://www.ipernity.com" || referrer == "http://ipernity.com") {
					this.insertComment("Seen on Ipernity home page",referrer);
					return;
			}
			// END ADD
			// ADD 0.92: IpernityFox resolution; thanks Carsten
			  if (referrer == "http://foto-cs.de/ipernityfox.html") {
				this.insertComment("Seen on IpernityFox", referrer);
				return;
			}
			// END ADD
 			var tmp = referrer.split("/");
			if (this.isNumeric(tmp[4]))
				// TODO: monitorare se sono nello stream dell'utilizzatore dello script e usare your
				theUser = 'a user\'s';
			else
//				theUser = '<b>' + tmp[4] + '</b>';
			{	// 0.9: removing parameters from address
				noParams=tmp[4].split("?");
				theUser = '<b>' + noParams[0] + '</b>';
			}
			if (referrer.indexOf('/blog/') >= 0) {
			this.insertComment('Coming from ' + theUser + ' blog',referrer);
			// /blog/* --> a blog
			// /blog/*/* --> a blog entry
			} else if (referrer.indexOf('/doc/') >= 0) {
			this.insertComment('Coming from ' + theUser + ' photostream',referrer);
			// /doc/* --> a user's stream
			// /doc/*/* --> a user's shot
			// /doc/*/album/* --> a user's album
			// /doc/*/album/*/comments --> a user's album comments
			// /doc/*/album/*/comments/* --> a user's album comment
			// /doc/*/*/in/album/* --> a user's shot in a album
			// /doc/*/*/in/album/*?from=* --> same user different album
			} else if (referrer.indexOf('/home/') >= 0) {
				if (referrer.indexOf('/network') >= 0) {
					if (referrer.indexOf('/0/photo') >= 0) {
						this.insertComment('Seen in '+ theUser+ ' network latest photos',referrer);
					} else{
						this.insertComment('Seen in ' +theUser +' network',referrer);
					}
				} else if (referrer.indexOf('/activity') >= 0) {
					this.insertComment('Seen in my account recent activity',referrer);
				} else if (referrer.substr(referrer.indexOf('/home/')).length >6) { // rivedere
				this.insertComment('Seen in ' + theUser + ' home page',referrer);
				} else {
/*				this.insertComment('Seen on Ipernity home page',referrer);
*/				this.insertComment('Seen on Ipernity front page',referrer);
				}
			// /home/* --> a user's home
			// /home/*/activity --> a user's stream recent activity
			// /home/*/network --> a user's network
			// /home/*/network/0/photo --> network lastest photos
			// /home/ --> Ipernity home
			} else if (referrer.indexOf('/explore/popular')>=0) {
			// /explore/popular
			this.insertComment('Seen in Explore what\'s hot',referrer);
			} else if (referrer.indexOf('/explore/latest')>=0) {
			// /explore/latest
			this.insertComment('Seen in Explore latest docs and posts',referrer);
			} else if (referrer.indexOf('/explore/profile')>=0) {
			// /explore/profile --> member tags for a user
			this.insertComment('Seen in the member tags for a user',referrer);
			} else if (referrer.indexOf('/explore/keyword')>=0) {
			// /explore/keyword/*
			this.insertComment('Seen in Explore keyword tag',referrer);
			// TODO: aggiungere explore home page
			} else if (referrer.indexOf('/group')>=0) {
		
				if (referrer.indexOf('/discuss')>=0) 
					this.insertComment('Seen in ' + this.resolveGroup(referrer) +'group discussion',referrer)
				else if (referrer.indexOf('/doc')>=0) 
					this.insertComment('Seen in ' + this.resolveGroup(referrer) +'group pool', referrer)
				else
					this.insertComment('Coming from ' + this.resolveGroup(referrer) +'group home page',referrer);
			// /group/ --> a group homepage
			// /group/*/doc --> a group pool
			// /group/*/discuss/* --> a group discussion
			} else if (referrer.indexOf('/search/doc')>=0) {
					this.insertComment('Coming from a search',referrer);
			// /search/doc --> a search
			} else if (referrer.indexOf('/mail/view/inbox')>=0) {
					this.insertComment('Coming from a mailbox','javascript:void(0)');
			// /mail/view/inbox --> a mail message
			} else if (referrer.indexOf('/tag')>=0) {
					this.insertComment('Coming from a tag cloud',referrer);
			} else {
			//alert(referrer); // uncomment this line if you want to help me improve this script
			this.insertComment("Coming from here", referrer);
			}
		},
		isNumeric: function(aString) {
		var ValidChars = "0123456789.";
		var isNumeric=true;
		var Char;

 
			for (i = 0; i < aString.length && isNumeric == true; i++) 
			{ 
				Char = aString.charAt(i); 
				if (ValidChars.indexOf(Char) == -1) 
				{
					isNumeric = false;
				}
			}
			return isNumeric;
		},
		resolveGroup: function(referrer) {
			var mySlash=referrer.indexOf('/group')+6;
			var myNextSlash=referrer.substr(mySlash).indexOf('/');
			if (myNextSlash==0) myNextSlash=referrer.length;
			var myString=referrer.substr(mySlash+1,myNextSlash-1).toLowerCase();
			//alert( myString);
			switch (myString) {
				case '25651':
				case 'ww.wonders': risposta = 'the Weekend Walk Wonderz '; break;
				case '25262':
				case 'germany': risposta = 'the Germany '; break;
				case '25345':
				case 'macro': risposta = 'the Macro '; break;
				case '25342':
				case 'i-central': risposta = 'the i-Central '; break;
				case '25925':
				case 'geometric-abstracts': risposta = 'the Geometric Abstracts '; break;
				case '25390':
				case 'nightshots': risposta = 'the Night Shots '; break;
				case '25495':
				case 'absurd': risposta = 'the \'the Absurd\' '; break;
				case '25456':
				case 'bokeh': risposta = 'the Bokeh '; break;
				case '25458':
				case 'minimal': risposta = 'the Less is More '; break;
				case '26080': risposta = 'the ** PIX EDIT ** '; break;
				case '25731':
				case 'groupiperdirectory': risposta = 'the Ipernity Groups Directory '; break;
				case '25235':
				case 'bestoftheweek': risposta = 'the Best of the Week '; break;
				case '25702':
				case 'b.w': risposta = 'the B&W '; break;
				case '25279':
				case 'blackandwhite': risposta = 'the Black and White '; break;
				case '27386': risposta = 'the * Crazy Faces * '; break;
				case '26757': risposta = 'the asymetrix '; break;
				case '25962':
				case 'manipulatedreality': risposta = 'the Manipulated reality '; break;
				case '25952':
				case 'metropolis': risposta = 'the Metropolis '; break;
				case '25618':
				case 'flowerotica': risposta = 'the Flowerotica '; break;
				case '25709':
				case 'myself': risposta = 'the \"me, myself and I\" '; break;
				case '25454':
				case 'strongcolors': risposta = 'the Strong colors '; break;
				case '25259': risposta = 'the Art libre... '; break;
				case '25542':
				case 'selfportraits': risposta = 'the selfportraits '; break;
				case '25216':
				case 'architecture': risposta = 'the Architecture '; break;
				case '25404':
				case 'portrait': risposta = 'the Portrait '; break;
				/*0.7*/
				case '25399':
				case 'travelphoto': risposta = 'the Travel Photo Forum '; break;
				case '25660':
				case 'catchy-colours': risposta = 'the Catchy Colours '; break;
				case '25332':
				case 'landscape': risposta = 'the Landscape '; break;
				case '25517':
				case 'fogdustmistbrouillardnebel': risposta = 'the fog, dust, mist... '; break;
				case '25592':
				case 'grafitti': risposta = 'the Graffiti '; break;
				case '25486': risposta = 'the Animal Photos '; break;
				case '25937':
				case 'red': risposta = 'the Red! '; break;
				case '25318':
				case 'streetphotography': risposta = 'the Candid Street Photography '; break;
				case '25312': risposta = 'the Ipernity meetings '; break;
				case '25283':
				case 'flower': risposta = 'the Flowers '; break;
				case '25956':
				case 'takealook': risposta = 'the Take a look! '; break;
				/* 0.8*/
				case '25280': risposta = 'the Mono images '; break;
				case '25218':
				case 'sky.creative': risposta = 'the Sky is creative... '; break;
				case '25392':
				case 'experiment': risposta = 'the Time For Experiments '; break;
				case '25547':
				case 'woods': risposta = 'the into the woods '; break;
				case '26486':
				case 'girls': risposta = 'the Girls - An Ode... '; break;
				case '25936':
				case 'blue': risposta = 'the Blue! '; break;
				case '28032':
				case 'light': risposta = 'the Light '; break;
				/*0.9*/
				case '25479':
				case 'blackwhite': risposta= 'the B&W ';break;
				case '25589':
				case 'sony-r1': risposta = 'the Sony R1 '; break;
				case '26222':
				case 'tree': risposta = 'the Tree '; break;
				case '26294':
				case 'illusions': risposta = 'the Illusions '; break;
				case '26411':
				case 'spring': risposta = 'the 4 Seasons - Spring '; break;
				case '26412':
				case 'summer': risposta = 'the 4 Seasons - Summer '; break;
				case '26295':
				case 'illusionsofnature': risposta = 'the Illusions of Nature '; break;
				case '26413':
				case 'fall': risposta = 'the 4 Seasons - Fall '; break;
				case '26414':
				case 'winter': risposta = 'the 4 Seasons - Winter '; break;
				case '25563':
				case 'clouds': risposta = 'the Clouds '; break;
				case '25560':
				case 'backlighting': risposta = 'the Backlighting '; break;
				case '26943':
				case 'fruits': risposta = 'the Fruits and Vegetables '; break;
				case '28026':
				case 'stone': risposta = 'the Stone '; break;
				case '25906':
				case 'leaves': risposta = 'the Leaves '; break;
				case '25340':
				case 'dark': risposta = 'the The Dark Side '; break;
				case '29054': risposta = 'the One pic/One song '; break;
				case '25277':
				case 'mpph': risposta = 'the mpph '; break;
				case '25734':
				case 'markart.photography': risposta = 'the professional photographers '; break;
				case '29029': risposta = 'the Another view of the world '; break;
				case '25314': risposta = 'the chimney-roofs-walls '; break;
 				case '25711': risposta = 'the urban life '; break;
				case '28334':
				case 'sky-limit': risposta = 'the The Sky is the Limit '; break;
				case '25840':
				case 'reflection': risposta = 'the Reflection '; break;
				case '25716':
				case 'contemporaryabstracts': risposta = 'the Contemporary Abstracts '; break;
				case '28209': risposta = 'the Photographes Super Amateurs '; break;
				case '25260':
				case 'hdr': risposta = 'the HDR '; break;
				case '28302': risposta = 'the * most creative * '; break;
				case '28610':
				case 'unofficialgroup': risposta = 'the Unofficial Ipernity Ideas and Wish '; break;
				case '25937':
				case 'red': risposta = 'the Red! '; break;
				case '25810':
				case 'praiseandcurseofthecity': risposta = 'the Praise and Curse of the City '; break;
				case '26094': risposta = 'the An Ocean of Blue '; break;
				case '25482': risposta = 'the Sunrise - Sunset: '; break;
				
				default: risposta='a '; break;
				}
		return risposta;
		},
		// ver 0.91: sostituito font size=-2 con lo span
		insertComment: function(comment, url) {
			var html = comment;
			// TODO: correggere ordine i e span
			if(url)
			html = '<span style="font-size: smaller;"><a href="'+url+'" title="Seen on...">'+comment+'</a>';
			html = "\n\n--\n<i>"+html+"</i>"+
			' <em>(<a href="http://www.consulentionline.org/Ipernity-Add-referer-into-comments.html">?</a>)</em></span>';
			thisTextArea = document.evaluate(
											 "//textarea[@id='comment_content']",
											 document,
											 null,
											 XPathResult.FIRST_ORDERED_NODE_TYPE, null
											 ).singleNodeValue;
			if(thisTextArea) {
				if(thisTextArea.value.indexOf(html) < 0) 
					thisTextArea.value += html;
				thisTextArea.selectionStart = thisTextArea.selectionEnd = 0;
			}
		}, 
	}
		
	//======================================================================
	// launch
	try {
		window.addEventListener("load", function () {
									try {
										
										// update automatically (http://userscripts.org/scripts/show/2296)
										// win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
									} catch (ex) {} 
									
									var ipernitygp = new IpernityReferComment();
		}, false);
	} catch (ex) {}
})();
