$(document).ready(function() {
	//inizializzo aj navigation
	$.address.change(function(event) {

		//ajLoading(false);

		if(event.value == '/')
			event.value = "home.php";

		$.get('./' + event.value,function(data){
			//content
			$("#maincontent").html(data);
			//ajLoading(true);
			toolTip();

			//mainmenu
			if($("#mainmenu_left").size() == 1){
				var p = $.address.value().replace("/","").split("?")[0];
				if(p == "") p = "home.php";
				$("#mainmenu_left a").removeClass('selected');
				$("#mainmenu_left a[href=\"#"+p+"\"]").addClass('selected');
			}

		});
	});

});
/*
function ajLoading(remove){
	if(remove){
		$("#maincontent").fadeTo(200,1);
	}else{
		$("#maincontent").fadeTo(0,0.5);
	}
}
*/
function toolTip(){
	$( ".tt" ).tooltip({ show:false,hide: false, position: { my: "center bottom-5", at: "center top"  } })
}
function diag(event,w,url,titl,func){
	p(event);
	createDiag();
	$.get('../../dialogs/' + url, function(data){
		$('#dialog').html(data) ;

		$('#dialog').dialog({
			width: w,
			modal: true,
			resizable: false,
			title: titl,
			open: function( event, ui ) {
				if(func)
					func();
				toolTip();
			}
		});
	});
}

function p(e){
	if(window.event)
		window.event.cancelBubble = true;
	else
		e.stopPropagation();
}

function createDiag(){
	$('#dialog').remove();
	var el = document.createElement('div');
	el.id = 'dialog';
	document.body.appendChild(el);
}
function closeDiag(){
	$('#dialog').dialog("destroy");
}
function recenterDiag(){
	$("#dialog").dialog("option", "position", "center");
}

function reloadHome(){
	get('home.php','maincontent');
}
function get(url,destId, funct){
	$.get(url,function(data){
		if(destId)
			$('#'+destId).html(data);
		if(funct)
			funct(data);
		toolTip();
	});
}
function post(formId,destId,funct){
	var f = $("#"+formId);
	$.ajax({
		type: "POST",
		url: f.attr('action'),
		data: f.serialize(),
		success: function(data){
			if(destId)
				$('#'+destId).html(data);
			if(funct)
				funct(data);
			toolTip();
		}
	});
	return false;
}

function orderStatus(img,id){
	img.src = '../../../images/loading32.gif';
	$.get('home.php?status='+id,function(data){
		img.src = '../../../images/'+data;
	});
}

function updateOrderIcons(){
	$.get('home.php?getIcons',function(data){
		var ids = data.split(";");

		$(".lavIcon").each(function(index, element) {
            var id = $(this).attr("id").replace("icoabb","");

			if(jQuery.inArray(id, ids) != -1){
				$(this).attr("src",$(this).attr("src").replace("-off","-on"));
			}else{
				$(this).attr("src",$(this).attr("src").replace("-on","-off"));
			}
        });

	});
}

function changeTgl(el, lav_cod, field){

	var img = $(el).prev('img');
	var original_img = img.attr('src');
	img.attr('src','../../../images/loading32.gif');
	$.get('actions.php?editTaglia&lav_cod='+lav_cod+'&field='+field+'&val='+el.value,function(data){
		img.attr('src',original_img);
		if(data != "")
			alert(data);
	});
}
function changeLav(el, lav_cod){
	var img = $(el).prev('img');
	var original_img = img.attr('src');
	img.attr('src','../../../images/loading32.gif');

	$.get('actions.php?editLavoro&lav_cod='+lav_cod+'&val='+el.value,function(data){
		if(data != '')
			alert(data);
		reloadHome();
	});
}

function showItem(id){
	$("#abb_show").html("");
	get('../../dialogs/abbigliamento_choose.php?id='+id+'&lav_cod='+$('#lav_cod').val()+'&lavoro_id='+$('#lavoro_id').val(),'abb_show',function(data){
		$('#available_articles').find('a').removeClass('selected');
		$('#art'+id).addClass('selected');
		var name = $('.ui-dialog-title').html().split(' - ')[0].split(' ')[1];
		$('.lav_name').html(name);
	});
}

function generateOrder(id){
	//aggiorno database

	$('#procedi').hide();
	$('#generazione').show().html('Aggiornamento database in corso... attendere');

	get("gorder.php",false,function(data){	
		if(data == ""){
			$('#generazione').html('Generazione ordine in corso... attendere');
			get('../../dialogs/order_generate.php?gen&id='+id,'dialog');
		}else{
			alert("Errore:\n" + data);
		}
	});
}

function selectArticle(){
	$('#qty').hide().next('img').show();
	post('f_selAbb',false,function(data){
		$('#qty').show().next('img').hide();

		if(data != ''){
			alert(data);
		}else{
			closeDiag();
			alert("Salvataggio avvenuto con successo!");
		}
	});
}
