/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
function addAttrValue(pWhere, pWhich, pWhat) {

    if ($(pWhere).attr(pWhich) != undefined) {
        if ($(pWhere).attr(pWhich).indexOf(pWhat) == -1) {
            $(pWhere).attr(pWhich, $(pWhere).attr(pWhich)+' '+pWhat);
        }
    }
}

/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
function removeAttrValue(pWhere, pWhich, pWhat) {
    if ($(pWhere).attr(pWhich) != undefined) {
        if ($(pWhere).attr(pWhich).indexOf(pWhat) != -1) {
            $(pWhere).attr(pWhich, $(pWhere).attr(pWhich).replace(pWhat, ''));
        }
    }
}

/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
function serialize(pObject) {
	var retString = "";
	var	controlName;
	$("[type=text], [type=radio], [type=checkbox], [type=password], [type=hidden], select, textarea", $(pObject)).each(function() {
		controlName = $(this).attr("name");
		if (controlName != undefined && controlName != '') {
			retString += (controlName+"="+$(this).val()+"&");
		}
	})
	return retString;
}
/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
function ajaxSubmit(pObject, pUrl, pFunction) {

	if (pFunction != undefined && pFunction != '') {
		eval(pFunction+"("+($(pObject).attr("id")?"'#"+$(pObject).attr("id")+"'":"")+", 'START')");
	}

	var parameters = serialize($(pObject));

	var couples = parameters.split('&');
	var tmp; var myData = {};
	for(i=0; i<couples.length; i++) {
		if (couples[i] != undefined && couples[i] != '') {
			tmp = couples[i].split('=');
			myData[tmp[0]] = tmp[1];
		}
	}

	$.ajax({
		url:pUrl,
		data:myData,
		type:'POST',
		success:function (result){
			if (result.substr(0,2) == 'KO') {
				if (pFunction != undefined && pFunction != '') {
					eval(pCallbpFunctionack+"("+($(pObject).attr("id")?"'#"+$(pObject).attr("id")+"'":"")+", 'FAIL', "+result+")");
				}
			} else {
				if (pFunction != undefined && pFunction != '') {
					eval(pFunction+"("+($(pObject).attr("id")?"'#"+$(pObject).attr("id")+"'":"")+", 'SUCCESS', "+result+")");
				}
			}
		}
	});
}
/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
function openModalContent(pContent, pWidth, pHeight, pParams, pFunction) {
	//if (pContent != "" || pContent != undefined) {
		
		$("body").append('<div id="_ModalOverlay" style="display:none"></div>').append('<div id="_ModalContent" style="display:none"></div>');
		//$("body").append('<div id="_ModalOverlay"></div>');
		$("#_ModalOverlay").css({
			"position":"fixed",
			"top":"0px",
			"left":"0px",
			"width":"100%",
			"height":"100%",
			"backgroundColor":"#000000",
			"zIndex":"30"
		});
		$("#_ModalContent").css({
			"position":"fixed",
			"top":"0px",
			"left":"0px",
			"width":"100%",
			"height":"100%",
			//"backgroundColor":"#FFFFFF",
			"zIndex":"40"
		});
		
		//$("#_ModalOverlay").fadeTo(1000, .6);
		if ($(pContent).length) { // If it's an existing div
			if (pWidth != "" || pWidth != undefined) {
				pWidth = $(pContent).width()+"px";
			} else {
				pWidth = ($(window).width()-100)+"px";
			}
			if (pHeight != "" || pHeight != undefined) {
				pHeight = $(pContent).height()+"px";
			} else {
				pHeight = ($(window).height()-50)+"px";
			}
			
			var content = $(pContent).clone();
			contentWidth = content.width();
			contentHeight = content.height();
			
			$("#_ModalContent").html(content);
			content.css({
				"position":"fixed",
				"top":"50%",
				"left":"50%",
				"width":pWidth+"px",
				"height":pHeight+"px",
				"backgroundColor":"#FFFFFF",
				"marginLeft":(-pWidth/2)+"px",
				"marginTop":(-pHeight/2)+"px"
			});
			$("#_ModalContent").fadeIn(1000);
			$("#_ModalOverlay").fadeTo(1000, .6);
		} else {
			if (pWidth == "" || pWidth == undefined) pWidth = ($(window).width() - 50)+"px";
			if (pHeight == "" || pHeight == undefined) pHeight = ($(window).height() - 50)+"px";
			if (pParams == "" || pParams == undefined) pParams = "";
			
			$("#_ModalContent").css({
				"position":"fixed",
				"top":"50%",
				"left":"50%",
				"width":pWidth+"px",
				"height":pHeight+"px",
				"backgroundColor":"#FFFFFF",
				"marginLeft":(-pWidth/2)+"px",
				"marginTop":(-pHeight/2)+"px"
			});
			
			$.ajax({
		        type: "POST",
		        url: "core/ajax.loadContent.php?c="+pContent,
		        data: pParams,
		        success: function(data, txtStatus, xmlReq){
		            $("#_ModalContent").html(data);
		            $("#_ModalContent").fadeIn(500, function(){
		            	if (pFunction != '' && pFunction != undefined) {
			                eval(pFunction+'("#_ModalContent")');
			            }
		            });
		            $("#_ModalOverlay").fadeTo(1000, .6);
		        }
		    });
		}
			
	//}
}
function closeModalContent() {
	$("#_ModalContent").fadeOut(500);
	$("#_ModalOverlay").fadeOut(500, 0, function() {
		$(this).remove();
		$("#_ModalContent").remove();
	});
}
/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
function loadContent(pWhere, pContent, pParams, pFunction) {
	$(pWhere).html('<img src="images/ajax-loader.gif" />');
    $.ajax({
        type: "POST",
        url: "core/ajax.loadContent.php?c="+pContent,
        data: pParams,
        success: function(data, txtStatus, xmlReq){
            //pWhere.append(data);
    		$(pWhere).fadeOut(300, function(){
    			pWhere.html(data);
                $(pWhere).slideDown(300, function(){
                	if (pFunction != '' && pFunction != undefined) {
                        eval(pFunction+"($('#"+$(pWhere).attr('id')+"'))");
                    }
                });
    		});
        },
        error: function (data, textStatus, error) {
			if (textStatus == 'error') {
				alert("AJAX Error!!\n\nFile: "+this.url+"\nMethod: "+this.type+"\nError: "+data.statusText+"\nStatus: "+data.status);
			}
		}
    });
}
/* ****************************************************************** */
/* ****************************************************************** */
/* ****************************************************************** */
var oldPos;
var newPos;
function setImagesSortable(pItemType, pItemId, pSelector, pItem, pIdSelector) {
	$(pSelector).sortable({
		start:function(event, ui) {
			oldPos = $(pItem, pSelector).index($(ui.item));
		},
		stop:function(event, ui) {
			newPos = $(pItem, pSelector).index($(ui.item));
			var id = parseInt($(pIdSelector, $(ui.item)).val());
			
			var product;
			if ($("#product_id").length) {
				product = $("#product_id").val();
			} else {
				product = 0;
			}
			
			var master;
			if ($("[name='master_photo[]']", $(ui.item)).length) {
				master = $("[name='master_photo[]']", $(ui.item)).val();
			} else {
				master = 0;
			}
			
			var category;
			if ($("[name='finiture_category[]']", $(ui.item)).length) {
				category = $("[name='finiture_category[]']", $(ui.item)).val();
			} else {
				category = 0;
			}
			
			var rowId = $(ui.item).attr("id").substr(3,1);
			$.ajax({ 			
				url:"ajax.imagePosition.php?product="+product+"&where="+pItemType+"&id="+id+"&master="+master+"&cat="+category+"&new="+newPos+"&old="+oldPos,
				type: "POST",
				success : function(result){
					if (result=='KO') {
						// Restore old position
						alert("Si sono verificati errori in fase di ordinamento immagine\n\nSe il problema persiste contattare il supporto tecnico.");
						$(ui.item).sortable('cancel');
					}
				},
				error: function () {
					alert('not working');
					
				}
			});
		}
	});
}
