$(function(){
	$(".content-file-icon").bind("click",function(event){
		var obj = new e4FileIconClicked();
		obj.toogle(event, this);
	});
});

var e4FileIconClicked = function(){
	
	this.effectName    = "blind";
	this.effectOptions = {direction: "vertical"};
	this.effectSpeed  = 1000;
	this.fileType;
	
	var thisObj = this;
	
	this.toogle =  function (event, elem){ 
		
        var id = $(elem).attr('id');
		var itemId = id.substr(id.lastIndexOf('-') + 1);
		var tmp = id.substr(0, id.lastIndexOf('-'));
		var fileType = tmp.substr(tmp.lastIndexOf('-') + 1);
		thisObj.fileType = fileType;
		var outputElem = 'content-file-container-' + fileType + '-' + itemId;
		
		var itemTypeTmp = tmp.substr(0, tmp.lastIndexOf('-'));
		var itemType    = itemTypeTmp.substr(itemTypeTmp.lastIndexOf('-')+1);
		if ($(elem).hasClass('file-active-icon')){
            thisObj.hideElem(elem, outputElem);
            return false;
        }
		
		if ($('#' + outputElem).hasClass('file-already-shown')) {
			$('.file-active-icon').removeClass('file-active-icon');
			
			$(elem).addClass('file-active-icon');
			if ($('.file-active-container').is(':visible')) {
				$('.file-active-container:visible').hide(thisObj.effectName, 
				    thisObj.effectOptions, thisObj.effectSpeed, function(){
					thisObj.showElem(outputElem);
				});
			}else{
				thisObj.showElem(outputElem);
			}
		}
		else {
			$.ajax({
				type: "POST",
				url: e4BaseUrl + "plugins-file/index/index",
				data: "item_type=" + itemType + "&file_type=" + fileType + "&item_id=" + itemId,
				dataType : "html",
				beforeSend : function(){
					$(elem).parent().prepend( "<div class='content-files-icons-loader'>&nbsp;</div>" );
				},
				success: function(msg){
				
					$('.file-active-icon').removeClass('file-active-icon');
					$(elem).addClass('file-active-icon');
					
					if (thisObj.fileType != 'video' && thisObj.fileType != 'audio'){
						$('#' + outputElem).html(msg);
					}
					   
					
					if ($('.file-active-container').is(':visible')) {
						$('.file-active-container').hide(thisObj.effectName, 
						thisObj.effectOptions, thisObj.effectSpeed, function(){
							if (thisObj.fileType != 'video' && thisObj.fileType != 'audio'){
								thisObj.showElem(outputElem, undefined, elem);
							}else{
								thisObj.showElem(outputElem, msg, elem);
							}
							
						});
					}
					else {
						if (thisObj.fileType != 'video' && thisObj.fileType != 'audio') {
							thisObj.showElem(outputElem, undefined, elem);
						}
						else {
							thisObj.showElem(outputElem, msg, elem);
						}
					}	
				}
			});
		}
	};
	
	this.showElem = function(outputElem, msg, loaderParent){
	
        $('.file-active-container').removeClass('file-active-container');
		$('#' + outputElem).addClass('file-active-container');
		if (!$('#' + outputElem).hasClass('file-already-shown')) 
			$('#' + outputElem).addClass('file-already-shown');
		//alert(thisObj.effectName + thisObj.effectOptions + thisObj.effectSpeed);
		$('#' + outputElem).show(thisObj.effectName, thisObj.effectOptions, thisObj.effectSpeed, function(){
			if (msg != undefined){
				$(this).html(msg);
			}
			if (loaderParent != undefined){
				$(loaderParent).parent().children('.content-files-icons-loader').remove();
			}
		} );
	};
	
	this.hideElem = function(iconElem, outputElem){
		$('#'+outputElem).hide(thisObj.effectName, thisObj.effectOptions, thisObj.effectSpeed, function(){
			$(iconElem).removeClass('file-active-icon');
		    $(this).removeClass('file-active-container');  	 
		});
	};
}

var e4FileStat = function(id){
	$.ajax({
        type: "POST",
        url: e4BaseUrl + "plugins-file/index/stat",
        data: "id=" + id,
        dataType : "html",
        success: function(msg){
        }
    });
}
