function nl2br (str, is_xhtml) {
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

function strip_tags(html){

	//PROCESS STRING
	if(arguments.length < 3) {
		html=html.replace(/<\/?(?!\!)[^>]*>/gi, '');
	} else {
		var allowed = arguments[1];
		var specified = eval("["+arguments[2]+"]");
		if(allowed){
			var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>';
			html=html.replace(new RegExp(regex, 'gi'), '');
		} else{
			var regex='</?(' + specified.join('|') + ')\b[^>]*>';
			html=html.replace(new RegExp(regex, 'gi'), '');
		}
	}

	//CHANGE NAME TO CLEAN JUST BECAUSE 
	var clean_string = html;

	//RETURN THE CLEAN STRING
	return clean_string;
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function twConnect(blogurl){
	var tw_win = popup(blogurl+"/linker?n=tw","linker");
}

function popup(url,windowTitle){
	var width = 600;
	var height = 500;
	var left = (screen.width - width)/2;
	var top = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=yes';
	params += ', menubar=no';
	params += ', resizable=no';
	params += ', scrollbars=yes';
	params += ', status=no';
	params += ', toolbar=no';
	return window.open(url,windowTitle,params);
}

function launch_site(blogurl){
    if (window.siteWindow != null && !window.siteWindow.closed) { // if window is still around
		if (confirm("Your editor window is still open.  Re-authenticate web site editor?\n(If you are currently working in that window any data may be lost.)")) {
			// if we close and reopen it, then we should be able to force focus even if browser restricts focus()
			window.siteWindow.close();
			window.siteWindow = window.open(blogurl+"/launcher?l=ws",'ws_Launcher');
		}
    } else {
		window.siteWindow = window.open(blogurl+"/launcher?l=ws",'ws_Launcher');
    }
}

function launch_allc(blogurl){
    if (window.acWindow != null && !window.acWindow.closed) { // if window is still around
		if (confirm("Your AllClients window is still open.  Re-authenticate AllClients?\n(If you are currently working in that window any data may be lost.)")) {
			// if we close and reopen it, then we should be able to force focus even if browser restricts focus()
			window.acWindow.close();
			window.acWindow = window.open(blogurl+"/launcher?l=ac",'ac_Launcher');
		}
    } else {
		window.acWindow = window.open(blogurl+"/launcher?l=ac",'ac_Launcher');
    }
}

function launch_twitter(screenname){
    if (window.twWindow != null && !window.twWindow.closed) { // if window is still around
		// if we close and reopen it, then we should be able to force focus even if browser restricts focus()
		window.twWindow.close();
		window.twWindow = window.open("https://twitter.com/#!/"+screenname,'tw_Launcher');
    } else {
		window.twWindow = window.open("https://twitter.com/#!/"+screenname,'tw_Launcher');
    }
}

function launch_fb(){
    if (window.fbWindow != null && !window.fbWindow.closed) { // if window is still around
		// if we close and reopen it, then we should be able to force focus even if browser restricts focus()
		window.fbWindow.close();
		window.fbWindow = window.open("http://facebook.com/",'fb_Launcher');
    } else {
		window.fbWindow = window.open("http://facebook.com/",'fb_Launcher');
    }
}

function fbConnect(){
    FB.login(function(response) {
		if (response.authResponse) {
			FB.api("/me", function(response2){}); // not sure we need this
			jQuery('#FB_id').val(response.authResponse.userID);
			jQuery('#FB_token').val(response.authResponse.accessToken);
			jQuery('#fbConnect').submit();
		};
    }, {scope:'read_stream,publish_stream,manage_pages,read_insights,offline_access'}); // oAuth 2.0
}

function showCharCount(testId,resultId) {
	var test = jQuery('#'+testId);
	var result = jQuery('#'+resultId);
	var tWarning = jQuery('#tWarning');
	var tw_incl = jQuery('#tw_incl');

	var len = test.val().length;
	if (len <= 140) {
		result.html("Length: <span id=\"msgCounter\">"+len+"</span>");
		tWarning.html("");
	} else {
		result.html("Length: <span id=\"msgCounter\" class='overTwitter'>"+len+"</span>");
		if (tw_incl.attr('checked')) {
			tWarning.html("Message will be shortened for Twitter.");
		}
		else {
			tWarning.html("");
		}
	}
}

// usage:  window.top.prettyPhotoClose() from within an iframe opened in prettyPhoto
function prettyPhotoClose() {
	$.prettyPhoto.close();
}

function deleteScheduledPost(id) {
	if (confirm("Delete the selected post?")){
		window.location="/dashboard?deletePost="+id;	
	}
}

jQuery(document).ready(function($) {

	//tabs
	jQuery('#right_admin .tab_content').hide();
	jQuery('#right_admin .tab_content:first').show();
	jQuery('#right_admin ul.tab-nav li:first').addClass('active-tab');

	jQuery('#right_admin ul.tab-nav li a').click(function(){
		jQuery('#right_admin ul.tab-nav li').removeClass('active-tab');
		jQuery(this).parent().addClass('active-tab');
		var currentTab = jQuery(this).attr('href');
		jQuery('#right_admin .tab_content').hide();
		jQuery(currentTab).show();
		return false;
	});//end of tabs
	
		
	//Hide (Collapse) the toggle containers on load
	jQuery('.toggle_content').hide();
	jQuery('.toggle_content.openbox').show();
	//change the text on the span for clicking
	jQuery('.toggle').click(function() {
 		if (jQuery('em').is('.minus16')) {
    			jQuery('em').addClass('plus16').removeClass('minus16');
		} else {
    			jQuery('em').addClass('minus16').removeClass('plus16');
		}
	});	
	//Slide up and down on click
	jQuery('.toggle').click(function(){
		jQuery('.toggle_content').slideToggle(400);
	});
	
	// do your checks of the radio buttons here and show/hide what you want to
  	jQuery('.toggle_scheduler').hide();
  	jQuery('input:radio[name="option_post"]').filter('[value="postToday"]').attr('checked', true);
  	if (jQuery("#postSchedule:checked").length > 0){ jQuery('.toggle_scheduler').show(); }

  	// add functionality for the onclicks here
  	jQuery("#postSchedule").click(function() {
    		jQuery('.toggle_scheduler').fadeIn();
  	});

  	jQuery('#postToday').click(function() {
    		jQuery('.toggle_scheduler').fadeOut();
  	});
  	
  	jQuery('a.newsIdeas').click(function() {
  		jQuery('#myStrategy, #WeeklyStats, #LaunchPad, #Twitter, #fbStats').hide(400);
  		jQuery('#newsHeadlines').show(400);
  		
  	});
  	jQuery('#newsHeadlines .headlineClose').click(function(){
  		jQuery('#newsHeadlines').hide(400);
  		jQuery('#myStrategy, #WeeklyStats, #LaunchPad, #Twitter, #fbStats').show(400);
  	});
  	


}); //thats all for doc ready

