
/* CORE */
/**
 * Submit'ina forma, ir jeigu buvo klaidu - jas parodo, jei ne - redirektina. Daugiausiai naudojama backende.
 */ 

function setAjaxSubmit(selector, config) {
	
	if (typeof(config)=='undefined') {
		var config = new Array();
	}
	if (typeof(config['error_tag'])=='undefined') {
		config['error_tag'] = 'span';
	}
	if (typeof(config['error_class'])=='undefined') {
		config['error_class'] = 'field_error_message';
	}
	
	
	$(document).ready(function() { 
		if (typeof(tinyMCE)=='object') {
			$(selector).bind('form-pre-serialize', function(e) {
			    tinyMCE.triggerSave();
			});		
		}
		
		form_url = $(selector).attr('action');
		if (form_url.match(/\?/)) {
			form_url += '&';
		} else {
			form_url += '?';
		}
		form_url += 'ajax_submit';
		
		var options = { 
			beforeSubmit : function() {
				height = $(selector).innerHeight();
				width = $(selector).innerWidth();
				$(selector).prepend('<div style="width:'+width+'px; height:'+height+'px;" class="ajax_overlay">&nbsp;<\/div>');
			},
			success: function(responseText, statusText) {
				$(selector+' .ajax_overlay').remove();
				if (responseText) {
				 	$(config['error_tag']+'.'+config['error_class']).remove();
				 	$(selector+' .error_message').remove();
					$(selector+' .error').removeClass('error');
					// TODO: sitoj vietoj reiktu kazkokio tikrinimo ar tai json'as, ar bent jau exception'u gaudyma padaryti.
					var errors = eval('('+responseText+')');
				 	error_str = "";
				 	if (typeof(errors.return_url)!='undefined') {
				 		document.location = errors.return_url;
			 		} else
				 	for (i in errors) {
				 		el = $(selector+' [name='+i+']:first');
			 			if (!el.length) {
			 				msg = '';
			 				if (errors[i].label) {
			 					msg += errors[i].label+': ';
			 				}
			 				msg += errors[i].message;
			 				error_str += (msg+'<br/>');
			 			} else {
					 		el.addClass('error');
					 		el.parent().append('<'+config['error_tag']+' class="'+config['error_class']+'">'+errors[i].message+'<\/'+config['error_tag']+'>');
				 		}
				 	}
				 	$(selector+' .error:first').focus();
				 	if (error_str) {
				 		$(selector).prepend('<div class="error_message">'+error_str+'</div>');
				 	}
			 	} else {
			 		document.location = $(selector+' input[name=return_url]').val();
			 	}
			}, 
			url: form_url  // override for form's 'action' attribute 
		}; 
		// bind form using 'ajaxForm' 
		$(selector).ajaxForm(options);
	}); 
}

/**
 * Submit'ina forma, ir atvaizduoja rezultata (template'a).
 */
 
function submitAndReplace(form_selector, area_selector, template) {
	var options = {
		beforeSubmit : function() {
			height = $(area_selector).innerHeight();
			width = $(area_selector).innerWidth();
			$(area_selector).prepend('<div style="width:'+width+'px; height:'+height+'px;" class="ajax_overlay">&nbsp;<\/div>');
		},
		success : function(responseText){
			$(area_selector).empty().before(responseText).remove();
			// $(area_selector).html(responseText);
		},
		url : '?display='+template
	};
	$(form_selector).ajaxForm(options);
}

function ajaxLinks(link_selector, area_selector, template) {
	$(function(){
		$(link_selector).click(function(){
			url = this.href;
			if (url.match(/\?/)) {
				url += '&';
			} else {
				url += '?';
			}
			url += 'display='+template;
			$.ajax({
				beforeSend : function() {
					height = $(area_selector).innerHeight();
					width = $(area_selector).innerWidth();
					$(area_selector).prepend('<div style="width:'+width+'px; height:'+height+'px;" class="ajax_overlay">&nbsp;<\/div>');
				},
				url: url,
				success: function(html){
					$(area_selector).empty().before(html).remove();
				}
			});
			return false;
		});
	});	
}
