// check ajax javascript
function FW_evalScripts(container)
{	
	if(typeof container == "string")
	{		
		$(container + " .FW_Javascript").each(function() {		
			eval($(this).text());
		});
	}
	else
	{
		eval(container.find(".FW_Javascript").text());
	}
}

// should not be here in ajax library, create proper one ...
function FW_getFileInfo(data)
{
	var m = data.match(/(.*)\/([^\/\\]+)(\.\w+)$/);
	return {path: m[1], file: m[2], extension: m[3]}
}

// set ajax forms
function FW_ajaxForms(id, from, to, beforeCall, afterCall)
{	
	// remove all events
	$(id).unbind("submit");	
	
	$(id).each(function(){
		// define options
		var form = $(this);
		
		// after loading
		var afterLoading = function(data){
			if(to != null)
			{
				// clear content
				$(to).html("");				
				
				// get data	
				if(from != null && from.length > 0)
				{					
					data = $(data).find(from).html();
				}
				
				// set data				
				$(to).html(data);
				
				// run scripts
				FW_evalScripts(to);							
			}
			if(afterCall)
			{				
				afterCall(data);				
			}			
		};
					
		var formOptions = {					
			beforeSubmit: beforeCall,   			 
   			success: afterLoading
		};		
		
		// setup ajax form link
		$(this).ajaxForm(formOptions);
	});
}

function FW_loadContent(url, to, beforeLoad, afterLoad, data)
{
	//$(to).html("");
	
	// before loading
	if(beforeLoad)beforeLoad();
	
	$(to).load(url, data, 
		function(){
			if(afterLoad != null)
			{
				afterLoad();				
			}			
		}
	);	
}

function FW_reloadLayer(url, from, to, beforeLoad, afterLoad, data)
{			
	// define url
	url = url + (from.length > 0 ? ' ' + from : "");
	
	// reload layer
	$(to).load(url, data != null ? data : "", function(){			
		if(afterLoad != null)afterLoad();
	});			


	
}

function FW_confirm(object)
{
	// get confirmer
	var confirmQuestion = $(object).attr('confirm');		
	if(confirmQuestion)
	{
		if(!confirm(confirmQuestion))
			return false;
	}	
	return true;
}

function FW_initAjaxLinks(beforeLoad, afterLoad, data)
{	
	$("a.FW_ajaxLink").unbind("click");
	$("a.FW_ajaxLink").click(function(){
		// get url
		var ajaxUrl = $(this).attr('href');	
		
		// is there a confirmer?
		if(!FW_confirm($(this)))
		{
			return;
		}			
						
		// reload specified layer
		FW_reloadLayer(ajaxUrl, "FW_ajaxContent", "FW_ajaxContent_ac", beforeLoad, afterLoad, data);
		
		return false;
	});
	
}

