var fields;
var resp;
var datar;
$(document).ready(function(){
	
	createForm();
	
	if(keep_left_sidebar != true)
	{
		$('#sidebar').empty().css({backgroundImage:'url(/img/left_sidebar.new.jpg)'});
	}
	
	if( $('#radioForm div p').length == 0)
	{
		$('#radioForm').css({bottom: '50px'});
	}
	
	$('.button').click(function(){
		if(validates())
		{
			if($(this).hasClass('showExt') )
			{
				$('#cExt').slideDown();
			}else{
				if($(this).hasClass('submit') )
				{
	
				}else{
					$('#cExt').slideUp();
					$('input[name=opt]').attr('checked',false);
				}
				
				if($(this).hasClass('video'))
				{
					showVid();
					return false;
				}
				
				if($(this).hasClass('thanks'))
				{
					var thanks = true;
				}
				
				if($(this).hasClass('calc'))
				{
					showCalc();
					return false;
				}
				
				
				var reqVars = $('input').serialize();

				if($(this).hasClass('secondBtn')){
					reqVars += "&secondBtn=true";
				}
				if($(this).hasClass('thirdBtn')){
					reqVars += "&thirdBtn=true";
				}

                                //alert(reqVars);
				
				resp = $.post( '/emails', reqVars, function(data){ 
				//	alert(data);
				if(thanks || data == 'thanks' || data == 'thanks-video')
				{
					if(data == 'thanks-video')
					{
						showVideoThanks();
					}
					else
					{
						showThanks();
					}
				}else{
					datar = data;
					location.href=data;
					
				}
				
				}, 'text' );
			}
		}else{
			alert('There is a problem with your input, verify all fields are correct.')
		}
		return false;
	});
	
	fields = $('#cForm').children('dl').children('dd').children('label').children('input');
	
	fields.keyup(function(){
		validates();
	});
	
});

function createForm()
{
	$('#minimap').remove();
        $('#searchform').remove();

	$('#right_sidebar').css({width:'288px'});
	$('#cForm').children('dl').children('dt').children('span').hide();
	$('#cExt').hide();

	// choose right background based on how many buttons are encountered
			$('#kaContactWidget').css({backgroundImage:'url(/img/form_'+ $('#cButtons').children('.button').length +'.jpg)'});
			
	switch ( $('#cButtons').children('.button').length ) {
		case 1:
			if ($('#cButtons').children('.button').length == 1 )
			{
				$('#cButtons').css({top: '350px'});
			}
			
		break;
		
		default:
			return true;
			break;
	}
	
}

function validates()
{
	var invalid = 0;
	fields.each(function(i){
		if(!validate( fields.eq(i), i ))
		{
			invalid++;
		}
	});
	if(!invalid == 0)
	{
		return false;
	}else{
		return true;
	}
	
}

function validate(field, i)
{
	if( field.val().length >= 2 )
	{
		$('#cForm').children('dl').children('dt').eq(i).children('span').fadeOut();
		return true;
	}else{
		$('#cForm').children('dl').children('dt').eq(i).children('span').fadeIn();
		return false;
	}
}

function showThanks()
{
	$('body').prepend('<div id="thanks_popup"><img src="/img/new/thanks.jpg" alt ="thanks" /></div>');
	$('#thanks_popup').hide().fadeIn().click(function(){
		$('#thanks_popup').fadeOut();
		$('input[type=text]').val('');
		$('#cExt').slideUp();
		$('input[name=opt]').attr('checked',false);
	});
}

function showVideoThanks()
{
	$('body').prepend('<div id="thanks_popup"><img src="/img/new/thanks-video.jpg" alt ="thanks" /></div>');
	$('#thanks_popup').hide().fadeIn().click(function(){
		$('#thanks_popup').fadeOut();
		$('input[type=text]').val('');
		$('#cExt').slideUp();
		$('input[name=opt]').attr('checked',false);
	});
}

function showVid()
{
	$('body').prepend('<div id="thanks_popup" style="padding-top:200px"><object width="355" height="285"><param name="allowfullscreen" value="true"/><param name="allowscriptaccess" value="always" /><param name="wmode" value="transparent"/><param name="movie" value="/video.swf" /><embed src="/video.swf" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="355" height="285"></embed></object> </div>');
	$('#thanks_popup').hide().fadeIn().click(function(){
		$('#thanks_popup').fadeOut();
		$('input[type=text]').val('');
		$('#cExt').slideUp();
		$('input[name=opt]').attr('checked',false);
	});
}
function showCalc()
{
	$(window.location).attr('href', '/calc2');
}

