/**
* JavaScript for mod_prod.php
*
* @package Site
* @subpackage Templates
* @author Pavel "Papi" Jartsev <papi@digitalfruit.ee>
*/

function initOnLoad()
{
	$$('#product-parts .edata a, #product-parts .odata a').each( function( el ) {
		var tr = el.up(1) || null;
		if ( tr ) {
			tr.onclick = el.onclick;
			el.onclick = null;
		}
	});
}

function removeShoppingCart( form, prod )
{
	form.elements['qty[' + prod + ']'].value = 0;
	form.act.value = 'chg';
	form.submit();
}

function togglePartInfo( part_id, info )
{
	var el = $('toggle'+part_id);
	var caller_tr = el.up(1);
	var table = caller_tr.parentNode;
	var td, tr;
	var id = el.id;

	// hide listing
	tr = $('tr'+id);
	if ( tr ) {
		Effect.BlindUp( 'div'+id, {
			duration: 0.4,
			afterFinish: function( obj ) {
				table.removeChild( tr );
				el.removeClassName( 'toggle-close' );
				el.addClassName( 'toggle-open' );
				caller_tr.removeClassName( 'toggle-opened-top' );
			}
		});

	// show listing
	} else {
		var a = $a({ 'class': 'button' }, I18N.add_to_inquiry );
		Event.observe( a, "click", function(){
			var qty = $('qty'+part_id).value;
			if ( qty > 0 ) {
				$('doc_prod_form').request({
					parameters: {
						'part_id': part_id,
						'qty': qty
					},
					onSuccess: function( transport ) {
						var json = transport.responseJSON;
						$('response'+id).update( json.message );
						$('inquiry-count').update( json.inquiry_count );
						Element.show( 'response'+id );
						window.response_id = 'response'+id;
						window.setTimeout( 'Effect.Fade( response_id, {duration: 1})', 2000 );
					}
				});
			}
		});

		var infoTable = $table({ className: 'info-table' }, $tbody(
			$tr([
				$td({ 'class': 'label' }, I18N.part_ships+':' ),
				$td( info[0] ),
				$td({ 'class': 'label' }, I18N.part_ship_weight+':' ),
				$td( info[1] ),
				$td({ colSpan: 2 }, $input({ id: 'qty'+part_id, value: 1, 'class': 'text', type: 'text' }), $span( I18N.quantity+':' ))
			]),
			$tr([
				$td({ 'class': 'label' }, I18N.part_sell_qty+':' ),
				$td( info[2] ),
				$td({ 'class': 'label' }, I18N.part_cat_page+':' ),
				$td( info[3] ),
				$td({ colSpan: 2 }, a )
			])
		));

		tr = $tr({ id: 'tr'+id, 'class': 'toggle-opened-bottom' },
			$td({ colSpan: caller_tr.cells.length, 'class': 'toggle-opened-bottom-td' },
				$div({ id: 'div'+id, style: 'display: none; position: relative;' },
					infoTable,
					$div({ id: 'response'+id, 'class': 'response', style: 'display: none;' }))));

		table.insertBefore( tr, caller_tr.nextSibling );
		caller_tr.addClassName( 'toggle-opened-top' );

		Effect.BlindDown( 'div'+id, {
			duration: 0.4,
			afterFinish: function( obj ) {
				el.removeClassName( 'toggle-open' );
				el.addClassName( 'toggle-close' );
			}
		});
	}
}

function updateShoppingCart( form )
{
	form.act.value = 'chg';
	form.submit();
}

