function createMbContainer(modelId, newContainerId, id){
	$(modelId).clone().appendTo('#containers').attr("id", newContainerId + id);
	$("#" + newContainerId + id).buildContainers();
	return "#" + newContainerId + id;
}

function cleanDownloadsActionJson(){
	actionsJson.actions[actions.download] = [];
}

function recordDownload(fileId){
	if (actionsJson != null && actionsJson != undefined){
		if (fileId != null){
			if (actionsJson.actions[actions.download] == undefined)
				actionsJson.actions[actions.download] = [];
			actionsJson.actions[actions.download].push([new Date().getTime(), fileId, 0]);
		}
		if (actionsJson.actions[actions.download].length > downloadBuffer){
			var jsonString = $.toJSON(actionsJson);
			$.ajax({
			   type: "POST",
			   url: "recordActionJson" + extension + "?json=" + escape(jsonString),
			   success : function(){
				updateActionJson();
			   }
			});
			cleanDownloadsActionJson();
		}
	}
}

function updateActionJson(){
	for (var actionType in actions){
		if (actionsJson.actions[actionType] != undefined && actionsJson.actions[actionType] != null && actionsJson.actions[actionType].length > 0) {
			for (var i = 0; actionsJson.actions[actionType].length - 1; i++){
				actionsJson.actions[actions.download][i][2] = 1;
		    }
		}
	}
}

/*******************************************************************************
 * EDIT - DELETE USERS & FILES
 ******************************************************************************/
 
function actionMultiDelete(reason){
	var compt = 0;
	$('.select').each(function(){
		if ($(this).attr('checked'))
			compt++;
	});
	if (compt == 0){
		alert(getText('MultiDeleteAlert'));
		return;
	}
	// Multi Delete
	$('.select').each(function(){
		if ($(this).attr('checked')){
			var fileId = $(this).val();
			if (actionsJson.actions[actions.deleteFile] == undefined || actionsJson.actions[actions.deleteFile] == null)
				actionsJson.actions[actions.deleteFile] = [];
			var exists = false;
			for (pos = 0; pos < actionsJson.actions[actions.deleteFile].length; pos++){
			    var act = actionsJson.actions[actions.deleteFile][pos];
			    if (act[1] == fileId)
			    	exists = true;
			}
			if (!exists){
				if (reason != undefined && reason != ""){
					actionsJson.actions[actions.deleteFile].push([new Date().getTime(), fileId, 0]);
					actionsJson.message = reason;
				}
				else
					actionsJson.actions[actions.deleteFile].push([new Date().getTime(), fileId, 0]);
			}
		}
	});
	var jsonString = $.toJSON(actionsJson);
	$.ajax({
	   type: "POST",
	   url: "recordActionJson" + extension + "?recordSync=true",
	   data : {'json' : jsonString},
	   success: function(json){
			if (json.code == 0){
				if (json.actions != undefined && json.actions[actions.deleteFile] != undefined){
					var returnMessages = json.actions[actions.deleteFile];
					for (msg = 0; msg < returnMessages.length; msg++){
						var message = returnMessages[msg];
						var id = message[0];
						var returnedMsg = message[1];
						$('#' + id).html(getText(returnedMsg)).css('color', 'red');
					}
					for (pos = 0; pos < actionsJson.actions[actions.deleteFile].length; pos++){
					    var act = actionsJson.actions[actions.deleteFile][pos];
					    act[2] = 1;
					    actionsJson.actions[actions.deleteFile][pos] = act; 
					}
				}
			} else {
				$.jGrowl('',{
					theme: 	'error',
					header: json.message,
					life:	5000,
					sticky: false
				});
				for (pos = 0; pos < actionsJson.actions[actions.deleteFile].length; pos++){
				    var act = actionsJson.actions[actions.deleteFile][pos][2] = 1;
				}
			}
		updateActionJson();
	   }
	});
}

/*******************************************************************************
 * GLOW
 ******************************************************************************/
	 
