var basePath = '';
var sitePath = basePath + 'index.php';

/**
 * getPhotoWall
 * 
 * Gets photo wall contents
 */
function getPhotoWall()
{
	$('#photo_wall').load(sitePath + '/thebar/get_photo_wall', function() {
		popPhotos();
		
		$('#album').change(function() {
			popPhotos();
		});
	});
}

/**
 * popPhotos
 * 
 * Populates photo list
 */
function popPhotos()
{
	var albumId = $('#album').val();
	
	$('#photos').slideUp('normal').hide();
	
	$.ajax({
		url: sitePath + '/thebar/get_photos',
		type: 'POST',
		data: 'album_id=' + albumId,
		dataType: 'json',
		success: function(response) {
			$('#photos').html('');
			
			var total = response.length;
			
			var rows = '';
			for(var i = 0; i < total; i++)
			{
				var photoId = response[i].photo_id;
				var caption = response[i].caption;
				
				var photo = '<div id="photo_' + photoId + '" class="photo">';
				photo += '<a href="' + response[i].image_path + response[i].image + '" rel="prettyPhoto[pp_gal]" pp_id="' + photoId + '" title="' + caption + '">';
				photo += '<img title="' + response[i].caption + '" src="' + response[i].thumb_path + response[i].image + '" />';
				photo += '</a></div>';
				
				rows += photo;
			}
			
			$('#photos').append(rows);
		},
		complete: function() {
			$('#photos').slideDown('normal').show();
						
			$("#photos").find('a').prettyPhoto({
				counter_separator_label: ' of ',
				theme: 'dark_rounded',
				modal: true,
				intercept: function(a, b) {
					$('#photo_wall').dialog('close');
				},
				allowComments: true,
				commentsUrl: sitePath + '/thebar/get_comments/' + new Date().getTime(),
				callback: function() {
					$('#photo_wall').dialog('open');
				}
			});
		}
	});
}

/**
 * showModal
 */
function showModal()
{
	$.resetMsg();
	
	$('#photo_wall').dialog('destroy');
	
	$('#photo_wall').dialog({
		bgiframe: true,
		resizable: false,
		height: 500,
		width: 820,
		modal: true,
		position: 'center',
		closeOnEscape: true,
		buttons: {
			'Close': function() {
				$(this).dialog('destroy');
			},
			'Add Photo': function() {
				addPhoto();
			}
		},
		open: function() {
			if ($('#add:hidden').length == 0)
			{
				$('#display').slideToggle('normal');
				$('#add').slideToggle('normal');
			}
		},
		close: function() {
			if ($('.pp_overlay').length != 0) return false;
			$(this).dialog('destroy');
		}
	});
	
}

/**
 * addButtonToggle
 * 
 * Toggle display of add button
 */
function addButtonToggle()
{
	$('#display').slideToggle('normal');
	$('#add').slideToggle('normal');
	
	var $add = $('button:contains("Add Photo")');
	
	if ($add.length) 
	{
		$('#photo_wall').dialog('option', 'buttons', {
			'Show Photos': function() {
				addButtonToggle();
			},
			'Submit': function() {
				$('#photo_form').submit();
			}
		});
		
		/*
		$add = $('button:contains("Show Photos")');
		$add.html('Add Photo');
		*/
	}
	else
	{
		$('#photo_wall').dialog('option', 'buttons', {
			'Close': function() {
				$(this).dialog('destroy');
			},
			'Add Photo': function() {
				addPhoto();
			}
		});
	}
	
	
}

/**
 * addPhoto
 * 
 * Shows add photo form
 */
function addPhoto() 
{
	addButtonToggle();
	
	resetAddPhoto();
	
	$('#more').click(function() {
		$('#more_photos').slideDown('normal').removeClass('hidden');
		$(this).addClass('hidden');
	});
	
	setupAddPhotoRules();
}

/**
 * displayPhotos
 * 
 * Shows album and photo list
 */
function displayPhotos(msg, err)
{
	addButtonToggle();
	
	msg = msg || '';
	err = err || '';
		
	resetAddPhoto();
	
	if (msg != '') $.showInfo(msg);
	if (err != '') $.showError(err, 'err_div');
}

/**
 * resetAddPhoto
 * 
 * Clears add photo form
 */
function resetAddPhoto()
{
	$.formReset('photo_form');
	
	$('#msg_div').html('').hide();
	$('span.alert-icon').addClass('hidden');
	$('div.field').removeClass('field-error');
	$('span.error').remove();
	
	$('#more_photos').hide(); // addClass('hidden') doesn't work
	$('#more').removeClass('hidden');
}

