var speed = 200;
var totalRequired = 0;
var totalPassed = 0;

$(document).ready(function() {
	init();
});

function init()
{
	if($('#slideshow1').length!=0) slideshow1(); 		
	if($('#slideshow2').length!=0) slideshow2();	
	if($('#map').length!=0) map();	
	if($('form').length!=0) placeholders();			
	if($('#loan_app_wrap').length!=0) applications();
			
	if($('.print').length!=0) printWindow();	
	$('.disabled').click(function(e){e.preventDefault(); })
	
	$('.triggerSubmit').click(function(){
		$('#freeform').trigger('submit');
	});
}



function slideshow1(){
	$('#slideshow1').cycle({
		delay: 1000, 
		timeout:  6500,
		speed: 1000
	});
}

function slideshow2(){
	$('#slideshow2').cycle({ 
		fx:     'fade', 
		next:   '#next', 
		timeout: 0
	});
}

function printWindow(){
	$('.print').click(function(e){
		e.preventDefault();
		window.print();
	});
}

function map(){
	$("#map").gMap({
		zoom: 14,
		controls: ["GSmallZoomControl3D"],
		maptype: G_PHYSICAL_MAP,
		scrollwheel: false,
		markers: [{
			address:"1301 North Second Street Philadelphia , PA , 19122",
			latitude:  39.97096,
			longitude: -75.13922,
			popup: false,
			iconanchor: [-60,20],
			infowindowanchor: [-60,40], 			
			html: "Finanta<br/>1301 North Second Street<br/>Philadelphia,PA ,19122" 
		}] 
	});
}

// function placeholders(){	
// 	$('input:not(#submit,:hidden,:radio,:checkbox,:checked), textarea').each(function(){
// 		if(!elementSupportAttr('input','placeholder')) {
// 			$(this).val($(this).attr('placeholder')).addClass('placeholder');
// 
// 			$(this).focus(function(){
// 				if($(this).val()=="" || $(this).val()==$(this).attr('placeholder')) {
// 					$(this).val('').removeClass('placeholder');	
// 				}
// 
// 			}).blur(function(){				
// 				if ($(this).val()==$(this).attr('placeholder') || $(this).val()==""){
// 					$(this).val($(this).attr('placeholder')).addClass('placeholder');
// 				}
// 
// 			});
// 		}
// 	});		
// }

function placeholders(){	
	$('input:not(#submit,:hidden,:radio,:checkbox,:checked,.button)').each(function(){
		if(!elementSupportAttr('input','placeholder')) {
			
			var ph = $(this).attr('placeholder');
			
			if($(this).val()=="" || $(this).val()==$(this).attr('placeholder')) {
				$(this).addClass('placeholder').val(ph);	
			}
			

			$(this).focus(function(){
				if($(this).val()=="" || $(this).val()==$(this).attr('placeholder')) {
					$(this).val('').removeClass('placeholder');	
				}

			}).blur(function(){				
				if ($(this).val()==$(this).attr('placeholder') || $(this).val()==""){
					$(this).val($(this).attr('placeholder')).addClass('placeholder');
				}
			});
		}
	});		
}


function elementSupportAttr(ele,attr){
	var check = document.createElement(ele);
	if(attr in check){	
		return true;
	}	
	else{
		return false;
	}
}

function applications(){
	specialFormFunctionality();
	totalRequired = $('#loan_app_wrap').find('input[required]').length;
	
	$('#next_step').click(function(e){ formStep(e, 'next'); });
	$('#previous_step').click(function(e){ formStep(e, 'previous'); });
}

function formStep(e, dir){
	e.preventDefault();	
		
	var cur = $('.current').attr('id');
	var next = $('.current').next().attr('id');
	var prev = $('.current').prev().attr('id');
	
	if (dir == "next" && next != undefined) nextStep(dir, cur, next);	
	if (dir == "previous" && prev != undefined) prevStep(dir, cur, prev);
}