// theme are : success, error, warning
function showGlow(val, theme, life){
	
	if (theme == null || theme == undefined)
		theme = 'success';
	if (life == null || life == undefined)
		life = 3000;
	
	if (val == undefined){
		
		/** Messages * */
		
		var message = $('#message').html();
		if (message != ''){
			$('#message').html('');
			$.jGrowl('',{
				theme: 	theme,
				header: message,
				life:	life,
				sticky: false
			});
		}
		
		/** Erreurs * */
		
		var error = $('#error').html();
		if (error != ''){
			$('#error').html('');
			$.jGrowl('<p></p>' + error,{
				theme: 	theme,
				header: 'ERROR',
				life:	life,
				sticky: false
			});
		}
	} else if (val == "errorAjax"){
		$.jGrowl('',{
			theme: 	theme,
			header: 'database error',
			life:	life,
			sticky: false
		});
	} else if (error){
		$.jGrowl('',{
			theme: 	theme,
			header: val,
			life:	life,
			sticky: false
		});
	} else{
		$.jGrowl('',{
			theme: 	theme,
			header: val,
			life:	life,
			sticky: false
		});
	}
}


/*******************************************************************************
 * DATATABLE
 ******************************************************************************/

// DATATABLE SIZE SORT
jQuery.fn.dataTableExt.oSort['num-html-asc']  = function(a,b) {
	var x = a.replace( /<.*?>/g, "" );
	var y = b.replace( /<.*?>/g, "" );
	var xko, xmo, xgo, yko, ymo, ygo;
	if (x.indexOf("Ko", 0) != -1) 
		xko = x.substr(x.indexOf("Ko", 0), 2);
	if (x.indexOf("Mo", 0) != -1)
		xmo = x.substr(x.indexOf("Mo", 0), 2);
	if (x.indexOf("Go", 0) != -1)
		xgo = x.substr(x.indexOf("Ho", 0), 2);
	if (y.indexOf("Ko", 0) != -1)
		yko = y.substr(y.indexOf("Ko", 0), 2);
	if (y.indexOf("Mo", 0) != -1)
		ymo = y.substr(y.indexOf("Mo", 0), 2);
	if (y.indexOf("Go", 0) != -1)
		ygo = y.substr(y.indexOf("Go", 0), 2);
	x = parseFloat( x );
	y = parseFloat( y );
	if (xko != undefined){
		if (yko != undefined)
			return ((x < y) ? -1 : ((x > y) ?  1 : 0));
		return -1;
	} 
	if (xmo != undefined){
		if (ymo != undefined)
			return ((x < y) ? -1 : ((x > y) ?  1 : 0));
		if (ygo != undefined)
			return -1;
		return 1;
	}
	if (xgo != undefined){
		if (ygo != undefined)
			return ((x < y) ? -1 : ((x > y) ?  1 : 0));
		return 1;
	}
	return 1;
};

jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
	var x = a.replace( /<.*?>/g, "" );
	var y = b.replace( /<.*?>/g, "" );
	var xko, xmo, xgo, yko, ymo, ygo;
	if (x.indexOf("Ko", 0) != -1) 
		xko = x.substr(x.indexOf("Ko", 0), 2);
	if (x.indexOf("Mo", 0) != -1)
		xmo = x.substr(x.indexOf("Mo", 0), 2);
	if (x.indexOf("Go", 0) != -1)
		xgo = x.substr(x.indexOf("Ho", 0), 2);
	if (y.indexOf("Ko", 0) != -1)
		yko = y.substr(y.indexOf("Ko", 0), 2);
	if (y.indexOf("Mo", 0) != -1)
		ymo = y.substr(y.indexOf("Mo", 0), 2);
	if (y.indexOf("Go", 0) != -1)
		ygo = y.substr(y.indexOf("Go", 0), 2);
	x = parseFloat( x );
	y = parseFloat( y );
	if (xko != undefined){
		if (yko != undefined)
			return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
		return 1;
	} 
	if (xmo != undefined){
		if (ymo != undefined)
			return ((x < y) ? 1 : ((x > y) ? -1 : 0));
		if (ygo != undefined)
			return 1;
		return -1;
	}
	if (xgo != undefined){
		if (ygo != undefined)
			return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
		return -1;
	}
	return -1;
};

jQuery.fn.dataTableExt.oSort['dimensions-asc'] = function(a,b) {
	var widtha, heighta, widthb, heightb, wa, ha, wb, hb;
	
	if (a.indexOf(" x ", 0) != -1){ 
		widtha = a.substr(0, a.indexOf(" x "));
		heighta = a.substr(a.indexOf(" x ") + 3, a.length);
	}
	if (b.indexOf(" x ", 0) != -1){ 
		widthb = b.substr(0, b.indexOf(" x "));
		heightb = b.substr(b.indexOf(" x ") + 3, b.length);
	}
	wa = parseFloat(widtha);
	ha = parseFloat(heighta);
	
	wb = parseFloat(widthb);
	hb = parseFloat(heightb);
	
	if (wa * ha >= wb * hb)
		return 1;
	return -1;
};

