$(window).load(function() 
{
	//if ($('#latest-friends').attr('class') == 'tabs-container')
	//	showTab('latest-friends');
		
	if ($('#latest-yours').attr('class') == 'tabs-container')
		showTab('latest-yours');
	
	//if ($('#latest-all').attr('class') == 'tabs-container')
	//	showTab('latest-all');
});

tab_latest_friends = 'unloaded';
tab_latest_yours   = 'unloaded';

function showTab(id)
{	
	$('#latest-friends_link').css({'color':'#108489'}); 
	$('#latest-all_link').css({'color':'#108489'}); 
	$('#latest-yours_link').css({'color':'#108489'}); 	
	$('#'+id+'_link').css({'color':'#333333'});
	
	//alert(tabId);
	
	/*$('#latest-friends').attr('class','tabs-container tabs-hide'); 
	$('#latest-all').attr('class','tabs-container tabs-hide');
	$('#latest-yours').attr('class','tabs-container tabs-hide');	*/
	
	$('#latest-friends').css({'display':'none'}); 
	$('#latest-all').css({'display':'none'});
	$('#latest-yours').css({'display':'none'});
	
	$('#'+id).css({'display':'block'});
	
	if ((id == 'latest-friends') && (tab_latest_friends != 'unloaded'))
		return;

	if ((id == 'latest-yours') && (tab_latest_yours != 'unloaded'))
		return;
	
	getDataToTheTab(id);
}

function showTab2(id)
{
	$('#tabs-most_active').css({'display':'none'});
	$('#tabs-most_active_option').css({"background-color":"#333333"});
	
	$('#tabs-most_active_in_comments').css({'display':'none'});
	$('#tabs-most_active_in_comments_option').css({"background-color":"#333333"});
	
	$('#tabs-most_popular_persons').css({'display':'none'});
	$('#tabs-most_popular_persons_option').css({"background-color":"#333333"});
	
	$('#tabs-most_popular_posts').css({'display':'none'});
	$('#tabs-most_popular_posts_option').css({"background-color":"#333333"});

	
	$('#'+id).css({'display':'block'});
	$('#'+id+'_option').css({"background-color":"#7eb524"});
}

function getPostUrl(id)
{
	if (id > 12821824)
		return "/w/"+id; else
			return "/post-view/id,"+id;
}


