// Photo Postcard
// 29-30 September 2006
// Leah Culver and Tantek Celik
// icons by Leslie Chicoine
//
// Copyright 2006 some rights reserved
// cc-by-2.5
// http://creativecommons.org/licenses/by/2.5/
//
// ==UserScript==
// @name          Photo Postcard
// @namespace     http://leahculver.com
// @description   quick way to send a photo postcard to a friend
// @include       http://flickr.com/photos/*
// @include       http://www.flickr.com/photos/*
// ==/UserScript==
//
// fuck you, 'www'!
// see nowww.org for details

// postcard images
normsrc = "data:image/gif,GIF89a%2F%00%18%00%A2%00%00%8D%8D%8D%84%84%84%C5%C5%C5%C4%C4%C4%83%83%83%FF%FF%FF%00%00%00%00%00%00!%F9%04%00%00%00%00%00%2C%00%00%00%00%2F%00%18%00%00%03%9CX%BA%DC%FE0%CAI%AB%BD8%EB%CD%BB%FF%A0C%04%C42%9AD%19%90%D1%20%BC%83%1B%0B%B1%3C0e%11%14y%99%9B%92%5Be%00%40%EDt%8A%23%2B%19t%08%18%AE%82%A0%C8%5C%1C%7F%BC*%E4%D9%106%A6%D6%C6I7%CE%B6%9CR%17%AD%40%0Cu%D1N%AA%5B%C1e%3C_0%B6%9Cw%E4%E5H%3D)go%0Em%0A%7FHYWfXvh43Rr%7D%7C')%8B%89%85%16%86%8C%89%3E%9Df%85x%903%A4.%7BK%3Df%3B%97%12%00%AF%B0%B1%B2%7Bs%B5%B6%B7%B8%B9%BA%BB%15%09%00%3B";
oversrc = "data:image/gif,GIF89a%2F%00%18%00%A2%00%00%C0%C0%C0%F01%00%EFA%A5%AA%AA%AA%F0A%A6%A9%A9%A9%00%00%00%FF%FF%FF!%F9%04%00%00%00%00%00%2C%00%00%00%00%2F%00%18%00%00%03%BCx%BA%DC%FE0%9E!%AB%BD%E5%EA%CD2%FF%96%07%8E%8FH%9E%8AI%1A%EC%D2*%ACq%C4%1DC%DC%82%40%E4%F8%9E%0B%0C%D9l8%14%BA%1A%94%05A%23%00%B8%5E%C2%A8%D1%98%B29%A8%81%C0%A1y%ECR%A3%0D%D3%B2A%5Dpad%E94%CC%00%92g1%19%C1%89%AA%9A%AF%8Fy%DD%AEp%07%E14g0Sj%83TI%0Ac%7F%0EzhEh%60%60%7C%07%8AGq2%82_44DFbLt%9D%A22%92%9Em%3D8%3A%AA7%8D%83%8E%A4%90%2F%0A%88%0A%00%B6%B7%B8%B9%A1(*%7B%1F%BD%BE%1B%B4%C1%1C%C0%C4!%03%05%05%C9%CA%CD%CB%C9%03%CC%CC%CB%D4%D1%CA%D1%D8%CE%CF%CE%00%09%00%3B";
downsrc = "data:image/gif,GIF89a%2F%00%18%00%B3%00%00%AA%AA%AA%BF%BF%BF%EFA%A5%C0%C0%C0%EFA%A6%EF%40%A5%CA%CA%CA%F01%00%F00%00%F0A%A6%A9%A9%A9%00%00%00%FF%FF%FF%00%00%00%00%00%00%00%00%00!%F9%04%00%00%00%00%00%2C%00%00%00%00%2F%00%18%00%00%04%CE%D0%A8%A9%00%A0%D3%EA%7C%3B%BDU%07b%D5%241h%AA%AEl%EB%BE%D3%2B%CFtZ%D5xn%2BzO%C7%BE%60%EB%26D-%8E)%A4%11y%5C%A8%80%8C%84T%40H%10%04%D3%84%40P(%A8%9C%0C'X%BC%02%EFR%09%1C!%90T%8E%97%C9%154%5DV%1D%10%0C%C1%20%CE7%87%8Dr%00h%2C~)%02l%80_cJ%7FO%3C(%5EuMH%09%7BEg(%04%84-%09%88%97%0CP%02%9B%93Nk%8AmK%8B~s%9B%2C%95%7D%8Dd%7Fo(%AC%92%93y%9E~%B8M%89fDQ8%87%B0o%B2%B4%A0%8F%0CU%09%05RZUX%CDR%9Ea%B4%C6%D5%AB%C9%0C%01%DC%03%DD%DF%DE%DC%D4EP%9FA%E5%E6%3D%E8%E99%EB%EC5%C0%EF%ED%11%00%3B";

