// ==UserScript==
// @name          Flickr Photo Page Enhancer
// @description	  Adds links to different sizes directly to a Flickr photo page, generates html code to easily copy'n paste medium-size thumbnail & description, and automatically loads your 20 most used tags when you click "Add Tag."
// @namespace     http://www.rhyley.org/gm/
// @include       http://flickr.com/photos/*
// @include       http://www.flickr.com/photos/*

//by Fabricio Zuardi (http://www.hideout.com.br)
//
// Modified by Jason Rhyley (jason AT rhyley DOT org)
// Rewrote script. Tweaked interface and updated to work with new flash-free flickr.
// Modified again by Danny Dawson (greasymonkey AT quasistoic DOT org)
// Changed the Copy Html to embed the medium size image instead of the thumbnail.  Rhyley and Fabricio: you guys rock.
//
// ==/UserScript==

(function() {

	//if a photo page
	if (document.getElementById("button_bar")) {
	
	p = photo_hash[location.pathname.split('/')[3]];
	
	var photo_prefix = "http://photos"+p.server+".flickr.com/"+p.id+"_"+p.secret
	var largephoto_url = photo_prefix + "_o.jpg"
	var mediuphoto_url = photo_prefix + ".jpg"
	var smallphoto_url = photo_prefix + "_m.jpg"
	var thumbphoto_url = photo_prefix + "_t.jpg"
	var squarphoto_url = photo_prefix + "_s.jpg"

	//This whole bit inserts a 'Large Size' link to the original size jpg

	var containerA = document.createElement("li");
	containerA.setAttribute("class","Stats");
	var linkA = 'View sizes: <a href="' + largephoto_url + '" style="text-decoration: none;">Original</a>, ';
	linkA += '<a href="' + mediuphoto_url + '" style="text-decoration: none;">Medium</a>, ';
	linkA += '<a href="' + smallphoto_url + '" style="text-decoration: none;">Small</a>, ';
	linkA += '<a href="' + thumbphoto_url + '" style="text-decoration: none;">Thumbnail</a>, ';
	linkA += '<a href="' + squarphoto_url + '" style="text-decoration: none;">Square</a>';
	containerA.innerHTML = linkA;

	//This next bit inserts a 'Copy HTML' link which shows a textarea
	//with the thumbnail & description suitble for copy & paste.
	
	var containerB = document.createElement("li");
	containerB.setAttribute("class","Stats");
	var linkB = document.createElement("a");
	linkB.setAttribute("style","text-decoration: none;");
	linkB.setAttribute("href",'javascript:showHTML(0);');
	linkB.setAttribute("id","linkB");	
	linkB.appendChild(document.createTextNode("Show Quicklink"));
	containerB.appendChild(linkB);

	var texty = document.createElement("textarea");
	texty.setAttribute("rows","6");
	texty.setAttribute("cols","25");
	texty.setAttribute("style","display:none");
	texty.setAttribute("id","texty");
	if (document.getElementById("title_div" + p.id)) 
		p.title = document.getElementById("title_div" + p.id).innerHTML;
		else p.title = '';
	if (document.getElementById("description_div" + p.id)) 
		p.description = document.getElementById("description_div" + p.id).innerHTML;
		else p.description = '';
	var photo_html = '<a title="' + p.title + '" href="' + document.location + '"><img src="' + mediuphoto_url + '"></a> ' + p.description;
	texty.appendChild(document.createTextNode(photo_html));

	addlInfo = document.getElementsByTagName("ul")[0]
	addlInfo.appendChild(containerA);
	addlInfo.appendChild(containerB);
	addlInfo.parentNode.insertBefore(texty, addlInfo.nextSibling);
	
	window.showHTML = function showHTML(showing){
		if(!showing) {
			document.getElementById("texty").style['display'] = 'block';
			document.getElementById("linkB").href = 'javascript:showHTML(1);';
			document.getElementById("linkB").innerHTML = 'Hide Quicklink';
			document.getElementById("texty").select();
		} else {
			document.getElementById("texty").style['display'] = 'none';
			document.getElementById("linkB").href = 'javascript:showHTML(0);';
			document.getElementById("linkB").innerHTML = 'Show Quicklink';
		}
	}

	
	// This bit makes clicking the "Add Tag" link automatically populate your most popular tags
	tagadderlink = document.getElementById('tagadderlink');	
	if(tagadderlink) tagadderlink.firstChild.setAttribute("onclick", "tagrs_showForm(); tagrs_showPopular(20, '"+p.id+"'); return false;");


	}//close if a photo page

})();