function generateHtml(array,type)
{
	var html='';
	var commentHtml = '';
	var premium = '';
	var private_post = '';
	var login_color = '';
	var posts_text = '';
	var avatar_icon_plusik = '';
	var plusik_logo_nick = '';
	var quote = '';
		
	switch (type)
	{
		case 'post':
			if (array.no_comments)
				commentHtml = '<span style="color:#4b8ab5">Komentarze zablokowane</span>'; else
				{
					if (array.comments_count == 0)
						commentHtml = ' <a href="javascript:getComments('+array.id+',2);" style="color:#4b8ab5"><img src="/static/img/comment.png" style="vertical-align:middle" /> Dodaj komentarz</a>'; else
							commentHtml = '<a href="javascript:getComments('+array.id+',1);" style="color:#4b8ab5"><img src="/static/img/comment.png" style="vertical-align:middle" /> Pokaż komentarze ('+array.comments_count+')</a>';
				}
			
			if (array.login_color)
				login_color = ' style="color:#'+array.login_color+'"';
			
			if (array.posts_id > 0)
			{
				html = ' <div class="message">'+
	            '   <div class="msg_top"></div>'+
	            '   <div class="msg_center">'+
                '		<table width="100%">'+
	            '          <tbody><tr>'+
	            '              <td><a'+login_color+' href="/profile/'+array.login+'" title="'+array.login+'"><img alt="'+array.login+'" class="avatar_small" src="'+array.avatar_url+'"> </a><a href="/profile/'+array.login+'" title="'+array.login+'">'+array.login+'</a> dodał komentarz: <span class="comment_value">'+array.text+'</span> do <a href="/post-view/id,'+array.posts_id+'">wpisu</a></td>'+
                '           <td width="100" align="right" style="color: rgb(136, 136, 136); font-size: 10px;">'+array.date_text+' </td>'+
                '       </tr>'+
                '     </tbody></table>'+
                '</div>'+
                '<div class="msg_bottom"></div>'+
	            '</div>';
			} else
			{
				if (array.premium != 'null')
				{
					premium = '<div class="plusik_add" id="plusik_add_'+array.id+'" style="width:200px; float:right;"><span id="votes_count_'+array.id+'">'+array.votes_count+'</span> <a href="javascript:addVote('+array.id+');"><img src="/static/im/plusik_icon_small.png" border="0" /> plusik</a></div>';
				} else
					premium = '';
				
				if (array.private_post != 'null')
				{
					private_post = '<span style="font-weight:bold;">Wpis prywatny</span>';
				} else
					private_post = '';
				
				if (array.quote_author_login != 'null')
				{
					posts_text = '<table width="100%">'+
				                        '<tr>'+
				                        '<td width="45" valign="top" align="left">'+
				                        '	<img border="0" src="/static/im/plusik_quote.png" />'+
				                        '</td>'+
					                    '<td valign="top">'+
					                    '    <div>'+array.text+'</div>'+
					                    '    <div class="quote_autor">Autor cytatu: <a href="/profile/'+array.quote_author_login+'">'+array.quote_author_login+'</a></div>'+
					                    '</td>'+
					                '</tr>'+
					              '</table>'; 
					
				} else
						posts_text = array.text;
						
				if (array.premium != 'null')
					avatar_icon_plusik = '<span class="avatar_icon_plusik"><img src="http://moblo.pl/static/im/plusik_avatar_icon.png"></span>';
				
				if (array.premium != 'null')
					plusik_logo_nick = '<a href="/plusik"><img src="/static/im/plusik_logo_nick.png" border="0"></a>';
				
				if (array.premium != 'null')
					quote = '<a href="javascript:void(0);" onclick="quotePost('+array.id+')" style="color:#4b8ab5; margin-left:10px;"> Cytuj</a>'
				
				html = '<div class="message">'+
		           '<div class="msg_top"></div>'+
		           '<div class="msg_center">'+
		           '<table width="100%">'+
		           ' <tr>'+
		           '       <td width="70" valign="top"><a title="{$wordText}" href="/profile/'+array.login+'" style="position:relative;"><img src="'+array.avatar_url+'" class="avatar" height="65" width="65" alt="{$wordText}"/>'+avatar_icon_plusik+'</a></td>'+
		           '       <td valign="top">'+
		           private_post+
		           '       	<h3 class="message_meta"><a'+login_color+' title="{$wordText}" href="/profile/'+array.login+'">'+array.login+'</a>'+plusik_logo_nick+' dodano: '+array.date_text+'</h3>'+
		           '         <h2 class="message_content">'+posts_text+'</h2>'+
		           '         <div class="message_options" style="height:20px; margin-top:5px" align="right">'+
		           '                            <table width="100%">'+
		           '                                <tr>'+
		           '                                   <td align="left">  '+    
		           '                                  '+commentHtml+
		           quote+
		           '              <a href="'+getPostUrl(array.id)+'" style="color:#4b8ab5; margin-left:10px;">Link</a>'+
		           '                                  </td>'+
		           '                                  <td align="right"></td>'+
		           premium+
		           '                               </tr>'+
		           '                            </table>'+
		           '              </div>'+
		           '           </td>'+
		           '       </tr>'+
		           '    </table>'+
		           '    <div style="width:100%; float:left; height:auto; text-align:center; color:red; font-weight:bold;" id="post_msg_'+array.id+'"></div>'+
		           '    <div id="comments-'+array.id+'"style="width:100% height:auto; overflow:hidden;">'+
		           '    '+
		           '    </div>'+
		           '    <div id="addCommentDiv-'+array.id+'"style="width:100% height:auto; overflow:hidden; display:none">'+
			       '                                     <div style="width:100%; height:auto; overflow:hidden; margin-top:10px;">'+
			       '                                         <div><textarea id="addCommentTo-'+array.id+'" style="width:622px; height:50px; padding:3px; border:1px solid #98D5EB; background:url(/static/img/input_bg.gif) no-repeat scroll left top transparent;" rows="2"></textarea></div>'+
			       '                                         <div style="margin-top:1px" align="right"><img src="/static/img/dodaj.png" style="cursor:pointer" onclick="sendComment('+array.id+')" /></div>'+
			       '                                     </div>'+
			       ' 	</div>'+
			       '</div>'+
			       '<div class="msg_bottom"></div>'+
		           ' </div>';
			}
			break;
	}
	
	return html;
}

