$( function () {

        if ( window.location.hash != '' ) {
			product = window.location.hash.substring(1);;
		}

		// unify to single dct cookie for all this stuff
		//         if ( product == '' ) {
		//             product = $.cookie('futuresProduct');
		//         }
		
		if ( product != '' ) {
			datasetLoad ( product );
		}

		$('#controller-update').click( function () {

				$(this).blur();

				product = $('#product').val();

				if ( 'A' == product ) {
					product = 'A';
					product += ( $('#load').val() )
						? $('#load').val()
						: '*';
					product += ( $('#state').val() )
						? $('#state').val()
						: '*';
				} else if ( 'C' == product ) {
					product = 'C';
				} else if ( 'E' == product ) {
					product = 'E' + $('#generator').val();
				} else if ( 'G' == product ) {
					product = 'G';
				}

				datasetLoad ( product );

			});


		$('#controller #product').change( function () {
				$(this).blur();
				controllerOptionsEnable ();

				product = $('#product').val();
				$('#controller-update').click();
			});

		$('#controller #state').change( function () {
				$(this).blur();
				$('#controller-update').click();
			});

		$('#controller #load').change( function () {
				$(this).blur();
				$('#controller-update').click();
			});

		$('#controller #generator').change( function () {
				$(this).blur();
				$('#controller-update').click();
			});

		controllerOptionsEnable ();

		// active drilldown links in dataset by delegation
		$('#dataset').bind('click', function(e){
				var link = $(e.target); 
				product = link.attr('rel');
				
				if ( undefined != product ) {
					var product_id = product.substr ( 0, 1 );

					// set dropdowns to match link
					$('#product').val( product_id );

					if ( 'A' == product_id ) {
						var state = product.substr ( 2, 1 );
						if ( state == '*' ) {
							state = '';
						}
						$('#state').val( state );

						var load = product.substr ( 1, 1 );
						if ( load == '*' ) {
							load = '';
						}
						$('#load').val( load );
					}

					if ( 'E' == product_id ) {
						var generator = product.substr ( 1, 1 );
						$('#generator').val( generator );
					}

					if ( product != undefined ) {
						datasetLoad ( product );
					}
				}

				return false;
			});

		// 		refreshInterval = window.setInterval ( 'datasetRefresh();', ( 1000 ) );
 		refreshInterval = window.setInterval ( 'datasetRefresh();', ( 5 * 60 * 1000 ) );

	});

function datasetLoad ( product ) {

	if ( 'A' == product.substr(0,1) ) {
		$('#product-currency').html ( 'AUD' );
	} else if ( 'C' == product.substr(0,1) ) {
		$('#product-currency').html ( 'USD' );
	} else if ( 'O' == product.substr(0,1) ) {
		$('#product-currency').html ( 'AUD' );
	} else if ( 'E' == product.substr(0,1) ) {
		$('#product-currency').html ( 'NZD' );
	} else if ( 'G' == product.substr(0,1) ) {
		$('#product-currency').html ( 'AUD' );
	}

   	$('#dataset').load ( '/market_futures?product=' + product, function () {
			window.location.hash = product;

			if ( $('#market-closed').length > 0 ) {
				clearInterval ( refreshInterval );
			}
		});
}

function datasetRefresh() {
	if ( window.location.hash != '' ) {
		product = window.location.hash.substring(1);;
		datasetLoad ( product );
	}
}

function controllerOptionsEnable () {
	var active = $('#product').val();
	$('#controller .controller-option-active').removeClass('controller-option-active');
	$('#controller-option-' + active.toLowerCase() ).addClass('controller-option-active');
}


function log ( s ) {
	if ( $('#log').length == 0 ) {
		$('#background').prepend('<div id="log" style="background: #fc0;">C</div>');
	}
	$('#log').html(s);

}