// On-load functions ####################################################################

$(document).ready(function(){
	faqControls();
	homeControls();
	$('#dialog').jqm();
	$('#open_calculator').click(function(){ openModal('modules/calculator.php'); });
	$('.home_top').flash({ src: 'flash/upright.swf ', width: 922, height: 365, wmode: 'transparent', version: '9.0.0', flashvars: { xml: 'xml/upright.xml' }
	},{ expressInstall: true });
});

// Home controls (top region) #########################################################

function homeControls(){
	$('#htb_controls').children('a').each(function(){
		$(this).mouseover(function(){
			
			var rel = $(this).attr('rel');
			var curclass = $(this).attr('class');
			
			if(curclass!='active'){
				$('#htb_controls a.active').removeClass('active');
				$(this).addClass('active');
				$('.ht_banners li.active').fadeOut(200).removeClass('active');
				$('.ht_banners li#htb_'+rel).fadeIn(300).addClass('active');
			}
		});
	});
	
}

// Show madal dialog ##################################################################

function openModal(url){
	$('#dialog').jqmShow();
	$('#modal_content').html('<div class="align-center note"><br />Loading...<br /><br /></div>');
	$('#modal_content').load(url,function(){ 
	  calculator();
	});
}

// Newsletter subscribe #################################################################

function subscribe(n,e,o,p){
	$.get('modules/subscribe.php?name='+n+'&email='+e+'&org='+o+'&phone='+p);
}

// Calculator ###########################################################################

function calculator(){
	$('.question').each(function(){
		$(this).change(function(){
			var q1 = $('#q1:checked').val();
				if(q1 == null){ q1 = 0; }
				q1 = (q1*1);
			var q2 = $('#q2:checked').val();
				if(q2 == null){ q2 = 0; }
				q2 = (q2*1);
			var q3 = $('#q3:checked').val();
				if(q3 == null){ q3 = 0; }
				q3 = (q3*1);
			var q4 = $('#q4:checked').val();
				if(q4 == null){ q4 = 0; }
				q4 = (q4*1);
			var q5 = $('#q5:checked').val();
				if(q5 == null){ q5 = 0; }
				q5 = (q5*1);
			var q6 = $('#q6:checked').val();
				if(q6 == null){ q6 = 0; }
				q6 = (q6*1);
			var q7 = $('#q7:checked').val();
				if(q7 == null){ q7 = 0; }
				q7 = (q7*1);
				
			var q8 = ($('#q8').val()*1);
				
			
			var score = (q1+q2+q3+q4+q5+q6+q7+q8);

			if(score>=5){
				score='<span style="color: #a52a2a;">'+score+'</span>';
			}
			
			$('#calc_score').html(score);
		});
	});
}

// FAQ Controls #########################################################################

function faqControls(){
	$('.faq').children('div').children('h3').children('a').each(function(){
		$(this).addClass('faq_toggle');
		$(this).click(function(){ 
		  $(this).parent('h3').next('p').slideToggle(200);
		  return false;
		});
		$(this).parent('h3').next('p').hide();
	});
}

// Comments #############################################################################

function loadComments(id){
	$('#comments').load('modules/comments.php?id='+id);
}

function submitComment(id){
	var author = $('#author').val();
	var comment = $('#comment').val();
	
	if (author == '' || comment == '') {
		$('.notification').show();
		$('.notification').fadeOut(8000);
	}
	else {
		$('.notification').hide();
		$.post('modules/comment_save.php?id='+id, $('#comment_form').serializeArray());
		$('#comments ul').append('<li><span class="cmt_author">' + author + ' just posted:</span><p>' + comment + '</p></li>');
		// Clear fields
		$('#author').val('').focus();
		$('#comment').val('');
	}
}

function deleteComment(id){
	$('#cmt'+id).fadeOut(200);
	$.get('modules/comment_delete.php?id='+id);
}

// Email form ###########################################################################

function sendNewsletterSignup(){
	$.post('modules/newsletter_popup.php?submit=t', $('#subscribe_form').serializeArray(), function(data){
      $('#modal_content').html(data);$('.notification').show().fadeOut(8000);
	});
}

function sendEmail(){
	$.post('modules/emailform.php?submit=t', $('#email_form').serializeArray(), function(data){
      $('#emailform').html(data);$('.notification').fadeOut(8000);
	});
}

function sendSignUp(){
	$.post('modules/signupform.php?submit=t', $('#email_form').serializeArray(), function(data){
      $('#emailform').html(data);$('.notification').fadeOut(8000);
	});
}

function sendAskAHI(){
	$.post('modules/askahi.php?submit=t', $('#askahi_form').serializeArray(), function(data){
      $('#modal_content').html(data);$('.notification').fadeOut(8000);
	});
}

function sendWebinarEnroll(){
	$.post('modules/webinar_enroll.php?submit=t', $('#webinarenroll_form').serializeArray(), function(data){
      $('#modal_content').html(data);$('.notification').fadeOut(8000);
	});
}

