Ext.ns('JJRemote');
Ext.ns('SpecTool');

JJRemote = {
	actionsUrl:'http://webproducts.jj-invision.com/Actions.aspx',
	
	init: function() {
		JJRemote.initTopNav();
		JJRemote.initSignoutLink();
		JJRemote.initIframe();
	},

	initTopNav: function() {
		var topNav = Ext.get('topNav');

		if (topNav) {
			// do jsonp request to check if user is logged in
			Ext.ux.JSONP.request({
				url : JJRemote.actionsUrl,
				params: {
					format:'json',
					action:'checkstatus',
					unique:(new Date().getTime())
				},
				success : function(response) {
					if (response.status == 'user') {
						Ext.get('navUserName').child('span').update(response.user);
						topNav.removeClass('hideUser');
						topNav.addClass('hideGuest');
					}
					else if (response.status == 'visitor') {
						topNav.removeClass('hideGuest');
						topNav.addClass('hideUser');
					}
				},
				failure : function(response) {

				},
				callback : function(response) {

				},
				scope : this
			});
		}
	},

	initSignoutLink: function() {
		var signoutLink = Ext.get('signoutLink');
		if (signoutLink) {
			signoutLink.on('click', function(e, el){
				e.preventDefault();
				Ext.ux.JSONP.request({
					url : JJRemote.actionsUrl,
					params: {
						format:'json',
						action:'signout'
					},
					success : function(response) {
						location.href = '/products/cart/login';
					},
					failure : function(response) {

					},
					callback : function(response) {

					},
					scope : this
				});
			});
		}
	},

	currentIframeClass:'',
	toClassName: function(url) {
		var parts = url.split('/');
		var last = parts[parts.length-1];
		var pageName = last.split('.')[0];
		return pageName;
	},

	initIframe: function() {
		// set event handler on iframe so every time it loads we can check if the user is logged in
		var iframe = Ext.get('cartFrame');
		if (iframe) {
			// Remove page header so iframe contents will be used as header
			var pageTitle = Ext.get('pageTitle');
			if (pageTitle) {
				pageTitle.remove();
			}

			// Set class based on src
			var className = JJRemote.toClassName(iframe.dom.src);
			iframe.addClass(className);
			JJRemote.currentIframeClass = className;
			iframe.dom.onload = function() {
				// Remove old class name
				Ext.get(this).removeClass(JJRemote.currentIframeClass);
				var newClassName = JJRemote.toClassName(this.src);
				Ext.get(this).addClass(newClassName);
				JJRemote.initTopNav();
			}
		}
	}
};

SpecTool = {
	selectStyle: function() {
		if (Ext.get('specToolStyleId').dom.value != 0) {
			location.href = '/pages/Specifications/?style=' + Ext.get('specToolStyleId').dom.value;
		}
	},

	formSubmit: function() {
		if (Ext.get('specToolBackingId').dom.value == '0') {
			return false;
		}
		//zipcode is not required
		/*
		if (!Ext.get('specToolZip').dom.value.length) {
			return false;
		}
		*/
		//color is not required
		/*
		if (Ext.get('specToolColorId').dom.value == '0') {
			return false;
		}
		*/
		return true;
	}
};

Ext.EventManager.on(window, 'load', function() {
	JJRemote.init();
});
