var accordionCurrent = 'vote';
var tweetMessageSet = false;

function accordionRevert()
{
	if( accordionCurrent.length > 1 )
	{
		resetAccordion();
		accordionSwap('vote', 1);
	}
}

function accordionSwap(value, skipScrollBrowser)
{
	if(typeof skipScrollBrowser == "undefined") {
		skipScrollBrowser = 0;
	}


	// Change button image to gradient version
	accordionStaticImage(value);

	// If new button is clicked, hide previous block and revert image.
	if( (accordionCurrent.length > 1) && (value != accordionCurrent) )
	{
		document.getElementById('accordion_widget_' + accordionCurrent).style.display = 'none';
		accordionRevertImage(accordionCurrent);
	}

	// Don't scroll browser on image flip
	if( skipScrollBrowser != 1 )
		accordionScrollBrowser(); // Scroll boxes into view

	// Set new block to visible
	document.getElementById('accordion_widget_' + value).style.display = 'block';

	accordionCurrent = value;

	if( value == 'embed' )
		swapEmbed();
	else if( value == 'share' )
	{
		setTimeout("changeShare();", 200);
		captcha_swap('ftaf_captcha2');
	}
	else if( value == 'forward' )
		captcha_swap('ftaf_captcha');

}

function accordionStaticImage(value)
{
	// With gradient background.
	if( value == 'twitter' )
		document.getElementById('accordion_button_' + value).className = 'accordion_buttons_active_blue';
	else
		document.getElementById('accordion_button_' + value).className = 'accordion_buttons_active';

	// Change button action
	if( value == 'share' )
		document.getElementById('accordion_button_' + value).innerHTML = "<a href=\"javascript:hideTwitter(); hideEmail();\"><img src='" + ak_url + "img/btn_player2_" + value + "3.jpg' border='0' /></a>&nbsp;";
	else
		document.getElementById('accordion_button_' + value).innerHTML = "<a href=\"javascript:;\"><img src='" + ak_url + "img/btn_player2_" + value + "3.jpg' border='0' /></a>&nbsp;";
}

function accordionRevertImage(value)
{
	document.getElementById('accordion_button_' + value).className = 'accordion_buttons_normal';
	document.getElementById('accordion_button_' + value).innerHTML = "<a href=\"javascript:accordionSwap('" + value + "');\"><img src='" + ak_url + "img/btn_player2_" + value + ".jpg' border='0' onmouseover=\"this.src='" + ak_url + "img/btn_player2_" + value + "2.jpg';\" onmouseout=\"this.src='" + ak_url + "img/btn_player2_" + value + ".jpg';\" /></a>&nbsp;";
}

function countFtafNumChars(argAppend) {
        var numChars = document.getElementById('ftaf_mynote' + argAppend).value.length;
        if (numChars >= 1000) {
                document.getElementById('ftaf_mynote' + argAppend).value =  document.getElementById('ftaf_mynote' + argAppend).value.substring(0, 1000);
                document.getElementById('forward_count' + argAppend).innerHTML = '1000';
        }
        else {
                document.getElementById('forward_count' + argAppend).innerHTML = numChars;
        }
}

// Append variable added to support two sections for the forward boxes.
function ajaxFtaf(argAppend)
{
	var myname = document.getElementById('ftaf_myname' + argAppend).value;
	var myaddr = document.getElementById('ftaf_myaddr' + argAppend).value;
	var copyme = document.getElementById('ftaf_copyme' + argAppend).checked;
	var emailaddr = document.getElementById('ftaf_emailaddr' + argAppend).value;
	var mynote = document.getElementById('ftaf_mynote' + argAppend).value;
	var mycode = document.getElementById('ftaf_captcha_code' + argAppend).value;

	myappend = '';
	if( copyme == true )
		myappend = '&copyme=' + encodeURIComponent(copyme);

	params = 'id=' + current_entry_id + '&sendarticle=1&inline=1&myname=' + encodeURIComponent(myname) + '&myaddr=' + encodeURIComponent(myaddr) + '&emailaddr=' + encodeURIComponent(emailaddr) + '&mynote=' + encodeURIComponent(mynote) + "&captcha_code=" + encodeURIComponent(mycode) + myappend;

	result = ajaxFunction('/ftaf.php', params, 'POST');

	if( result == 'success' )
	{
		document.getElementById('ftaf_message' + argAppend).innerHTML = "Your message has been sent.";
		document.getElementById('ftaf_submit' + argAppend).style.display = 'none';
	}
	else
		document.getElementById('ftaf_message' + argAppend).innerHTML = result;

	return false;
}