jQuery.fn.dataTableExt.oSort['dimensions-desc'] = function(a,b) {
	var widtha, heighta, widthb, heightb, wa, ha, wb, hb;
	
	if (a.indexOf(" x ", 0) != -1){ 
		widtha = a.substr(0, a.indexOf(" x "));
		heighta = a.substr(a.indexOf(" x ") + 3, a.length);
	}
	if (b.indexOf(" x ", 0) != -1){ 
		widthb = b.substr(0, b.indexOf(" x ", 0));
		heightb = b.substr(b.indexOf(" x ") + 3, b.length);
	}
	wa = parseFloat(widtha);
	ha = parseFloat(heighta);
	
	wb = parseFloat(widthb);
	hb = parseFloat(heightb);
	
	if (wa * ha >= wb * hb)
		return -1;
	return 1;
};

jQuery.fn.dataTableExt.oSort['time-asc'] = function(a,b) {
	var houra, mina, hourb, minb, wa, ha, wb, hb;
	
	if (a.indexOf(" h") != -1){ 
		if (a.indexOf(" h") == 1)
			houra = a.substr(0, 1);
		else 
			houra = a.substr(0, 2);
	}
	if (a.indexOf("min") != -1){
		if (a.indexOf("min") == 2)
			mina = a.substr(0, 1);
		else if (a.indexOf("min") == 3)
			mina = a.substr(0, 2);
		else { // with hour
			if (a.indexOf("min") - a.indexOf("h") == 2)
				mina = a.substr(a.indexOf("h") + 1 , 1);
			else
				mina = a.substr(a.indexOf("h") + 1 , 2);
		}
	}
	if (b.indexOf(" h") != -1){ 
		if (b.indexOf(" h") == 1)
			hourb = b.substr(0, 1);
		else 
			hourb = b.substr(0, 2);
	}
	if (b.indexOf("min") != -1){
		if (b.indexOf("min") == 2)
			minb = b.substr(0, 1);
		else if (b.indexOf("min") == 3)
			minb = b.substr(0, 2);
		else { // with hour
			if (b.indexOf("min") - b.indexOf("h") == 2)
				minb = b.substr(b.indexOf("h") + 1 , 1);
			else
				minb = b.substr(b.indexOf("h") + 1 , 2);
		}
	}
	
	if (houra != undefined){
		ha = parseInt(houra);
		if (hourb == undefined)
			return 1;
		hb = parseInt(hourb);
		if (isNaN(ha))
			return -1;
		if (isNaN(hb))
			return 1;
		if (ha > hb)
			return 1;
		if (ha < hb)
			return -1;
		// compare min
		if (mina != undefined){
			ma = parseInt(mina);
			if (minb != undefined){
				mb = parseInt(minb);
				if (!isNaN(ma)){
					if (isNaN(mb))
						return 1;
					if (ma >= mb)
						return 1;
					return -1;
				}
				return -1;
			}
			return 1;
		}
	}
	if (hourb != undefined)
		return -1;
	
	// compare min
	if (mina != undefined){
		ma = parseInt(mina);
		if (minb != undefined){
			mb = parseInt(minb);
			if (!isNaN(ma)){
				if (isNaN(mb))
					return 1;
				if (ma >= mb)
					return 1;
				return -1;
			}
			return -1;
		}
		return 1;
	}
};

