/* 
Share.js Version 1.5.1

EXAMPLE
1) set these
	share.url = "";
	share.title = "";
	share.description = "";
	share.tweet = "";
2) call them like this
	share.send("twitter");
	share.send("facebook");
	...
3) set custom setting for site
	share.digg.title = "";
*/

Share = function(){
	/*Private*/


	/*Public / Privileged*/
	return {
		// required
		url: null,
		title: null,
		tracking: false,
		tracking_link: null,
		
		// optional
		description: null,
		property: null,
		unique_tracking_id: '_button',
		
		// specific
		tweet: null,
		
		// custom settings
		delicious: {},
		digg: {},
		facebook: {},
		myspace: {},
		reddit: {},
		stumbleupon: {},
		twitter: {},

		getLink: function(site){
			this.tracking_link = null;
			var link = null
			switch (site) {
				case 'delicious':
					// check for custom settings
					var url = typeof this.delicious.url != 'undefined' ? this.delicious.url : this.url;
					var title = typeof this.delicious.title != 'undefined' ? this.delicious.title : this.title;
					var description = typeof this.delicious.description != 'undefined' ? this.delicious.description : this.description;
					this.tracking_link = 'http://delicious.com';

					if (url && title) {
						link = 'http://delicious.com/save?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&notes=' + encodeURIComponent(description);
					}
					break;
				case 'digg':
					// check for custom settings
					var url = typeof this.digg.url != 'undefined' ? this.digg.url : this.url;
					var title = typeof this.digg.title != 'undefined' ? this.digg.title : this.title;
					this.tracking_link = 'http://digg.com';

					if (url && title) {
						link = 'http://digg.com/submit?phase=2&partner=[partner]&url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
					}
					break;
				case 'facebook':
					// check for custom settings
					var url = typeof this.facebook.url != 'undefined' ? this.facebook.url : this.url;
					var title = typeof this.facebook.title != 'undefined' ? this.facebook.title : this.title;
					this.tracking_link = 'http://www.facebook.com/sharer.php';
					
					if (url) {
						link = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title);
					}
					break;
				case 'myspace':
					// check for custom settings
					var url = typeof this.myspace.url != 'undefined' ? this.myspace.url : this.url;
					var title = typeof this.myspace.title != 'undefined' ? this.myspace.title : this.title;
					var description = typeof this.myspace.description != 'undefined' ? this.myspace.description : this.description;
					this.tracking_link = 'http://www.myspace.com';
					
					if (url && title) {
						link = 'http://www.myspace.com/Modules/PostTo/Pages/?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title) + '&c=' + encodeURIComponent(description);
					}
					break;
				case 'reddit':
					// check for custom settings
					var url = typeof this.reddit.url != 'undefined' ? this.reddit.url : this.url;
					var title = typeof this.reddit.title != 'undefined' ? this.reddit.title : this.title;
					this.tracking_link = 'http://www.reddit.com';
					
					if (url && title) {
						link = 'http://www.reddit.com/r/reddit.com/submit?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
					}
					break;
				case 'stumbleupon':
					// check for custom settings
					var url = typeof this.stumbleupon.url != 'undefined' ? this.stumbleupon.url : this.url;
					var title = typeof this.stumbleupon.title != 'undefined' ? this.stumbleupon.title : this.title;
					this.tracking_link = 'http://www.stumbleupon.com';
					
					if (url && title) {
						link = 'http://www.stumbleupon.com/submit?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
					}
					break;
				case 'twitter':
					this.tracking_link = 'http://twitter.com';
					if (this.tweet) {
						link = 'http://twitter.com/home?status=' + encodeURIComponent(this.tweet);
					}
					break;
			}

			return link;
		},
		
		send: function (site)
		{

			var link = this.getLink(site);

			if (this.tracking && typeof s == "object" && this.tracking_link) {
				var property = this.property?this.property+'_':'';//s.prop4.substring(s.prop4.lastIndexOf(':') + 1);
				sCode.trackOutboundClick(this.tracking_link, property + 'share' + site + this.unique_tracking_id);
			}

			if (link) 
				if(!window.open(link))
					alert('Please disable your popup blocker and try again.');
		},
		setUrl: function(url){
			this.url = url;
		},
		setTweet: function(str){
			this.tweet = str;
		},
		setTitle: function(str){
			this.title = str;
		},
		setDescription: function(str){
			this.description = str;
		},
		debugVars: function(){
			if(console){
				console.log('url:'+this.url);
				console.log('title:'+this.title);
				console.log('tracking:'+this.tracking);
				console.log('tracking_link:'+this.tracking_link);
				console.log('description:'+this.description);
				console.log('property:'+this.property);
				console.log('unique_tracking_id:'+this.unique_tracking_id);
				console.log('tweet:'+this.tweet);
				console.log('delicious:');
				console.log(this.delicious);
				console.log('digg:');
				console.log(this.digg);
				console.log('facebook:');
				console.log(this.facebook);
				console.log('myspace:');
				console.log(this.myspace);
				console.log('reddit:');
				console.log(this.reddit);
				console.log('stumbleupon:');
				console.log(this.stumbleupon);
				console.log('twitter:');
				console.log(this.twitter);
			}
		}
	};

}

var share = new Share();