// empty filename from form textfield 
function emptyFile(element) {

  document.getElementById(element).value = "";
  document.getElementById('hidden_'+element).value = "";
}

// add filename to form textfield
function addCustomFile(filename, element) {

  document.getElementById(element).value = filename;
  document.getElementById('hidden_'+element).value = filename;
}

// add file to FCK
function addFile(element, dir, lang, table) {

  window.open('/?method=filebrowser&fullscreen=true&element='+
              element+'&opendir='+dir+'&lang='+lang+'&table='+table,"filebrowser",
              "toolbar=0,location=0,directories=0,status=no,menubar=0,scrollbars=yes,width=660,height=510,resizable=no,top=0,left=0");
}

// add new item to 3rd party tables - needs ajax to update existing elements after add
function addNewElement(table, lang) {

  window.open('/?method=form&table='+table+'&lang='+lang+'&fullscreen=true',"filebrowser",
              "toolbar=0,location=0,directories=0,status=no,menubar=0,scrollbars=yes,width=660,height=475,resizable=no,top=0,left=0");
}

function openFilebrowser(table, lang) {

  window.open('/?method=filebrowser&table='+table+'&lang='+lang+'&fullscreen=true&nofilelinks=true',"filebrowser",
              "toolbar=0,location=0,directories=0,status=no,menubar=0,scrollbars=yes,width=678,height=510,resizable=no,top=0,left=0");
}

// Delete a file
function deleteFile(filename, action, question) {
    var location = window.location.href;
    if( confirm( question ) ) {
        $.post(
            location, {
                action: action
            , selectedfile: filename
        }, function(data) {
            window.location = location;
        });
    }
}

// TinyMCE insert image
function InsertCustomImage( url ) {
    var oEditor = window.parent;
    oEditor.tinyMCE.execCommand('mceInsertContent',false,'<img src="'+url+'" alt="" />');
    //win.document.getElementById(input).value = url;
    tinyMCEPopup.close();
}

// TinyMCE insert custom file
function InsertCustomLink( url, filename ) {
    var oEditor = window.parent;
    var printname = prompt('Linkkisana', '');
        if(printname==null || printname=='') printname = filename;
    
    oEditor.tinyMCE.execCommand('mceInsertContent',false,'<a href="'+url+'">'+printname+'</a>');
    tinyMCEPopup.close();
}

// TinyMCE filebrowser call
function utopiaFileBrowser (field_name, url, type, win) {

    /* If you work with sessions in PHP and your client doesn't accept cookies you
    might need to carry the session name and session ID in the request string (can
    look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5"). These lines of code
    extract the necessary parameters and add them back to the filebrowser URL
    again.*/
    
    var cmsURL = "?method=filebrowser&fullscreen=true";
    //var originalCss = tinyMCE.activeEditor.settings.popup_css;
    //tinyMCE.activeEditor.settings.popup_css = null;
    
    tinyMCE.activeEditor.windowManager.open({
        file : cmsURL,
        title : '',
        width : 650,  // Your dimensions may differ - toy around with them!
        height : 500,
        resizable : "yes",
        inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
        close_previous : "no"
    }, {
        window : win,
        input : field_name
    });
    //tinyMCE.activeEditor.settings.popup_css = originalCss;
    
    return false;
}

// The uploadSuccess event handler.  This function variable is assigned to upload_success_handler in the settings object
var uploadSuccessEventHandler = function (file, server_data) {
    window.location = window.location.href;
};

// Event handler when upload starts
var uploadStartHandler = function (file) {
    var newwidth    = document.getElementById('utopia_filebrowser_newwidth').value;
    var newname     = document.getElementById('utopia_filebrowser_newname').value;
    var selectmenu  = document.getElementById('utopia_filebrowser_settings');
    var imgsettings = selectmenu.options[selectmenu.selectedIndex].value;
    
    this.addPostParam('newname', newname);
    this.addPostParam('newwidth', newwidth);
    this.addPostParam('settings', imgsettings);
    return true;
};

// Multiupload
function multiUpload(session, url, action) {
    var swfu;
	var settings = {
		flash_url : "shared/swf/swfupload.swf",
		file_post_name : "Filedata",
		upload_url: url,	// Relative to the SWF file
		post_params: {
		    "PHPSESSID" : session
		    , "action" : action
		},
		file_size_limit : "10 MB",
		file_types : "*.*",
		file_types_description : "All Files",
		file_upload_limit : 20,
		file_queue_limit : 0,
		custom_settings : {
			progressTarget : "utopia_filelist"
			,cancelButtonId : "utopia_filebrowser_cancel"
		},
	    debug: false,
	    
	    // Button settings
	    button_placeholder_id: "utopia_filebrowser_fileselect",
        button_width: "110",
        button_height: "23",
		button_text: '<span class="utopia_filebrowser_file">'+action+'</span>',
        button_text_style: ".utopia_filebrowser_file { font-family: Arial; font-size:11px; }",
        button_text_left_padding: 24,
        button_text_top_padding: 3,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor : SWFUpload.CURSOR.HAND,
        
		// The event handler functions are defined in handlers.js
		//file_dialog_start_handler : file_dialog_start_handler,
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStartHandler, //uploadStart
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess, //uploadSuccessEventHandler //uploadSuccess
		upload_complete_handler : uploadSuccess,
		queue_complete_handler : uploadSuccessEventHandler	// Queue plugin event //queueComplete
	};

    return swfu = new SWFUpload(settings);
}


//
$(document).ready(function() {
    $(".utopia_filebrowser_fileselect").click(function() {
        var itemsChecked = $("input[@class=utopia_filebrowser_fileselect]:checked").length; 
        // swfupload
        if (itemsChecked > 0) {
            $("#utopia_filebrowser_save").show();
            $(".swfupload").hide();
            $("#utopia_filebrowser_file").hide();
        } else {
            $("#utopia_filebrowser_save").hide();
            $(".swfupload").show();
            $("#utopia_filebrowser_file").show();
        }
    });
});