jQuery.fn.dataTableExt.oSort['time-desc'] = function(a,b) {
	var houra, mina, hourb, minb, wa, ha, wb, hb;
	
	if (a.indexOf(" h") != -1){ 
		if (a.indexOf(" h") == 1)
			houra = a.substr(0, 1);
		else 
			houra = a.substr(0, 2);
	}
	if (a.indexOf("min") != -1){
		if (a.indexOf("min") == 2)
			mina = a.substr(0, 1);
		else if (a.indexOf("min") == 3)
			mina = a.substr(0, 2);
		else { // with hour
			if (a.indexOf("min") - a.indexOf("h") == 2)
				mina = a.substr(a.indexOf("h") + 1 , 1);
			else
				mina = a.substr(a.indexOf("h") + 1 , 2);
		}
	}
	if (b.indexOf(" h") != -1){ 
		if (b.indexOf(" h") == 1)
			hourb = b.substr(0, 1);
		else 
			hourb = b.substr(0, 2);
	}
	if (b.indexOf("min") != -1){
		if (b.indexOf("min") == 2)
			minb = b.substr(0, 1);
		else if (b.indexOf("min") == 3)
			minb = b.substr(0, 2);
		else { // with hour
			if (b.indexOf("min") - b.indexOf("h") == 2)
				minb = b.substr(b.indexOf("h") + 1 , 1);
			else
				minb = b.substr(b.indexOf("h") + 1 , 2);
		}
	}
	
	if (houra != undefined){
		ha = parseInt(houra);
		if (hourb == undefined)
			return -1;
		hb = parseInt(hourb);
		if (isNaN(ha))
			return 1;
		if (isNaN(hb))
			return -1;
		if (ha > hb)
			return -1;
		if (ha < hb)
			return 1;
		// compare min
		if (mina != undefined){
			ma = parseInt(mina);
			if (minb != undefined){
				mb = parseInt(minb);
				if (!isNaN(ma)){
					if (isNaN(mb))
						return -1;
					if (ma >= mb)
						return -1;
					return 1;
				}
				return 1;
			}
			return -1;
		}
	}
	if (hourb != undefined)
		return 1;
	
	// compare min
	if (mina != undefined){
		ma = parseInt(mina);
		if (minb != undefined){
			mb = parseInt(minb);
			if (!isNaN(ma)){
				if (isNaN(mb))
					return -1;
				if (ma >= mb)
					return -1;
				return 1;
			}
			return 1;
		}
		return -1;
	}
};

jQuery.fn.dataTableExt.oSort['fr_date-asc']  = function(a,b) {
    var ukDatea = a.split('/');
    var ukDateb = b.split('/');
     
    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
     
    return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};
 
jQuery.fn.dataTableExt.oSort['fr_date-desc'] = function(a,b) {
    var ukDatea = a.split('/');
    var ukDateb = b.split('/');
     
    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
     
    return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
};

/*******************************************************************************
 * OTHERS
 ******************************************************************************/

function getActionFromUrl(url){
	var action;
	action = url.substring(0,url.indexOf(extension));
	action = action.substring(action.lastIndexOf("/") + 1);
	return action;
}

function getValueFromParameter(params){
	if (params == undefined || params == null)
		return "";
	var pos = params.indexOf("=");
	if (pos != -1)
		return params.substring(pos + 1);
	return "";
}

function searchIdFromHref(href){
	return href.substring(href.indexOf('id=') + 3);
}

$.fn.removeChildFromDom = function(s) {
    if (!this) return;
    var el = document.getElementById(this.attr("id"));
    if (!el) return;

    var bin = document.getElementById("temp");

    // before deleting el, recursively delete all of its children.
    while (el.childNodes.length > 0) {
        if (!bin) {
            bin = document.createElement("DIV");
            bin.id = "temp";
            document.body.appendChild(bin);
        }
        bin.appendChild(el.childNodes[el.childNodes.length - 1]);
        bin.innerHTML = "";
    }
    el.parentNode.removeChild(el);

    if (!bin) {
        bin = document.createElement("DIV");
        bin.id = "temp";
        document.body.appendChild(bin);
    }
    bin.appendChild(el);
    bin.innerHTML = "";
};

function getFunctionName(func, otherName){
    var name = func.toString();
    name = name.substring(9, name.indexOf('('));
    if (name == "" && otherName != undefined)
    	return otherName;
    return name;
}

function getReadableSize(size){
	var newSize, readableSize;
	if (size >= 1073741824) {
		newSize = Math.round((size / 1073741824) * 100) / 100;
		readableSize = newSize + " Go";
	} else if (size >= 1048576) {
		newSize = Math.round((size / 1048576) * 100)/100;
		readableSize = newSize + " Mo";
	} else if (size >= 1024) {
		newSize = Math.round((size / 1024)*100)/100;
		readableSize = newSize + " Ko";
	} else {
		readableSize = "0 Ko";
	}
	return readableSize;
}

function getScrollPosition(){
	return Array((document.documentElement && document.documentElement.scrollLeft) 
		|| window.pageXOffset || self.pageXOffset 
		|| document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) 
		|| window.pageYOffset || self.pageYOffset 
		|| document.body.scrollTop);
}
