
var club_order = new Array();
var club_save_count = 0;

function club_show_menu( id ) {
	var id = id;

	subitems = menu_items[id]["items"];

	$('#content').empty();

	for( e in subitems ) {
		
		var submenu = subitems[e];
		
		$('#content').append('<h2 style="border-bottom: 1px solid #ffffff; clear: both; margin-top: 20px;">'+submenu['title']+'</h2>');
		//$('#layout_main').append('<div></div>');
		
		for( ee in prod_coll ) {
			
			var prod = prod_coll[ee];

			// skip prod not in this categ
			if( prod['id_categ'] != submenu['id_categ'] ) {
				continue;
			}

			var html = '';
			html += '<div style="width: 200px; height: 200px; margin: 10px; float: left;">';
			html += '<table><tr><td>';
			html += '<img src="files/'+prod['img_th']+'" />';
			html += '<br/>';
			html += '<a href="/item/'+prod['id']+'" target="_blank">'+prod['title']+'</a>';
			html += '<br/>';
			html += '<strike>'+prod['price_sell']+'$</strike> '+prod['price']+'$';
			html += '<br/>';
			html += '<img src="img/round_minus.png" style="vertical-align: middle; cursor: pointer;" onclick="club_item_rem('+prod['id']+');" /> ';
			html += '<input type="text" id="input_'+prod['id']+'" value="'+( club_order[prod['id']] ? club_order[prod['id']] : 0 )+'" style="width: 20px; font-size: 15px; font-weight: bold;" onchange="club_item_update('+prod['id']+');" /> ';
			html += '<img src="img/round_plus.png" style="vertical-align: middle; cursor: pointer;" onclick="club_item_add('+prod['id']+');" />';
			html += '</td><td>';
			html += '</td></tr></table>';
			html += '</div>';

			$('#content').append(html);
			
		}
		
		$('#content').append('<br style="clear: both;" /><br style="clear: both;" />');
			
	}
	
}

function club_show_item( id ) {
	var id = id;

	$('#content').empty();

	var prod = prod_coll[id];

	var html = '';
	html += '<div style="width: 200px; height: 200px; margin: 10px; float: left;">';
	html += '<table><tr><td>';
	html += '<img src="files/'+prod['img_th']+'" />';
	html += '<br/>';
	html += '<a href="/item/'+prod['id']+'" target="_blank">'+prod['title']+'</a>';
	html += '<br/>';
	html += '<strike>'+prod['price_sell']+'$</strike> '+prod['price']+'$';
	html += '<br/>';
	html += '<img src="img/round_minus.png" style="vertical-align: middle; cursor: pointer;" onclick="club_item_rem('+prod['id']+');" /> ';
	html += '<input type="text" id="input_'+prod['id']+'" value="'+( club_order[prod['id']] ? club_order[prod['id']] : 0 )+'" style="width: 20px; font-size: 15px; font-weight: bold;" onchange="club_item_update('+prod['id']+');" /> ';
	html += '<img src="img/round_plus.png" style="vertical-align: middle; cursor: pointer;" onclick="club_item_add('+prod['id']+');" />';
	html += '</td><td>';
	html += '</td></tr></table>';
	html += '</div>';

	$('#content').append(html);

}

function club_item_add( id ) {
	// update qty in input
	var qty = $('#input_'+id).val();
	qty++;
	$('#input_'+id).val(qty);
	
	// update qty in order
	club_order[id] = qty;
	
	club_order_show();
}

function club_item_rem( id ) {
	// update qty in input
	var qty = $('#input_'+id).val();
	qty--;
	qty = qty < 0 ? 0 : qty;
	$('#input_'+id).val(qty);
	
	// update qty in order
	club_order[id] = qty;
	
	club_order_show();
}

function club_item_update( id ) {
	var qty = $('#input_'+id).val();

	// update qty in order
	club_order[id] = qty;
	
	club_order_show();
}

function club_order_show() {
	
	var amounttot = 0.00;
	var html = '';
	
	for( id in club_order ) {
		
		if( club_order[id] <= 0 ) {
			continue;
		}
		
		prod = prod_coll[id];
		amount = prod['price'] * club_order[id];
		amount = Math.round( amount*100 , 2 ) / 100;
		
		amounttot += amount;

		html += '<div style="border-bottom: 1px dashed #aaaaaa;">';
		html += '<a href="#" onclick="club_show_item('+prod['id']+');return false;" style="color: #000000; font-weight: bold;">'+prod['title']+'</a>';
		html += '<br />';
		html += ''+prod['price']+' X '+club_order[id]+' = '+(amount.toFixed(2))+'$';
		html += '</div>';

	}

	$('#order').html( html );

	html = '<b>TOTAL: '+(amounttot.toFixed(2))+'$</b>';
	html += '<form method="post" action="club/order.php"><input type="hidden" name="data" value="'+club_order_tostring()+'"><input type="image" src="img/club_confirm.png" onclick="club_submit();" style="cursor: pointer;" /></form>';	
	
	$('#total').html( html );

	if( scrollpos > posstart.top ) {
		$('#order_block').css({position: 'absolute', top: scrollpos+50 });
	} else {
		$('#order_block').css({position: 'absolute', top: posstart.top });
	}
	
	club_save_count++;
	if( club_save_count >= 5 ) {
		club_save_order();
		club_save_count = 0;
		//alert('auto save: '+club_order_tostring());
	}
	
}

function club_save_order() {
	$.post(
		"/club/autosave.php" ,
		{data:club_order_tostring()}
	);
}

function club_order_tostring() {
	var str = '';
	for( e in club_order ) {
		if( str ) {
			str += ',';
		}
		str += e+':'+club_order[e];
	}
	return str;
}

function club_order_fromstring( str ) {
	var o = new Array();
	var r = str.split(',');
	for( e in r ) {
		p = r[e].split(':');
		o[ parseInt(p[0]) ] = parseInt(p[1]);
	}
	return o;
}

function club_submit() {
	club_save_order();
	
}
