/*

Plugin Name: Twit It Up!
Version: 1.0.1
Plugin URI: http://www.jonbergan.com/twit-it-up/
Description: Twit It Up is a simple WordPress plugin that allows your visitors to tweet/retweet your blog posts quickly and easily directly from your blog.
Author: Jon Bergan
Author URI: http://www.jonbergan.com/

Copyright 2009 Jon Bergan (jon@jonbergan.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

function tiu_tweet_this()
{
	var errors = ''

	if (document.getElementById('ttUsername'+document.getElementById('ttPostId').value).value == '')
		errors = errors + '* You did not specify your Twitter username!\n';

	if (document.getElementById('ttPassword'+document.getElementById('ttPostId').value).value == '')
		errors = errors + '* You did not specify your Twitter password!\n';

	if (document.getElementById('ttTweet'+document.getElementById('ttPostId').value).value == '')
		errors = errors + '* You did not specify your tweet message!\n';

	if (errors != '')
	{
		errors = 'You cannot tweet this blog post unless you complete all of the required fields!\n\nPlease correct the following errors and try again:\n\n' + errors;
		alert(errors);
	} else {
		getContent('tiuForm'+document.getElementById('ttPostId').value, 'tiuLoader'+document.getElementById('ttPostId').value, wp_plugin_url+'/twit-it-up/twit-it-up-send.php?id='+document.getElementById('ttPostId').value+'&user='+document.getElementById('ttUsername'+document.getElementById('ttPostId').value).value+'&pass='+document.getElementById('ttPassword'+document.getElementById('ttPostId').value).value+'&msgbody='+document.getElementById('ttTweet'+document.getElementById('ttPostId').value).value);
	}
}

function tiu_display_login_form(id, msgbody)
{
	getContent('tiuForm'+id, 'tiuLoader'+id, wp_plugin_url+'/twit-it-up/twit-it-up.tem.php?id='+id+'&msgbody='+msgbody);
}

function newAjaxObject()
{
	var ajaxObj = null;
	var strName;
	
	try 
	{

		ajaxObj = new XMLHttpRequest();

	} catch (stupidBrowserException) {

		if (navigator.userAgent.indexOf("MSIE")>=0)
		{ 
			strName = "Msxml2.XMLHTTP";
			if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
				strName="MSXML2.ServerXMLHTTP.4.0"

			try { 
				ajaxObj = new ActiveXObject(strName);
			} catch(e) { 
				alert("Error: Scripting for ActiveX might be disabled - Please enable and try again!"); 
			} 
		} else {
			alert("Error: You need to use an AJAX supported browser to use this application!\n\nPlease upgrade your browser to the latest version.");
		}
	}

	return ajaxObj;
}

function getContent(ajaxID, ajaxLoaderID, ajaxURL)
{
	aLoadInto = document.getElementById(ajaxID);
	aLoader = document.getElementById(ajaxLoaderID);
	
    aLoadInto.className = 'ajaxDivHide';

	aLoader.innerHTML = '<p><span><img src="'+wp_plugin_url+'/twit-it-up/twit-it-up-ajax-loader.gif" alt="Loading" /> Loading ... One Moment Please!</span></p>';
    aLoader.className = 'ajaxDivShow';

    var ajaxObj = newAjaxObject();
    ajaxObj.onreadystatechange = function() { handleExtraContent(ajaxID, ajaxLoaderID, ajaxObj); };
    ajaxObj.open("GET", ajaxURL, true);
    ajaxObj.send(null);
}

function handleExtraContent(ajaxID, ajaxLoaderID, ajaxObj)
{
	if ((ajaxObj.readyState == 4) && (ajaxObj.status == 200))
	{
		aLoadInto = document.getElementById(ajaxID);
		aLoader = document.getElementById(ajaxLoaderID);
	
		aLoader.innerHTML = '';
		aLoader.className = 'ajaxDivHide';

 	 	aLoadInto.className = 'tiuForm';
		aLoadInto.innerHTML = ajaxObj.responseText;
		
		// Small hack to stop IE from displaying empty div's
		if (navigator.userAgent.indexOf("MSIE")>=0)
			if (aLoadInto.innerHTML.length == 0)
				aLoadInto.style.height = "0px";
	}
}