/**
 * setupAddPhotoRules
 * 
 * Validation rules for add photo
 */
function setupAddPhotoRules() {
	$.validator.addMethod('requirePhoto', function(value) {
		var valid = false;
		$('input[type="file"]').each(function() {
			if ($(this).val()) 
			{
				valid = true;
				return false;
			}
		});
		
		return valid;
	}, 'At least one photo is required.');
	
	$('#photo_form').validate({
		rules: {
			album_id: 'required',
			guest_name: 'required',
			guest_email: {
				required: true,
				email: true
			},
			new_photo_1: {
				requirePhoto: true,
				accept: 'png|jpe?g|gif'
			},
			new_photo_2: {
				accept: 'png|jpe?g|gif'
			},
			new_photo_3: {
				accept: 'png|jpe?g|gif'
			},
			new_photo_4: {
				accept: 'png|jpe?g|gif'
			},
			new_photo_5: {
				accept: 'png|jpe?g|gif'
			}
		},
		messages: {
			album_id: $.Msgs('required'),
			guest_name: $.Msgs('required'),
			guest_email: {
				required: $.Msgs('required'),
				email: $.Msgs('validEmail')
			},
			new_photo_1: {
				accept: $.Msgs('image')
			},
			new_photo_2: {
				accept: $.Msgs('image')
			},
			new_photo_3: {
				accept: $.Msgs('image')
			},
			new_photo_4: {
				accept: $.Msgs('image')
			},
			new_photo_5: {
				accept: $.Msgs('image')
			}
		},
		onkeyup: false,
		focusInvalid: true,
		errorElement: 'span',
		errorPlacement: function(error, element) {
			var icon = $(element).nextAll('span.alert-icon');
			$(error).insertAfter(icon);
		},
		highlight: function(element, errorClass) {
			$(element).nextAll('span.alert-icon').removeClass('hidden');
			$(element).parent().addClass('field-error');
		},
		unhighlight: function(element, errorClass) {
			$(element).nextAll('span.alert-icon').addClass('hidden');
			$(element).parent().removeClass('field-error');
		},
		submitHandler: function(form) {
			submitAddPhoto();
		}
	});
}

/**
 * submitAddPhoto
 * 
 * Submits form to server
 */
function submitAddPhoto()
{
	$.ajaxFileUpload({
		url: sitePath + '/thebar/submit_photo',
		type: 'POST',
		secureuri: false,
		fileElementId: 'new_photo',
		extraForm: '#photo_form',
		dataType: 'script',
		success: function(response) {
			
		}
	});
}

/**
 * showAllPhotoComments
 * 
 * Shows photo comments in html
 */
function showAllPhotoComments(photoId)
{
	$('div.comment:hidden').slideToggle('normal');
	$('#a_all').hide();
}

/**
 * addPhotoComment
 * 
 * Shows add comment form
 */
function addPhotoComment(photoId)
{
	$('#comment_form').slideToggle('normal');
	$('#add_comment').slideToggle('normal');
	
	if ($('.pp_content').height() < 220)
	{
		$('.pp_content').css({
			'height': '220px'
		});
		
		$('#pp_comments_list').css({
			'height': '180px'
		});
	}
	
	setupAddPhotoCommentRules();
}

/**
 * setupAddPhotoCommentRules
 * 
 * Validation rules for adding comment
 */
function setupAddPhotoCommentRules()
{
	$('#comment_form').validate({
		rules: {
			comment_name: 'required',
			comment_email: {
				required: true,
				email: true
			},
			comment_text: 'required'
		},
		focusInvalid: true,
		errorElement: 'span',
		errorPlacement: function(error, element) {
			
		},
		highlight: function(element, errorClass) {
			$(element).addClass('comment-error');
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass('comment-error');
		},
		submitHandler: function(form) {
			submitAddPhotoComment();
		}
	});
}

/**
 * submitAddComment
 * 
 * Submits new comment
 */
function submitAddPhotoComment()
{
	$.ajax({
		url: sitePath + '/thebar/submit_photo_comment',
		type: 'POST',
		data: $('#comment_form').serialize(),
		dataType: 'json',
		success: function(response) {
			if(response == true) $('#pp_comments_list').load(sitePath + '/thebar/get_comments/' + new Date().getTime(), {pp_id: $('#comment_photo_id').val()});
			else $.showError(null, 'comment_err_div');
		}
	});
}

function showAd()
{
	$('#audition_wrapper').show();
}

function hideAd()
{
	$('#audition_wrapper').hide();
}