function captcha_swap(id)
{
	document.getElementById(id).src = '/captcha/securimage_show.php?' + Math.random();
}

function accordionInlineVote(is_vlog) {
	inlineVote(current_entry_id, is_vlog);
}

function ajaxTrackEmbed(size)
{
        ajaxFunction('/ajax_share_track.php', 'id=' + current_entry_id + "&svc_name=embed" + size, 'POST');
}

function swapEmbed()
{
	var url = ajaxFunction('/ajax_article.php', 'id=' + current_entry_id + '&method=videourl', 'GET');
	var url = encodeURIComponent(url);

        var flashvars = {};
	flashvars.embed_url = url;
        var params = {};
        params.menu = "false";
        params.quality = "best";
        params.allowscriptaccess = "sameDomain";
        params.allowfullscreen = "false";
	params.wmode = 'transparent';
        swfobject.embedSWF("/embed_this_video.swf?v=3.1a", "embed_flash_content", "607", "180", "9.0.0", false, flashvars, params);
}

function ajaxSubscribe()
{
	var myfirstname = document.getElementById('firstname').value;
	var mylastname = document.getElementById('lastname').value;
	var myemailaddr = document.getElementById('subscribe_emailaddr').value;

	params = 'subme=1&inline=1' + '&firstname=' + encodeURIComponent(myfirstname) + '&lastname=' + encodeURIComponent(mylastname) + '&emailaddr=' + encodeURIComponent(myemailaddr);

	result = ajaxFunction('/submail.php', params, 'POST');

	if( result == 'success' || result == 'already_subscribed' )
	{
		if( result == 'already_subscribed' )
			document.getElementById('subscribe_error_message').innerHTML = myemailaddr + " is already set to receive new article notifications.";
		else
			document.getElementById('subscribe_error_message').innerHTML = "Thank you for subscribing. You will receive a confirmation email shortly to complete the subscription process.";

		document.getElementById('subscribe_submit').style.display = 'none';
	}
	else
		document.getElementById('subscribe_error_message').innerHTML = result;
}

function ajaxQuickBlogcast()
{
	var domain = blog_domain.replace('http://', 'https://');
//	var domain = blog_domain.replace('http://', 'http://');
	document.getElementById('QBCForm').action = domain + '/qbc_share.php?id=' + current_entry_id + '&inline=1&sendarticle=1';
	document.getElementById('QBCIframe').style.display = '';
	return true;
}

function changeShare()
{
	// Custom internal API's
	var api_options = new Array('twitter', 'delicious', 'facebook', 'myspace', 'digg', 'technorati', 'reddit', 'slashdot', 'stumbleupon');
	for( i=0; i<api_options.length; i++ )
	{
		var myvalue = api_options[i];
		document.getElementById('add_' + myvalue).href = "/share_api.php?id=" + current_entry_id + "&service=" + myvalue;
	}

	//changeTweet();
}

function changeTweet()
{
	if( (tweetMessageSet == true) || (is_vlog == 0) )
		return false;

	var title = ajaxFunction('/ajax_article.php', 'id=' + current_entry_id + '&method=title', 'GET');
	var url = ajaxFunction('/ajax_article.php', 'id=' + current_entry_id + '&method=link', 'GET');
	var short_title = title;
	if( short_title.length > 85 )
	{
		short_title = short_title.substring(0, 82);
		short_title = short_title + '...';
	}

	getShortenedUrl(url, short_title);
}

function showTwitter()
{
	return false;
	if( document.getElementById('twitter_table_outer_div').style.display == 'block' )
		hideTwitter();
	else
	{
		hideEmail();
		document.getElementById('twitter_table_outer_div').style.display = 'block';
	}
}

