/*

ShareBox

Creates a ShareThis like link box, for sharing URLs on different social websites.
Relies on MooTools 1.2 and socialhistory.js by Aza Raskin <http://www.azarask.in/blog/post/socialhistoryjs/>

Takes an options object which may include:
	URL               :  The URL to share.
	title             :  The title of URL to share (used on some sites).
	description       :  A description of the content to share. May include HTML and i currently only used for MySpace.
	additional_sites  :  An array of objects with a name and a URL. Will extend the sites list.
	rearrange         :  Whether or not to rearrange the list of sites, so that sites visited by the user will be in the top of list.
	hide_irrelevant   :  Whether or not to hide the sites not visited by the user.
	content_type      :  The type of link to share (eg. 'article', 'video', 'image', 'song'). Will be used in the box header.
	header_text       :  The template for the box header text. %t will be replaced by content_type.
	intro_text        :  This text will - if present - be inserted before the box in a <span> tag. Example use: 'Share on'

*/
var ShareBox = new Class({
	initialize : function(options)
	{
		if (options == null)
		{
			options = {};
		}
		
		// Find the sharebox
		this.box = document.getElement('#sharebox');
		
		// Create Social History object
		this.social_history = SocialHistory();
		
		// List of sites we support
		// This is an array of objects with a name and an URL.
		// The URL should contain at least the string %u which will be replaced by the URL to share.
		// Optionally the URL can contain %t to be replaced with a title and %d to be replaced by a (HTML) description.
		this.sites = [
			{ name : 'Reddit', URL : 'http://reddit.com/submit?url=%u&title=%t'}
			,{ name : 'Facebook', URL : 'http://www.facebook.com/share.php?u=%u' }
			,{ name : 'MySpace', URL : 'http://www.myspace.com/Modules/PostTo/Pages/?l=3&u=%u&t=%t&c=%d'}
			,{ name : 'Delicious', URL : 'http://delicious.com/post?url=%u&title=%t'}
			,{ name : 'Digg', URL : 'http://digg.com/submit?phase=2&url=%u&title=%t'}
			,{ name : 'StumbleUpon', URL : 'http://www.stumbleupon.com/submit?url=%u&title=%t'}
			,{ name : 'Technorati', URL : 'http://www.technorati.com/faves?add=%u'}
			,{ name : 'Slashdot', URL : 'http://slashdot.org/bookmark.pl?url=%u&title=%t'}
		];
		
		// Add additional sites if specified in options
		if (options.additional_sites != null)
		{
			this.sites.extend(options.additional_sites);
		}
		
		// Whether or not to put visited sites in the top of list
		if (options.rearrange != null)
		{
			this.rearrange = options.rearrange;
		}
		else
		{
			this.rearrange = true;
		}
		
		// Whether or not to hide irrelevant sites
		if (options.hide_irrelevant != null)
		{
			this.hide_irrelevant = options.hide_irrelevant;
		}
		else
		{
			this.hide_irrelevant = false;
		}
		
		// Link to share
		this.URL = options.URL;
		
		// Title of link to share
		this.title = options.title || options.URL;
		
		// Description of link to share
		this.description = options.description || this.title;
		
		// Content type of link to share (eg. article, video, image, song). This will be used in the box header
		this.content_type = options.content_type || 'artikel';
		
		// The template for the box header text. %t will be replaced by content_type
		this.header_text = options.header_text || 'Del %t på:';
		
		// This text will - if present - be inserted before the box in a <span> tag. Example use: 'Share on'
		this.intro_text = options.intro_text || '';
		
		this.render()
	}
	
	,render : function()
	{
		// Clean the box before putting anything into it
		this.box.empty();
		
		// Intro text, if desired
		if (this.intro_text != null)
		{
			new Element('span', { 'text' : this.intro_text + ' ' }).inject(this.box, 'before');
		}
		
		this.box.addClass('collapsed');
		// Create header
		this.box.grab(new Element('h3', {
			'text' : this.header_text.replace('%t', this.content_type)
		}));
		
		// Create list
		var priority_found = false; // Whether or not a priority site has been found.
		var not_visited_sites = []; // List of sites less relevant to the user.
		var ul = new Element('ul');
		this.sites.each(function(site) {
			// Create list item
			var li = this.render_list_item(site);
			
			// Set as priority if visited by user and no priority site has not been found yet
			if (!priority_found && this.social_history.doesVisit(site.name) == true)
			{
				li.addClass('priority');
				priority_found = true;
			}
			
			// Visited sites should be added no matter what
			if (this.social_history.doesVisit(site.name) == true)
			{
				ul.grab(li);
			}
			// Hide irrelevant sites if desired
			else if (!this.hide_irrelevant)
			{
				// Save irrelevant sites for later if desired
				if (this.rearrange)
				{
					not_visited_sites.push(li);
				}
				else
				{
					ul.grab(li);
				}
			}
		}.bind(this));
		
		// Attach less relevant sites
		not_visited_sites.each(function(site) {
			ul.grab(site);
		});
		
		// Add a site if none of them got through to the list, to avoid an empty list for users who never visited any of the listed sites.
		if (ul.getElements('li').length == 0)
		{
			ul.grab(this.render_list_item(this.sites[0]));
		}
		
		// If no priority site is found, default to the first one
		if (!priority_found)
		{
			ul.getElement('li').addClass('priority');
		}
		this.box.grab(ul);
		
		// Make box expand and collapse on mouse over/out if there are more than 1 item in the list
		if (this.box.getElements('ul li').length > 1)
		{
			this.box.addEvents({
				'mouseover' : function()
				{
					var priority_init_position = this.getElement('li.priority').getPosition();
					this.setStyles({
						'left' : priority_init_position.x + 'px'
						,'top' : priority_init_position.y + 'px'
					});
					this.removeClass('collapsed');
					this.addClass('expanded');
					var priority_new_position = this.getElement('li.priority').getPosition();
					this.setStyles({
						'left' : (priority_init_position.x - (priority_new_position.x - priority_init_position.x)) + 'px'
						,'top' : (priority_init_position.y - (priority_new_position.y - priority_init_position.y)) + 'px'
					});
				}
				,'mouseout' : function()
				{
					this.removeClass('expanded');
					this.addClass('collapsed');
				}
			});
		}
	}
	
	,render_list_item : function(site)
	{
		// Create list item
		var li = new Element('li');
		
		// Insert link
		li.grab(new Element('a', {
			'text' : site.name
			,'href' : site.URL.replace('%u', this.URL).replace('%t', this.title).replace('%d', this.description)
			,'title' : site.name
		}));
		return li;
	}
});

/*
document.addEvent('domready', function()
{
	// Find the priority element
	var priority_found = false; // Whether or not a priority site has been found.
	var not_visited_sites = [];
	// Loop through sites
	box.getElements('li').each(function(item) {
		var site_name = item.getElement('a').get('text');
		// Find default site
		if (sl.doesVisit(site_name) == true)
		{
			// Mark as default if no default has been found yet
			if (!priority_found)
			{
				item.addClass('priority');
				priority_found = true;
			}
		}
		// Register not-visited sites
		else
		{
			not_visited_sites.push(item);
		}
	});
	
	// Move not visited sites down the list
	not_visited_sites.each(function(item) {
		box.getElement('ul').grab(item);
	});
	
	// If no default site is found, make the first one default
	if (!priority_found)
	{
		box.getElement('li').addClass('priority');
	}
});
*/
