// JavaScript Document

var agent = '';
var page = 1;
var tweets = (jQuery(window).width() < 1280) ? 5:6; 	
var tweetWidth;

function onResize()
{
	tweets = (jQuery(window).width() < 1280) ? 5:6;
	
	tweetWidth = (jQuery(document).width() / tweets);
	tweetWidth = Math.ceil(tweetWidth);
	
	jQuery('div#timeline').css
	(
		'height', jQuery(document).height() - jQuery('div#timeline').offset().top
	);
	
	jQuery('div#timeline ul li').width(tweetWidth);
	jQuery('div#timeline ul').css('right', '0px');
	jQuery('div#timeline ul').width(tweetWidth * 15);
}

function is_iOS()
{
	return agent.match(/(iphone|ipod|ipad)/);
}

function startVideo()
{
	var params = { wmode: 'transparent'};
	var attributes = { id: 'video-flash', name: 'video-flash', bgcolor:'#000000', allowFullScreen:true };
	var flashvars = {};
	
	if(is_iOS())
	{
		jQuery('div.player a.preview').bind
		(
			'click', function()
			{
				jQuery(this).hide();
				jQuery('#video-flash').show();
			}
		);
	}
	else
	{
		jQuery('div.player a.preview').bind
		(
			'click', function()
			{				
				jQuery(this).hide();
				
				swfobject.embedSWF(jQuery('#video-flash video').attr('swf'), 'video-flash', 640, 345, '9.0.0', '../../media/swf/expressInstall.swf', flashvars, params, attributes);
				jQuery('#video-flash').show();
			}
		);
	}
	
	jQuery('div.player a.preview').click();
}

function searchTwitter()
{
	jQuery('div#timeline ul li').remove();

	var statuses = "http://search.twitter.com/search.json?q=%23eunaopresto&page=" + page + "&per_page=100&callback=?";
	var container = jQuery("#timeline ul");

	jQuery.getJSON
	(
		statuses, function(data)
		{	
			jQuery.each
			(
				data.results, function(_i, _item)
				{
					var tweet = _item;
					
					var createdAt = tweet.created_at.split(' ');
					var publishedDate = tweet.created_at.split(" ")[1] + '/' + tweet.created_at.split(" ")[2] + '/' + tweet.created_at.split(" ")[3];
					var imageProfile = (tweet.profile_image_url == 'http://static.twitter.com/images/default_profile_normal.png') ? 'images/jpg/image_avatar_default_twitter.jpg' : tweet.profile_image_url;
					
					var userData = "<a href=\"http://www.twitter.com/#!/" + tweet.from_user + "\" target=\"_blank\">" + tweet.from_user + "</a> <br />" + publishedDate + " <img alt=\" \" src=\"" + imageProfile + "\" />";
					
					var post = jQuery("<li><div class=\"balloon\"><p>" + tweet.text + "</p></div><span class=\"arrow\"><img alt=\" \" src=\"images/png/image_icon_arrow_2.png\" /></span><p class=\"info\"> " + userData + "</p></li>");
					
					jQuery(container).append(post);
					
					post.find('span.arrow').css('top', post.find('div.balloon').height() + 40 + 'px');
					
					onResize();
				}
			);
		}
	);	
	
	jQuery('div#timeline ul').fadeIn('fast');
	
	slideTweets();
}

function slideTweets()
{
	jQuery('#timeline ul').delay(8000).animate
	(
		{
			right: '-=' + getTweetWidth()
		}, 1000, function()
		{
			if(jQuery('div#timeline ul').offset().left < -2) slideTweets();
			else
			{
				jQuery('div#timeline ul').fadeOut
				(
					'fast', function()
					{
						onResize();
						searchTwitter();
					}
				);
			}
  		}
	);
}

function getTweetWidth()
{
	return tweetWidth;
}
