function addAlbum()
{
    if ($('#title').val() == '')
    {
        $('#errors').append('Please, fill in album\'s title.').show();
        return false;
    }

    if ($('#checkbox').attr('checked') == false)
    {
        $('#errorsCheckbox').append("Please, confirm the album will not contain any forbidden material!").show();
        return false;
    }

    $.post('/user/image/addalbum/',
    {
        'title': $('#title').val(),
        'location': $('#location').val(),
        'status': $('#status').val()
    },
    function (responceObject){
    uploadPhotos(responceObject)});
}

function deleteAlbum(albumID)
{
    var answer = confirm('This action will also delete all the images of the album!Do you wish to delete it anyway?');
    if (answer)
    {
        $.post('/user/image/deletealbum/',
        {
            'albumID': albumID
        }, function (responceObject)
        {
            if (responceObject == 'success');
            {
                $("#albumID_" + albumID).empty().html('<tr><td class="albums_item_center"><div style="text-align: center; padding: 0px 0px 8px 0px;"><strong style="color: #FF7700; font-weight: bold;">The album and all its images have been successfully deleted!</strong></div></td></tr>');
            }
        });
    }
}

function displayResult()
{
    showPage('photos');
}

function editAlbum(albumID, modify)
{
    $.post('/user/image/editalbum/',
        {
            'albumID': albumID,
            'coverID': $('#coverIDEdit').val(),
            'modify': modify,
            'title': $('#edAlbumTitle').val(),
            'location': $('#edAlbumLocation').val(),
            'status': $('#edAlbumStatus').val()
        },
        function(objResp)
        {
            if (modify)
            {
                document.location.href = '/user/index/index/citem/photos/';
                return true;
            }

            $('#other_container').html(objResp.content);
            $('#edAlbumTitle').val(objResp.albumDetails.title);
            $('#edAlbumTitleHead').text(objResp.albumDetails.title);
            $('#edAlbumLocation').val(objResp.albumDetails.location);
            $('#coverIDEdit').val(objResp.albumDetails.coverID);

            if (objResp.albumDetails.coverID)
                $('#edImg').attr('src','/public/media/Images/Thumbnails/Large/' + objResp.albumDetails.userID + '/' + objResp.albumDetails.coverID +'.jpg');
            else
                $('#edImg').attr('src','http://mvpspot.com/public/images/empty_images.jpg');

            switch (objResp.albumDetails.status)
            {
                case 'public':
                    $('#edPublic').attr('selected','selected');
                    break;
                case 'private':
                    $('#edPrivate').attr('selected','selected');
                    break;
                case 'limited':
                    $('#edLimited').attr('selected','selected');
                    break;
            }
        },
        'json'
    );

    $('#main_profile_container').hide();
    $('#other_container').show();
}

function moveToAlbum(albumID, currentAlbum)
{
    var imagesArr = new Object;
    imagesArr = $("input[type='checkbox']:checked");

    if (imagesArr.length < 1)
        return false;

    $.ajax(
    {
        url: '/user/image/move/albumID/' + albumID + '/currentAlbum/' + currentAlbum,
        type: 'post',
        data: imagesArr,
        success: hideImageBoxes(imagesArr)
    });
}

function hideImageBoxes(imagesArr)
{
    $.each(imagesArr, function()
    {
        id = $(this).val();
        $("#img_cell_" + id).remove();
    });
}

function processAllImages(sel)
{
    if (sel == 0)
        $("input[id*='imageID']").attr('checked', false);
    else
        $("input[id*='imageID']").attr('checked', true);
}

function deleteImages()
{
    var imageID = new Object;
    var imageID = $("input[name*=imageID]:checked");

    if (imageID.length < 1)
        return false;

    $.ajax(
    {
        url: '/user/image/multipledelete/',
        type: 'post',
        data: imageID,
        success: hideImageBoxes(imageID)
    });
}

function deleteOneImage(imageID, elementID)
{
    if (imageID < 1)
        return false;

    $.post('/user/image/multipledelete/',
    {
        'imageID': imageID
    }, function (responceObject)
    {
        if (responceObject == 'success');
        {
            $("div[id='upd_image_" + elementID + "']").empty().html('<strong class="upload_add_more_text">This image was sucessfully deleted!</strong>');
        }
    });

}

function editImages(albumID)
{
    var imageID = new Object;
    var imageID = $("input[type='checkbox']:checked");

    if (imageID.length < 1)
        if (imageToEdit)
            imageID = 'imageID[]='+imageToEdit;
        else
            return false;

    $.ajax(
    {
        url: '/user/image/edit/albumID/' + albumID,
        type: 'post',
        data: imageID,
        dataType: 'json',
        success: outputResult
    });
}

function modifyImages(albumID)
{
    var categories = $("select[name*=categoryID]").serialize();
    var titles = $("input[id*=title_]").serialize();
    var tags  = $("input[id*=tags_]").serialize();

    if (titles.length < 1)
        return false;

    $.ajax(
    {
        url: '/user/image/edit/modify/1/albumID/' + albumID,
        type: 'post',
        dataType: 'json',
        data: categories + '&' + titles + '&' + tags,
        success: window.location.href = "/user/index/index/citem/viewalbum/fid/" + albumID
    });
}