function nextStep(dir, curStep, nextStep){		
	if (checkStep(curStep, nextStep)) {	
		if($('#'+nextStep).next().attr('id')==undefined) $('#next_step').fadeOut(speed);	
		if (dir=="next" && curStep=='step_1') $('#previous_step').fadeIn(speed);
		updateNav(nextStep);	
		animateSwitch($('.current'), $('#'+nextStep));
	}
}

function prevStep(dir, curStep, prevStep){
		if($('#'+curStep).next().attr('id')==undefined) $('#next_step').fadeIn(speed);
		if (dir=="previous" && prevStep=='step_1') $('#previous_step').fadeOut(speed);	
		updateNav(prevStep);
		animateSwitch($('.current'), $('#'+prevStep));		
}

function updateNav(step){
	$('.current_step').removeClass();
	$('#nav_'+step).addClass('current_step');
}

function animateSwitch(cur, step){
	$(cur).fadeOut(speed, function(){
		$(this).removeClass('current');
		$(step).fadeIn(speed, function(){
			placeholders();
			$(this).addClass('current');
		});
	});
}


function checkStep(curStep, nextStep){
	var required = $('#'+curStep).find('input[required]').length;
	var passed = 0;
	
	for (var i=0; i < required; i++) {
		var requiredField = $('#'+curStep).find('input[required]')[i].id;
		
		// $(this).val()!="" && $(this).val() != $(this).attr('placeholder') && $(this).attr('required')
		
		if($('#'+requiredField).val()!="" && $('#'+requiredField).val()!=$('#'+requiredField).attr('placeholder')){
			$('#error_message').fadeOut(speed);
			$('#'+requiredField).removeClass('missing_required');
			++passed;
		}
		
		else{
			$('#error_message').fadeIn(speed);
			$('#'+requiredField).addClass('missing_required');
		}
	};
	
	if(passed==required) {
		totalPassed += passed;	
		
		if (totalRequired==totalPassed && $('#'+nextStep).next().attr('id')==undefined) { 
			$('.disabled').removeClass('disabled').addClass('triggerSubmit');
			$('#submit_form').fadeIn(speed).addClass('triggerSubmit');
			
			$('.triggerSubmit').click(function(){
				submitForm(curStep, nextStep);
				
			});
		}	
		
		return true;
	}	
	else return false;
}

function submitForm(curStep, nextStep){
	if(checkStep(curStep,nextStep)){
		$('#freeform').trigger('submit');
	}	
}

function specialFormFunctionality(){	
	optionalTxtField('loan_marital', 'mar_status_other', 'mar_status_exp');
	optionalTxtField('loan_hearother', 'hear_other', 'specify');	
	optionalTxtField('loan_elig_a_radio', 'eligibility_a_yes', 'eligibility_a_comments');
	optionalTxtField('loan_elig_b_radio', 'eligibility_b_no', 'eligibility_b_comments');
	optionalTxtField('loan_elig_m', 'eligibility_m_yes', 'eligibility_m_explain');
	optionalTxtField('finan_requ_other', 'finan_requ_other_other', 'finan_requ_other_specify');
	optionalTxtField('loan_elig_i_radio', 'eligibility_i_yes', 'eligibility_i_explain');
	optionalTxtField('loan_elig_n', 'eligibility_n_yes', 'eligibility_n_explain');
	optionalTxtField('loan_elig_m', 'eligibility_m_yes', 'eligibility_m_comments');
	optionalTxtField('loan_elig_j_radio', 'eligibility_j_yes', 'eligibility_j_comments');
}

function optionalTxtField(inputName, id, txtfield){
	$('#'+txtfield).hide();
	$('input[name="'+inputName+'"]').click(function(){
		if($('#'+id+':checked').length==1) $('#'+txtfield).fadeIn(speed);
		else $('#'+txtfield).fadeOut(speed);
		
		placeholders();
	});	
	
	
}



