/**
 * Odswieza liste albumow
 */
function refresh_album_list(album_id)
{
	$.ajax({
		type: 'POST',
		url: $('form#a_add_album').attr('action').replace('add_album', 'a_get_albums'),
		data: $('form#a_add_album').serialize(),
		dataType: "json",

		beforeSend: function()
		{
			$('select[name=album_id]').after('<div id="form_loading" class="loading right"></div>');
		},
		success: function(msg)
		{
			if(msg != 'false')
			{
				var options = '';
				for(var i = 0; i < msg.length; i++)
				{
					options += '<option value="' + msg[i].album_id + '">' +
								msg[i].album_name +
								'</option>';
				}
				$("select#album_id").html(options);

				try
				{
					if (album_id > 0)
					{
						$('#album_id').val(album_id);
					}
				} catch(e){}
			}
		},
		complete: function()
		{
			$('#form_loading').remove();
		}
	});
}

/**
 * Pobiera komentarze
 */
function get_comments(url)
{
	$.ajax({
		type: 'POST',
		url: url,
		dataType: "json",

		beforeSend: function()
		{
			$('#comments_list div.navi').html('<div id="comment_list_loading" class="loading"></div>');
		},
		success: function(msg)
		{
			if(msg.error == 'false')
			{
				var comments = '';
				for(var i = 0; i < msg.result.length; i++)
				{
					comments += '<li class="' + msg.result[i].li_class + (!i ? ' first' : '') + '">' +
						'<div class="user">' +
						msg.result[i].avatar_link +
						'<div class="small">' + msg.result[i].profile_link + '</div>' +
						'<div class="small light">' + msg.result[i].city + '</div>' +
						'</div>' +
						'<div class="comment">' +
						'<div class="head"><div class="left"><div class="subtitle">' + msg.result[i].date + '</div></div></div>' +
						'<div class="content">' + msg.result[i].comment + '</div>' +
						'</div>' +
						'</li>';
				}
				if( $('#comments_list ul').length < 1 )
					$('#comments_list div.no_comments').replaceWith('<ul class="comments"></ul>');
				
				$('#comments_list ul').html(comments);
				$('#comments_list div.navi').html(msg.pages);
				
				$.scrollTo('#comments_container');
			}
		},
		complete: function()
		{
			$('#comment_list_loading').remove();
		}
	});
}

