var uploadedImageId = 0;

function confirmMediaAll(txt)
{
    var is_public = $('#public_all').attr('checked');
    var is_publish = $('#publish_all').attr('checked');
    var is_adult = $('#adult_all').attr('checked');
    var category_id = $('#category_all').val();

    if (confirm(txt)) {
        callA('confirmInactiveMediaAll', {
            public: is_public,
            publish: is_publish,
            adult: is_adult,
            category_id: category_id
        }, function(unused) {
            $('#wrap_uploaded').html('');
            iTotalUploads = 0;
            uploadedImageId = 0;
            $('#uploaded_topbar').hide();
        });
    }
}

function removeMediaAll(txt)
{
    if (confirm(txt)) {
        callA('removeInactiveMediaAll', {}, function(unused) {
            $('#wrap_uploaded').fadeOut('fast');
            iTotalUploads = 0;
            uploadedImageId = 0;
            $('#uploaded_topbar').hide();
        });
    }
}

function confirmMedia(media_id, title_msg)
{
    var is_public = $('#public_' + media_id).attr('checked');
    var is_publish = $('#publish_' + media_id).attr('checked');
    var is_adult = $('#adult_' + media_id).attr('checked');
    var title = $('#title_' + media_id).val();

    var category_id = $('#category_' + media_id).val();


    if (title == '') {
        error_message(title_msg);
        return;
    }
    if (is_adult === undefined) is_adult = false;

    callA('confirmInactiveMedia', {
        id: media_id,
        title: title,
        public: is_public,
        publish: is_publish,
        adult: is_adult,
        category_id: category_id
    }, function(unused) {
        $('#wrap_media_' + media_id).fadeOut('fast');
    });
    //$('#public_' + media_id).attr('checked');
}

function removeMedia(media_id, txt)
{
    if (confirm(txt)) {
        callA('removeInactiveMedia', {id: media_id}, function(unused) {
            iTotalUploads--;
            $('#wrap_media_' + media_id).fadeOut('fast');
            if (iTotalUploads <= 0) {
                $('#uploaded_topbar').hide();
            }
        });
    }
}

function ajaxFileUpload()
{
    if (uploaded_last !== undefined && uploaded_last > -1) {
        uploadedImageId = uploaded_last+1;
        uploaded_last = -1;
        iTotalUploads = 0;
    }
    $("input:file").each(function(index, domElement) {
        if (domElement.id.substr(0,11) == 'upload_file') {
            if ($(domElement).val() != '') {
                uploadImg(domElement.id);
            }
        }
    });

    return false;
}

function ajaxURLUpload()
{
    if (uploaded_last !== undefined && uploaded_last > -1) {
        uploadedImageId = uploaded_last+1;
        uploaded_last = -1;
        iTotalUploads = 0;
    }
    $("input:text").each(function(index, domElement) {
        if (domElement.id.substr(0,10) == 'upload_url') {
            if ($(domElement).val() != '') {
                uploadURL(domElement.id, $(domElement).val());
                $(domElement).val('');
            }
        }
    });

    return false;
}

function uploadImg(fileElementId)
{
    //alert(uploadedImageId);
    //return;
    var oImage = document.createElement('img');
    oImage.src = '/images/ajax-loader-3.gif';
    var oEl = document.createElement('div');
    oEl.className = 'uploaded_preview';
    oEl.id = 'uploading_' + uploadedImageId;
    oEl.style.paddingTop = '5px';
    oEl.style.marginBottom = '10px';
    $('#wrap_uploaded').append(oEl);

    $('#'+oEl.id).append(oImage);

    iTotalUploads++;

    if (iTotalUploads > 0) {
        $('#uploaded_topbar').show();
    }

    $.ajaxFileUpload
    (
        {
            url:'/ajax.php?type=json&fx=upload&el=' + fileElementId +
                '&target_el=' + uploadedImageId,
            secureuri:false,
            fileElementId: fileElementId,
            dataType: 'json',
            success: function (data, status)
            {
                if(typeof(data.error) != 'undefined') {
                    if(data.error != '') {
                        error_message(data.error);
                        return;
                    }
                }

                callA('getHTMLUploaded', {id: data.new_id}, function(html) {
                    $('#uploading_' + data.target_el).html(html);
                    $('#uploading_' + data.target_el).css('padding-top', '0px');
                    $('#uploading_' + data.target_el).css('margin-bottom', '0px');
                });
            },
            error: function (data, status, e)
            {
                error_message(e);
                return;
            }
        }
    );

    uploadedImageId++;
}


function uploadURL(fileElementId, url)
{
    var oImage = document.createElement('img');
    oImage.src = '/images/ajax-loader-3.gif';
    var oEl = document.createElement('div');
    oEl.className = 'uploaded_preview';
    oEl.id = 'uploading_' + uploadedImageId;
    oEl.style.paddingTop = '5px';
    oEl.style.marginBottom = '10px';
    $('#wrap_uploaded').append(oEl);

    $('#'+oEl.id).append(oImage);

    iTotalUploads++;

    if (iTotalUploads > 0) {
        $('#uploaded_topbar').show();
    }

    callA('upload_url', {
        el: fileElementId,
        url: url,
        target_el: uploadedImageId
    }, function(txt) {
        if (txt === '0') {
            $('#uploading_' + uploadedImageId).html('');
        } else {
            var ll = txt.split(',');

            callA('getHTMLUploaded', {id: ll[0]}, function(html) {
                $('#uploading_' + ll[1]).html(html);
                $('#uploading_' + ll[1]).css('padding-top', '0px');
                $('#uploading_' + ll[1]).css('margin-bottom', '0px');
            });
        }
    });
    /*
    $.ajaxFileUpload
    (
        {
            url:'/ajax.php?type=json&fx=upload&el=' + fileElementId +
                '&target_el=' + uploadedImageId,
            secureuri:false,
            fileElementId: fileElementId,
            dataType: 'json',
            success: function (data, status)
            {
                if(typeof(data.error) != 'undefined') {
                    if(data.error != '') {
                        error_message(data.error);
                        return;
                    }
                }

                callA('getHTMLUploaded', {id: data.new_id}, function(html) {
                    $('#uploading_' + data.target_el).html(html);
                    $('#uploading_' + data.target_el).css('padding-top', '0px');
                    $('#uploading_' + data.target_el).css('margin-bottom', '0px');
                });
            },
            error: function (data, status, e)
            {
                error_message(e);
                return;
            }
        }
    );
    */

    uploadedImageId++;
}

















function error_message(msg)
{
    alert(msg);
}

function callA(func, params, handler)
{
    var dataArray = params;
    dataArray.fx = func;

    $.ajax({
        url: '/ajax.php',
        data: dataArray,
        type: 'GET',
        dataType: 'text',
        timeout: 20000,
        error: function() {

        },
        success: function(txt) {
            handler(txt);
        }
    });
}

function showHide(divname)
{
    var obj = document.getElementById(divname);
    if (obj.style.display == 'none' ){
        obj.style.display = 'block';
    } else {
        obj.style.display = 'none';
    }
}
