/*
*	Easyshare 1.11 by Johan Hornqvist
*
*	to activate easyshare u need prototype and scriptaculous
*	to activate this function on a page simply use this function
*	new EasyShare("ui-draggable");
*	"ui-draggable" is the element class that the script will look for and activate as a shareble object. The element has to be an image
* 	If the image has a link as parent it will share that link, if not it will send the browsers uri.
*
* 	Added a new functionality. If u now send in a second class, this one marking links for click function for sharing, a click for share will pop up.
*	new EasyShare("ui-draggable","clickToShare");
*
* 	Easyshare will now hide the tag object that always land on the top. To prevent screen from jumping we add the width and hight on the parent object.
*/

var EasyShare = Class.create();
EasyShare.prototype = {
	initialize:function(targetClass,clickClass){
		this.clickClass = clickClass;
		this.targetClass = targetClass;
		this.targetEffect = this.overlayEffect = null;
		var obj = this;
		Event.observe(window,"load",function(){obj.activateSharing()});
	},
	activateSharing:function(){
		Draggables.addObserver("click");
		//Making the objects shareble
		this.images = $$("."+this.targetClass);

		if(this.clickClass != null)
			this.links = $$("."+this.clickClass);
		else
			this.links = new Array();

		var obj = this;
		this.images.each(function(img){
			if(Prototype.Browser.IE)
				img.writeAttribute("title","");
			obj.bindMyFunctions(img,obj);
		});
		this.links.each(function(link){
			obj.bindMyClickFunctions(link,obj);
		});


		//Creating drop areas
		var targetDiv = new Element("div",{"id":"targets"}).setStyle({"display":'none',"marginTop":"10px"});
		var ul = new Element("ul");
		ul.insert(new Element("li",{"id":"facebook"}).insert(new Element("a",{"href":"http://www.facebook.com"}).insert("<!-- -->")));
		ul.insert(new Element("li",{"id":"twitter"}).insert(new Element("a",{"href":"http://twitter.com"}).insert("<!-- -->")));
		ul.insert(new Element("li",{"id":"delicious"}).insert(new Element("a",{"href":"http://delicious.com"}).insert("<!-- -->")));
		ul.insert(new Element("li",{"id":"pusha"}).insert(new Element("a",{"href":"http://www.pusha.se/"}).insert("<!-- -->")));
		targetDiv.insert(new Element("div",{"class":"left"}));
		targetDiv.insert(ul);
		targetDiv.insert(new Element("div",{"class":"right"}));
		targetDiv.insert(new Element("div",{"class":"dropHere","id":"dropSign"}));

		$$("body").first().insert(targetDiv);
		//Activating drop areas
		Droppables.add('facebook', {
			hoverclass:"dropHoverFb",
			onDrop: function(drag,drop) {
				obj.windowOpener('facebook',drag);
			}
		});
		Droppables.add('twitter', {
			hoverclass:"dropHoverTw",
			onDrop: function(drag,drop) {
				obj.windowOpener('twitter',drag);
			}
		});
		Droppables.add('delicious', {
			hoverclass:"dropHoverDe",
			onDrop: function(drag,drop) {
				obj.windowOpener('delicious',drag);
			}
		});
		Droppables.add('pusha', {
			hoverclass:"dropHoverPu",
			onDrop: function(drag,drop) {
				obj.windowOpener('pusha',drag);
			}
		});
	},
	activateDrag:function(e){
		e.stop();
		this.drageble = true;
	},
	bindMyFunctions:function(img,obj){
		img.observe("mouseenter", obj.createTip);
		img.observe("mousedown", obj.activateDrag);
		img.observe("mousemove", obj.createDrager);
		img.observe("mouseleave",function(e){
			if($$("#tip").length > 0)
				$("tip").remove();
			this.drageble = false;
		});
		img.observe("mouseup",function(){this.drageble=false;})
	},
	bindMyClickFunctions:function(link,obj){
		if( typeof obj.images.first() == "undefined")
			link.href = "#";
		else
			link.href = obj.images.first().readAttribute("src");
		link.observe("mouseenter", obj.activateDrag);
		link.observe("click", obj.createDrager);
		link.observe("mouseleave",function(e){
			this.drageble = false;
		});
	},
	windowOpener:function( shareMedia,obj ){
		var url = encodeURIComponent(obj.readAttribute("rel"));
		var title = encodeURIComponent(obj.readAttribute("title"));
		if(shareMedia == 'facebook')
			url = "http://www.facebook.com/share.php?u="+url;
		else if(shareMedia == 'twitter')
			url = "http://twitter.com/home?status=Currently reading"+url;
		else if(shareMedia == 'delicious')
			url	= 'http://delicious.com/save?v=5&noui&jump=close&url='+url+'&title='+encodeURIComponent(title);
		else if(shareMedia == "pusha")
			url = 'http://www.pusha.se/posta?url='+url+'&title='+title;
		if($$("#helper").length > 0)
			$("helper").remove();
		if($$("#overlayES").length > 0)
			$("overlayES").remove();
		$("targets").hide();
		window.open(url, shareMedia,'toolbar=no,width=550,height=550');
	},
	createTip:function(e) {
		e.stop();
		if($$("#tip").length === 0){
			var tip = new Element("div",{"id":"tip"}).insert("<span>Drag och släpp för att dela<\/span><span class='arrow'><\/span>").setOpacity(0).setStyle({ "left":e.pageX + 30+"px", "top":e.pageY - 16+"px","display":"block","zIndex":99998 });
			$$("body").first().insert(tip);
			if(Prototype.Browser.IE)
				tip.fade({ duration: 0.5, from: 0, to: 0.7 });
			else
				tip.fade({ duration: 0.5, from: 0, to: 1 });

		}
	},
	createDrager : function(e){
		if(this.drageble){
			if($$("#helper").length === 0){
				var url = (e.element().up(0).tagName.toLowerCase() == "a")?e.element().up(0).href:location.href;
				if(e.element().tagName.toLowerCase() == "img"){
					var imgUrl = e.element().readAttribute("src");
					var dragImg = new Element("img",{"id":"thumb","src":imgUrl});
					if($("dropSign").hasClassName("clickHere")){
						$("dropSign").removeClassName("clickHere");
						$("dropSign").addClassName("dropHere");
					}
				} else {
					var imgUrl = e.element().readAttribute("href");
					if(imgUrl == "#" )
						var dragImg = new Element("div").insert($$("title").first().innerHTML);
					else
						var dragImg = new Element("img",{"id":"thumb","src":imgUrl});	
					if($("dropSign").hasClassName("dropHere")){
						$("dropSign").removeClassName("dropHere");
						$("dropSign").addClassName("clickHere");
					}
				}
				var helper = new Element("div",{"id":"helper","class":"draggable","rel":url,"title":e.element().readAttribute("title")}).insert(
						dragImg ).setStyle({"left":Event.pointerX(e)+15+'px',"top":Event.pointerY(e)+15+'px',"cursor":"pointer","zIndex":99999});
				if(e.element().readAttribute("title") != null && e.element().readAttribute("title").length > 0)
					helper.insert(new Element("div").insert(e.element().readAttribute("title")));
				$$("body").first().insert(new Element("div",{"id":"overlayES","class":"overlay"}).setOpacity(0.7)).insert(helper);
				var obj = this;
				if(obj.targetEffect != null)
					obj.targetEffect.cancel();
				if(obj.overlayEffect != null)
					obj.overlayEffect.cancel();
				$("overlayES").observe("mouseup",function(){
					if(obj.targetEffect != null)
						obj.targetEffect.cancel();
					if(obj.overlayEffect != null)
						obj.overlayEffect.cancel();
					if($$("#helper").length > 0)
						Element.remove("helper");
					if($$("#overlayES").length > 0){
						obj.overlayEffect = Effect.Fade("overlayES",{duration:0.5,afterFinishInternal:function(){$("overlayES").remove();}});
					}
					obj.targetEffect = Effect.BlindUp('targets',{duration:0.5});
				});
				if($$("#tip").length > 0)
					$("tip").remove();
				var width = document.viewport.getWidth()/2 - $("targets").getWidth()/2;
				var top = window.pageYOffset ||	document.documentElement.scrollTop || document.body.scrollTop
				$("targets").setStyle({"left":width+"px","top":top+"px","height":"auto"});
				this.targetEffect = Effect.BlindDown('targets',{duration:0.5});
				var drager = new Draggable($("helper"),{
					starteffect:false,
					endeffect:false,
					zindex:99999,
					onEnd:function(obj,e){
					if(obj.targetEffect != null)
						obj.targetEffect.cancel();
					if(obj.overlayEffect != null)
						obj.overlayEffect.cancel();
						if($$("#helper").length > 0)
							Element.remove("helper");
					if($$("#overlayES").length > 0){
						obj.overlayEffect = Effect.Fade("overlayES",{duration:0.5,afterFinishInternal:function(){
							if($$("#overlayES").length > 0)
								$("overlayES").remove();
						}});
					}
					obj.targetEffect = Effect.BlindUp('targets',{duration:0.5});
						drager.destroy();
						$$("object").each(function(obj){
							obj.show();
						});

					},
					onDrag:function(){
						if($$("#tip").length > 0)
							$("tip").remove();	
					}
				});

				$$("object").each(function(obj){
					var height = (obj.up().getHeight() > obj.getHeight())?obj.up().getHeight():obj.getHeight();
					var width = (obj.up().getWidth() > obj.getWidth())?obj.up().getWidth():obj.getWidth();
					obj.up().setStyle({"height":height+"px","width":width+"px"});
					obj.hide();
				});
				if(Prototype.Browser.IE){
					//Prototype cant find the leftClick for IE so this is what we call a "fulhack" in sweden!
					var oldIsLeftClick = Event.isLeftClick;
					Event.isLeftClick = function(e){ return true; }
					drager.initDrag(e);
					Event.isLeftClick = oldIsLeftClick;
				} else
					drager.initDrag(e);				
			}
		} else {
			if($$("#tip").length > 0)
				$("tip").setStyle({ "left":e.pageX + 30+"px", "top":e.pageY - 16 +"px"});
		}
	}
};