scripty = "doingit = false;"
+ "function doit() {"
+ "  i = document.getElementById('postcard_button');"
+ "  d = document.getElementById('postcardDialogDiv');"
+ "  if (!doingit) {"
+ "    i.src = i.getAttribute('downsrc');"
+ "    doingit = true;"
+ "    d.style.visibility = 'visible';"
+ "  } else {"
+ "    i.src = i.getAttribute('oversrc');"
+ "    doingit = false;"
+ "    d.style.visibility = 'hidden';"
+ "  }"
+ "}"
+ "function cya() {"
+ "  i = document.getElementById('postcard_button');"
+ "  d = document.getElementById('postcardDialogDiv');"
+ "  i.src = i.getAttribute('normsrc');"
+ "  doingit = false;"
+ "  d.style.visibility = 'hidden';"
+ "}"
+ "function sendPostcard() {"
+ "  pform = document.getElementById('postcard_form');"
+ "  loc = window.location;"
+ "  addy = document.getElementById('address').value;"
+ "  message = document.getElementById('message').value;"
// + "  photosection = document.getElementById('photoswftd');"
// + "  children = photosection.childNodes;"
// + "  for (i = 0; i < children.length; i++) {"
// + "    child = children[i];"
// + "    if (child.className == 'photoImgDiv') {"
// + "      goodImage = child.firstChild;"
// + "      if (goodImage) {"
// + "        photoURL = goodImage.src;"
// + "      }"
// + "    }"
// + "  }"
+ "  photos = document.getElementsByTagName('div');"
+ "  for(i = 0; i < photos.length; i++) {"
+ "    if(photos[i].className == 'photoImgDiv') {"
+ "      photoURL = photos[i].firstChild.src;"
+ "    }"
+ "  }"
+ "  document.getElementById('messagetext').value = message;"
+ "  document.getElementById('photourl').value = loc;"
+ "  document.getElementById('imageurl').value = photoURL;"
+ "  pform.submit();"
+ "}"
;