function setAs(option, albumID)
{
    var coverID = $("input[name*=imageID]:checked");
    $('div[id*=remote_]').css('border', '1px solid #DCDCDC');

    if (coverID.length < 1)
        return false;

    if (option == 'cover')
        $.ajax(
        {
            url: '/user/image/setcover/albumID/' + albumID,
            type: 'post',
            data: coverID,
            dataType: 'json',
            success:
            function (respondObject)
            {
                $('#remote_' + respondObject).css('border', '1px solid #006A89');
                $('#coverDone').show();
                setTimeout("$('#coverDone').hide()", 2000);
            }
        });
    else
        if (option == 'profile')
        {
            $.ajax(
            {
                url: '/user/image/default/',
                type: 'post',
                data: coverID,
                dataType: 'json',
                success:
                function (respondObject)
                {
                    $('#remote_' + respondObject).css('border', '1px solid #008BB5')
                    $('#profDone').show();
                    setTimeout("$('#profDone').hide()", 2000);
                }
            });
        }

    processAllImages(0);
}

function popupPos(evt, elementID)
{
    var posx = 0;
    var posy = 0;
    evt = (evt) ? evt : (window.event) ? window.event : "";
    if(evt)
    {
        if (evt.pageX || evt.pageY)
        {
            posx = evt.pageX;
            posy = evt.pageY;
        }
        else if (evt.clientX || evt.clientY)
        {
            posx = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            posy = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }

        offset=78;
        $("#maxi_thumb_" + elementID).css("top", (posy - 80- offset/2) + 'px');
        $("#maxi_thumb_" + elementID).css("left",  (posx - 145) + 'px');
    }
}

function uploadPhotos(albumID)
{
    $.post('/user/image/upload/',
    {
        'albumID': albumID
    }, outputResult,"json");
}

function showLoadingImg(elementID)
{
    $("div[id='ajaxLoader_" + elementID + "']").show();
}

function checkItem(elementID)
{
    var title_img   = '';
    var img     = '';

    //getting rid of old error notifications
    $("div[id='img_error_" + elementID + "']").css('display', 'none');
    $("div[id='img_error_2_" + elementID + "']").css('display', 'none');
    $("div[id='img_title_error_" + elementID + "']").css('display', 'none');
    $("#errors_after_ajax").hide();
    $("input[id='img_" + elementID + "']").css('border', '1px solid #89B8C6');
    $("input[id='img_title_" + elementID + "']").css('border', '1px solid #89B8C6');

    title_img   =  $("input[id='img_title_" + elementID + "']").val();
    img         =  $("input[id='img_" + elementID + "']").val();

    if (img == '' &&  title_img == '')
    {
        $("div[id*=ajaxLoader]").hide();
        return true;
    }
    else
    {
        if (img != '' &&  title_img != '' && $('#alreadySubmitted_' + elementID).val() == 'no')
       {
            $('#alreadySubmitted_' + elementID).val('yes');
            $('#image_mltpl_upload_' + elementID).submit();
            return true;
        }
        else
        {
            if (img != '' &&  title_img == '')
            {
                $("div[id='img_title_" + elementID + "']").css('border', '2px solid #DD0000');
                $("div[id='img_title_" + elementID + "']").css('borderCollapse', 'collapse');
                $("div[id='img_title_error_" + elementID + "']").css('display', 'block');

                $("input[id='img_title_" + elementID + "']").attr("onchange", "showLoadingImg('" + elementID + "');" + " checkItem('" + elementID + "')");
            }
            else
            {
                $("div[id='img_" + elementID + "']").css('border', '2px solid #DD0000');
                $("div[id='img_" + elementID + "']").css('borderCollapse', 'collapse');
                $("div[id='img_error_" + elementID + "']").css('display', 'block');
            }
            $("div[id*=ajaxLoader]").hide();
        }
    }
}

function afterSubmitDisplay(responceObject)
{
    $("div[id*=ajaxLoader]").hide();
    if (responceObject.errors)
    {
        $('#errors_after_ajax').css('display', 'block');
        $('#errors_after_ajax').html(responceObject.errors);
        $('#alreadySubmitted_' + responceObject.elementID).val('no');
    }
    else
    {
        var userID = $('#userIDUpl').val();
        jQuery.each(responceObject.filesID, function(i, val)
        {
            newThumbSmall = 'http://mvpspot.com/public/media/Images/Thumbnails/71x78/'+ userID +'/' + val + '.jpg';
            newThumbLarge =  'http://mvpspot.com/public/media/Images/Thumbnails/168x184/'+ userID +'/' + val + '.jpg';
            linkToImage = "/user/image/show/ImageID/" + val;
            fileTitle = $("input[id='img_title_" + i +"']").val();

            $("img[id='ready_thumb_mini_" + i + "']").attr('src', newThumbSmall);
            $("a[id='ready_a_mini_" + i + "']").attr('href', linkToImage);
            $("img[id='ready_thumb_maxi_" + i + "']").attr('src', newThumbLarge);
            $("div[id='img_thumb_" + i + "']").show();
            $("div[id='upd_image_" + i + "']").attr('name', val);

            //make input fields inactive
            $("input[id='img_" + i +"']").remove();
            $("input[id='img_title_" + i +"']").remove();
            $("div[id='file_title_" + i +"']").empty();
            $("div[id='file_title_" + i +"']").html('<strong class="upload_add_more_text">Title: <strong class="upload_little_titles">' + fileTitle + '</strong></strong>');
        });
    }
}

function addMorePhotos(nrPhotos)
{
    $('#morePhotos').empty();
    if (nrPhotos >2)
        $("div[id*=upd_image_]").show();
    else
        $("div[id*=upd_image_]:not(:last)").show();
}