function googleTranslateElementInit() {
	new google.translate.TranslateElement({pageLanguage: "af"}, "google_translate_element");
};

function rotator() {
	var rotator = $('#main-page #rotator'), //Caching Objects
		rotatorstories = $('#rotator-stories .item', rotator),
		rotatornavitems = $('#rotator-nav .item', rotator);
	rotatornavitems.each(function(index){
		$(this).click(function(){
			$(this).addClass('active').siblings().removeClass('active');
			rotatorstories.filter('.active').removeClass('active').fadeOut(function() {
    			rotatorstories.eq(index).addClass('active').fadeIn('slow');
 			});
		});
	});
	rotatornavitems.eq(0).click();
}

function tweet() {
	var tweets = $('#socialmedia .twitter #twtr-widget-1');
}

function cufon() {
	Cufon.replace('#main-header #main-nav .page_item a', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page #info h2', { fontFamily: 'caeciliaold' });
    Cufon.replace('#sidebar .side-nav h1', { fontFamily: 'caeciliaold' });
    Cufon.replace('#sidebar .side-nav ul .page_item a', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page #info h3', { fontFamily: 'caeciliaitalic' });
    Cufon.replace('#main-page #rotator #rotator-stories .item .info p', { fontFamily: 'caeciliaitalic' });
    Cufon.replace('#main-page .socialmediafeeds .events p', { fontFamily: 'caeciliaroman' });
    Cufon.replace('#main-page .socialmediafeeds .events h4', { fontFamily: 'caeciliaroman' });
    Cufon.replace('#main-page .events p', { fontFamily: 'caeciliaroman' });
    Cufon.replace('#main-page .#rotator #rotator-nav .item', { fontFamily: 'caeciliaold' });
    Cufon.replace('footer #footer-nav ul .item', { fontFamily: 'caeciliaold' });
    Cufon.replace('footer h1:not(span)', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page #socialmedia h2', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page .socialmediafeeds h2', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page .socialmediafeeds .events h3', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page .events h3', { fontFamily: 'caeciliaold' });
    Cufon.replace('#main-page .events h4', { fontFamily: 'caeciliaroman' });
}

function menulinks() {
	var menulinks = $('#main-header #main-nav ul .page_item'),
		submenulinks = $('#sidebar ul .page_item a'); //Caching Objects
	$('a', menulinks).hover(function(){
		$(this).css('color','#F89B24');
		Cufon.replace($(this), { fontFamily: 'caeciliaold' });
	},function () {
		$(this).css('color','#333');
		Cufon.replace($(this), { fontFamily: 'caeciliaold' });
	});
	submenulinks.hover(function(){
		$(this).css('color','#F89B24');
		Cufon.replace($(this), { fontFamily: 'caeciliaold' });
	},function () {
		$(this).css('color','#333');
		Cufon.replace($(this), { fontFamily: 'caeciliaold' });
	});
}
function response(form) {
	if($('#response').length){
		form.siblings(':not("header,#response, #payment")').addClass('hidden');
		$('#response .'+($('#wp-form').attr('class').replace(' hidden', '').replace('clear ', ''))).removeClass('hidden');
		$('#response').siblings('header').children('h2').hide();
	}
}
function wpform() {
	var form = $('#main-page.form #wp-form'),
		explanation = $('#main-page.form #explanation'),
		miscellaneous = $('#main-page.form #miscellaneous'),
		alerttext = ''; //Caching Objects
	/*$(explanation).hide();
	$(privacypolicy).hide();*/
	//form.show();
		
	if($('#main-page #prevent-default').length){
		explanation.addClass('hidden');
	}
	response(form);
	$(explanation).submit(function() {
		$(this).hide();
		form.show();
		$('ul li input',form).autotab_magic();
		$('#housing_down_payment_amount', form).parent().css('padding-left',$('#housing_dependents_disabled_multi', form).parent().outerWidth());
		miscellaneous.show();
		return false;	 
	});
	$('#home_buyer_signature, #cohome_buyer_signature',form).parent().siblings('.title').remove();
	$('#home_buyer_signature',form).parentsUntil('form').before(miscellaneous);
	$('ul.optional',form).hide();
	$('#housing_additional_info', form).parent().addClass('clear');
	$('#preapproved_loan_closing_date_1,#preapproved_loan_broker, #preapproved_loan_broker_email, #preapproved_loan_education_class_multi,#housing_down_payment_amount,#cohome_buyer_continued_other,#cohome_buyer_continued_employer,#home_buyer_continued_other,#home_buyer_continued_employer',form).parent().hide();
	$('#preapproved_loan_multi',form).parent().siblings(".form-item:not('.title')").hide();
	$('#about_phc_bank_tail_8',form).hide();
	$('ul:first',form).before('<div class="optional-item clear right"><label for="optional_choice">Do you have a Co-Home Buyer?</label><br><select name="optional_choice" id="optional_choice"><option value="optional_choice_1" name="optional_choice_1" id="optional_choice_1">No</option><option value="optional_choice_2" name="optional_choice_2" id="optional_choice_2">Yes</option></select></div>').addClass('clear');
	$('#optional_choice',form).change(function() {
	  if($("option:selected", this).text()=='Yes'){
	  	$('ul.optional',form).show();	
	  }else{
	  	$('ul.optional',form).hide();	
	  }
	});
	
	$('#preapproved_loan_multi',form).change(function() {
	  if($("option:selected", this).text()=='Yes'){
	  	$('#preapproved_loan_officer_name, #preapproved_bank, #preapproved_loan_officer, #preapproved_loan_contract_multi',form).parent().show();
	  }else{
	  	$('#preapproved_loan_multi',form).parent().siblings(".form-item:not('.title')").hide();
	  }
	});
	$('#housing_down_payment_multi',form).change(function() {
	  if($("option:selected", this).text()=='Yes'){
	  	$('#housing_down_payment_amount',form).parent().show();
	  }else{
	  	$('#housing_down_payment_amount',form).parent().hide();
	  }
	});
	$('#about_phc_bank_multi',form).change(function() {
	  if($("option:selected", this).text()=='Other'){
	  	$('#about_phc_bank_tail_8',form).show();
	  }else{
	  	$('#about_phc_bank_tail_8',form).hide();
	  }
	});	
	$('#preapproved_loan_contract_multi',form).change(function() {
	  if($("option:selected", this).text()=='Yes'){
	  	$('#preapproved_loan_closing_date_1,#preapproved_loan_broker, #preapproved_loan_broker_email, #preapproved_loan_education_class_multi',form).parent().show();
	  }else{
	  	$('#preapproved_loan_closing_date_1,#preapproved_loan_broker, #preapproved_loan_broker_email, #preapproved_loan_education_class_multi',form).parent().hide();
	  }
	});
	
	$('#cohome_buyer_continued_employed_multi',form).change(function() {
	  if($("option:selected", this).text()=='Employed'){
	  	$('#cohome_buyer_continued_employer',form).parent().show();
	  	$('#cohome_buyer_continued_other',form).parent().hide();
	  }else if($("option:selected", this).text()=='Other'){
	  	$('#cohome_buyer_continued_other',form).parent().show();
	  	$('#cohome_buyer_continued_employer',form).parent().hide();
	  }else{
	  	$('#cohome_buyer_continued_employer',form).parent().hide();
		$('#cohome_buyer_continued_other',form).parent().hide();	
	  }
	});		
	$('#home_buyer_continued_employed_multi',form).change(function() {
	  if($("option:selected", this).text()=='Employed'){
	  	$('#home_buyer_continued_employer',form).parent().show();
	  	$('#home_buyer_continued_other',form).parent().hide();
	  }else if($("option:selected", this).text()=='Other'){
	  	$('#home_buyer_continued_other',form).parent().show();
	  	$('#home_buyer_continued_employer',form).parent().hide();
	  }else{
	  	$('#home_buyer_continued_employer',form).parent().hide();
		$('#home_buyer_continued_other',form).parent().hide();	
	  }
	});	
	formvalidation(form);	
}

function wpform2() {
	var form = $('#main-page.form #wp-form'),
		alerttext = ''; //Caching Objects	
	response(form);
	
	$('ul li input',form).autotab_magic();
	formvalidation(form);
}

function formvalidation(form){
	$(form).submit(function() {
		$('.error',form).removeClass('error');
		alerttext='';
		
		$('input:not(:hidden, #form-submit, .notneeded, #home_buyer_cell_phone_number_1, #home_buyer_cell_phone_number_2, #home_buyer_cell_phone_number_3, #cohome_buyer_email, #donation_memorial_info, #donation_additional_comments)',form).each(function(){
			if($(this).val()=='' || $(this).val()=='$'){
				$(this).parent().addClass('error');
				alerttext = 'alert';
			}
		});
		
		$('select:not(:hidden, #form-submit, #optional-choice, .notneeded)',form).each(function(){
			if(!$('option:selected',this).val()){
				$(this).parent().addClass('error');
				alerttext = 'alert';
			}
		});
		
		if(alerttext){
			alert('The highlighted fields need to be filled before you can continue this process.');
		}else{
			return true;
		}
		
		return false;
	});
}

function questions() {
	var questionbutton = $('.questions'),
		questionbox = $('#questions-box'),
		questionclose = $('#questions-close', questionbox); //Caching Objects

	questionbutton.click(function(){
		questionbox.show();
	});
	questionclose.click(function(){
		questionbox.hide();
	});
	questionbox.submit(function() {
		$('input, textarea', questionbox).removeClass('error');
		var data = {name: $('#questions_name', questionbox).val(),
					question: $('#questions_question', questionbox).val(),
					email: $('#questions_email', questionbox).val() };
		if(data.name){		
			$.post("wp2/wp-content/themes/phc/question-process.php", data,function(response){
				alert("Your question has been mailed successfully.");
				$('input', questionbox).val('');
				questionbox.hide();
			}).error(function(response) {
				alert("There was an error emailing your question.");
			});
		}else{
			$('input:not(#questions-submit), textarea', questionbox).each(function(){
				if($(this).val()==''){
					$(this).parent().addClass('error');
				}
			});
			alert("Please fill out the fields highlighted in red.");
		}
		return false;	 
	});
}

function payment_ajax() {
	var paymentform = $('#payment'),
	submitbutton = $('.submit', paymentform),
	data = {},
	processing = false; //Caching Objects
	submitbutton.click(function(){
		$('.errormessage').remove();
		$('input:not(.submit,:hidden)', paymentform).each(function(){
			if($(this).val()){
				data[$(this).attr('name')]=$(this).val();	
			}
		});
		if(!processing){
			processing = true
			$.post("wp2/wp-content/plugins/wp-form/aim.php", data,function(response){
				if(response){
					paymentform.before('<div class="errormessage"><h2>There was an error processing your credit card:</h2><br/>'+response+'</div>');
					processing = false;
				}else{
					paymentform.submit();				
				}
			}).error(function(response) {
				paymentform.before('<div class="errormessage"><h2>There was an error processing your credit card:</h2><br/>'+response+'</div>');
				processing = false;
			});
		}
	});
}

$(document).ready(function(){
	if($('#main-page #rotator #rotator-stories').length){
		rotator();
	}
	if($('#socialmedia .twitter #twtr-widget-1').length){
		tweet();
	}
	if($('#main-page .facebook .wall').length){
		$('#main-page .facebook .wall').fbWall({
			id:'portlandhousingcenter',
			accessToken:'206158599425293|259670c18806606c8cdb6349.1-5140349|SHFzfimKdrietIhWZhl5Y_Kff7Y',
			showGuestEntries:true,
			showComments:true,
			max:1,
			timeConversion:24
		});
	}
	if($('#main-page #wp-form.Registration').length){
		wpform();
	}
	if($('#main-page #wp-form.Donation').length){
		wpform2();
	}
	
	if($('.additional-info').length){
		$('.additional-info').each(function(index){
			$(this).hover(
			  function () {
			    $('span',this).show();
			  }, 
			  function () {
			     $('span',this).hide();
			  }
			);
		});
	}
	if($('#payment').length){
		payment_ajax();
	}
	questions();
	cufon();
	menulinks();
	$('footer #google_translate_element .goog-te-combo').live('change', function() {
	  setTimeout("cufon()",2000);
	});
	$('#main-page form.surveyme')
	
	/*if($('video')){
		$('video').mediaelementplayer({
			features: ['current','progress','duration','playpause','forward','volume']	
		});
		
	}*/
});



