$(document).ready(function () {


        $("#upload_form").submit(function() {
            var message = '';
            if ($('#upload_form input[type="radio"]:checked').val() != 'url' && $('#upload_form #the_file').val() == '') {
                message += "* Please select a file to upload\n";
            }
            if ($('#upload_form input[type="radio"]:checked').val() == 'url' && $('#upload_form #the_url').val() == '') {
                message += "* Please enter a URL to upload\n";
            }

            if ($('#upload_form input[type="radio"]:checked').val() == undefined) {
                message += "* Please select a file type\n";
            }
            if (message) {
                alert(message);
                return false;
            }
            if ($('#upload_form input[type="radio"]:checked').val() == 'video') {
                var uid = $('#upload_form #uid').val();
                var leftVal = (screen.width / 2) - 200;
                var topVal = (screen.height / 2) - 200;
                window.open('/progresswindow.php?UPLOAD_IDENTIFIER='+uid,'UploadMeter','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no,width=400,height=400,left='+leftVal+',top='+topVal, true);
            }
            $('#home_iframe').show();
            $('#upload_form').hide();
            return true;
        });

        $("#multi_upload_form").submit(function() {
            var message = '';
            var file_inputs = $('#multi_upload_form input[type="file"]');
            var found_file = false;
            for (var i = 0; i < file_inputs.length; i++) {
                if (file_inputs[i].value != '') {
                    found_file = true;
                }
            }
            if (!found_file) {
                message += "* Please select a file to upload\n";
            }
            if ($('#multi_upload_form input[type="radio"]:checked').val() == undefined) {
                message += "* Please select a file type\n";
            }
            if (message) {
                alert(message);
                return false;
            }
            $('#home_iframe').show();
            $('#multi_upload_form').hide();
            return true;
        });

/*	--------------------------------------------------
	Account Settings
	--------------------------------------------------	*/

		// Toggle the theming sections
		$("input#select-theme").click(function() {
			$("div#custom-theme-settings").hide();
			$("select#theme-list").removeAttr("disabled");
		});
		
		$("input#create-theme").click(function() {
			$("div#custom-theme-settings").show();
			$("select#theme-list").attr("disabled", true);
		});
		
		// Toggle inputs on profile pictures
		$("#profile-pic-link").click(function() { $("#profile-picture-link").attr("checked", true); });
		$("#profile-pic-upload").click(function() { $("#profile-picture-upload").attr("checked", true); });
	
		// Toggle the passwords
		$("div.password a").click(function () { $("div.password").toggle(); return false; });
		
		$("div.account-delete a").click(function () { $("div.account-delete").toggle(); return true; });
	
		$("#delete-reason").change(function() {
			if ($("#delete-reason").val() == "other") {
				$("#delete-reason-other").show();
			} else {
				$("#delete-reason-other").hide();
			}
		});

        
/*	--------------------------------------------------
	Uploaders
	--------------------------------------------------	*/

		// Toggle inline uploader for bulk uploader
		$(".toggle-upload").click(function () {
			$("form.upload").toggle();
			$("form.upload span.new-album").hide(); // Hide the span containing the input, submit, cancel
			$("form.upload div.actions select").css("display", "inline"); // Show the list
			$("form.upload div.actions select option.choose").attr("selected", true); // Select the first option, Choose album...
			return false;
		});
		
		// Toggle the resize pulldown when selecting the video/image radio button using classes because toggle will use display: block;
		$("form.upload #video").click(function() { 
                $("#dimension").hide(); 
                $("#video-settings").css("display", "inline"); 
                $("#the_url").val('').hide(); 
                $("#the_file").css("display", "inline") 
        });
		$("form.upload #image").click(function() { 
                $("#dimension").css("display", "inline"); 
                $("#video-settings").hide(); 
                $("#the_url").val('').hide();; 
                $("#the_file").css("display", "inline") 
        });
        $("form.upload #url").click(function() { 
                document.forms.upload_form.reset();
                $("span.file-type #url").attr('checked','checked');
                $("#dimension").css("display", "inline"); 
                $("#video-settings").hide(); 
                $("#the_file").hide(); 
                $("#the_url").css("display", "inline") 
        });
		
		// Show the create new album section when necessary
		$("#multi_upload_form select.add-to-album").change(function () {
			$("#multi_upload_form select option.create-new:selected").each(function () {
				$("#multi_upload_form select.add-to-album").hide(); // Hide the select list
				$("#multi_upload_form span.new-album").css("display", "inline"); // Show the create new flow
        		var newAlbum = $("#multi_upload_form div.actions .input-text").val(); // Set a variable
			});
        })

		$("#upload_form select.add-to-album").change(function () {
			$("#upload_form select option.create-new:selected").each(function () {
				$("#upload_form select.add-to-album").hide(); // Hide the select list
				$("#upload_form span.new-album").css("display", "inline"); // Show the create new flow
        		var newAlbum = $("#upload_form div.actions .input-text").val(); // Set a variable
			});
        })
                        
        // Cancel out of the new album, take me back to the select list
        $("form.upload div.actions a.cancel").click(function() {
			$("form.upload span.new-album").hide(); // Hide the span containing the input, submit, cancel
			$("form.upload div.actions select").css("display", "inline"); // Show the list
            $("#create-new-album").val("");
			$("form.upload div.actions select option.choose").attr("selected", true); // Select the first option, Choose album...
        });

/*	--------------------------------------------------
	Bulk Actions
	--------------------------------------------------	*/
		
		// On click, show me the tags input field
		$(".toggle-tagging").click(function () {
			$("li.tagging").toggle();
			return false;
		});
		
		// On click, enable bulk actions, show the bulk actions bar, and add a class of selected to the Organize link
		$("p#organize, ul.bulk-actions-options li.close a").click(function () {				
			$("p#organize").toggleClass("active");
			$("form.bulk-actions div.browsestuff ul li a div, ul.bulk-actions-options, ul.filters").toggle();
			if ($("form.bulk-actions #organize-status").val() == 'true') {
				$("form.bulk-actions #organize-status").val('false');
				return false;
			} else {
				$("form.bulk-actions #organize-status").val('true');
				return true;
			}
		});
		
		// Add to album
		$("form.bulk-actions select#add-to-album").change(function () {
			$("form.bulk-actions select option.create-new:selected").each(function () {
				$("form.bulk-actions span.add-to-album").hide(); // Hide the select list
				$("form.bulk-actions span.new-album").css("display", "inline"); // Show the create new flow
        		var newAlbum = $("form.bulk-actions div.actions .input-text").val(); // Set a variable
			});
        })

        // Cancel out of the new album, take me back to the select list
        $("form.bulk-actions a.cancel").click(function() {
			$("form.bulk-actions span.new-album").hide(); // Hide the span containing the input, submit, cancel
			$("form.bulk-actions span.add-to-album").css("display", "inline"); // Show the list
			$("form.bulk-actions select option.choose").attr("selected", true); // Select the first option, Choose album...
        });

				
		// On click, add class to show img, vid, or fx is selected
		$("form.bulk-actions div.browsestuff ul li a").click(function () {
			if ($("form.bulk-actions #organize-status").val() == 'true') {
				if ($(this).hasClass('selected')) {
					$(this).removeClass('selected');
					$(this).find('input[type="checkbox"]').removeAttr('checked');
				} else {
					$(this).addClass('selected');
					$(this).find('input[type="checkbox"]').attr('checked','checked');
				}
				return false;
			} else { return true; }
		});
		
		// On click, toggle the checkbox
		$("form.bulk-actions div.browsestuff ul li a input:checkbox").focus(function() {
			if ($(this).attr('checked')) {
				$(this).removeAttr('checked');
			} else {
				$(this).attr('checked', true);
			}
			return false;
		});
		
		// On click, apply checked to all checkboxes and add a class of selected
		$(".select-all").click(function () {
			$("form.bulk-actions input[type=checkbox]").attr('checked', 'checked');
			$("form.bulk-actions li a").addClass("selected");
			return false;
		});
		
		// On click, remove checked to all checkboxes and remove selected class
		$(".deselect-all").click(function () {
			$("form.bulk-actions input[type=checkbox]").attr('checked', '');
			$("form.bulk-actions li a").removeClass("selected");
			return false;
		});

        $("form.bulk-actions ul.bulk-actions-options #add-button").click(function() {           
            if (!$("form.bulk-actions ul.bulk-actions-options #add-to-album").val()) {
                alert("Please select an album");
                return false;
            }
        	if (confirm("Do you really want to update the album association?")) {
				$("form.bulk-actions #action").val('move');
				$("#bulk-actions-form").submit();
			}
			return false;

        });
        $("form.bulk-actions ul.bulk-actions-options #create-add-button").click(function() {           
            if (!$("form.bulk-actions ul.bulk-actions-options #create-album").val()) {
                alert("Please enter an album name");
                return false;
            }
        	if (confirm("Do you really want organize these items in a new album?")) {
				$("form.bulk-actions #action").val('move');
				$("#bulk-actions-form").submit();
			}
			return false;

        });

		$("form.bulk-actions ul.bulk-actions-options li.delete").click(function() {
			if (confirm("Do you really want to delete these items?")) {
				$("form.bulk-actions #action").val('delete');
				$("#bulk-actions-form").submit();
			}
			return false;
		});

		$(".tagging #save-tags").click(function() {
			if (confirm("Do you want to apply these tags to all these items?")) {
				$("form.bulk-actions #action").val('tag');
				$("#bulk-actions-form").submit();
			}
		});

		$(function() { $(".input-date").date_input(); });

/*	--------------------------------------------------
	Album Functions
	--------------------------------------------------	*/

		$("li.toggle-album-info a").click(function() {
			$("li.toggle-album-info a").parent().toggleClass("active");
			$("div#album-info").toggle();
			$("li.album-info").toggle();
		});

		// Prompt members to confirm deletion
		$("div.delete-album a").click(function() {
			if (confirm("Are you sure you want to delete this album?")) {
                $('form.bulk-actions #action').val('delete');
                $('#new-album').submit();
            }
		});

        $(function(){$('#album-description').keyup(function(){limitChars('album-description',150,'counter');})});

        $("#new-album").submit(function() {
            var message='';
            if ($("#album-name").val() === '')
            {
                message = "* Please enter a valid album name\n";
                $("#album-name").css('border', '1px solid red');
            }
            if (message !== '') {
                alert(message);
                return false;
            }
            return true;

        });

});
function confirmDelete(type)
{
    var message = "Are you sure you want to delete this";
    if (type == 'image') {
        message = message + " image?";
    } else if (type == 'video') {
        message = message + " video?";
    } else if (type == 'flek') {
        message = message + " TinyFX?";
    } else if (type == 'album') {
        message = message + " Album?\nNote: your content will not be removed";
    }
    return confirm(message);
}