$(function(){
	// utworz okno dialogowe
	$("#add_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		width: 600
	});

	// dodawanie albumu
	$('#a_add_button').click(function(){
		$.ajax({
			type: 'POST',
			url: $('form#a_add_album').attr('action'),
			data: $('form#a_add_album').serialize(),
			dataType: 'json',
			
			beforeSend: function()
			{
				$('form#a_add_album button#a_cancel_button').after('<div id="form_loading" class="loading right"></div>');
			},
			success: function(msg)
			{
				$('#form_loading').remove();
				
				if (msg.error == 'false')
				{
					if(typeof after_album_add != 'undefined')
					{
						window.location = after_album_add + '/' + msg.album_id;
					}
					else
					{
						refresh_album_list(msg.album_id);
					}
					$('#add_dialog').dialog('close');
				}
				else
				{
					// wyswietl bledy walidacji
					messages = msg.error_msg;
					for(var i = 0; i < messages.length; i++)
					{
						if ($('#'+messages[i].element+'_error').attr('class'))
						{
							if (messages[i].msg)
							{
								$('#'+messages[i].element+'_error').html(messages[i].msg).show();
							}
							else
							{
								$('#'+messages[i].element+'_error').hide();
							}
						}
					}
				}
			},
			error: function()
			{
				$('#form_loading').remove();
			}
		});
		return false;
	});

	$('#a_cancel_button').click(function(){
		$('#add_dialog').dialog('close');
		return false;
	});

	$('#add_album').click(function(){
		$('#album_name').val('');
		$('#add_dialog .form_error').hide();
		
		$('#add_dialog').dialog('open');
		
		//refresh_album_list(1);
		
		return false;
	});
	
	
	// okno dialogowe raportowania
	$("#report_dialog").dialog({
			autoOpen: false,
			bgiframe: true,
			modal: true,
			width: 600
		});

	// dodawanie raportu
	$('#r_add_button').click(function(){
		$.ajax({
			type: 'POST',
			url: $('form#r_add_report').attr('action'),
			data: $('form#r_add_report').serialize(),
			dataType: 'json',
			
			beforeSend: function()
			{
				$('form#r_add_report button#r_cancel_button').after('<div id="form_loading" class="loading right"></div>');
			},
			success: function(msg)
			{
				if (msg.error == 'false')
				{
					$('#report_dialog').dialog('close');
					msg_dialog = $('<div/>').attr('id', 'report_msg_dialog').attr('title', 'Info').html(msg.result).dialog({autoOpen: false, bgiframe: true, modal: true, width: 400, buttons: { Ok: function() { $(this).dialog('close'); }}}).dialog('open');
				}
				else
				{
					// wyswietl bledy walidacji
					messages = msg.error_msg;
					for(var i = 0; i < messages.length; i++)
					{
						if ($('#'+messages[i].element+'_error').attr('class'))
						{
							if (messages[i].msg)
							{
								$('#'+messages[i].element+'_error').html(messages[i].msg).show();
							}
							else
							{
								$('#'+messages[i].element+'_error').hide();
							}
						}
					}
				}
			},
			complete: function()
			{
				$('#form_loading').remove();
			}
		});
		return false;
	});

	$('#r_cancel_button').click(function(){
		$('#report_dialog').dialog('close');
		return false;
	});

	$('#make_report').click(function(){
		$('#report_reason').val('');
		$('#report_dialog .form_error').hide();
		
		$('#report_dialog').dialog('open');
		
		return false;
	});
	
	
	// okno dialogowe udostepniania mailem
	$("#share_mail_dialog").dialog({
			autoOpen: false,
			bgiframe: true,
			modal: true,
			width: 600
		});

	// udostepnianie mailem
	$('#s_add_button').click(function(){
		$.ajax({
			type: 'POST',
			url: $('form#s_add_mail').attr('action'),
			data: $('form#s_add_mail').serialize(),
			dataType: 'json',
			
			beforeSend: function()
			{
				$('form#s_add_mail button#s_cancel_button').after('<div id="form_loading" class="loading right"></div>');
			},
			success: function(msg)
			{
				if (msg.error == 'false')
				{
					$('#share_mail_dialog').dialog('close');
					msg_dialog = $('<div/>').attr('id', 'report_msg_dialog').attr('title', 'Info').html(msg.result).dialog({autoOpen: false, bgiframe: true, modal: true, width: 400, buttons: { Ok: function() { $(this).dialog('close'); }}}).dialog('open');
				}
				else
				{
					// wyswietl bledy walidacji
					messages = msg.error_msg;
					for(var i = 0; i < messages.length; i++)
					{
						if ($('#'+messages[i].element+'_error').attr('class'))
						{
							if (messages[i].msg)
							{
								$('#'+messages[i].element+'_error').html(messages[i].msg).show();
							}
							else
							{
								$('#'+messages[i].element+'_error').hide();
							}
						}
					}
				}
			},
			complete: function()
			{
				$('#form_loading').remove();
			}
		});
		return false;
	});

	$('#s_cancel_button').click(function(){
		$('#share_mail_dialog').dialog('close');
		return false;
	});

	$('#share_mail').click(function(){
		$('#shared_mail').val('');
		$('#share_mail_dialog .form_error').hide();
		
		$('#share_mail_dialog').dialog('open');
		
		return false;
	});
	
	
	$('#share_forum').click(function() {
		var link = $(this).attr('href');
		var page = location.href;
		var msg = $('<input/>').val('[url='+page+'][img]'+link+'[/img][/url]').addClass('form_input').css('width', '100%').focus(function () { $(this).select(); });
		
		forum_dialog = $('<div/>').attr('id', 'share_forum_dialog').attr('title', 'Link').html(msg).dialog({autoOpen: false, bgiframe: true, modal: true, width: 400, buttons: { Ok: function() { $(this).dialog('close'); }}}).dialog('open');
		
		return false;
	});
	
	
	$('a.gallery_resize').live('click', function() {
		var big;
		var item = $('#gallery_main_item');
		var type = $(this).attr('rel');
		
		if(!$('#gallery_main_item_big').length)
		{
			big = $('<div/>').attr('id', 'gallery_main_item_big').addClass('box').html( item.html() ).insertBefore('#content').hide();
			
			if(type == 'video')
			{
				source = big.find('div.gallery_main_item_source');
				nh = parseInt(source.css('height')) * 913 / parseInt(source.css('width'));
				source.css('width', '913px').css('height', nh+'px');
			}
			else
			{
				source = big.find('a.gallery_main_item_source');
				source.find('img').attr('src', source.attr('href')).css('width', '913px');
				big.find('.i_gallery_photo_resize').addClass('i_gallery_photo_resize_back');
			}
		}
		else
		{
			big = $('#gallery_main_item_big');
		}
		
		item.toggle();
		big.toggle();
		
		var prev = $('.navi a.arrow_left');
		var next = $('.navi a.arrow_right');
		var prev_link = prev.attr('href');
		var next_link = next.attr('href');
		
		if(big.is(':visible'))
		{
			if(prev && prev_link.indexOf('/size/big') < 0)
			{
				prev.attr('href', prev_link + '/size/big');
			}
			if(next && next_link.indexOf('/size/big') < 0)
			{
				next.attr('href', next_link + '/size/big');
			}
		}
		else
		{
			if(prev)
			{
				prev.attr('href', prev_link.replace('/size/big', ''));
			}
			if(next)
			{
				next.attr('href', next_link.replace('/size/big', ''));
			}
		}
		
		return false;
	});
	
	if(window.location.href.indexOf('/size/big') >= 0)
	{
		$('a.gallery_resize:first').trigger('click');
	}
	
	
	// stronicowanie komentarzy
	$('#comments_list div.navi p.pagination a').live('click', function() {
		var url = $('#add_comment form').attr('action') + '/' +$(this).attr('rel');
		get_comments(url);
		return false;
	});
	
	// dodawanie komentarza
	$('#add_comment form').submit(function(){
		var form = $(this);
		var text = $('textarea#comment').val();
		
		if(!text.length)
		{
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: form.attr('action'),
			data: form.serialize(),
			dataType: 'json',
			
			beforeSend: function()
			{
				$('#add_comment button#comment_submit').after('<div id="comment_form_loading" class="loading right"></div>');
			},
			success: function(msg)
			{
				if (msg.error == 'false')
				{
					$('#comments_list').show();
					get_comments( form.attr('action') );
					$('textarea#comment').val('');
					$('.comments_counter').text( msg.msg );
				}
				else
				{
					// wyswietl bledy walidacji
					messages = msg.error_msg;
					
					if(typeof(messages) == "string")
					{
						msg_dialog = $('<div/>').attr('id', 'msg_dialog').attr('title', 'Error').html(messages).dialog({autoOpen: false, bgiframe: true, modal: true, width: 400, buttons: { Ok: function() { $(this).dialog('close'); }}}).dialog('open');
					}
					else
					{
						for(var i = 0; i < messages.length; i++)
						{
							$('#'+messages[i].element).after('<div id="'+messages[i].element+'_error" class="form_error" style="display: none;"></div>');
							if ($('#'+messages[i].element+'_error').attr('class'))
							{
								if (messages[i].msg)
								{
									$('#'+messages[i].element+'_error').html(messages[i].msg).show();
								}
								else
								{
									$('#'+messages[i].element+'_error').hide();
								}
							}
						}
					}
				}
			},
			complete: function()
			{
				$('#comment_form_loading').remove();
			}
		});
		return false;
	});
	
	// ikony
	$('.photo_image_caption').hide();
	$('.photo_image').hover(function() {
		$(this).find('.photo_image_caption').show();
	}, function() {
		$(this).find('.photo_image_caption').hide();
	});
});