

function OmegaException( message, details ) {
	this.message = message;
	this.details = $.extend( {}, details );
}

var OmegaWeb = {
	
	currentProduct:null,
	partImages:[],

	identify:function( form ) {
		$('#idResults').empty();
		try {
			var modelInfo = ModelIdentifier.identify( form['model'].value, form['monthday'].value, form['yearshift'].value );
			if( ! OmegaWeb.modelExists( modelInfo.model ) ) {
				throw new OmegaException( "Model not found." );
			}
			$('#modelIdentifierTpl').tmpl( modelInfo ).appendTo('#idResults');
		}
		catch( e ) {
			$('#modelIdentifierErrorTpl').tmpl( e ).appendTo('#idResults');
		}
	},

	modelExists:function( modelNo ) {
		return $('.model_' + modelNo ).length > 0;
	},

	collapseHeader:function( callback ) {
		$('header').animate({
			'height':'100px'
		}, 300, function() {
			$('header').css( 'cursor', 'pointer' );
			$('header').click( function() {
				document.location.href = "./";
			} );
			if( callback ) {
				callback();
			}
		} );				
// will need to figure something out for IE7.				
//				$('header').height('100px');
	},
	
	setProduct:function( data ) {
		OmegaWeb.currentProduct = data;
		$('body').trigger('productChanged', OmegaWeb.currentProduct );
	},
	
	showCustomize:function() {
		OptionsSelector.startOver();
		OmegaWeb.showCard( 'customize' );
	},
	
	showBom:function( options ) {
		if( OmegaWeb.currentProduct == null ) {
			return;
		}
		$('#errorMessages').empty();
		var bomUrl = Omega.config.baseBomUrl + OmegaWeb.currentProduct.bomUrl;
		jQuery.getJSON( bomUrl, function( data ) {
			$('#productDetails').slideUp( function() {
				$('#productDetails').html( data.html );
				Omega.partsImages = data.partImages;
				if( options && options.unfiltered ) {
					
				}
				else {
					OmegaWeb.filterBom();
				}			
				$('#productDetails').slideDown();
			} );
			
		} )
		.error( function() { console.log( 'Unable to load BOM. Current product: "' + OmegaWeb.currentProduct + '"' ) } );	
	},
	
	filterBom:function() {
		jQuery.each( OptionsSelector.selectedOptions(), function( index, option ) {
			var elemId = option.elemId;
			var selector = null;
			if( elemId.base == 'sw' ) {
				// filter by seat width
				selector = '.sw:not(.' + option.elemId.full + ')';	
			}
			else {
				// filter by other option
				selector = '\'.' + elemId.parent + ':not(.' + elemId.parent + '_0,.' + elemId.base + ')\'';
			}
			$('.bomTable ' + selector ).hide();
			
		} );
	},
	
	showFeatures:function() {
		if( OmegaWeb.currentProduct == null ) {
			return;
		}
		var url = Omega.config.baseFeaturesUrl + OmegaWeb.currentProduct.featuresUrl;
		jQuery.ajax(
			url,
			{
				dataType:"json",
				success:function( data, textStatus, jqHXR ) {
					$('#productDetails').slideUp( function() {
						$('#productDetails').empty();
						$('#featuresTpl').tmpl( data ).appendTo('#productDetails');
						$('#productDetails').slideDown();
					} );
				},
				statusCode:{
					404: function() {
						OmegaWeb.showError( 'Unable to load features for product id: "' + OmegaWeb.currentProduct.id + '"' );
					}
				}
			}
		);
	},
	
	showError:function( message ) {
		$('#errorMessages').empty();
		$('#errorMessageTpl').tmpl( { "message":message } ).appendTo('#errorMessages');
	},

	showCard:function( cardId, callback ) {
		$('#errorMessages').empty();
		$('#content').slideUp( function() {
			$('#content .card').hide();
			$('#content #' + cardId + '.card').show();
			$('#content').slideDown(
				function() {
					if( jQuery.isFunction( callback ) ) {
						callback();
					}
				}
			);
		} );
	},
	
	getProductImageMetadata:function( data ) {
		// sets up some hints for the jquery template.
		var hasImages = data.assemblyImg != null || data.drawings.length > 0;
		var firstImage = null;
		if( hasImages ) {
			firstImage = data.assemblyImg != null ? '/images/assemblies/' + data.assemblyImg : '/images/tech_drawings/' + data.drawings[0].image_src;
		}
		var meta = {
			hasImages:hasImages,
			firstImage:firstImage
		}
		return meta;
	},
	
	loadProductInfo:function( productId ) {
		
		var url = OmegaWeb.productInfoUrlFor( productId );
		
		jQuery.getJSON( url, function( data ) {
			OmegaWeb.setProduct( data );
			$('#content').slideUp( function() {
				$('#content .card').hide();
				$('#content #productSummary.card').show();
				
				$('#productSummary').empty();
				
				$('#productSummaryTpl').tmpl( $.extend( data, OmegaWeb.getProductImageMetadata( data ) ) ).appendTo('#productSummary');
				
				$('#content').slideDown( function() { $('body').trigger('productSummaryVisisble', OmegaWeb.currentProduct ) } );
			} );
			
		} )
		.error( function() { console.log( 'Unable to load BOM for product id: "' + productId + '"' ) } );
	},
	
	productInfoUrlFor:function( productId ) {
		return Omega.config.productInfoDir + productId + '.json';
	},
	
	bomUrlFor:function( productId ) {
		return Omega.config.baseBomUrl + 'bom_' + productId + '.json';
	},
	
	featuresUrlFor:function( productId ) {
		return Omega.config.baseFeaturesUrl + productId + ".json";
	}
};

