$(document).ready(function(){
	$("form.ajax").live('submit',function(){
		
			mask();
		
			doAjaxPost($(this).attr("action")+"&mod=ajax&formId="+$(this).attr("id"),$(this).serializeArray());
			//faDialog("alert","Eroare!","Va rugam sa completati toate campurile","alert","inchide");
		
		return false;
	});
});

function doAjax(unde){
	$.ajax({
		type: "GET",
		cache: false,
		url: unde,
		dataType: "script",
		success: function(){

		},
		error: function(){
			alert("Eroare! Va rugam incercati din nou");
		}
	});
}


function doAjaxPost(unde,parametrii){
	$.ajax({
		type: "POST",
		cache: false,
		url: unde,
		data: parametrii,
		dataType: "script",
		success: function(){

		},
		error: function(){
			alert("Eroare! Va rugam incercati din nou");
		}
	});
}

function mask(){

	$.blockUI({
		message: "<div class='masca'>Asteptati</div>",
		css: {
			top:  ($(window).height() - 120) /2 + 'px',
			left: ($(window).width() - 120) /2 + 'px',
			width: '120px' ,
			cursor: 'default'
		},
		overlayCSS:{
			background: '#CCC',
			cursor: 'default'
		}
	});
}

function unmask(){

	$.unblockUI();
}

function initCalendare(){
	$(".datePicker").datepicker({
		dateFormat: 'dd.mm.yy',
		changeMonth: true,
		changeYear: true

	});
}

function incarcaDialog(id,titlu,ce,w,h){

	var $dialog = $('<div id="'+id+'"></div>').html("<div class='loading'></div>").dialog({
		autoOpen: false,
		title: titlu,
		modal: true,
		resizable: false,
		closeOnEscape: false,

		close: function(){
			$(this).remove();
		}
	});
	if(w){
		$dialog.dialog("option","width",w);
	}
	if(h){
		$dialog.dialog("option","height",h);
	}

	$dialog.dialog("open").load(ce);

}

function faOverlay(id,titlu){
	var $dialog = $('<div id="'+id+'"></div>').html("<div class='loading'></div>").dialog({
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
		modal: true,
		close: function(){
			$(this).remove();
		},
		resizable: false,
		buttons: { },
	title: titlu});
}

function faConfirm(id,titlu,txt,actiune){
	var $dialog = $('<div id="'+id+'"></div>').html('<span class="ui-icon ui-icon-notice" style="float:left; margin:0 7px 20px 0;"></span>'+txt).dialog({
		modal: true,
		resizable: false,
		close: function(){
			$(this).remove();
		},
		buttons: {
			"Da, sunt sigur" : function(){
				eval(actiune);
			},
			"Cancel": function(){
				$(this).dialog('close');
			}
		},
	title: titlu});

}

function faDialog(id,titlu,txt,icoana,actiune,extra){
	switch(icoana){
		case "alert":
		txt= '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + txt;
		break;
		case "star":
		txt= '<span class="ui-icon ui-icon-star" style="float:left; margin:0 7px 20px 0;"></span>' + txt;
		break;
	}
	var $dialog = $('<div id="'+id+'"></div>').html(txt).dialog({
		modal: true,
		close: function(){
			$(this).remove();
		},
		resizable: false,
		buttons: {
			OK: function(){
				switch(actiune){
					case "inchide":
					$(this).dialog('close');
					break;

					case "refresh":
					window.location.reload();
					break;

					case "redirectare":
					self.location.href=extra;
					break;
					case "custom":
					eval(extra);
					break;
				}

			}
		},
	title: titlu});

}


function faCampEditabil(id){

	$("#"+id).tinymce({
		// Location of TinyMCE script
		script_url : 'tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		plugins : "style,advhr,iespell,table,inlinepopups,contextmenu,directionality,noneditable,visualchars,xhtmlxtras,template,advlist,advlink",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sub,sup,|,bullist,numlist,|,outdent,indent,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,backcolor,styleselect,fontsizeselect,|,link,unlink,|,hr,charmap",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,

		// Example content CSS (should be your site CSS)
		content_css : "editor.css"

		// Drop lists for link/image/media/template dialogs



	});
};

function faCampuriEditabile(id){
	$("#"+id).hide();
	$("#"+id).append("<p class='butoane'><input type='submit' class='button' value='Salveaza'></p>");
	$("#"+id).html("<form action='"+$("#"+id).attr("actiune")+"&fieldId="+id+"' method='post' class='ajax' id='form_"+id+"'>"+$("#"+id).html()+"</form>");
	$("#"+id).find(".buttons").hide();
	$("#"+id).find(".faraText").remove();
	$("#"+id).find(".elem").each(function(){
		var txt=$(this).html();
		var html="";
		switch($(this).attr("tip")){
			case "textarea":
			html="<textarea class='form' name='"+$(this).attr("nume")+"' rows='5' cols='80'>"+txt+"</textarea>";
			break;
			case "textareaRich":
			html="<textarea class='form rich' name='"+$(this).attr("nume")+"' style='width: 100%; height: 300px;'>"+txt+"</textarea>";
			break;
			default:
			html="<input type='text' class='form' name='"+$(this).attr("nume")+"' size='"+$(this).attr("lungime")+"'>";
			break;
		}
		$(this).html(html)
		$(this).find("input").attr("value",txt);

	});

	faTextareas(id);
	$("#"+id).show();
}


function faTextareas(id){

	$("#"+id+ " textarea.rich").tinymce({
		// Location of TinyMCE script
		script_url : 'tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		plugins : "paste,style,advhr,table,iespell,inlinepopups,contextmenu,directionality,noneditable,visualchars,xhtmlxtras,template,advlist,advlink",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sub,sup,|,bullist,numlist,|,outdent,indent,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,|,forecolor,backcolor,fontsizeselect,",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,

		// Example content CSS (should be your site CSS)
		content_css : "textarea.css",



		paste_use_dialog : false,
		paste_auto_cleanup_on_paste : true,
		paste_convert_headers_to_strong : true,
		paste_strip_class_attributes : "all",
		paste_remove_spans : true,
		paste_remove_styles : true



	});
};

