// JavaScript Document

var TUMBLR_MAX_HEIGHT = 205;
var TUMBLR_MAX_WIDTH = 320;

function display_new_tumblr( t )
{
	function determine_ratio( w, h )
	{
		//-max width, height = 320, 192 : h/w = 0.6
		if( h < 0 || w < 0 || isNaN(w) || isNaN(h) )
			return false;
		
		//if( h > 192 || w > 320 )
		//{
			if( h/w == (TUMBLR_MAX_HEIGHT/TUMBLR_MAX_WIDTH) )
				return [TUMBLR_MAX_WIDTH,TUMBLR_MAX_HEIGHT];
			if( h/w > (TUMBLR_MAX_HEIGHT/TUMBLR_MAX_WIDTH) )
			{
				w = (TUMBLR_MAX_HEIGHT / h) * w;
				return [Math.floor(w),TUMBLR_MAX_HEIGHT];
			}
			else
			{
				h = (TUMBLR_MAX_WIDTH / w) * h;
				return [TUMBLR_MAX_WIDTH,Math.floor(h)];
			}
		//}
	}
	
	function new_img_ratio( img )
	{
		//console.log( img.height + ' | ' + img.width );
		if( img.height < 0 || img.width < 0 || isNaN(img.width) || isNaN(img.height) )
			return false;

		if( img.height / img.width == (TUMBLR_MAX_HEIGHT/TUMBLR_MAX_WIDTH) )
			return [TUMBLR_MAX_WIDTH,TUMBLR_MAX_HEIGHT];
		if( img.height / img.width > (TUMBLR_MAX_HEIGHT/TUMBLR_MAX_WIDTH) )
		{
			return [Math.floor((TUMBLR_MAX_HEIGHT / img.height) * img.width),TUMBLR_MAX_HEIGHT];
		}
		else
		{
			return [TUMBLR_MAX_WIDTH,Math.floor(((TUMBLR_MAX_WIDTH / img.width) * img.height))];
		}
	}
	
	posts_vector = posts_start === false? false : posts_start - t['posts-start'];
	posts_start = t['posts-start'];
	posts_total = t['posts-total'];
	t = t['posts'][0];
	
	new_tumblr_html = '';
	
	if( !t )
	{
		$('#tumblr-wrapper').html( 'Sorry, either there are no posts for that content type or the tumblr script is experiencing difficulties...' );
	}
	else
	{
		switch( t['type'] )
		{
			case 'photo':
				var img = new Image();
				$(img).attr('src', t['photo-url-250']);
				$(img).load( function()
				{
					var img_size = new_img_ratio( img );
					//console.log( img_size );
					if( img_size )
					{
						if( img_size[0] >= 220 )
						{
							$(img).css( 'width', img_size[0] );
							$(img).css( 'height', img_size[1] );
							$('#tumblr-wrapper').children().remove();
							$('#tumblr-wrapper').html( '' );
							$('#tumblr-wrapper').append( img );
						}
						else
						{
							if( posts_start === false || posts_start == 0 || posts_vector === false )
								update_tumblr( -1 );
							else
								update_tumblr( posts_vector > 0? 1 : -1 );
						}
					}
				});
				$(img).error( function()
				{
					new_tumblr_html = '<span class="twentyone dark_gray">Our apologies: the video is not available...</span>';
				});
			break;
			case 'video':
				$('#tumblr-wrapper').children().remove();
				$('#tumblr-wrapper').html( '' );
				h = t['video-player'].match(/height="(\d{1,5})"/i);
				if( h )
				{
					new_size = determine_ratio( 400, h[1] );
					//console.log( h + ', ' + new_size );
		
					t['video-player'] = t['video-player'].replace(/400/g,new_size[0]);
					if( new_size )
					{
						temp_regex = new RegExp( h[1], 'g' );
						t['video-player'] = t['video-player'].replace(temp_regex,new_size[1]);
					}
					
					//-set wmode in object
					if( t['video-player'].match(/wmode=/i) )
						t['video-player'] = t['video-player'].replace(/wmode=".*"/i, 'wmode="transparent"');
					else
						t['video-player'] = t['video-player'].replace('<object', '<object wmode="transparent"');
	
					if( t['video-player'].match(/wmode/) && t['video-player'].match(/(window|opaque)/) )
					{
						t['video-player'].replace(/(window|opaque)/i,'transparent');
					}
					else
					{
						t['video-player'] = t['video-player'].replace(/<embed/, '<param name="wmode" value="transparent" /><embed' );
					}
		
					//console.log( t['video-player'] );
					$('#tumblr-wrapper').html( t['video-player'] );
				}
				else if( t['video-player'].match(/script/i) )
				{
					new_tumblr_html = '<span class="twentyone dark_gray">Our apologies: the video is not available...</span>';
				}
				else
				{
					new_tumblr_html = '<span class="twentyone dark_gray">Our apologies: the video is not available...</span>';
				}
			break;
			case 'quote':
				new_tumblr_html = '<div style="float:right;width:265px;padding:10px 10px 0 4px;text-align:left;">';
				new_tumblr_html += '<div class="dark_gray twentyone">'+onesixty(t['quote-text'],128)+'</div>';
				new_tumblr_html += '<br />';
				if( t['quote-source'] )
					new_tumblr_html += '<div class="dark_gray twelve" style="text-align:right;">&mdash; ' + onesixty(t['quote-source'],128) + '</div>';
				new_tumblr_html += '</div>';
				new_tumblr_html += '<img src="images/tumblr/quote.gif" />';
			break;
			case 'link':
				new_tumblr_html = '<div class="dark_gray twelve" style="float:right;width:259px;padding:13px 10px 0 0;text-align:left;">';
				new_tumblr_html += '<a href="' + t['link-url'] + '" class="dark_gray twentyone" target="_blank">' + (t['link-text']? onesixty(t['link-text'],40) : onesixty(t['link-url'],80)) + '</a>';
				new_tumblr_html += '<br /><br />';
				new_tumblr_html += onesixty(t['link-description'],128);
				new_tumblr_html += '</div>';
				new_tumblr_html += '<img src="' + _DIR('images') + 'tumblr/protocol.gif" />';
			break;
			case 'audio':
				new_tumblr_html = t['audio-player'].replace(/color=FFFFFF/,'color=464646');
				new_tumblr_html += '<br /><br />';
				new_tumblr_html += t['audio-caption'];
			break;
			default:
				new_tumblr_html = '<div id="tumblr-regular-title" class="dark_gray twentyone">' + onesixty(t['regular-title'],45) + '</div>';
				new_tumblr_html += '<div id="tumblr-regular-body" class="dark_gray twelve">' + onesixty(t['regular-body'],78) + '</div>';
			break;
		}
		
		if( new_tumblr_html )// && document.getElementById )
			$('#tumblr-wrapper').html( new_tumblr_html );
	}
}

