var zipratesSubmit = function() { return false; };

window.addEvent('domready', function(){						 
	
	// Begin Question Wizard
	var quest_panel_height = 270,
		quest_panel_width = 480,
		quest_panel_padding = 40;
	
	function initQuestions() {
			
		$$('div.quest_mask').each(function(el) {
			el.setStyles({
				width: quest_panel_width,
				height: quest_panel_height,
				visibility: 'visible'
			});

			$$('table.quest_grid tbody tr td').each(function(ele) {
				var questDiv_quest_panel  = new Element('div', {
					'class': 'quest_panel',
					'html': ele.get('html'),
					'styles': {
						'height': (quest_panel_height - (quest_panel_padding * 2)),
						'width': (quest_panel_width - (quest_panel_padding * 2)),
						'padding': (quest_panel_padding)
					}
				});
				ele.erase('html');
				questDiv_quest_panel.inject(ele);
			});
			
		
		});
	}
	initQuestions();
	
	function questMove(direction, multiplier) {
		$$('div.quest_panels').each(function(el) {
			var currentTop = el.getStyle('margin-top').toInt();
			var currentLeft = el.getStyle('margin-left').toInt();
			var moveDistance = 0;
			
			var quest_panels_effect = new Fx.Morph(el, {duration: 'long', transition: Fx.Transitions.Cubic.easeOut});
			
			if (direction == 'up') {
				moveDistance = currentTop + (quest_panel_height * multiplier);
				//el.setStyle('margin-top', moveDistance);
				quest_panels_effect.start({
						'margin-top': [currentTop, moveDistance]
				});
			} else if (direction == 'down') {
				moveDistance = currentTop - (quest_panel_height * multiplier);
				//el.setStyle('margin-top', moveDistance);
				quest_panels_effect.start({
						'margin-top': [currentTop, moveDistance]
				});
			} else if (direction == 'left') {
				moveDistance = currentLeft + (quest_panel_width * multiplier);
				//el.setStyle('margin-left', moveDistance);
				quest_panels_effect.start({
						'margin-left': [currentLeft, moveDistance]
				});
			} else if (direction == 'right') {
				moveDistance = currentLeft - (quest_panel_width * multiplier);
				//el.setStyle('margin-left', moveDistance);
				quest_panels_effect.start({
						'margin-left': [currentLeft, moveDistance]
				});
			} else {
				alert('That direction is not supported.');
			}
		});
	}
	questMove('right',2);
	
	$$('a.quest_up1').each(function(el) {
		el.addEvent('click', function(){
			questMove('up',1);
		});
	});
	$$('a.quest_down1').each(function(el) {
		el.addEvent('click', function(){
			questMove('down',1);
		});
	});
	$$('a.quest_left1').each(function(el) {
		el.addEvent('click', function(){
			questMove('left',1);
		});
	});
	$$('a.quest_right1').each(function(el) {
		el.addEvent('click', function(){
			questMove('right',1);
		});
	});
	
	$$('a.quest_reset').each(function(el) {
		el.addEvent('click', function(){
			$$('div.quest_panels').each(function(el) {
				el.setStyles({
					'margin-top': '0px',
					'margin-left': '0px'
				});
				questMove('right',2);
			});										  	
		});
	});
	// End Question Wizard

	// Zip Rates
	var zipRatesEnable = true;
	if ($('obtutorialxy') && zipRatesEnable){
		var ziprateZip 		= Cookie.read('ziprateZip'),
			ziprates		= $$('span.ziprate'),
			zipratesform 	= $('ziprateform'),
			tutorialeffect	= new Fx.Morph('obtutorialxy', {duration: 'long', transition: Fx.Transitions.Bounce.easeOut}),
			cmsRate			= parseFloat($('cms-rate').get('html')),
			cmsZipRate		= parseFloat($('cms-ziprate').get('html'));
				
		ziprates.each(function(el, index) {
			el.setStyle('font-weight', 'bold');
		});

		// Tutorial popup 
		var initializetutorial = function() {
			$('obtutorialxy').setStyle('margin-top', '0px');
			tutorialeffect.start({
				'margin-top': 75
			});
			$('ziprateform-zip').focus();
			ziprates.each(function(el){
				el.set('html', cmsRate);
			});
		};
		
		var zipratesInitialize = function() {
			if (ziprates!='') {
				//alert(ziprates + ' found.');
				if ( $chk(ziprateZip) ) {
					ziprates.each(function(el){
						//alert('Found one!');
						var ziprateZips = new Array(330040,330085,330120,330175,330290,330435,330450,330470,332015,332020,332035,332040,332075,332080,332085,332090,332095,332110,332120,332125,332130,332140,332160,332170,332180,332195,332200,332510,332545,332560,332575,332580,332600,332605,332610,332635,332660,332665,332670,332675,332680,332690,332700,332720,332735,332745,332750,332760,334170);
						ziprateZips.sort(); //sort the list to cover any mistakes 
						for(i=0; i<ziprateZips.length; i++){
							if (ziprateZip == ziprateZips[i]) {
								el.set('html', cmsZipRate);
								$('obtutorialxy').setStyle('margin-top', '-5000px');
								//var flashObj = document.getElementById('FlashObjID');
								//flashObj.callFlashFunction (cmsRate);
								i=ziprateZips.length+1;
							} else {
								el.set('html', cmsRate);
							}	
						}	
					});
				} else {
					initializetutorial();
				}	
			} else {
				//alert('No ziprate found.');
			}
		};
		zipratesInitialize();
		
		zipratesSubmit = function() {
			//var e = new Event(e).stop();
	    	var tutorialCookie = Cookie.write('ziprateZip', ($('ziprateform-zip').value *5), {duration: 0});
	    	//zipratesInitialize();
			$('obtutorialxy').setStyle('margin-top', '-5000px');
			return true;
		};
		
		var tutorialclose = $$('.tutorialclose');
		tutorialclose.each(function(el, index) {
			el.addEvents({
				click: function(){
					var tutorialCookie = Cookie.write('ziprateZip', 'Skipped', {duration: 0});
					$('obtutorialxy').setStyle('margin-top', '-5000px');
					}
			});
		});
		var tutorialcloseperm = $$('.tutorialcloseperm');
		tutorialcloseperm.each(function(el, index) {
			el.addEvents({
				click: function(){
	    			var tutorialCookie = Cookie.write('ziprateZip', 'Skipped', {duration: 0});
					$('obtutorialxy').setStyle('margin-top', '-5000px');
					}
			});
		});
		if ($('ziprateform')) {
			var zipratesSubmits = $('ziprateform').getChildren('input[type=submit]');
			zipratesSubmits.addEvents({
				click: function(){
					zipratesSubmit();
				}
			});
		}
		var tutorialOpen = $$('.ziprateset');
		tutorialOpen.each(function(el, index) {
			el.addEvents({
				click: function(){
					var tutorialCookie = Cookie.dispose('ziprateZip');
					ziprateZip = Cookie.read('ziprateZip');
					initializetutorial();
				}
			});
		});

	}

});
