jQuery.myItems = function(spannum, options) {
    var maxItems = 10000;

    var $spannum = $(spannum);
    var numItems = 0;
    
    var $jcookie;
    var arrayIds = Array();
	var itemsQ = Array();
	var idQu="";
    $.myCookie("MyItems",90);
	 var res = $.myCookie.read();
	 

	 
   	var arrContent=Array();
  	 var cookieContent = $.myCookie.results;
     if (cookieContent != null) {
			arrContent=cookieContent.split("-");
			if(arrContent[0]!=""){
			var idContent= arrContent[0];
			 if (idContent.length > 0) {
            	arrayIds= idContent.split(",");			
        	}
			
			idQu=arrContent[1];						
			if (idQu.length > 0) {
            	itemsQ= idQu.split(",");			
    		}
			}
    }
	
  
    numItems = arrayIds.length;
    $spannum.html(numItems);

	jQuery.myItems.addToMyItems = function(value) {
        if (numItems < maxItems) {
            $boton = $("#id-" + value);
            $boton.addClass("btn2gr");        
			q=1;
            addInternalList(value,q);
        } else if (itemsStore.length >= maxItems) {
            alert("ha alcanzado el máximo de items que puede almacenar en la cesta");
        }
    }
		jQuery.myItems.addToMyItemsQ = function(value,cantidad) {
        if (numItems < maxItems) {
            $boton = $("#id-" + value);
            $boton.addClass("btn2gr");        
			q=cantidad;
		
            addInternalList(value,q);
        } else if (itemsStore.length >= maxItems) {
            alert("ha alcanzado el máximo de items que puede almacenar en la cesta");
        }
    }
	jQuery.myItems.deleteItemId = function(value) {
		deleteItemId(value);
	}
	jQuery.myItems.changeNumberMyItems =function(value,quant){
		changeNumberMyItems(value,quant);
	}
	
	function changeNumberMyItems(value,quant){
		for (i = 0; i < arrayIds.length; i++) {
            if (arrayIds[i] == value) {		
                itemsQ[i]=quant;				
                break;
            }
        }

         saveCookieIds();
		 window.location.href = window.location.href;
        
	}
    function addInternalList(value,q) {
		if (!existItemIds(value)) {
            arrayIds.push(value);
			itemsQ.push(q);
            numItems++;
            $spannum.html(numItems);
            saveCookieIds();
        }else{
			alert("Este producto ya existe en su pedido.");
		}
    }

    function existItemIds(value) {
        for (i = 0; i < arrayIds.length; i++) {
            if (arrayIds[i] == value) {
                return true;
            }
        }
        return false;
    }

 
    function saveCookieIds() {
        var content = "";
        var contentQ = "";
        for (i = 0; i < arrayIds.length; i++) {
            content = content + arrayIds[i] + ",";
		}		
		content = content.substring(0, content.length - 1);
        for (i = 0; i < itemsQ.length; i++) {
            contentQ  = contentQ + itemsQ[i] + ",";
		}		
		contentQ  = contentQ .substring(0, contentQ.length - 1);
		var resSave=content+"-"+contentQ;	
		if(itemsQ.length==0){
			resSave="";
		}
        $.myCookie.save(resSave);
    }

    function deleteItemId(iditem) {
        for (i = 0; i < arrayIds.length; i++) {
            if (arrayIds[i] == iditem) {		
                arrayIds.splice(i, 1);				
                itemsQ.splice(i, 1);				
                break;
            }
        }
		 updateNumItemsId();
		 
         saveCookieIds();
        
		window.location.href = window.location.href;

    }
	function updateNumItemsId() {
        $spannum.html(arrayIds.length);
        numItems = arrayIds.length;
    }
}

function addToMyItems(value){
if(value){
$.myItems.addToMyItems(value);	
    }
}

function changeNumberMyItems(value){
	var quant=$("#but-"+value).val();
	if (quant!= parseInt(quant)){
		alert("introduzca un valor numérico");
	}else{
		if(value){
			$.myItems.changeNumberMyItems(value,quant);	
   		 }
	}
}
function deleteItem(value){
if(value){
    $.myItems.deleteItemId(value);	
    }
}
function itemEl(idItem,quantity){ 
    //Inicializo las propiedades y métodos 
	
   this.quantity = quantity;
   this.idItem = idItem;
} 



