function DiacloneSearchApp() {

	window.SammyApp = Sammy('#tabs-1', function() {
	  
	  this.bind('root', function(e, data) {
        this.redirect('#/');
      });
	  
	  this.get('#/', function() {
	  	App.removeAll();
	  	
		/* Append our initial root view */
	    App.rootView.appendTo('#tabs-1');
	    App.subnav.set('currentParams',{});
	  });
	  
	  this.get('#/results/', function() {
	  	App.removeAll();
	  	
	   	var data = {
		   	Keywords 	: this.params['Keywords'],
		  	Exact		: parseInt(this.params['Exact']) ? 1 : 0,
		  	RArea		: this.params['RArea'],
			PType		: this.params['PType'],
			Analyte		: this.params['Analyte'],
			Species		: this.params['Species'],
			Clone		: this.params['Clone'],
			FC			: this.params['FC'],
			p			: this.params['p'],
			p2			: this.params['p2']
	   	}
	  	
	    App.subnav.set('currentParams',data);
	    App.startLoading();
	
	  	jQuery.get('/product-search/ajax-search-result.asp', data, function(html) {
	  		App.resultsView.appendTo('#tabs-1');
	  		App.resultsView.set('content', html);
	  		App.endLoading();
	  	}, 'html');    
	    
	    return false;
	  });
	  
	  this.get('#/refine/', function() {
	  	App.removeAll();
	  	
	   	var data = {
		   	Keywords 	: this.params['Keywords'],
		  	RArea		: this.params['RArea'],
			PType		: this.params['PType'],
			Analyte		: this.params['Analyte'],
			Species		: this.params['Species'],
			Clone		: this.params['Clone'],
			FC			: this.params['FC']
	   	}  	
	  
	    App.subnav.set('currentParams',data);
		App.startLoading();
	
	  	jQuery.get('/product-search/ajax-product-search.asp', data, function(html) {
	  		App.endLoading();
	  		App.refineView.appendTo('#tabs-1');
	  		App.refineView.set('content', html);
	  	}, 'html');
	  	
	  	return false;
	  });
	  
	  
	  this.get('#/product/', function() {
	  	App.removeAll();  	
	   	var data = {
	   		ID			: this.params['ID'],
			Cat			: this.params['Cat'],
		   	Keywords 	: this.params['Keywords'],
		  	Exact		: parseInt(this.params['Exact']) ? 1 : 0,
		  	RArea		: this.params['RArea'],
			PType		: this.params['PType'],
			Analyte		: this.params['Analyte'],
			Species		: this.params['Species'],
			Clone		: this.params['Clone'],
			FC			: this.params['FC'],
			p			: this.params['p'],
			p2			: this.params['p2']
	   	}  	
	  
	    App.subnav.set('currentParams',data);
	    App.startLoading();
	
	  	jQuery.get('/product-search/ajax-product-details.asp', data, function(html) {
	  		App.productView.appendTo('#tabs-1');
	  		App.productView.set('content', html);
	  		App.endLoading();
	  	}, 'html');
	  });
	  
	  
	  this.get('#/molecule/:id', function() {
	  	jQuery('#tabs').tabs( "select" , 3 );
	  
		var data = { 'ID' : this.params['id'] };
			
		jQuery.get('/product-search/ajax-filter-test-molecules.asp',data,function(html){
			App.browseByMoleculeView.set('content',html);
		},'html');
	  });	  
	});


	jQuery('#refineSearch').find('select').live('change', function() {
		jQuery('#refineSearch').submit();
	});

	window.App = Ember.Application.create({
	  rootElement	: '#tabs-1'
	});
	
	App.rootView = Ember.View.create({
	  templateName	: 'diaclone-search-root',
	  tabContent	: jQuery('#tabs-1').html()
	});
	
	App.resultsView = Ember.View.create({
	  templateName	: 'diaclone-search-results',
	  content		:''
	});
	
	App.refineView = Ember.View.create({
	  templateName	: 'diaclone-search-refine',
	  content		:''
	});	

	App.productView = Ember.View.create({
	  templateName	: 'diaclone-search-product',
	  content		:''
	});
	
	App.browseByMoleculeView = Ember.View.create({
		templateName	: 'diaclone-search-browse-by-molecule',
		content			: ''
	});
	
	App.subnav = Ember.View.create({
	  templateName	: 'diaclone-search-subnav',
	  printProduct	: function(key, value) {
	  	if (arguments.length === 1) {
	  		var id = this.currentParams.ID;
	  		if (id) {
	  			return '<li class="link"><a href="#/print" id="product-search-print-result" data-id="'+id+'">Print Result</a></li>'
	  		}
	  	}
	  }.property('currentParams'),
	  currentParams	: {},
	  refineSearch	: function(key, value) {
	  	if (arguments.length === 1) {
	  		var str = '';
	  		jQuery.each(this.currentParams, function(key,value) {
	  			if (value) {
	  				str+= key+'='+value+'&';
	  			}
	  		});
	  		if (str) {
	  			return '<li class="link"><a href="#/refine/?'+str+'">Refine Your Search</a></li>';
	  		}
	  	} else {
	  		
	  	}
	  }.property('currentParams')
	});
	
	App.startLoading = function() {
		jQuery('#tabs-1').append('<div id="diaclone-loading" style="text-align:center"><b>Loading...</b></div>');
	}	

	App.endLoading = function() {
		jQuery('#tabs-1').find('#diaclone-loading').remove();
	}	
	
	App.removeAll = function() {
		App.rootView.remove();
		App.resultsView.remove();
		App.refineView.remove();
		App.productView.remove();
		
		/* Scroll to the top and make sure we're on the first tab */
		jQuery(window).scrollTop(0);
		jQuery('#tabs').tabs( "select" , 0 );
	}
	
	/* Add our browse by molecule template and setup our onclick events */
	App.browseByMoleculeView.appendTo('#tabs-4');
	
	/* Set up our subnavigation */
	jQuery('#sectnav').after('<div id="diaclone-search-subnav"></div>')
	App.subnav.appendTo('#diaclone-search-subnav');
	
	/* Give our app a class to target it */
	jQuery('#tabs-1').addClass('diaclone-search-app');
	
	jQuery('a[href=#tabs-1]').click(function() {
		 SammyApp.trigger('root');
		 return false;
	});
	
	jQuery.get('/product-search/ajax-browse-by-product.asp', function(html) {
		jQuery('#tabs-2').html(html)
	},'html');

	jQuery.get('/product-search/ajax-browse-by-application.asp', function(html) {
		jQuery('#tabs-3').html(html)
	},'html');
	
	jQuery('#product-search-print-result').live('click',function(event) {
		var id 	= jQuery(this).attr('data-id');
		var url	= 'http://www.gen-probe.com/product-search/product-print.asp?ID='+id;
		window.open(url, 'PrintResult', 'width=850,height=600,scrollbars=yes');
		event.preventDefault();
	});
	
	/* Remove anything in tab#1 and start the App */
	jQuery('#tabs-1').html('');
	SammyApp.run('#/');
	
}

jQuery(function() {
	if (jQuery('.last.act a').text() == 'Product Search') {
		jQuery.get('/product-search/templates.html', function(html) {
			jQuery('head').append(html);
			jQuery.getScript('/js/ember.min.js', function() {
				DiacloneSearchApp();
			});
		}, 'html');
	}
});
