/*  Create the basic Social object, which will contain useful Social Media controls  */
if(typeof Social!="object")Social=new(function(){this._version="0.1"})();


/*  Social.Base - The Base object which all SocialMedia Controls (should) extends, providing the same basic functionality  */
Social.Base = function(){this._window={};};
Social.Base.prototype._createURL = function( oRequest, bOmitEmptyQueryParams )
{
	/*  Anatomy of oRequest:
	 *	oRequest = {
	 *    [protocol:'http'],
	 *    domain:'domain.com',
	 *    [port:80],
	 *    [path:'/'],
	 *    [query:Object],
	 *    [hash:'myhash']
	 *	}
	 */
	if ( typeof oRequest.domain == "string" && oRequest.domain != "" )
	{
		var sQuery  = "";
		if ( typeof oRequest.query == "object" )
			for ( var p in oRequest.query )
				if ( !bOmitEmptyQueryParams || ( oRequest.query[ p ] != "" && oRequest.query[ p ] != null ) )
					sQuery += ( sQuery == "" ? "?" : "&" ) + p + "=" + encodeURIComponent( oRequest.query[ p ] );

		return ( typeof oRequest.protocol == "string" ? oRequest.protocol : "http" ) +
			"://" +
			oRequest.domain + 
			( typeof oRequest.port == "number" && oRequest.port != 80 ? ":" + oRequest.port : "" ) + 
			( typeof oRequest.path == "string" ? oRequest.path : "/" ) +
			sQuery +
			( typeof oRequest.hash == "string" ? "#" + oRequest.hash : "" );
	}
	return false;
};
Social.Base.prototype._getWindowOptions = function()
{
	var sReturn = "";
	if ( typeof this._window == "object" )
	{
		if ( ( typeof this._window.width != "undefined" || typeof this._window.height != "undefined" ) )
		{
			if ( typeof this._window.toolbar == "undefined" )
				this._window.toolbar = 0;
			if ( typeof this._window.status == "undefined" )
				this._window.status = 0;
			if ( typeof this._window.scrollbars == "undefined" )
				this._window.scrollbars = 1;
		}

		for ( var p in this._window )
			sReturn += ( sReturn != "" ? "," : "" ) + p + "=" + this._window[ p ];
	}

	return sReturn;
};
Social.Base.prototype._createCampaignURL = function( sURL, sSource )
{
	if ( typeof Campaign != "undefined" )
	{
		var nQueryStart = sURL.indexOf( "?" );
		var nHashStart  = sURL.indexOf( "#" );
		var oQuery      = {};

		if ( nQueryStart > 0 )
		{
			var aQueryPart = sURL.substr( nQueryStart + 1, nHashStart > 0 ? nHashStart : sURL.length - nQueryStart + 1 ).split( "&" );
			for ( var i = 0; i < aQueryPart.length; ++i )
			{
				var aPair = null;
				if ( ( aPair = aQueryPart[ i ].match( /([a-zA-Z0-9_-]+)=(.*)/ ) ) && aPair && aPair.length > 1 )
					oQuery[ aPair[ 1 ] ] = aPair[ 2 ];
			}
			sURL = sURL.substr( 0, nQueryStart );
		}

		oQuery.cam = Campaign.id;
		oQuery.src = sSource;
		oQuery.med = Campaign.medium || "share";

		var aQuery = [];
		for ( var p in oQuery )
			aQuery.push( p + "=" + oQuery[ p ] )
		sURL += "?" + aQuery.join( "&" );
	}
	return sURL;
};
Social.Base.prototype._open = function( sURL, bRedirect )
{
	if ( !sURL )
		return false;

	if ( !bRedirect )
	{
		var oPopup = window.open( sURL, "sharepopup", this._getWindowOptions() );
		return oPopup == null ? sURL : true;
	}
	return document.location = sURL;
};


/*  Social.Facebook - Facebook interactivities (www.facebook.com)  */
Social.Facebook = function(){};
Social.Facebook.prototype = new Social.Base;
Social.Facebook.prototype.share = function( sTitle, sLink, bRedirect )
{
	if ( !sLink )
		sLink = this._createCampaignURL( document.location.href, "facebook" );
	if ( !sTitle )
		sTitle = document.title;

	this._window.width  = 768;
	this._window.height = 400;

	return this._open( this._createURL( {
		domain:"www.facebook.com",
		path:"/sharer.php",
		query:{
			u:sLink,
			t:sTitle
		}
	}, true ), bRedirect );
};

/*  Social.Twitter - Twitter interactivities (www.twitter.com)  */
Social.Twitter = function(){};
Social.Twitter.prototype = new Social.Base;
Social.Twitter.prototype.share = function( sText, bRedirect )
{
	if ( !sText )
		sText = document.title + " " + this._createCampaignURL( document.location.href, "twitter" );

	this._window = false;

	return this._open( this._createURL( {
		domain:"twitter.com",
		path:"/home/",
		query:{
			status:sText
		}
	}, true ), bRedirect );
};


