/**
 * Javascript for Citibank
 * 
 */
	
	var slideSpeed = 1000;
	var currentStep = 1;
	var ajaxURL = "profiler.asp";
	
	$(document).ready(function() {
		
		// add odd and even classes
			
			$('#one_questions, #two_questions').children('li').addClass('even');
		
		
		
		// add numbers
			
			$('#one_questions .question').each(function(i) {
				//var newPos = '-'+((i%2) ? 0 : 40) +'px -'+(30*i)+'px';
				var newPos = '-40px -'+(30*i)+'px';
				var html = '<div class="number hdn" style="background-position: '+newPos+'"></div>';
				$(this).before(html);
			}).wrap('<div class="clearfix">');
			$('#two_questions .question').each(function(i) {
				//var newPos = '-'+((i%2) ? 0 : 40) +'px -'+(30*i)+'px';
				var newPos = '-40px -'+(30*i)+'px';
				var html = '<div class="number hdn" style="background-position: '+newPos+'"></div>';
				$(this).before(html);
			}).wrap('<div class="clearfix">');
		
		
		
		// add slider actions
			
			$('#one_btn').click(function() {
				
				// set current step
					currentStep = 1;
				
				// slide second part up
					$('#two_questions').slideUp(slideSpeed);
				
				// slide 'my' questions up or down
					var slider = $('#one_questions');
					if(slider.css('display') == 'none') slider.slideDown(slideSpeed);
					else slider.slideUp(slideSpeed);
				
				// prevent default action
					return false;
				
			});
			
			$('#two_btn').click(function() {
				
				// set current step
					currentStep = 2;
				
				// validate first item
					if(!validateFirstPart()) return false;
				
				// slide first part up
					$('#one_questions').slideUp(slideSpeed);
				
				// slide 'my' questions up or down
					var slider = $('#two_questions');
					if(slider.css('display') == 'none') slider.slideDown(slideSpeed);
					else slider.slideUp(slideSpeed);
				
				// prevent default action
					return false;
				
			});
		
		
		
		// submit button
			
			$('#btn_submit').click(function() {
				if(validateFirstPart()) {
					if(currentStep == 2 && validateSecondPart()) {
						$.post(ajaxURL, $("#profilerForm").serialize(), function(r) {
							$('#ajaxContent').html(r);
							showOverlayer();
						});
					} else {
						currentStep = 2;
						$('#one_questions').slideUp(slideSpeed);
						$('#two_questions').slideDown(slideSpeed);
					}
				} else {
					currentStep = 1;
					$('#one_questions').slideDown(slideSpeed);
					$('#two_questions').slideUp(slideSpeed);
				}
				return false;
			});
		
		
		
		// hovers
			
			$('#one_questions, #two_questions').children('li').hover(function() {
				$(this).removeClass('even').addClass('odd');
				switchNumbers();
			}, function() {
				$(this).removeClass('odd').addClass('even');
				switchNumbers();
			});
		
		
		
		// resize overlayer
			
			$(window).resize(function() {
				resizeOverlayer();
			});
		
		
		
	});
	
	function switchNumbers() {
		$('#one_questions .number').each(function(i) {
			if($(this).parent('li').hasClass('odd')) {
				var newPos = '0 -'+(30*i)+'px';
				$(this).css('background-position', newPos);
			} else {
				var newPos = '-40px -'+(30*i)+'px';
				$(this).css('background-position', newPos);
			}
		});
		$('#two_questions .number').each(function(i) {
			if($(this).parent('li').hasClass('odd')) {
				var newPos = '0 -'+(30*i)+'px';
				$(this).css('background-position', newPos);
			} else {
				var newPos = '-40px -'+(30*i)+'px';
				$(this).css('background-position', newPos);
			}
		});
	}




/**
 * Validation functions
 * 
 */
	
	function validateFirstPart() {
		
		// reset
			var valid = true;
			hideErrors();
		
		// validate part one
			var currentName = '';
			$('#one_questions input[type="radio"]').each(function() {
				
				// start new validation group
					if(currentName != $(this).attr('name')) {
						
						// set name
							currentName = $(this).attr('name');
						
						// get checked inputs
							var currentIsValid = $('input[name="'+currentName+'"]:checked').size();
						
						// compare those
							if(!currentIsValid) valid = false;
						
					}
				
			});
		
		// show error
			if(!valid) showErrors();
		
		// return result
			return valid;
		
	}
	
	function validateSecondPart() {
		
		// reset
			var valid = true;
			hideErrors();
		
		// validate part one
			var currentName = '';
			$('#two_questions input[type="radio"]').each(function() {
				
				// start new validation group
					if(currentName != $(this).attr('name')) {
						
						// set name
							currentName = $(this).attr('name');
						
						// get checked inputs
							var currentIsValid = $('input[name="'+currentName+'"]:checked').size();
						
						// compare those
							if(!currentIsValid) valid = false;
						
					}
				
			});
		
		// show error
			if(!valid) showErrors();
		
		// return result
			return valid;
		
	}
	
	function hideErrors() {
		$('#error').hide();
		$('#submit').removeClass('error');
	}
	
	function showErrors() {
		$('#error').show();
		$('#submit').addClass('error');
	}




/**
 * Overlayer functions
 * 
 */

	function showOverlayer() {
		
		// resize the shizzle
			resizeOverlayer();
		
		// show overlayer
			$('#overlayer').show();
		
		// fix png
			$(document).pngFix();
		
		// add actions to close button
			$('#overlayer_close').live('click', function() {
				hideOverlayer();
				return false;
			});
		
		// add actions to redo button
			$('.btn_redo').click(function() {
				hideOverlayer();
				self.scrollTo(0,0);
				return false;
			});
		
	}
	
	function hideOverlayer() {
		$('#one_questions').slideDown(slideSpeed);
		$('#two_questions').slideUp(slideSpeed);
		$('#overlayer').hide();
	}
	
	function resizeOverlayer() {
		
		// get values
			var fadeTo = 0.75;
			var winW = parseFloat($(window).width());
			var winH = parseFloat($(window).height());
			var scrT = parseFloat($(document).scrollTop());
			var docH = parseFloat($(document).height());
			var newT = scrT+(winH/2);
			var newL = (winW/2)-(867/2);
				
		// set items
			$('#overlayer_bg').css({ 'width': winW, 'height': winH }).fadeTo(0, fadeTo);
			$('#overlayer_bg').height(docH);
			$('#overlayer_window, #overlayer_window_bg').css({ 'top': newT, 'left': newL, 'margin-left': 0 });
		
	}




