var BitlyAPI = {
	oConfig: {},							//a config object that needs to be passed in 
	shorten: "http://api.bit.ly/shorten",	//URL for shortening	
	expand: "http://api.bit.ly/expand",		//URL for expand
	stats: "http://api.bit.ly/stats",		//URL for stats	
	info: "http://api.bit.ly/info",			//URL for getting INFO
	setConfig: function(a_oConfig){
		this.oConfig = a_oConfig;
	},
	
	/**
	 * This is a function that will be called and given a url to shrink and a 
	 * function to call when the url has been shrunk.
	 **/
	shortenURL: function (a_urlArr,a_callBack){
		var useProxy = false;
		
		if(!this.oConfig){
			a_callback({errorCode:"MS_1",errorMessage:"No Config Object set"});
			return;
		}
		
		if(this.oConfig.proxy && this.oConfig.proxy !=""){
			for(var i=0; i < a_urlArr.length; i++){
				a_urlArr[i] = this.oConfig.proxy + escape(a_urlArr[i]);
			}
			useProxy = true;
		}
		
		var params=[];
		for(var i=0; i < a_urlArr.length; i++){
			params.push("longUrl="+ a_urlArr[i]);
		}
		this.buildURL(this.shorten, params.join("&"), a_callBack, useProxy);
	},
	
	/**
	 * This is a function that will be called and given a url to expand
	 **/
	expandURL: function (a_url,a_callBack){
		if(!this.oConfig){
			a_callback({errorCode:"MS_1",errorMessage:"No Config Object set"});
			return;
		}
		this.buildURL(this.expand, "shortUrl="+a_url, a_callBack);
	},
	
	/**
	 * give stats from URL and then call callBack
	 **/
	getStats: function (a_url,a_callBack){
		if(!this.oConfig){
			a_callback({errorCode:"MS_1",errorMessage:"No Config Object set"});
			return;
		}
		this.buildURL(this.stats,  "shortUrl="+a_url, a_callBack);
	},
	
	/**
	 * give info from URL and then call callBack
	 **/
	getInfo: function (a_url,a_callBack){
		if(!this.oConfig){
			a_callback({errorCode:"MS_1",errorMessage:"No Config Object set"});
			return;
		}
		this.buildURL(this.info,  "shortUrl="+a_url, a_callBack);
	},
	
	/**
	 * This function will only be called internally, it will make the actual 
	 * transaction between us and bitly
	 **/
	buildURL: function(a_apiURL, a_sParams, a_callBack, a_useProxy){
		var params = [];
		for(var k in this.oConfig.bitlyConfig)
			params.push(k+"="+ this.oConfig.bitlyConfig[k]);
		params.push(a_sParams);
			
		var sParams = "?" + params.join("&") + "&callback=?";
		
		$.getJSON(a_apiURL+	sParams, a_callBack);
	}
}

/**
 * this is the class you will probably interact with, it interfaces with the  
 * bitly api when needed 
 **/
var BitlyHelper = {
	callBack: function(){},
	bIsBeingUsed: false,
	findLinkReg: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i,
	/**
	 * feed this a string and a callback, then let the rest of the function
	 * do its thing.  It will find and replace all the links you could need
	 **/
	findAndReplaceLinks: function(a_str, a_callBack){
		BitlyHelper.bIsBeingUsed = true;
		BitlyHelper.callBack = a_callBack;
		BitlyHelper.getURL(a_str);
	},
	
	/**
	 * Feed this some text, and a callback.  it will find the URLs greater than 
	 * 20, and that aren't bit.ly or j.mp convert them and re build the string, 
	 * then return it to the callback
	 **/ 
	getURL: function (a_text) {
		var findLink = BitlyHelper.findLinkReg;
		var tempArr = a_text.split(" ");
		var links = [];
		var cleanURLArr = [];
		for(var i =0; i < tempArr.length; i++){
			if(tempArr[i].search(findLink) >= 0){
				if(tempArr[i].indexOf("bit.ly") == -1 && tempArr[i].indexOf("j.mp") == -1 && tempArr[i].length > 20){
					cleanURLArr = cleanURL(tempArr[i])
					links.push(cleanURLArr[0]);
				}
				else{
					continue;
				} 
			}
		}
		if(links.length > 0)
			BitlyAPI.shortenURL(links,function(a_json){BitlyHelper.handleShortenURLs(a_text, a_json)});
		else{
			BitlyHelper.callBack(a_text);
			bIsBeingUsed = false;
			BitlyHelper.callBack = null;
		}
	},
	/**
	 * take the shorten URLs and re build the original string, then call the 
	 * callback and get mark this bitly helper as being done
	 **/
	handleShortenURLs: function(a_text, a_json){
		if(a_json.errorCode != 0)
			return {errorCode: a_json.errorCode, errorMessage: a_json.errorMessage};
		
		var findLink= BitlyHelper.findLinkReg;
		var tempArr = a_text.split(" ");
		var cleanURLArr= [];
		//go thorugh all the words in the sentence
		for(var i =0; i < tempArr.length; i++){
			//make sure its a link
			if(tempArr[i].search(findLink) >= 0){
				//make sure its a URL we think needs to be shrunk 
				if(tempArr[i].indexOf("bit.ly") == -1 && tempArr[i].indexOf("j.mp") == -1 && tempArr[i].length > 20){
					for(var k in a_json.results){
						cleanURLArr = cleanURL(tempArr[i])
						if(k == cleanURLArr[0]){
							tempArr[i] = a_json.results[k].shortUrl + cleanURLArr[1];
						}
					}
				}
				else{
					continue;
				} 
			}
		}

		BitlyHelper.callBack(tempArr.join(' '));
		bIsBeingUsed = false;
		BitlyHelper.callBack = null;
	},
	/**
	 * feed this a NON html string, and it will find the http:// links and 
	 * replace with a html link
	 **/ 
	convertMessageToHTMLWithLinks: function(a_text){
		var tempArr = a_text.split(" "); 
		for(var i=0; i < tempArr.length; i++){
			tempArr[i] = tempArr[i].replace(BitlyHelper.findLinkReg, '<a href="$1">$1</a>');
		}
		return tempArr.join(" "); 
	},
	f:function(){}
}