var _imageUploadStatus = 'closed';
var _imageUpload = null;

function showImageUpload( word_id, user_id, evt ){

	if( _imageUploadStatus == 'closed' ){
		if( _imageUpload == null ){
			_imageUpload = $("<div id=\"image_upload\" onclick=\"cancelBubble( event );\"><form name=\"image_upload\" enctype=\"multipart/form-data\" action=\"\" method=\"post\"><strong>Upload an image</strong><br /><input name=\"image_ul\" size=\"30\" type=\"file\" id=\"image_ul\"><input name=\"upload\" id=\"upload\" value=\"Upload\" type=\"button\" onclick=\"ajaxFileUpload('" + word_id + "', '" + user_id + "');\"></form></div>").addClass("divImageUpload").appendTo("body");
		}
		if( evt.pageX ){
			_imageUpload.css("left", evt.pageX).css("top", evt.pageY);
		}
		else{
			_imageUpload.css("left", evt.clientX).css("top", evt.clientY);
		}
		_imageUpload.fadeTo("slow", 1);
		_imageUpload.show();
		_imageUploadStatus = 'open';
		cancelBubble( evt );
	}
}

function showLogin( evt ){

	alert( 'You need to log in.\nUse the login box at the top right of the screen,\nor register an account if you haven\'t already done so.' );
}

function hideImageUpload(){
	if( _imageUploadStatus == 'open' ){
		_imageUpload.fadeTo("slow", 0, function(){
			_imageUpload.hide();
		});
		_imageUploadStatus = 'closed';
	}
}

function clearWindows(){
//alert(_imageUploadStatus );
	hideImageUpload();
}



function cancelEvent ( e )
{
	if ( e.preventDefault ){
		e.preventDefault();
	}
	e.returnValue = false;
}


function cancelBubble ( e )
{
	if( e.stopPropogation ){
	    e.stopPropogation();
	}
	e.cancelBubble = true;
}

function ajaxFileUpload( word_id, user_id )
{
    //starting setting some animation when the ajax starts and completes
    $("#loading")
    .ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });
       
    /*
        prepareing ajax file upload
        url: the url of script file handling the uploaded files
                    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
        dataType: it support json, xml
        secureuri:use secure protocol
        success: call back function when the ajax complete
        error: callback function when the ajax failed
           
            */
    $.ajaxFileUpload
    (
        {
            url: _site_folder + 'ajax_functions.php?function_name=upload_image&word_id=' + word_id + '&user_id=' + user_id,
            secureuri:false,
            fileElementId:'image_ul',
            dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
						hideImageUpload();
					}else
					{
						alert(data.msg);
						hideImageUpload();
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
				hideImageUpload();
			}
        }
    )
       
    return false;

}  