if (document.getElementById('photoswftd'))
{
    //alert("hello world!");
	buttonBar = document.getElementById('button_bar');
	
	if (buttonBar) {
		img = document.createElement('img');
		img.src = normsrc;
		img.setAttribute("normsrc", normsrc);
		img.setAttribute("oversrc", oversrc);
		img.setAttribute("downsrc", downsrc);
		
		img.setAttribute("onmouseover", "if (!doingit) { this.src = this.getAttribute('oversrc'); }");
		img.setAttribute("onmouseout", "if (!doingit) { this.src = this.getAttribute('normsrc'); }");
		img.setAttribute("onmouseup", "doit(this);");
		img.setAttribute("id", "postcard_button");
		
		buttonBar.appendChild(img);
		
		container = document.createElement('div');
		container.setAttribute("style", "position: relative; display:inline; z-index: 69;");
		
		box = document.createElement('div');
		box.setAttribute("id", "postcardDialogDiv");
		box.setAttribute("style", "position: absolute; visibility: hidden; top: 0px; left: -47px; border: solid #999; border-width: 0 1px 1px; background: #FFFFFF; width: 250px;");
		
		topborder = document.createElement('div');
		topborder.setAttribute("style", "border: solid #999; border-width: 1px 0 0; margin-left: 46px; font-size: 1px;");
		box.appendChild(topborder);
		
		header = document.createElement('div');
		header.setAttribute("style", "padding: 4px 0; text-align: center; border-bottom: 1px solid #DDD;");
		header.appendChild(document.createTextNode("Send photo postcard:"));		
		box.appendChild(header);
		
		formdiv = document.createElement('div');
		formdiv.setAttribute("style", "margin: 10px;");
		f = document.createElement('form');
		f.setAttribute("id", "postcard_form");
		f.setAttribute("action", "http://ymail.unclehulka.com/postcard/previewpostcard.php");
		f.setAttribute("method", "post");
		/* formdiv.setAttribute("onsubmit", "alert('Flickr postcard sent!');"); */
		formdiv.appendChild(f);
		
		elabel = document.createElement('label');
		elabel.setAttribute("for", "address");
		elabel.setAttribute("style", "display: block;");
		elabel.appendChild(document.createTextNode("To email:"));
		f.appendChild(elabel);
		
		einput = document.createElement('input');
		einput.setAttribute("id", "address");
		einput.setAttribute("name", "address");
		einput.setAttribute("type", "text");
		einput.setAttribute("style", "width: 220px;");
		f.appendChild(einput);
		
		mlabel = document.createElement('label');
		mlabel.setAttribute("for", "message");
		mlabel.setAttribute("style", "display: block; margin-top: 1em;");
		mlabel.appendChild(document.createTextNode("Message:"));
		f.appendChild(mlabel);
		
		mtext = document.createElement('textarea');
		mtext.setAttribute("id", "message");
		mtext.setAttribute("style", "width: 220px; height: 50px;");
		f.appendChild(mtext);
		
		messagetext = document.createElement('input');
		messagetext.setAttribute("type", "hidden");
		messagetext.setAttribute("id", "messagetext");
		messagetext.setAttribute("name", "messagetext");
		f.appendChild(messagetext);
		
		photourl = document.createElement('input');
		photourl.setAttribute("type", "hidden");
		photourl.setAttribute("id", "photourl");
		photourl.setAttribute("name", "photourl");
		f.appendChild(photourl);

		imageurl = document.createElement('input');
		imageurl.setAttribute("type", "hidden");
		imageurl.setAttribute("id", "imageurl");
		imageurl.setAttribute("name", "imageurl");
		f.appendChild(imageurl);

		butt = document.createElement('input');
		butt.setAttribute("class", "Butt");
		butt.setAttribute("value", "SEND POSTCARD");
		butt.setAttribute("onclick","sendPostcard();");
		butt.setAttribute("style", "margin: 5px 0 0;");
		butt.setAttribute("type", "submit");
		f.appendChild(butt);
		
		box.appendChild(formdiv);

		footer = document.createElement('div');
		footer.setAttribute("style", "padding: 4px 0; text-align: center; border-top: 1px solid #DDD;");
		
		cancelLink = document.createElement('a');
		cancelLink.appendChild(document.createTextNode("Cancel"));
		//cancelLink.setAttribute("class", "plain");
		cancelLink.setAttribute("onmouseup", "cya();");
		footer.appendChild(cancelLink);
		box.appendChild(footer);
		
		container.appendChild(box);
   		buttonBar.appendChild(container);
    	
    	s = document.createElement('script');
    	s.setAttribute('type', 'text/javascript');
    	text = document.createTextNode(scripty);
    	s.appendChild(text);

		document.body.appendChild(s);

		wloc = window.location;
		moff = wloc.toString().indexOf('?m=sent');
		if (moff > -1) {
		  alert('Flickr postcard sent!');
		  window.location = wloc.toString().substr(0,moff);
		}		
	}
}