/*  Social.Hyves - Hyves interactivities (www.hyves.nl)  */
Social.Hyves = function(){};
Social.Hyves.prototype = new Social.Base;
Social.Hyves.prototype.tip = function( sTitle, sText, nRating, bRedirect )
{
	if ( !sTitle )
		sTitle = document.title;
	if ( !sText ) {
		sText = this._createCampaignURL( document.location.href, "hyves" );
		sText = '[url='+sText+']'+sText+'[/url]';
	}

	this._window.width  = 600;
	this._window.height = 500;

	return this._open( this._createURL( {
		domain:"www.hyves-share.nl",
		path:"/button/tip/",
		query:{
			tipcategoryid:12,
			rating:nRating || 5,
			title:sTitle,
			body:sText
		}
	}, true ), bRedirect );
};


/*  Social.Digg - Digg interactivities  */
Social.Digg = function(){};
Social.Digg.prototype = new Social.Base;
Social.Digg.prototype.submit = function( sTitle, sLink, sText, sMedia, sTopic, bSkipThumbnail, bRedirect )
{
	if ( !sTitle )
		sTitle = document.title;
	if ( !sLink )
		sLink = this._createCampaignURL( document.location.href, "digg" );

	return this._open( this._createURL( {
		domain:"digg.com",
		path:"/submit",
		query:{
			url:sLink,
			title:sTitle,
			bodytext:sText,
			media:sMedia,
			topic:sTopic,
			thumbnails:( bSkipThumbnail ? 0 : 1 )
		}
	}, true ), bRedirect );
};


/*  Social.LinkedIN - LinkedIN interactivities  */
Social.LinkedIN = function(){};
Social.LinkedIN.prototype = new Social.Base;
Social.LinkedIN.prototype.share = function( sTitle, sLink, sText, bRedirect )
{
	if ( !sTitle )
		sTitle = document.title;
	if ( !sLink )
		sLink = this._createCampaignURL( document.location.href, "digg" );

	return this._open( this._createURL( {
		domain:"www.linkedin.com",
		path:"/shareArticle",
		query:{
			mini:"true",
			url:sLink,
			title:sTitle,
			summary:sText,
			source:sTitle
		}
	}, true ), bRedirect );
};




/*  Social.Share - Uniform sharing  */
Social.Share = function()
{
	this._createShorthandFunctions();
};
Social.Share.prototype._createShorthandFunctions = function()
{
	window[ ( this._self = "SocialShareObject" ) ] = this;
	for ( var p in this )
		if ( typeof this[ p ] == "function" && p.substr( 0, 1 ) != "_" )
		{
			var sFunction = "share" + p.substr( 0, 1 ).toUpperCase() + p.substr( 1 );
			if ( typeof window[ sFunction ] == "undefined" )
				window[ sFunction ] = this[ p ];
		}
};
Social.Share.prototype.facebook = function( sTitle, sLink ) //  Also available as shareFacebook
{
	if ( !this._facebook )
		this._facebook = new Social.Facebook();
	return this._facebook.share( sTitle, sLink );
};
Social.Share.prototype.twitter = function( sText ) //  Also available as shareTwitter
{
	if ( !this._twitter )
		this._twitter = new Social.Twitter();
	return this._twitter.share( sText );
};
Social.Share.prototype.hyves = function( sTitle, sText, nRating ) //  Also available as shareHyves
{
	if ( !this._hyves )
		this._hyves = new Social.Hyves();
	return this._hyves.tip( sTitle, sText, nRating );
};
Social.Share.prototype.digg = function( sTitle, sLink, sText, sMedia, sTopic, bSkipThumbnail ) //  Also available as shareDigg
{
	if ( !this._digg )
		this._digg = new Social.Digg();
	return this._digg.submit( sTitle, sLink, sText, sMedia, sTopic, bSkipThumbnail );
};
Social.Share.prototype.linkedin = function( sTitle, sLink, sText ) //  Also available as shareLinkedin
{
	if ( !this._linkedin )
		this._linkedin = new Social.LinkedIN();
	return this._linkedin.share( sTitle, sLink, sText );
};

function shareFacebook( nPID )
{
	var oFaceBook = new Social.Facebook();
	return oFaceBook.share( false, oFaceBook._createCampaignURL( "http://www.hihangout.nl/" + ( nPID ? "?pid=" + nPID : "" ), "facebook" ) );
}

function shareTwitter( nPID )
{
	var oTwitter = new Social.Twitter();
	return oTwitter.share( document.title + " " + oTwitter._createCampaignURL( "http://www.hihangout.nl/" + ( nPID ? "?pid=" + nPID : "" ), "twitter" ) );
}

function shareHyves( nPID )
{
	var oHyves = new Social.Hyves();
	var sText  = oHyves._createCampaignURL( "http://www.hihangout.nl/" + ( nPID ? "?pid=" + nPID : "" ), "hyves" );
	sText      = '[url='+sText+']'+sText+'[/url]';
	return oHyves.tip( false, sText );
}


new Social.Share();
