<?php
include("../includes/session.php");
include("../includes/functions.php");
include("../includes/db.php");
session(1);

if(!empty($_GET['reorder'])){
	$ids = explode(",",$_GET['reorder']);
	foreach($ids as $v){
		mysql_query("UPDATE ordini_abb_lav SET ord = ".intval($ord)." WHERE id=".intval($v));
		$ord++;
	}
}

//nome della categoria
$sql = "SELECT abbcat_descr FROM abbigliamento_cat WHERE abbcat_id = ".intval($_REQUEST['cat_id']);
// echo $sql; echo "<br>"; die("st");
$result = mysql_query($sql);
$cat_name = mysql_result($result,0,'abbcat_descr');

//estraggo gli articoli di questa categoria di abbigliamento, per quest'ordine per questo tipo di lavoratore

$sql = "
	SELECT *
	FROM ordini_abb LEFT JOIN ordini_abb_lav ON ordini_abb.ordabb_id = ordini_abb_lav.ordabb_id
	WHERE abbcat_id = ".intval($_REQUEST['cat_id'])." AND lavoro_id = ".intval($_REQUEST['lav_id'])." AND ord_id = ".intval($_REQUEST['ord_id'])."
	ORDER BY ord, ordabb_nome, ordabb_cod
";
// echo $sql; echo "<br>"; die("st");

$result = mysql_query($sql);

$articoli_ordine = '';
$noitems_ordine = '';
if(mysql_num_rows($result) > 0){
	$noitems_ordine = ' style="display:none"';
	while($row = mysql_fetch_array($result)){

		$ordabb_cod = $row['ordabb_cod'];
		$ordabb_nome = $row['ordabb_nome'];

		$img = '../artimages/thumbs/'.$row['ordabb_id'].'.png';
		if(!is_file($img))
			$img = '../../images/noimage_thumb.png';

		$articoli_ordine .= '<a class="cell" href="javascript:editItem('.$row['ordabb_id'].')">
			<div class="img">
				<img src="../'.$img.'" />
			</div>
			<div class="text">'.$ordabb_nome.'</div>
			<input type="hidden" name="id" value="'.$row['id'].'" />
		</a>';
	}
}


?>
<div class="slider_outer">
    <div class="no_items"<?php echo $noitems_ordine;?>>Nessun articolo inserito</div>
    <div class="side fLeft"></div>
    <div class="slider">
        <?php echo $articoli_ordine;?>
    </div>
 	<div class="side fRight"></div>
	<div class="clear"><!-- --></div>
</div>

<div class="clear"><!-- --></div>

<div class="dialog_btns" style="border:0">
    <center>
    	<button class="btn" type="button" onclick="$('#ordabb_id').val(''); post('f_addAbb','dialog',function(){recenterDiag()})">NUOVO ARTICOLO</button>
    </center>
</div>

<form action="../../dialogs/abbigliamento_new.php" id="f_addAbb" onsubmit="return false;">
    <input type="hidden" name="cat_id" value="<?php echo intval($_REQUEST['cat_id']); ?>" />
    <input type="hidden" name="ord_id" value="<?php echo intval($_REQUEST['ord_id']); ?>" />
	<input type="hidden" name="lav_id" value="<?php echo intval($_REQUEST['lav_id']); ?>" />
	<input type="hidden" name="ordabb_id" id="ordabb_id" />
</form>

<script language="javascript">

var slider;
$(".slider_outer .side").mousedown(function(e) {

	var speed = 1;

	if($(this).is('.fRight')){ //slider dx
		slider = $(this).prev('.slider');
	}else{
		slider = $(this).next('.slider')
		speed = -speed;
	}

	var cells = $(slider).children('.cell').size();

	if(cells > 4)
		anim_speed = cells-4;
	else
		anim_speed = 1;

	$(slider).animate({
		scrollLeft: $(slider).offset().left * speed
	 }, anim_speed * 1000);

	return false;


}).mouseup(function(e) {
	$(slider).stop();
    return false;
});

$(function() {
	var restore;
	$( ".slider" ).sortable({
		items: ".cell",
		axis: "x",
		//containment: "parent",

		start: function( event, ui ) {
			$(ui.item).css('cursor','move');
		},
		update: function( event, ui ) {

			//update order
			$(ui.item).fadeTo(100,0.2).prepend('<div class="item_loading"></div>');

			var ids=""
			$(".cell").each(function(index, element) {
                ids += $(this).find('input').val() + ',';
            });

			$.get('../../dialogs/abbigliamento_add.php?reorder='+ids ,function(data){
				$(ui.item).find('.item_loading').remove();
				$(ui.item).fadeTo(1,1);
			});
		},
		stop: function( event, ui ) {
			$(ui.item).css('cursor','pointer');
		}

	}).disableSelection();

	updateOrderIcons();
});

function editItem(id){
	$('#ordabb_id').val(id);
	post('f_addAbb','dialog',function(){recenterDiag()});
}
</script>
