var j = jQuery

var ShareDis = function(el,title,url,position,alt_urls){
	var element = j(el)
	var pageTitle = title || document.title
	var pageURL = url || window.location
	var offset = [0,0]
	
	var timer

	var urls = [["Facebook", "http://www.facebook.com/share.php?u=__URL__&t=__TITLE__"],
						  ["Digg", "http://digg.com/submit?phase=2&url=__URL__&title=__TITLE__"],
						  ["Del.icio.us", "http://del.icio.us/post?url=__URL__&title=__TITLE__"],
						  ["Reddit", "http://reddit.com/submit?url=__URL__&title=__TITLE__"],
						  ["Google", "http://www.google.com/bookmarks/mark?op=edit&bkmk=__URL__&title=__TITLE__"],
						  ["StumbleUpon", "http://www.stumbleupon.com/submit?url=__URL__&title=__TITLE__"],
						  ["MSN Live", "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=__URL__&title=__TITLE__&top=1"]]

	if(alt_urls){
  	if(alt_urls[0]){
  	  urls = alt_urls
  	}else{
  	  alt_urls.shift()
  	  urls = urls.concat(alt_urls)
  	}
  }
	// Other common sites
/*				urls = urls.concat(
						 [["LinkedIn", "http://www.linkedin.com/shareArticle?mini=true&url=__URL__&title=__TITLE__&summary=&source="],
						  ["MySpace", "http://www.myspace.com/Modules/PostTo/Pages/?l=3&u=__URL__&t=__TITLE__&c="],
						  ["Y! Buzz", "http://buzz.yahoo.com/buzz?targetUrl=__URL__&headline=__TITLE__&src="],
						  ["Y! Bookmarks", "http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=__URL__&t=__TITLE__"],
						  ["Mixx", "http://www.mixx.com/submit?page_url=__URL__"],
						  ["Technorati", "http://www.technorati.com/faves?add=__URL__"],
						  ["Slashdot", "http://slashdot.org/bookmark.pl?url=__URL__&title=__TITLE__"]]) */


	// Create div for Share, cycle through to find unused id, just in case
	var sharePanel = document.createElement('div')
	var panelID = 'share-dis'
	for(var i=2; document.getElementById(panelID); i++){
		panelID = 'share-dis-'+i
	}
	sharePanel.id = panelID
	document.body.appendChild(sharePanel)
	sharePanel = j(sharePanel)
	sharePanel.addClass('share-dis')
	sharePanel.css({ 'display':'none',
					'position' : 'absolute',
					'z-index' : '3000' })

	// Create the UL container for links
	var shareUL = j('<ul></ul>').appendTo(sharePanel)

	// Loop through defined URLS, creating links
	for(var i=0; i<urls.length; i++){
		site = urls[i]
		if(!site[0] && !site[1]) return
		// Make new LI with a class of i.e. 'share-msn-live'
		var li = j('<li></li').appendTo(shareUL).addClass('share-'+site[0].toLowerCase().replace(/[^a-z]/g,'-'))

		var a = j('<a href="#" target="_new">'+site[0]+'</a>').appendTo(li)
		if(typeof site[1]=='string'){ // if the second param's a string, set the href
			var u = site[1].replace('__URL__',encodeURIComponent(pageURL)).replace('__TITLE__',encodeURIComponent(pageTitle))
			a.attr('href', u)
		}else if(typeof site[1]=='function'){ // if it's a function, have it called on click, and pass it url and title
		  var funk = site[1]
			a.click(function(e){
				funk(pageURL, pageTitle,e)
			})
		}
	}
	
	// Fix up the offset, now that everything's in place
	if(position){						
		if(typeof position == 'string'){
			if(position.match('top')){
				offset[1] = -sharePanel.outerHeight()
			}else if(position.match('bottom')){
				offset[1] = element.outerHeight()
			}else if(position.match('middle')){
				offset[1] = Math.round(element.outerHeight()/2 - sharePanel.outerHeight()/2)
			}
			if(position.match('left')){
				offset[0] = -sharePanel.outerWidth()
			}else if(position.match('right')){
				offset[0] = element.outerWidth()
			}else if(position.match('center')){
				offset[0] = Math.round(element.outerWidth()/2 - sharePanel.outerWidth()/2)
			}
		}else{
			offset = position
		}
	}
	
	var hide = function(){
		sharePanel.hide()
	}
	var show = function(){
		stopCountdown()
		sharePanel.css({top:element.offset().top+offset[1]+'px', left:element.offset().left+offset[0]+'px'})
		sharePanel.show()
	}
	// On mouseout, wait a second before hiding it, to be spaz-forgiving
	var outCountdown = function(){
		timer = setTimeout(hide, 500)
	}
	var stopCountdown = function(){
		clearTimeout(timer)
	}
	
	element.click(function(e){
		e.preventDefault()
		return false;
	})
	
	element.hover(show, outCountdown)
	sharePanel.hover(show, outCountdown)
	
}