Ext.BLANK_IMAGE_URL = '/images/s.gif';

var cochin = {
  	src: '/js/sifr/sifr3/melrow.swf',
	wmode:'opaque'  
};

sIFR.prefetch(cochin);
sIFR.activate();

sIFR.replace(cochin, {selector: '.sifr', css: {'.sIFR-root': { 'leading':0, 'display':'inline', 'font-size':'15px','color': '#003876'} , 'em' : {'color': '#d22d51', 'font-style':'normal','font-weight': 'normal' }}, 'tuneHeight':'-3'});
sIFR.replace(cochin, {selector: '.sifr-small', css: {'.sIFR-root': { 'leading':0, 'text-align':'right','font-size':'12px','color': '#003876'} , 'em' : {'color': '#d22d51', 'font-style':'normal','font-weight': 'normal' }}, 'tuneHeight':'-3'});


var melrowMenu;

Melrow = function() {
	
	return {

		scriptsLoaded : false,
		quoteWizard: null,
	
		init : function(){

			Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
			Ext.QuickTips.init();
		
			if (melrowMenu){
				var nav = new Ext.ux.ColumnNav({items:melrowMenu.items, link:melrowMenu.link});
				nav.on('link', function(n, id){
					document.location.href= nav.link + id;
				}, this);
				nav.render("cn-wrap");  
			}

			this.initFront();						
			this.initProduct();
			this.initMtw();
			this.initCms();
			
		},
		
		initCms : function(){

			var tw;
		
			if (Ext.get('contact-form')){
				var contactForm = new Melrow.ContactForm();
			    contactForm.render('contact-form');				
			}

			if (Ext.get('turnkey-wizard')){
				Ext.get('turnkey-wizard').on('click', function(){
					if (!tw) tw = new Melrow.TurnKeyWizard();
					tw.show();					
				}, this);
			}
		},
		
		initProduct : function(){
			if (Ext.get('product-tabs')) {
			 
			 	var items = [{contentEl:'overview', title:'Overview'}];
				if (Ext.get('specification')){
					items[items.length] = {contentEl:'specification', title:'Specifications'}	
				}
			 
				var tabs = new Ext.TabPanel({
					renderTo: 'product-tabs',
			        activeTab: 0,
			        bodyStyle:'padding:10px',
			        frame:true,
			        defaults:{autoHeight: true},
			        items: items
				});		
			}
		},

		initMtw : function(){

			if (Ext.get('login-form')) Melrow.Mtw.Manager.renderLogin();
			if (Ext.get('mtw-issues-grid')) Melrow.Mtw.Manager.renderGrid();

		},
		
		showQuoteWizard : function(id){
			LoadingIndicator.show();
			
			var show = function(){	 		 
				if (!this.quoteWizard){
					this.quoteWizard = new Melrow.QuoteWizard();							
					this.quoteWizard.on('show', function(){
						LoadingIndicator.hide();	 		 
					},this);
				}
				this.quoteWizard.show(id);			
			}			

			if (!this.scriptsLoaded){
			 	scripts = [{url:'/js/ext-form.js'},{url:'/js/MiscField.js'},{url:'/js/Wizard.js'},{url:'/js/melrow/QuoteWizard.js'}];
			 	sl = new ScriptLoader(scripts);
			 	sl.on('done', function(){
					this.scriptsLoaded = true;
					show.createDelegate(this)();					
				}, this);
			 	sl.load();
			} else {
				show.createDelegate(this)();
			}
		
		},
				
		initFront : function(){
			if (Ext.get('newsletter-signup-btn')){
				Ext.get('newsletter-signup-btn').on('click', function(e){
					var email = Ext.get('newsletter-email').dom.value;
					if (!Ext.form.VTypes.email(email)) {
						Ext.MessageBox.alert('Melrow newsletter signup', 'You have entered an invalid email address.');
					} else {
					 	Ext.Ajax.request({method:'POST', url:'/customer/subscribe/' + email, success: function(){
							Ext.MessageBox.alert('Melrow newsletter signup', 'Thank you for signing up for our newsletter.');	
						}, scope:this});
					}
				}, this);
			}
		}
		
	}
 
}();

Melrow.Mtw = {};

Ext.onReady(Melrow.init, Melrow);


