// ==UserScript==
// @name           pickpic
// @namespace      http://pickpic.timoklok.com
// @description    Allows the user to save any image on the internet into a shared database.
// @exclude        http://pickpic.timoklok.com/*
// @exclude        http://www.pickpic.timoklok.com/*
// ==/UserScript==


function GetImgUrl(img,pageUrl){
	urlImg=img.getAttribute('src');
		if (urlImg.substring(0,4)!='http'){
			if (urlImg[0]=='/'){
			urlArray = pageUrl.split("/");
			urlArray = urlArray[0]+"//"+urlArray[1]+"/"+urlArray[2];
			//urlArray=strinArray.join('/');
			urlImg=urlArray+urlImg;
			}
			else{
			stringArray = pageUrl.split("/");	
			stringArray.pop();
			urlArray=stringArray.join('/')+'/';
			urlImg=urlArray+urlImg;
			}			
		}
	return urlImg;	
}			



// Add jQuery
    var GM_JQ = document.createElement('script');
    GM_JQ.src = 'http://www.pickpic.timoklok.com/jquery-1.3.2.js';
    GM_JQ.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
    function GM_wait() {
        if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
    else { $ = unsafeWindow.jQuery; letsJQuery(); }
    }
    GM_wait();

// All your GM code must be inside this function
    function letsJQuery() {
        //alert($); // check if the dollar (jquery) function works
       $(function(){
				$("img").each(function(i) {
					
					
					
    				pageUrl = location.href;
    				img=this;
    				urlImg = GetImgUrl(img,pageUrl);
    				var wrapperdiv = $('<div class="imgwrap" style="position: relative;"></div>');
    				var formdiv = $('<div class="imgform"style="position: absolute; left: 5px; top: 5px; opacity:.75; padding:10px; font:Arial, Helvetica, sans-serif; font-size:9px; background-color:#FFFFFF;width 170px;display: none; z-index: 100;"><form action="http://www.pickpic.timoklok.com/insert.php" method="post"><input name="next" type="hidden" value="'+pageUrl+'" /><input name="url" type="hidden" value="'+urlImg+'" />Tags<br /><input type="text" size="24" name="tags" /><br /><br />Description<br /><textarea cols="21" name="desc"></textarea><br /><br /><button type="submit">Pick this image</button></form></div>' );
    				
    				if ($(this).parent().get(0).tagName=="A"){    					
    					$($(this).parent()).wrap(wrapperdiv);
    					$($(this).parent()).after(formdiv);					
    				}
    				else {
						$(this).wrap(wrapperdiv);
    					$(this).after(formdiv);
    				}
    				$(".imgwrap").mouseenter(function () {
        					$(".imgform",this).show();
    					}).mouseleave(function () {
        					$(".imgform",this).hide();
    					});
				});
			});

    }
