/***********************************************/
/* APP.JS : SUBSTOGO UNIVERSAL JS              */
/* COPYRIGHT(C) 2009-2010 SPINNAKER360 PTE LTD */
/***********************************************/

/* CUSTOM IE FLAGS */
if (Browser.Engine.trident) {
	if (Browser.Engine.version == 4) { Browser.isIE6 = true; }
	else if (Browser.Engine.version == 5) { Browser.isIE7 = true; }
	else if (Browser.Engine.version == 6) { Browser.isIE8 = true; }
}

window.addEvent('domready', function(){
	// (1) CSS CLASS APPENDIX
	var body = document.body,
		loginBox = $('loginBox'),
		loginBtn = $('loginProcessButton'),
		svc = $('serviceCheck');

	// add 'mac' class to <body> tag for all OSX browsers
	if (Browser.Platform.mac) { $(body).addClass('mac'); }
	// add 'win' class to <body> tag for all Windows browsers
	else if (Browser.Platform.win) { $(body).addClass('win'); }

	// add special css classes to <body> tag for Internet Explorer
	if (Browser.Engine.trident) {
		$(body).addClass('msie').addClass(Browser.isIE6? 'msie6' : Browser.isIE7 ? 'msie7' : Browser.isIE8 ? 'msie8' : '');
	} else if (Browser.Engine.gecko) {
		$(body).addClass('gecko');
	} else if (Browser.Engine.webkit) {
		$(body).addClass('webkit');
		if ((/version\/3/i).test(navigator.userAgent)) { $(body).addClass('safari3'); }
	} else if (Browser.Engine.presto) {
		$(body).addClass('opera');
	}

	// (2) CART STUFF...
	updateSneak(); initToolTip();

	if (loginBox) {
		// submit login form when pressing Enter in either email / password fields
		$$('#login_email', '#login_password').addEvent('keydown', function(ev) {
			if(ev.key == 'enter'){ login(); }
		});

		if (loginBtn) {
			loginBtn.addEvent('click', function() { login(); });
		}
	}

	// (3) SERVICE CHECK
	// submit postal code when enter is pressed
	// note: service availability check is only available on the homepage
	if (svc) {
		/****
		svc.addEvents({
			mouseenter: function() {
				var msg = $('serviceMessage');

				// remove all pending timeouts
				$clear(msg.retrieve('timeoutID'));
				msg.eliminate('timeoutID');
			},

			// clear service availability results 1 second after mouseout
            mouseleave: function() {
				var msg = $('serviceMessage'),
					timeoutID = (function() {
						if (this.innerHTML.length > 0) {
							this.eliminate('timeoutID');
							this.empty();

							var fx = new Fx.Morph('sscmid', {
								duration: 500,
								transition: Fx.Transitions.Sine.easeOut
							}).start({
								height: 43
							});
						}
					}).delay(1000, msg);

				// store the timeoutID so we can cancel
				// the pending timeout if the user mouses over
				// the service availability form once again
				msg.store('timeoutID', timeoutID);
			}
		});
		****/
		$('servicePC').addEvents({
			// submit service availability form on enter
			keydown: function(ev) {
				if (ev.key == 'enter') { checkService(); }
			},

			// clear postal code field on focus
			focus: function(ev) { ev.target.value = ''; },

			// show default postal code field text on blur
			blur: function(ev) {
				var t = ev.target;
				if (!String(t.value).replace(' ', '')) {
					t.value = 'Enter your postal code';
				}
			}
		});

		$('ssbtn').addEvent('click', function() {
			checkService();
		});
	}

	// (4) ORDERING INSTRUCTIONS
	// note: ordering instructions are only present on the homepage
	if ($('orderingInstructions')) {
		// slideshow for subway homepage's 5-step ordering instructions
		// note: requires custom Slideshow.Push class (location in js/slideshow.push.js)
		var ss = new Slideshow.Push('orderingInstructions', [
			'frame1.jpg', 'frame2.jpg', 'frame3.jpg',
			'frame4.jpg', 'frame5.jpg', 'frame0.jpg'
		], {
			delay: 10000,
			transition: 'back:in:out', // image slide effect
			direction: 'right', // image slide direction
			controller: { // controller fade-in effect
				duration: 500,
				transition: Fx.Transitions.Sine.easeIn
			},
			hu: 'images/', // image path relative to root
			loader: {
				// spinner images
				animate: ['images/loader-#.png', 12]
			}
		});
	}

	// (5) START CLOCKING?
	if(timeBomb!==null){ timeSync(); }
});