function submitDeleteAccountSettingsForm(form)
{
    var message='';
    if (confirm("Are you really sure you want to delete your account?")) {
        if (form.delete_password.value === '') {
            message = message + "* Please enter your password\n";
        }
        if (form.delete_reason.selectedIndex == 0) {
            message = message + "* Please select a reason\n";
        }
        if (form.delete_validation.value === '') {
            message = message + "* Please enter the security code\n";
        }
        if (message !== '') {
            alert(message);
            return false;
        } else {
            form.action.value = 'delete';
            form.submit();
        }
    }
    return false;
}
function submitUndeleteAccountSettingsForm(form)
{
    var message='';
    if (confirm("Are you really sure you want to undo the delete?")) {
        if (form.delete_password.value === '') {
            message = message + "* Please enter your password\n";
        }
        if (form.delete_validation.value === '') {
            message = message + "* Please enter the security code\n";
        }
        if (message !== '') {
            alert(message);
            return false;
        } else {
            form.action.value = 'undelete';
            form.submit();
        }
    }
    return false;
}
function submitPasswordAccountSettingsForm(form)
{
    var message='';
    if (form.current_password.value === '') {
        message = message + "* Please enter your current password\n";
    }
    if (form.new_password.value != form.confirm_new_password.value) {
        message = message + "* Your new passwords do not match\n";
    }
    if (form.new_password.value.length < 6) {
        message = message + "* Your new password is too short (minimum 6 characters)\n";
    }
    if (message !== '') {
        alert(message);
        return false;
    } else {
        form.submit();
    }
}