function getDataToTheTab(tabId)
{
	if (show_tab_count > 0)
	{
		var url = "http://moblo.pl/default-getPosts/type,"+tabId;
		
		if (tabId == 'latest-friends')
			tab_latest_friends = 'loaded';
		
		if (tabId == 'latest-yours')
			tab_latest_yours = 'loaded';
		
		/*$('#latest-friends_link').css({'color':'#108489'}); 
		$('#latest-all_link').css({'color':'#108489'}); 
		$('#latest-yours_link').css({'color':'#108489'}); 	
		$('#'+tabId+'_link').css({'color':'#333333'});
		
		//alert(tabId);
		
		/*$('#latest-friends').attr('class','tabs-container tabs-hide'); 
		$('#latest-all').attr('class','tabs-container tabs-hide');
		$('#latest-yours').attr('class','tabs-container tabs-hide');	*/
		
		/*$('#latest-friends').css({'display':'none'}); 
		$('#latest-all').css({'display':'none'});
		$('#latest-yours').css({'display':'none'});
		
		//alert('#'+tabId);
		$('#'+tabId).css({'display':'block'});
		//$('#'+tabId).attr('class','tabs-container');
		//$('#'+tabId+'_link').css({'color':'#333333'});*/
	
		$.getJSON(url, function(data) 
		{
			$('#'+tabId+' .moviesLoadingAnimation').remove();
			
			if (is_array(data.friendsPosts))
			{
				var html = '';
				$.each(data.friendsPosts, function(i, post) 
				{
					html = html+generateHtml(post,'post');
				});
	
				if (html == '')
					$('#latest-friends').html('Nie znaleziono żadnych wpisów Twoich znajomych. Dodaj kogoś do obserwowanych, żeby śledzić jego aktywność.'); else
					{
						html = html + '<div align="right"><a href="http://moblo.pl/friends-posts/'+userLogin+'" style="color: rgb(0, 102, 153); font-size: 12px; font-weight: bold; margin-right: 10px;">» Więcej wiadomości</a></div>';
						$('#latest-friends').html(html);
					}
			} else
			{
				if (tabId == 'latest-friends')
				{
					$('#latest-friends').html('Nie znaleziono żadnych wpisów Twoich znajomych. Dodaj kogoś do obserwowanych, żeby śledzić jego aktywność.');
				}
			}
			
			if (is_array(data.userPost))
			{
				var html = '';
				$.each(data.userPost, function(i, post) 
				{
					html = html+generateHtml(post,'post');
				});
				
				if (html == '')
					$('#latest-yours').html('Nie ma jeszcze żadnych wpisów w tej zakładce.'); else
					{
						html = html + '<div align="right"><a href="http://moblo.pl/profile/'+userLogin+'" style="color: rgb(0, 102, 153); font-size: 12px; font-weight: bold; margin-right: 10px;">» Więcej wiadomości</a></div>';
						$('#latest-yours').html(html);
					}
			} else
			{
				if (tabId == 'latest-yours')
				{
					$('#latest-yours').html('Nie znaleziono żadnych nowych wpisów.');
				}
			}
			
			if (is_array(data.latestAll))
			{
				var html = '';
				$.each(data.latestAll, function(i, post) 
				{
					html = html+generateHtml(post,'post');
				});
				
				html = html + '<div align="right"><a href="http://moblo.pl/post/page,1" style="color: rgb(0, 102, 153); font-size: 12px; font-weight: bold; margin-right: 10px;">» Więcej wiadomości</a></div>';
				$('#latest-all').html(html);
			} else
			{
				if (tabId == 'latest-all')
				{
					$('#latest-all').html('Nie znaleziono żadnych nowych wpisów.');
				}
			}
		});
	}
	
	show_tab_count = show_tab_count + 1;
}

function is_array(input) 
{
	return typeof (input) == 'object' && (input instanceof Array);
}

function addVote(id)
{
	var url = "/post-addvote";
	$('#post_msg_'+id).html('<img src="/static/img/ajax-loader.gif">');
	
	$.post(url, {id:id},
		function(data)
		{
			switch (data)
			{
				case 'ok':
					//$('#votes_count_'+id).html(parseInt($('#votes_count_'+id).html())+1);
					$('#plusik_add_'+id).html((parseInt($('#votes_count_'+id).html())+1)+' | Plusik wystawiony!');
					break;
				case 'owner':
					$('#plusik_add_'+id).html('Jesteś właścicielem tego wpisu. Nie możesz go ocenić :)');
					break;
					
				case 'user_vote_on_this_before':
					$('#plusik_add_'+id).html('Oceniałeś już wcześniej ten wpis.');
					break;
					
				case 'not_logged':
					$('#plusik_add_'+id).html('<span style="color:red;">Musisz się zalogować, żeby oceniać wpisy.</span>');
					break;
			}
			$('#post_msg_'+id).html('');
			//alert(data);
		}, "text");
}

function quotePost(id)
{
	var url = "/post-quote/";
	
	$.post(url, {id:id},
		function(data)
		{
			location.href = data;
		}, "text");
}