function hideTwitter()
{
	return false;
	document.getElementById('twitter_table_outer_div').style.display = 'none';
}

function showEmail()
{
	if( document.getElementById('email_table_outer_div').style.display == 'block' )
		hideEmail();
	else
	{
		hideTwitter();
		document.getElementById('email_table_outer_div').style.display = 'block';
	}
}

function hideEmail()
{
	document.getElementById('email_table_outer_div').style.display = 'none';
}

function countTwitterChars(myAppend)
{
	var numChars = document.getElementById('twt_message' + myAppend).value.length;
	if (numChars >= 140) {
		document.getElementById('twt_message' + myAppend).value =  document.getElementById('twt_message' + myAppend).value.substring(0, 140);
		document.getElementById('twt_count' + myAppend).innerHTML = '140';
	}
	else {
		document.getElementById('twt_count' + myAppend).innerHTML = numChars;
	}
}

function getShortenedUrl(url, title)
{
	var result = ajaxFunction('/ajax_short_url.php', 'url=' + url, 'GET');

	if( is_vlog == 1 )
	{
		document.getElementById('twt_message').value = 'Watching BobParsons.me vlog ' + title + ' ' + result;
	}
	else
	{
		document.getElementById('twt_message').value = 'Reading BobParsons.me blog ' + title + ' ' + result;
	}

	countTwitterChars('');
	tweetMessageSet = true;
}

function submitTwitter(myAppend)
{
	var domain = blog_domain.replace('http://', 'https://');
//	var domain = blog_domain.replace('http://', 'http://');
	document.getElementById('TweetForm').action = domain + '/ajax_tweet.php?id=' + current_entry_id;
	document.getElementById('TweetIframe').style.display = '';
	return true;
}

function browserHeight()
{
	if( window.innerHeight != null )
		return window.innerHeight;
	else if( document.documentElement && document.documentElement.clientHeight )
		return document.documentElement.clientHeight;
	else if( document.body != null )
		return document.body.clientHeight;
	else
		return null;
}

function positionFromTop()
{
	if( typeof window.pageYOffset != 'undefined' )
		return window.pageYOffset;
	else if( document.documentElement && document.documentElement.scrollTop )
		return document.documentElement.scrollTop;
	else if( document.body.scrollTop )
		return document.body.scrollTop;
	else
		return 0;
}

function accordionScrollBrowser()
{
	var browserViewableHeight = browserHeight(); // Viewable height of browser inner frame.
	var browserTopPosition = positionFromTop(); // Viewable position from top of browser.
	var divOffset = $('#player_buttons').position().top; // Position of player buttons relative to document.
	var boxHeight = 420; // Height from top of buttons to bottom accordion border.
	var divBottom = divOffset + boxHeight;

	if( divBottom > (browserViewableHeight + browserTopPosition) )
	{
		var scrollOffset = divBottom - (browserViewableHeight + browserTopPosition);
		$('html,body').animate({scrollTop: '+=' + scrollOffset + 'px'}, 1000);
	}
}

// Ajax Functions
var ajaxHttpRequest = null;

function ajaxCreateRequest() {
	try {
		ajaxHttpRequest = new XMLHttpRequest();
	} catch (trymicrosoft)  {
		try  {
			ajaxHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft)  {
			try {
				ajaxHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				ajaxHttpRequest = null;
			}
		}
	}
}

function ajaxFunction(url, params, method) {
	if(typeof params == "undefined") {
		params = '';
	}

	if(typeof method == "undefined") {
		method = "GET";
	}
	else if( method != "POST" ) {
		method = "GET";
	}

	if (ajaxHttpRequest == null) ajaxCreateRequest();
	if (ajaxHttpRequest != null) {

		if( method == "GET" ) {
			ajaxHttpRequest.open("GET", url + '?' + params, false)
			ajaxHttpRequest.send(null);
		} else {
			ajaxHttpRequest.open("POST", url, false);
			ajaxHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajaxHttpRequest.setRequestHeader("Content-length", params.length);
			ajaxHttpRequest.setRequestHeader("Connection", "close");
			ajaxHttpRequest.send(params);
		}
		if (ajaxHttpRequest.status == 200) {
			return ajaxHttpRequest.responseText;
		} else {
			return false;   
		}
	} else {
		return false;
	}
}