function dis_content( li )
{
	if( document.getElementById )
	{
		if( li.className == 'item button dissed' )
		{
			lis = li.parentNode.getElementsByTagName( 'li' );
			count = lis.length;
			for( i=0; i<count; i++ )
			{
				if( lis[i] != li )
					lis[i].className = 'item button dissed';
				else
					lis[i].className = 'item button';
			}
			update_tumblr( 2 );
		}
	}
}

function ajax_failure()
{
	new_html = '<img src="'+_DIR('images')+'tumblr/heartbeat.gif" alt="" />';
	new_html += '<div style="text-align:center;width:320px;"><span class="georg twentyone">Tumblr is not responding</span><br /><br /><span class="georg lh-eighteen twelve">Tumblr is currently unresponsive. To attempt reviving, please visit our <a href="http://manningproductions.tumblr.com/" target="_blank" title="manning productions tumblr feed">tumblr page</a>.</span></div>';
	$('#tumblr-wrapper').html( new_html );
}

function update_tumblr( offset )
{
	if( document.getElementById )
	{
		//-determine dis
		if( document.getElementById('tumblr-types') )
		{
			type = false;
			lis = Array();
			lis = document.getElementById('tumblr-types').getElementsByTagName('li');
			run = lis.length;
			for( i=0; i<run; i++ )
			{
				if( !lis[i].className.match('dissed') )
				{
					type = lis[i].childNodes[0].innerHTML;
					break;
				}
			}
		}
		
		//$.post( 'scripts/fetch_tumblr.php', {offset:offset,type:type,posts_total:posts_total}, function(data){ display_new_tumblr(data); }, 'json' );

		$.ajax({
			type: 'POST',
			url: 'scripts/fetch_tumblr.php',
			data: {offset:offset,type:type,posts_total:posts_total},
			success: function(data){ display_new_tumblr(data); },
			dataType: 'json',
			error: function(request,tstatus,errorThrown){ ajax_failure(); }
		});

		$('#tumblr-wrapper').html( '<img id="loader_tumblr" src="'+_DIR('images')+'loader_wheel.gif" alt="" />' );
	}
}

