<!--Copyright (C) 2014  Web3king - Alberto Bonacina

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->

<!-- Other JS plugins can be included here 
<script src="https://code.jquery.com/jquery.js"></script>-->
<script src="<?php echo base_url();?>js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="<?php echo base_url();?>js/jquery.ui.touch-punch.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="<?php echo base_url();?>js/bootstrap.min.js"></script>
<script src="<?php echo base_url();?>js/jquery.dataTables.min.js"></script>
<!--<script src="<?php echo base_url();?>js/jquery.filtertable.min.js"></script>-->
<script src="<?php echo base_url();?>js/bootstrap-datepicker.js"></script>

<!--<script src="<?php echo base_url();?>js/sb-admin.js"></script>-->

<script>
$(document).ready(function() {
    $('#myTable').dataTable();
    $('#ingTable').dataTable({
    	"bPaginate": false
    });
    $('#datepicker').datepicker({
    	format: 'yyyy/mm/dd'
    });
    $('#datepicker1').datepicker({
    	format: 'yyyy/mm/dd'
    });
    $('#datepicker2').datepicker({
    	format: 'yyyy/mm/dd'
    });
    $('.bigCheckbox').altCheckbox({
    	iconClass: "fontawesome-ok",
    	sizeClass: "big"
    });
    $(".draggable span").draggable({
    	appendTo: "body",
      	helper: "clone"
    });

    function createDroppedElement(trascinato,nomearray){
    	var li = document.createElement("div");
		//li.id = trascinato.attr('id');
		//li.innerHTML = trascinato.attr('name');
		li.innerHTML = '<input name="'+nomearray+'[]" type="checkbox" class="bigCheckbox '+nomearray+'" value="'+trascinato.attr('id')+'" checked="checked"/> '+trascinato.attr('name');
		li.className = "col-md-12 riga-selezionata";

		return li;
    }

    //Listener che mi serve per eliminare undefined che si crea nella lista che viene riordinata
    $(document).bind('domChanged', function(){
    	//elementi che hanno testo 'undefined'
     	elements = document.getElementsByClassName('riga-selezionata');
     	//loop attraverso gli elementi
     	Array.prototype.forEach.call(elements, function(el) {
		    // Do stuff with the element
		    if(el.innerHTML.indexOf('undefined') != -1){
		    	el.parentNode.removeChild(el);
		    }
		});

     	//Riapplico gli stili per gli input
		$('.bigCheckbox').altCheckbox({
    		iconClass: "fontawesome-ok",
    		sizeClass: "big"
    	});

	});

    //Listener che mi serve per capire quando un sort è stato modificato
	$(document).bind('sortChanged',function(){
		elementStarter = document.getElementsByClassName('starter');
		elementFirst = document.getElementsByClassName('first');
		elementSecond = document.getElementsByClassName('second');
		elementSweet = document.getElementsByClassName('sweet');
		elementOther = document.getElementsByClassName('other');
		elementWine = document.getElementsByClassName('wine');

		var stringa = '';
		//Ciclo l'array degli starter
		Array.prototype.forEach.call(elementStarter, function(el) {
			stringa = stringa + 'starter[]=' + el.value + '&';
		});
		//Ciclo l'array dei first
		Array.prototype.forEach.call(elementFirst, function(el) {
			stringa = stringa + 'first[]=' + el.value + '&';
		});
		//Ciclo l'array dei second
		Array.prototype.forEach.call(elementSecond, function(el) {
			stringa = stringa + 'second[]=' + el.value + '&';
		});
		//Ciclo l'array degli sweet
		Array.prototype.forEach.call(elementSweet, function(el) {
			stringa = stringa + 'sweet[]=' + el.value + '&';
		});
		//Ciclo l'array degli other
		Array.prototype.forEach.call(elementOther, function(el) {
			stringa = stringa + 'other[]=' + el.value + '&';
		});
		//Ciclo l'array degli other
		Array.prototype.forEach.call(elementWine, function(el) {
			stringa = stringa + 'wine[]=' + el.value + '&';
		});
		//Rimuovo ultimo & dalla stringa
		stringa = stringa.substring(0, stringa.length - 1)
		//alert(stringa);
	})

    $(".droppable-starter").droppable({
		drop: function(event, ui) {
			this.appendChild(createDroppedElement(ui.draggable,'starter'));
			$(document).trigger('domChanged');
		}
	}).sortable({
        update: function (event, ui) {
        	$(document).trigger('sortChanged');
    	}
	});
	$(".droppable-first").droppable({
		drop: function(event, ui) {
			this.appendChild(createDroppedElement(ui.draggable,'first'));
			$(document).trigger('domChanged');
		}
	}).sortable({
        update: function (event, ui) {
        	$(document).trigger('sortChanged');
    	}
	});
	$(".droppable-second").droppable({
		drop: function(event, ui) {
			this.appendChild(createDroppedElement(ui.draggable,'second'));
			$(document).trigger('domChanged');
		}
	}).sortable({
        update: function (event, ui) {
        	$(document).trigger('sortChanged');
    	}
	});
	$(".droppable-sweet").droppable({
		drop: function(event, ui) {
			this.appendChild(createDroppedElement(ui.draggable,'sweet'));
			$(document).trigger('domChanged');
		}
	}).sortable({
        update: function (event, ui) {
        	$(document).trigger('sortChanged');
    	}
	});
	$(".droppable-other").droppable({
		drop: function(event, ui) {
			this.appendChild(createDroppedElement(ui.draggable,'other'));
			$(document).trigger('domChanged');
		}
	}).sortable({
        update: function (event, ui) {
        	$(document).trigger('sortChanged');
    	}
	});
	$(".droppable-wine").droppable({
		drop: function(event, ui) {
			this.appendChild(createDroppedElement(ui.draggable,'wine'));
			$(document).trigger('domChanged');
		}
	}).sortable({
        update: function (event, ui) {
        	$(document).trigger('sortChanged');
    	}
	});

});


</script>


</body>
</html>
