function equalHeight(group)
{
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
	
		if(thisHeight > tallest)
		{
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

/////////////////////////////////////////////////////////////

function slideshow()
{
	$('#slideshow').cycle();
}

/////////////////////////////////////////////////////////////

function ajax_load()
{
	$('#wait').ajaxStart(function() {
		$(this).show();
	}).ajaxComplete(function() {
		$(this).hide();
	});
}

/////////////////////////////////////////////////////////////

function get_feeds()
{
	$.ajax({
		url: "/admin/ajax/get_feeds",
		type: "POST",
		dataType: 'json',
		success: function(data) {
			var idx = 1;
			
			for(key in data)
			{
				var value = data[key];
				
				$('ul#latest_news').append('<li><div class="date">' + key + '</div><ul id="day_' + idx + '"></ul></li>');
				
				for(k in value)
				{
					var v = value[k];
					
					for(i in v)
					{
						var val = v[i];
						
						if ($.isPlainObject(val))
						{
							if (val.service == "tumblr")
							{
								if (val.format == "video")
								{
									$('ul#day_' + idx).append('<li class="' + val.service + '"><div class="info">' + val.message + '</div><div class="video">' + val.video + '</div></li>');
								}
								else if (val.format == "regular")
								{
									$('ul#day_' + idx).append('<li class="' + val.service + '"><h3>' + val.message + '</h3><div class="post-content">' + val.body + '</div><a href="' + val.service_link + '">see post &rarr;</a></li>');
								}
								else if (val.format == "photo")
								{
									$('ul#day_' + idx).append('<li class="' + val.service + ' cf"><img src="' + val.service_link + '" alt="' + val.message + '" width="' + val.photo_width + '" height="' + val.photo_height + '" /><div class="photo_description">' + val.message + '</div></li>');
								}
								else
								{
									$('ul#day_' + idx).append('<li class="' + val.service + '">' + val.message + ' <a href="' + val.service_link + '">see post &rarr;</a></li>');
								}
							}
							else if (val.service == "twitter")
							{
								$('ul#day_' + idx).append('<li class="' + val.service + '">' + val.message + '</li>');
							}
							else
							{
								$('ul#day_' + idx).append('<li class="' + val.service + '">' + val.message + ' <a href="' + val.service_link + '">see post &rarr;</a></li>');
							}
						}
					}
				}
				idx++;
			}
			
			setTimeout( function() {
				$('ul#latest_news').fadeIn(1000, function() {
					equalHeight($('#content_wrap .content .equal'));
				});
			}, 200);
			// 
		}
	});
}

function homepage_tabs()
{
	$('.content.home ul#tab_control li a').click( function() {
		var idx = $(this).parent().index();
		
		$('.content.home ul#tab_control li a.active').removeClass('active');
		$(this).addClass('active');
		
		$('.content.home #tab_wrapper .tab').fadeOut(300, function() {
			$('.content.home #tab_wrapper .tab:eq(' + idx + ')').fadeIn(300);
		});
		
		return false;
	});
}

/////////////////////////////////////////////////////////////
$(document).ready(function() {
	homepage_tabs();
	get_feeds();
	slideshow();
	ajax_load();
});
