
SocialSharer = new Class({
	Implements: [Options],

	options: {
		transition:		Fx.Transitions.Cubic.easeOut,
		timeout: 1000,
		duration: 400
	},

	initialize: function(options) {
		this.setOptions(options);
		this.timer = null;
		this.curAnchor = null;
		this.links = [];
	},

	add: function(name, title, icon) {
		this.links.push({
			'name': name,
			'title': title,
			'icon': (icon) ? new Element('img', { src: icon }) : null
		});
	},


	show: function(anchor, opts) {
		this.clearTimer();
		if(this.links.length < 1) return;

		options = {
			title:	document.title,
			href:	document.location.href,
			direction: 'down'
		};
		try { for(var x in opts) options[x] = opts[x]; } catch(e) {};
		options.direction = (options.direction == 'up') ? 'up' : 'down';

		anchor = $(anchor);
		if(!anchor) return;
		if(this.div) {
			if(this.curAnchor == anchor) return;
			else this.doHide();
		}
		this.curAnchor = anchor;

		this.div = new Element('div', { styles: { display:'none' } });
		this.div.addClass('SocialShare');

		for(var i=0; i<this.links.length; ++i) {
			var row = new Element('div');

			if(this.links[i].icon) this.links[i].icon.inject(row);
			new Element('span').set('html', this.links[i].title).inject(row);

			row.store('shareName', this.links[i].name);
			row.store('shareTitle', options.title);
			row.store('shareHref', options.href);

			window.__SocialSharer = this;
			row.addEvent('mouseover', function() {
				$(this).addClass('hover');
				window.__SocialSharer.clearTimer();

				try { imageRollover($(this).getElement('img')); } catch(e) {};
			});

			row.addEvent('mouseout', function() {
				$(this).removeClass('hover');
				window.__SocialSharer.hide();

				try { imageRollout($(this).getElement('img')); } catch(e) {};
			});

			row.addEvent('click', function() {
				this.fireEvent('mouseout');
				window.__SocialSharer.click(this);
				window.__SocialSharer.doHide();
			});

			row.inject(this.div);
		}
		this.div.inject($(document.body));

		this.div.setStyles({
			top: (options.direction == 'up') ? (anchor.getTop() - anchor.getHeight() - 3) : (anchor.getTop() + anchor.getHeight() + 3),
			left: anchor.getLeft(),
			display: 'block',
			height: 1
		});

		this.div.setStyle('width', this.div.getScrollWidth());
		this.div.setStyles({
			overflow: 'hidden'
		});

		var toTop = (options.direction == 'up') ? (this.div.getTop() - this.div.getScrollHeight()) : this.div.getTop();
		new Fx.Morph(this.div, {
			transition: this.options.transition,
			duration: this.options.duration,
			onComplete: function() {
		
			}
		}).start({
			height: this.div.getScrollHeight()+2,
			top: toTop
		});
	},

	click: function(that) {
		that = $(that);
		if(!that) return;

		var shareName = that.retrieve('shareName');
		var shareTitle = that.retrieve('shareTitle');
		var shareHref = that.retrieve('shareHref');

		if(!shareName) return;
		if(!shareTitle) shareTitle = $$('title').get('text');
		if(!shareHref) shareHref = document.location.href;

		// make sure shareHref is absolute
		if(!shareHref.match(/^(a-z)+:\/\//i)) {
			if(!shareHref.match(/^\//)) shareHref = '/' + shareHref;
			var u = new URI();
			shareHref = u.get('scheme') + '://' + u.get('host') + shareHref;
		}

		switch(shareName) {
			case 'facebook':
				var href = 'http://www.facebook.com/sharer.php?u='+urlencode(shareHref)+'&t='+urlencode(shareTitle)+'';
				window.open(href, '_blank');
				break;

			case 'twitter':
				var href = 'http://twitter.com/home?status=' + urlencode(shareTitle + ' - ' + shareHref);
				window.open(href, '_blank');
				break;

			case 'email':
				form_friend_show('divFriendForm', shareTitle, shareHref);
				break;
		}
	},
	
	clearTimer: function() {
		clearTimeout(this.timer);
	},

	hide: function() {
		this.clearTimer();
		this.timer = setTimeout(function() {
			this.doHide();
		}.bind(this), this.options.timeout);
	},

	doHide: function() {
		if(this.div) {
			this.div.setStyle('display', 'none');
			this.div.destroy();
			this.div = null;
		}
	}

});