function asyncAjaxFunction(url, params, method, callbackFunction)
{
	if(typeof params == "undefined") {
		params = '';
	}

	if(typeof method == "undefined") {
		method = "GET";
	}
	else if( method != "POST" ) {
		method = "GET";
	}

	var asyncAjaxHttpRequest;
	try {
		asyncAjaxHttpRequest = new XMLHttpRequest();
	} catch (trymicrosoft)  {
		try  {
			asyncAjaxHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft)  {
			try {
				asyncAjaxHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				asyncAjaxHttpRequest = null;
			}
		}
	}

	if(asyncAjaxHttpRequest == null) asyncAjaxCreateRequest();
	if( asyncAjaxHttpRequest != null )
	{
		asyncAjaxHttpRequest.onreadystatechange = function()
		{
			if( asyncAjaxHttpRequest.readyState == 4 && callbackFunction != false )
				callbackFunction(asyncAjaxHttpRequest.responseText);
		}

		if( method == "GET" ) {
			asyncAjaxHttpRequest.open("GET", url, true);
			asyncAjaxHttpRequest.send(null);
		} else {
			asyncAjaxHttpRequest.open("POST", url, true);
			asyncAjaxHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			asyncAjaxHttpRequest.setRequestHeader("Content-length", params.length);
			asyncAjaxHttpRequest.setRequestHeader("Connection", "close");
			asyncAjaxHttpRequest.send(params);
		}
	}
}


// Vote for blog
function inlineVote(id, is_vlog)
{
	if( id > 0 )
	{
		op='liked';
		options = document.getElementById('blog_vote_form').opinion;

		for ( var i = 0 ; i < options.length; i++ )
			if ( options[i].checked ) op = options[i].value;

		result = ajaxFunction('/survey.php', 'inline=1&entry_id='+id+'&opinion='+op, 'GET');

		if( result == "success" || result == "duplicate" )
		{
			var my_append = '';
			if( (result == "duplicate") && (is_vlog == 1) )
				my_append = '<div style="color: red; clear: both; margin-top: 4px;">You have already voted for this vlog.</div>';
			else if( result == "duplicate" )
				my_append = '<div style="color: red; clear: both; margin-top: 4px;">You have already voted for this blog.</div>';

			document.getElementById('voting_results').innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="273" HEIGHT="80" id="FC2Column" ALIGN=""><PARAM NAME=movie id="vote_param" VALUE="/inc/Charts/FC_2_3_Bar2D.swf?chartWidth=230&chartHeight=70&dataUrl=/sdata.php?entry_id=' + id + '"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><PARAM NAME=wmode VALUE=transparent><EMBED src="/inc/Charts/FC_2_3_Bar2D.swf?chartWidth=230&chartHeight=70&dataUrl=/sdata.php?entry_id=' + id + '" quality=high bgcolor=#FFFFFF WIDTH="273" HEIGHT="80" NAME="FC2Column" id="vote_embed" ALIGN="" WMODE="transparent" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>' + my_append;

			document.getElementById('voting_query').style.display='none';
			unhideVotingResultsTimer = setTimeout("unhideVotingResults(1);", 500);
		}
	}
}

var unhideVotingResultsTimer = null;
function unhideVotingResults (dirty) {
	if ( unhideVotingResultsTimer ) clearTimeout(unhideVotingResultsTimer);
	unhideVotingResultsTimer = null;
	document.getElementById('voting_results').style.display='inline';
}

if ( typeof getCookie == 'function' && !getCookie('refsite') && !getCookie('refpage') ) {
	var today = new Date();
	today.setDate(today.getDate() + 1);
	setCookie('refsite', document.referrer?document.referrer:(window.location.href.match("rss=1")?'rss':''), today);
	setCookie('refpage', window.location.href, today);
}
