;(function($) {

	if(!window.PRO)
		window.PRO = {};
		
	$.extend(PRO, {
		dialog: null,
		overlay: null,
		dialogVisible: false,
		dialogProfiles: {
			survey: {
				title: 'Let us help you choose an eCommerce solution',
				url: 'survey-partial.html',
				load: function() {
					this
						.attr('data-dialog-profile', 'survey')
						.find('form').bind('submit', PRO.gradeSurvey);
					;
					
					// Fake generated content for the numbers in IE7 on the form
					if($(document.body).hasClass('ie7')) {
						var ctr = 0;
						this
							.find('form ol > li').each(function() {
								$(this).prepend('<span class="before">' + (++ctr) + '</span>');
							})
						;
					}
					
				},
				unload: function() {
					this.find('form').unbind('submit');
				}
			},
			magentoGo: {
				title: 'Our Recommended Solution',
				url: 'magento-go-partial.html',
				load: function() {},
				unload: function() {}
			},
            prostores: {
                title: 'Our Recommended Solution',
                url: 'prostores-partial.html',
                load: function() {},
                unload: function() {}
            },
            prostoresForES: {
                title: 'Our Recommended Solution',
                url: 'prostores-for-es-partial.html',
                load: function() {},
                unload: function() {}
            },
			magentoEnterprise: {
				title: 'Our Recommended Solution',
				url: 'magento-enterprise-partial.html',
				load: function() {},
				unload: function() {}
			},
			magentoGoForES: {
				title: 'Our Recommended Solution',
				url: 'magento-go-for-es-partial.html',
				load: function() {},
				unload: function() {}
			}
			
		},
		
		init: function() {
			var
				$dialog = $('#dialog'),
				$overlay = $('#overlay')
			;
			
			if(!$overlay[0])
				$overlay = $('<div id="overlay"></div>').appendTo(document.body);
			
			if(!$dialog[0])
				$dialog = $('<div id="dialog"><div class="dialog-header"><h2></h2><a href="#close" class="close ir" title="Close Dialog">Close Dialog</a></div><div class="dialog-body"></div></div>').appendTo(document.body);

			this.dialog = $dialog;
			this.overlay = $overlay;
			
			this.dialog.delegate('.close', 'click', function(ev) {
				ev.preventDefault();
				
				PRO.hideDialog();
			});
		},
		
		showDialog: function(profile) {
			// console.log("showDialog:", profile);  // TODO: Remove for Production
			
			this.dialog.show();
			this.overlay.show();
			this.dialogVisible = true;
			// Prevent scrolling while dialog is open
			$(document).bind('scroll', function(ev) {
				ev.preventDefault();
			});
			
			if(profile)
				this.setupDialogContent(profile);
		},
		hideDialog: function() {
			this.dialog.hide();
			this.overlay.hide();
			this.dialogVisible = false;
			// Re-enable scrolling when dialog closes
			$(document).unbind('scroll');
		},
		
		setupDialogContent: function(profile) {
			var
				opts = this.dialogProfiles[profile] || {},
				dialog = this.dialog,
				currentProfile = dialog.attr('data-dialog-profile') || null
			;
			
			// Run the unload method for any cleanup on the dialog content
			if(currentProfile && !!this.dialogProfiles[currentProfile].unload)
				this.dialogProfiles[currentProfile].unload.call(dialog);
			
			if(!!opts.url) {
				dialog.find('.dialog-body').empty().load(opts.url, function(res, stat, xhr) {
					// Update dialog title
					dialog.find('.dialog-header h2').text(opts.title);

					// Run custom load code for the current dialog content
					if(!!opts.load) {
						opts.load.call(dialog);
					}
				});
			}
		},
		
		gradeSurvey: function(ev) {
			var
				$form = $(this),
				data = $form.serializeArray(),
				newData = {},
				i = 0,
				dl = data.length,
				recommended = ''
			;

			// Validation
			if(dl < 5) {
				$form.find('h3').after('<p class="error">Please answer all 5 questions</p>');
				return false;
			} else {
				$form.find('.error').remove();
			}

			// Convert form data into usable k/v format
			for( ; i < dl; newData[data[i].name] = data[i].value, i++);

			// Survey assessment logic
			switch(true) {
				case newData.TwoMil === 'Yes':
					recommended = 'magentoEnterprise';
					break;
				case newData.TenK === 'Yes':
				case newData.Subscriptions === 'Yes':
				case newData.Digital === 'Yes':
					recommended = (newData.eBay === 'Yes') ? 'prostoresForES' : 'prostores';
					break;
				case newData.eBay === 'Yes':
					recommended = 'magentoGoForES';
					break;
				default:
					recommended = 'magentoGo';
					break;
			}
			
			// Show the next dialog
			PRO.setupDialogContent(recommended);
			
			return false;
		}
		
	});

	$(function() {
		PRO.init();
		
		PRO.hideDialog();
		
		$(document.body)
			.delegate('.dialog', 'click', function(ev) {
				ev.preventDefault();
				
				PRO.showDialog($(this).attr('data-dialog-profile'));
			});
		;
		
		// For dev purposes
		// $('.dialog').eq(0).click();
	});

})(jQuery);
