﻿/* Javascript to convert a long URL to a short URl using the bitly API. the shorten() method is part of the bitly API. 
*  The BiltyCB and BitlyClient are objects given by the bitly API.  
*/
var word = '';
var ret = '';

//BitlyCB.shortenResponse = function (data) {
//    try {
//        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) {
//            if (key = 'shortUrl')
//                s = first_result[key].toString() + "\n";
//        }
//        //alert(s);
//        var date = new Date();
//        var curDate = null;

//        do { curDate = new Date(); }
//        while (curDate - date < 4000);
//        word = s;
//        
//        //return s;
//    }
//    catch (Error) { alert('err'); }

//    ret += s + ' ';
//    return 'dfgdfg';
//}


//BitlyCB.expandResponse = function (data) {
//    try {
//        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 += first_result[key].toString() + "\n";
//        }
//        return s;
//    }
//    catch (Error) { return 'error'; }
//}


if (profileInfo.UseBitly) {
    BitlyCB.myShortenCallback = function (data) {
        var shortURL;
        var first_result;
        for (var r in data.results) {
            first_result = data.results[r];
            first_result['longUrl'] = r;
            break;
        }
        shortURL = first_result['shortUrl'];
        longURL = first_result['longUrl'];

        var d = shortURL;
        if (d != undefined) {
            var nextchar = '';
            var indx = $('txtBodyHTML5').get('value').indexOf(longURL);
            if (indx > -1)
                nextchar = $('txtBodyHTML5').get('value').substr(indx + longURL.length, 1);
            if (nextchar == ' ')
                $('txtBodyHTML5').value = ($('txtBodyHTML5').get('value')).replace(longURL, shortURL);
            indx = $('txtBodyHTML5').get('value').indexOf(longURL.replace('http://', ''));
            if (indx > -1)
                nextchar = $('txtBodyHTML5').get('value').substr(indx + longURL.replace('http://', '').length, 1);
            if (nextchar == ' ')
                $('txtBodyHTML5').value = ($('txtBodyHTML5').get('value')).replace(longURL.replace('http://', ''), shortURL);
        }

    }

    //function to trim the white spaces before & after.    
    String.prototype.trim = function () {
        return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
    }
    //function to check the end of the string for a given suffix.
    String.prototype.endsWith = function (str) {
        return (this.match(str + "$") == str)
    }
    //function to check the start of the string for a given preffix.
    String.prototype.startsWith = function (str) {
        return (this.match("^" + str) == str)
    }
    var jbitly = {
        shortURL: function () {
            word = '';
            ret = '';
            var regexp = new RegExp(/(?:(http|https|ftp):\/\/)?(?:[\w-]+\.)+[a-z]+/i)
            var temp = $('lala').get('html');
            var ret = "";

            var url = temp.trim();
            var text = new Array();
            text = url.toString().split(' ');

            for (var i = 0; i < text.length; i++) {
                //var bitLyString = "http://bit.ly/";
                var checktext = text[i].toString().toLowerCase().search(regexp);
                if (checktext != -1) {
                    BitlyClient.shorten(text[i], 'BitlyCB.shortenResponse');

                    wait(4000);

                }
                else
                    ret += text[i] + ' ';
            }

            $('lala').set('value', ret);
            return word;
        }
    }
}
