/**
 * intended for creating bookmark toggles
 */

/* parseUri JS v0.1.1, by Steven Levithan <http://stevenlevithan.com>
Splits any well-formed URI into the following parts (all are optional):
----------------------
- source (since the exec method returns the entire match as key 0, we might as well use it)
- protocol (i.e., scheme)
- authority (includes both the domain and port)
  - domain (i.e., host; can be an IP address)
  - port
- path (includes both the directory path and filename)
  - directoryPath (supports directories with periods, and without a trailing backslash)
  - fileName
- query (does not include the leading question mark)
- anchor (i.e., fragment) */
function parseUri(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	
	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	
	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	
	return uri;
}

Hash.implement({
	toObject:function() {
		var o = {}
		k = this.getKeys()
		for (var i in k) {
			o[k[i]] = this[k[i]]
		} 
		return o
	}
})

Bookmark = new Class({
	Implements:{
		set_params:function(params){
			this.params = new Hash(params)
			this._rebuild_href()
		},
		set_options:function(options){
			this.options = this.options || {}
			for (var i in options) {
				switch(i) {
					case 'url':
						this.options[i] = options[i]
						if (!options['icon']) {
							// we create a default icon from url
							var uri = parseUri(options[i])
							this.options.icon = uri.protocol + "://" + uri.domain + "/favicon.ico"
						}
					break;
					case 'default_params': this.default_params = new Hash(options[i])
					break;
					case 'params': this.params = new Hash(options[i])
					break;
					default: this.options[i] = options[i]
					break;
				}
			}
		},
		_rebuild_href:function() {
			var h = new Hash(this.default_params)
			h.combine(this.params)
			this.view.link.href = this.options.url + '?' + h.toQueryString()
		}
	},
	initialize: function(properties, options){
		this.set_options(options)
		
		this.view = {}
		this.view.link = new Element('a', new Hash(properties).combine({title:this.options.title}).toObject()).grab(
			this.view.image = new Element('img',{
				src:this.options.icon,
				alt:this.options.title,
				width: this.options.width || null,
				height: this.options.height || null
			}))
		this._rebuild_href()
    }
})

function create_bookmarks(bm) {
	var stumbleupon = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'stumbleupon',
			url:'http://www.stumbleupon.com/submit',
			width: '16',
			default_params:{},
			params:{url:bm.url}
		}
	)
	var delicious = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'delicious',
			url:'http://delicious.com/post',
			width: '16',
			default_params:{},
			params:{url:bm.url}
		}
	)
	var digg = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'digg',
			url:'http://digg.com/submit/',
			width: '16',
			default_params:{phase:"2"},
			params:{
				url:bm.url,
				title:bm.title || null
				}
		}
	)
	var facebook = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'facebook',
			url:'http://www.facebook.com/sharer.php',
			width: '16',
			default_params:{},
			params:{
				u:bm.url,
				t:bm.title || null
				}
		}
	)
	var reddit = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'reddit',
			url:'http://www.reddit.com/submit',
			width: '16',
			default_params:{},
			params:{
				url:bm.url,
				title:bm.title || null
				}
		}
	)
	var twitter = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'twitter',
			url:'http://twitter.com/home',
			width: '16',
			default_params:{},
			params:{status:bm.url}
		}
	)
	var misterwong = new Bookmark({'class':'bookmark',target:'_blank'}, {
			title: 'misterwong',
			url:'http://www.mister-wong.com/index.php',
			width: '16',
			default_params:{action:'addurl'},
			params:{
				bm_url:bm.url,
				bm_description:bm.description || null
				}
		}
	)

	var bookmarks = [stumbleupon,delicious,digg,facebook,reddit,twitter,misterwong]
	return bookmarks
}
