
var product = '';
var product_price = 0;
var page = 'shop.html';
var col = 0;
var shipping = new Array();
	shipping['CARD'] = [ 16.52 , 12.76 , 76.52 , 76.52 , 80.87 , 99.13 , 99.13 , 114.78 ];
	shipping['RACK'] = [ 17.39 , 26.04 , 161.74 , 161.74 , 168.70 , 201.74 , 201.74 , 265.22 ];
	shipping['STUDIO'] = [ 17.39 , 26.04 , 161.74 , 161.74 , 168.70 , 201.74 , 201.74 , 265.22 ];
	shipping['STUDIO2'] = [ 38.26 , 55.65 , 257.39 , 257.39 , 272.17 , 465.22 , 465.22 , 456.52 ];
var shipping_c = [ 'Canada' , 'USA' , 'Europe' , 'Africa' , 'Asia' , 'Australia' , 'New Zeland' , 'Central & South Americas' ];

function choose_product( button , selection ) {
	
	// remember product selection
	product = selection;
	product_price = parseInt( button.value );
	
	// set column to work on
	switch( product ) {
		case 'CARD': col = 2; break;
		case 'RACK': col = 3; break;
		case 'STUDIO': col = 4; break;
	}
	
	// hide all displayed options boxes
	// remove anchors
	$('#shop input:checkbox').hide();
	$('#shop .col_selected').attr('className','');
	$('#shop .addcart_on').attr('className','addcart_off');
	
	// show all option boxes of selected product
	var first = true;
	$('#shop tr').each(function(){

		// get second TD
		td = $(this).find('td:eq('+col+')');

		// highlight row and cart options
		if( $(td).attr('className') == 'addcart_off' ) {

			// add checkbox
			if( $(td).find('input').length == 0 ) {
				// add first anchor
				if( first ) {
					$(td).append('<a name="first_opt_'+col+'"></a>');
					first = false;			
				}
				// insert checkbox html
				$(td).append('<input type="checkbox" onclick="update(this);" />');
			}
			// show checkbox
			$(td).find('input').show();
			// hight option
			$(td).attr('className','addcart_on');

		} else {

			// hightlight col
			$(td).attr('className','col_selected');

		}

	});
	
	// show shipping options
	if( ! $('#shipping_options').length ) {
		show_sipping_options();
	}
	
	// change choose option link to target options
	$('#choose_options_link').eq(0).attr('href',current_page+'#first_opt_'+col);
	
	update();
		
	// go to first option
	//window.location.href = page + '#first_opt_'+col;
}

function show_sipping_options() {
	var html = '';
	var prices = shipping[product];
	var titles = shipping_c;
	
	html += '<div id="shipping_msg" style="display:none; margin: 5px; padding: 5px; background: #B73900; color: #ffffff;">Please select<br />a shipping option</div>';
	html += '<select id="shipping_options" onchange="update()">';
	html += '<option value=""> - please select - </option>'
	for( e in prices ) {
		var price = prices[e];
		var title = shipping_c[e];
		html += '<option value="'+e+'">'+title+'</option>'
	}
	html += '</select>';
	
	// show selection
	$('#shipping').html( html );
}

function update() {
	
	// show cart
	$('#cart_container').show();
	
	var total = 0.00;
	total += product_price;
	
	$('#cart').html('');
	
	// add product to cart
	$('#cart').append( 'Acxel2 '+product+' : '+product_price+'$<br />' );
	
	// traverse all rows
	$('#shop tr').each(function(){
		
		var tr = this;
		
		// get selected col
		var td = $(this).find('td:eq('+col+')');
		
		// find selected input in selected col
		var checkbox = $(td).find('input:checkbox').get(0);
		if( checkbox && checkbox.checked ) {
			
			// get option price
			var price = parseInt( $(td).attr('sdval') );
			// get option description
			var desc = $(tr).find('td:eq(0)').html();			
			
			// add option and price to list
			$('#cart').append( desc+' +'+price+'$<br />');
			
			// inc total
			total += price;
		}
		
	});
	
	// speacial process for option OpANA_Pack384
	if( $('#OpANA_Pack384 input').attr('checked') ) {
		// disable OpANA_Pack192
		$('#OpANA_Pack192 input').attr('disabled','disabled');
	} else {
		$('#OpANA_Pack192 input').attr('disabled','');
	}

	// add shipping options
	if( $('#shipping_options').eq(0).val() ) {
		var value = $('#shipping_options').eq(0).val();
		// get price
		var price = shipping[product][value];
		// get title
		var title = shipping_c[value];
		$('#cart').append( '<br />Shipping: '+title+' +'+price+'$<br />' );

		// hide messages
		$('#shipping_msg').hide(400);
		
		// inc total
		total += price;
	}
	
	// show total
	$('#cart').append( '<br />TOTAL: '+total+'$<br />' );
	
	// show confirm button
	if( product ) {
		$('#conf_btn').show();
	}
	
	// update order form
	var order_details = $('#cart').html();
	order_details = order_details.replace( '"' , '' );
	$('#order_details').val( order_details );
	$('#order_total').val( total );
}

function order_confirm() {
	if( ! $('#shipping_options').val() ) {
		$('#shipping_msg').show(400);
		return false;
	}
	$('#order_form').get(0).submit();
	return true;
}
