// global App class
var App = new function() {
  // init public scope
  this.root = '';
  this.helpers = {
	logOut : function() {
	  var btn = $('#logout');
	  var ht = $('html')[0]; 
	  if (btn.size()) {
		btn[0].onclick = function(e) {
		  ht.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)';  
		  if (confirm('Are you sure you want to log out?')) {
			return true;
		  } else {
			ht.style.filter = '';
			return false;
		  }
		};
	  }
	},
	
	loadFocus : function() {
	  var f = $('#loadfocus');
	  if (f.size()) {
		try {
		  f[0].focus();
		} catch (e) {};
	  }
	  return true;
	},
	
	confirmSave : function(m) {
	  return confirm('Are you sure you want to save' + (m.length ? ' ' : '') + m + '?\nConfirm all changes are correct before proceeding.');
	},	
	
	confirmDelete : function(m) {
	  return confirm('Are you sure you want to delete' + (m.length ? ' ' : '') + m + '?\nThis is cannot be reversed!');
	},
	
	confirmSend : function(m) {
	  return confirm('Are you sure you want to send' + (m.length ? ' ' : '') + m + '?');
	},
	
	confirmDefault : function(m) {
	  return confirm('Are you sure you want to' + (m.length ? ' ' : '') + m + '?');
	}	
  };
  
  this.init = function() {
	$(function() {
	  // get the root url
	  App.root = $('#logo a').attr('href');		   
			   
	  // init helpers
	  App.helpers.loadFocus();
	  App.helpers.logOut();
	  
	  // init misc
	  App.initMisc();
	  
	  App.initInterface();
	});	  
  };
  this.initInterface = function () {
    var logo, strip;
	
	// start any tabs
	$('.tabs').not('.normal').tabs({selectedClass: 'selected', fxSlide: true, fxFade: true, fxSpeed: 'slow', onShow: function() { $(arguments[1]).find('input[type=text]:first').focus(); } });	
	// init the menu
	$('ul.lava').lavaLamp({ fx: 'easeOutQuart', speed: 1000 });
	// bind quick enquiry ajax
	$('#s-enquire-f').ajaxForm({
	  beforeSubmit: function(a, $form) {
		var i = 0, e = [], $m = $('.m', $form);
		
		for (; i < a.length; i++) {
		  if (/^\s*$/.test(a[i].value)) {
			e.push(a[i].name);  
		  }
		}
		
		if (e.length) {
		  $m.html('Please fill in the following fields:<br /><br />' + e.join(', &nbsp;')).fadeIn(1500);
		  
		  try {
		    $('[@name=' + e[0] + ']', $form)[0].focus();
		  } catch(e) {};
		  
		  return false;
		} else {
		  $m.html('Sending...').fadeIn(1500);
		  return true;	
		}
	  },
	  success: function(d) {
		setTimeout(function() {
		  $('#s-enquire-f .m').html('Thank you for your enquiry, one of our staff will contact you shortly.');
		}, 1000);
	  },
	  clearForm: true,
	  semantic: true
	});
	// bind click to open enquiry form
	$('.show-enquiry').bind('click', function() { 
	  window.scrollTo(0); 

	  $('.tabs').triggerTab(3); 
	});	
	// quotes rotator
	$('#customer-quote').Rotator({ url: App.root + 'resources/xml/quotes.xml', delay: 10, speed: 'slow' });

	try {
	  logo = new SWFObject(App.root + 'resources/flash/logo.swf', 'logo', '360', '70', '9');
	  logo.addParam('wmode', 'transparent');
	  logo.addParam('menu', 'false');		
	  logo.write('logo');

	  strip = new SWFObject(App.root + 'resources/flash/strip.swf', 'flash-strip', '600', '150', '9');
	  strip.addParam('wmode', 'transparent');
	  strip.addParam('menu', 'false');		
	  strip.write('flash-strip');
	} catch (e) {};	
  };
  this.initMisc = function () {
	// set click handlers
	$('.confirmsave').bind( 'click', function() { 
	  return App.helpers.confirmSave( this.title ); 
	});
	$('.confirmdelete').bind( 'click', function() { 
	  return App.helpers.confirmDelete( this.title ); 
	});
	$('.confirmsend').bind( 'click', function() { 
	  return App.helpers.confirmSend( this.title ); 
	});
	$('.confirmdefault').bind( 'click', function() { 
	  return App.helpers.confirmDefault( this.title ); 
	});	
	// set messages to show
	$('.message').hide().fadeIn(1500);
	// fix bg flicker in IE 6
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {};	  
  };
};
App.init();
