jQuery(document).ready(function(){
	if(popLinks){
		jQuery("#fpc-content a").each(function(){
			if(
				fpcCheckLinkAttributes(jQuery(this)) &&
				fpcCheckLinkToAnchor(jQuery(this)) &&
				fpcCheckLinkJQuery(jQuery(this))
			){
				jQuery(this).attr("target","_blank");
			}
		});
	}
	if(popForms){
		jQuery("#fpc-content form").each(function(){
			if(
				jQuery(this).attr("action") != undefined &&
				jQuery(this).attr("target") == undefined
			){
				jQuery(this).attr("target","_blank");
			}
		});
	}
});

function fpcCheckLinkAttributes(jQueryl){
	return !(
		jQueryl.attr("target") != undefined ||
		jQueryl.attr("onclick") != undefined ||
		jQueryl.attr("mousedown") != undefined ||
		jQueryl.attr("mouseup") != undefined ||
		jQueryl.attr("href") == undefined
	);
}

function fpcCheckLinkToAnchor(jQueryl){
	return !(
		jQueryl.attr("href").indexOf("#") != -1 ||
		jQueryl.attr("href").indexOf("javascript") != -1
	);
}

function fpcCheckLinkJQuery(jQueryl){
	return !(jQueryl.data("events") != undefined);
}

function fbKarmaPost(url,postn,pic,desc,dest,targ,karmaType){
	karmaType = (karmaType == null)? 'feed' : karmaType;
	FB.ui(
		{
			method: karmaType,
			app_id  : appId,
			link: url,
			description: desc,
			name: postn,
			picture: pic,
			caption: ''
		},
		function(response) {
			if (response && response.post_id){
				switch(targ){
					case '':
						window.location = dest;
						break;
					case '_top':
						top.location.href = dest;
						break;
					default:
						document.location = dest;
						break;
				}
			} else {
				// don't do anything.
			}
		}
	);
}

function fpcGetTweets(container,twsn,twcount,showMeta,cname){
	if(!window.twttr){
		jQuery.getScript('https://platform.twitter.com/widgets.js');
	}
	var usrImg = '';
	var usrName = '';
	var usrSName = '';
	jQuery.ajax({
		url: 'https://api.twitter.com/1/statuses/user_timeline.json',
		data: 'screen_name='+twsn+'&count='+twcount+'&include_entities=1&include_rts=1',
		dataType:'jsonp',
		statusCode: {
			400: function(json) {
				jQuery('#'+container).html('Unable to retrieve tweets for user '+twsn+' at this time.<br>'+json[0].error);
			},
			404: function(json) {
				jQuery('#'+container).html('Unable to retrieve tweets for user '+twsn+' at this time.<br>'+json[0].error);
			},
			502: function(json) {
				jQuery('#'+container).html('Unable to retrieve tweets for user '+twsn+' at this time.<br>'+json[0].error);
			},
			503: function(json) {
				jQuery('#'+container).html('Unable to retrieve tweets for user '+twsn+' at this time.<br>'+json[0].error);
			}
		},
		success: function(json){
			var tweetList = '';
			tweetList += '<ul class="fpc-tweets">';
			for(tc = 0; tc < json.length; tc++){
				tweet = json[tc];
				tweetList += '<li class="fpc-tweet id-'+tweet.id_str+'">'+fpcFormatTweet(tweet,showMeta)+'</li>';
				usrImg = tweet.user.profile_image_url_https;
				usrName =  tweet.user.name;
				usrSName = tweet.user.screen_name;
			}
			tweetList += '<ul class="fpc-tweets">';
			jQuery('#'+container).html(tweetList);
			tweetHdr  = '<div class="fpc-tweets-header">';
			tweetHdr += '<a href="http://twitter.com/intent/user?screen_name='+usrSName+'" class="fpc-tweets-usr-link" target="_blank">';
			tweetHdr += '<img src="'+usrImg+'" class="fpc-tweets-usr-img"></a>';
			tweetHdr += '<a href="http://twitter.com/intent/user?screen_name='+usrSName+'" class="fpc-tweets-usr-link" target="_blank">';
			tweetHdr += usrSName + '</a> ';
			tweetHdr += usrName;
			tweetHdr += '</div>';
			jQuery('#'+container+' .fpc-tweets-header').remove();
			jQuery('#'+container).prepend(tweetHdr);
			if(cname != null){
				jQuery('#'+container).addClass(cname);
			}
			jQuery('#'+container+' .fpc-tweets li:odd').addClass('fpc-tweet-odd');
			jQuery('#'+container+' .fpc-tweets li:even').addClass('fpc-tweet-even');
		}
	});
}

function fpcFormatTweet(t,showMeta){
	var txt = t.text;
	var url_regex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	var usr_regex = /@(\w+)/gi;
	var hash_regex = /(?:^| )[\#]+([\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0600-\u06ff]+)/gi;
	txt = txt.replace(url_regex,'<a href="$1" target="_blank">$1</a>');
	txt = txt.replace(usr_regex,'@<a href="http://www.twitter.com/$1" target="_blank">$1</a>');
	txt = txt.replace(hash_regex,' <a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
	if(showMeta){
		txt += '<div class="fpc-tweet-meta">';
		txt += '<span class="fpc-tweetDate" href="#">'+fpcFormatTweetTime(t.created_at)+'</span>';
		txt += '<a class="fpc-tweetReply" title="Reply" href="https://twitter.com/intent/tweet?in_reply_to='+t.id_str+'">Reply</a>';
		txt += '<a class="fpc-tweetRetweet" title="Retweet" href="https://twitter.com/intent/retweet?tweet_id='+t.id_str+'">Retweet</a>';
		txt += '<a class="fpc-tweetFave" title="Favorite" href="https://twitter.com/intent/favorite?tweet_id='+t.id_str+'">Favorite</a>';
		txt += '</div><div style="clear:both;"></div>';
	}
	return txt;
}

function fpcFormatTweetTime(t){
	t = t.split(' ');
	/*
	tmm = t[1];
	tdd = t[2];
	tyy = t[5];
	tt  = t[3].split(':');
	ampm = (tt[0] < 12)? "am" : "pm";
	thr = (tt[0] > 12)? tt[0]-12 : tt[0];
	return tmm + ' ' + tdd + ', ' + tyy + ' ' + thr + ':' + tt[1] + ampm;
	*/
	return t[0]+' '+t[1]+' '+t[2];
}
