
// start SocNetLinks
var SocNetLinks = Class.create ({
	initialize: function(lnkFacebook, lnkTwitter)
	{
		this.lnkFacebook = lnkFacebook;
		this.lnkTwitter = lnkTwitter;

		this.currentURL = window.location.href.replace(window.location.hash,'');
		//console.log(this.currentURL);

		this.fbShareLnk = 'share.php?u=';
		this.twHomeLnk = 'home?status=';
		this.twContent = this.lnkTwitter.rel.replace(/ /g, '%20') + '%20';

        this.SetFacebookLink();

        this.SetTwitterLink();

	},
	SetFacebookLink: function()
	{
		this.lnkFacebook.newURL = this.lnkFacebook.href + this.fbShareLnk + this.currentURL;
		this.lnkFacebook.href = this.lnkFacebook.newURL;
	},
	SetTwitterLink: function()
	{
		this.lnkTwitter.newURL = this.lnkTwitter.href + this.twHomeLnk + this.twContent;// + this.currentURL;

        BitlyCB.shortenResponse = function(data)
        {
                //var s = '';
                var first_result;
                // Results are keyed by longUrl, so we need to grab the first one.
                for (var r in data.results)
                {
					first_result = data.results[r]; break;
                }
                //for (var key in first_result)
                //{
				//	s += key + ":" + first_result[key].toString() + "\n";
                //}
                //console.log(first_result.shortUrl);
                this.lnkTwitter.newURL += first_result.shortUrl;
                this.lnkTwitter.href = this.lnkTwitter.newURL;
        }.bind(this);
        BitlyClient.shorten(this.currentURL, 'BitlyCB.shortenResponse');

	}
});
// end SocNetLinks

// start VideoSocNetLinks
var VideoSocNetLinks = Class.create ({
	initialize: function(lnksFacebook, lnksTwitter)
	{
		this.lnksFacebook = lnksFacebook;
		this.lnksTwitter = lnksTwitter;

		this.currentURL = window.location.href.replace(window.location.hash,'');

		this.length = this.lnksFacebook.size();
		this.currentVidURL = new Array;
        for (var i=0; i<this.length; i++)
        {
            this.currentVidURL[i] = this.currentURL + '#video' + i;
        }

		this.fbShareLnk = 'share.php?u=';
		this.twHomeLnk = 'home?status=';

		this.lnksFacebook.each(function(el, i) {
			this.SetFacebookLinks(i);
		}, this);

		this.lnksTwitter.each(function(el, i) {
			this.SetTwitterLinks(i);
		}, this);

	},
	SetFacebookLinks: function(i)
	{
		this.lnksFacebook[i].newURL = this.lnksFacebook[i] + this.fbShareLnk + this.currentVidURL[i];
		this.lnksFacebook[i].href = this.lnksFacebook[i].newURL;
	},
	SetTwitterLinks: function(i)
	{

		var twContent = this.lnksTwitter[i].rel.replace(/ /g, '+') + '+';

		// build the new twitter link
		this.lnksTwitter[i].buildNewURL = this.lnksTwitter[i].href + this.twHomeLnk + twContent;// + this.currentURL;

		BitlyCB.shortenResponses = function(data)
		{
				var first_result;
				for (var r in data.results)
				{
					var longURL = r;
					first_result = data.results[r]; break;
				}
				//console.log(longURL + ' : ' + first_result.shortUrl); //it works!!!
				var bcbIndex = longURL.split('#video')[1];
				this.lnksTwitter[bcbIndex].buildNewURL += first_result.shortUrl;
				this.lnksTwitter[bcbIndex].href = this.lnksTwitter[bcbIndex].buildNewURL;
		}.bind(this);
		BitlyClient.shorten(this.currentVidURL[i], 'BitlyCB.shortenResponses');

	}
});
// end VideoSocNetLinks

