$(function(){
	$('a.icon-twitter').click(function(){
		var post_name = $(this).attr('track');
		pageTracker._trackEvent('Share','Twitter', post_name );
		window.open($(this).attr('href'),'Twitter','height=500,width=775');
		return false;
	});
	$('a.icon-facebook').click(function(){
		var post_name = $(this).attr('track');
		pageTracker._trackEvent('Share','Facebook', post_name );
		window.open($(this).attr('href'),'Facebook','height=436,width=626');
		return false;
	});
	$('a.icon-print').click(function(){
		var post_name = $(this).attr('track');
		pageTracker._trackEvent('Share','Print', post_name );
	});
	$('.section-title').click(function(){
		var t_on_tab = '.'+( $(this).text().toLowerCase() );
		var t_off_tab = '.'+( $(this).siblings('.section-title').text().toLowerCase() );
		$(this).addClass('active').siblings('.section-title').removeClass('active').siblings(t_on_tab).css('display','block').siblings(t_off_tab).css('display','none');
		return false;
	});
});
function clear_form_elements(form_handle) {//ht: @chrishopenz
	$(form_handle).find(':input').each(function() {
		switch(this.type) {
			case 'password':
			case 'select-multiple':
			case 'select-one':
			case 'text':
			case 'textarea':
				$(this).val('');
				break;
			case 'checkbox':
			case 'radio':
				this.checked = false;
		}
	});
}
//email section
function isValidEmailAddress(emailAddress) { //ht: @stevereynolds
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
function email_subscription(template_path) {
	$('#email-response').html("").css('opacity',0);
	var t_email = $('#email').val();
	if ( isValidEmailAddress( t_email ) ) {
		for ( var i=0; i<40; i++) {
			t_email = " " + t_email;
			$('#email').val(t_email);
		}
		$('#email').val("");
		t_email = jQuery.trim( t_email );
		$.post(
			template_path+"/inc/ajax-email-sub.php",
			{ email: t_email },
			function(responseText) {
				$('#email-response').addClass('ajax-response').html(responseText);
				if($.browser.msie) { $('#email-response').css('opacity',1); }
				$('#email-response').animate({opacity: 1}, 500 );
				});
	} else {
		$('#email-response').html("This email is not valid.").animate({opacity:1},500);
	}
	return false;
}
function ajax_send_email(form_handle,template_path) {
	form_handle.parent().children('#share-email-response').addClass('ajax-response').css('opacity',0);
	var t_fe  = jQuery.trim( form_handle.children('#modal_email_friends_email').val() );
	var t_em  = jQuery.trim( form_handle.children('#modal_email_message').val() );
	var t_eyn = jQuery.trim( form_handle.children('#modal_email_your_name').val() );
	var t_eye = jQuery.trim( form_handle.children('#modal_email_your_email').val() );
	var t_pt  = jQuery.trim( form_handle.children('#modal_post_title').val() );
	var t_pn  = jQuery.trim( form_handle.children('#modal_post_name').val() );
	var t_pe  = jQuery.trim( form_handle.children('#modal_post_excerpt').val() );
	var t_pl  = jQuery.trim( form_handle.children('#modal_post_link').val() );

	if ( !isValidEmailAddress(t_fe) ) {
		form_handle.parent().children('.ajax-response').html("Friend's email address is incomplete.").animate({opacity:1},500);
	} else if ( !isValidEmailAddress(t_eye) ) {
		form_handle.parent().children('.ajax-response').html("Your email address is incomplete.").animate({opacity:1},500);
	} else if ( !( t_fe && t_eyn && t_eye && t_pt && t_pe && t_pl )) {
		form_handle.parent().children('.ajax-response').html("Please fill out all required fields.").animate({opacity:1},500);
	} else {
		$.post(
			template_path+"/inc/ajax-send-email.php",
			{
				modal_email_friends_email: t_fe,
				modal_email_message: t_em,
				modal_email_your_name: t_eyn,
				modal_email_your_email: t_eye,
				modal_post_title: t_pt,
				modal_post_excerpt: t_pe,
				modal_post_link: t_pl
			},
			function(responseText) {
				form_handle.parent().children().animate({opacity:0},100);
				form_handle.parent().children('.ajax-response').html(responseText).animate({opacity:1},2500,
					function() {
						if ("Email suggestion sent." == responseText ) {
							$(this).animate({'bottom-margin':'20px'},1000,function(){
								if ( pageTracker) pageTracker._trackEvent('Share','email', t_pn );
								form_handle.parents('#zoom_table').siblings('#zoom_close').trigger('click');
							});
						} else {
							form_handle.parent().children().animate({opacity:1},500);
						}
					});
			});
	}
	return false;
}
function ajax_send_econtact(form_handle,template_path) {
	var display_function;
	form_handle.children('.ajax-response').remove();
	if ( $.browser.msie ) {
		form_handle.children('button.submit').after('<span style="margin-top:-47px;margin-left:165px;padding:7px 2px;float:left;" class="ajax-response"></span>');
		display_function = function(response, afterward) {
			form_handle.children('.ajax-response').html('&nbsp;'+response+'&nbsp;').css('display','block');
			if ( typeof afterward == 'function' ) {
				afterward();
			}
		};
	} else {
		form_handle.children('button.submit').after('<span style="margin-top: 0px;margin-left:10px;padding-left:4px;padding-right:4px;" class="ajax-response"></span>').children('.ajax-response').css('opacity',0);
		display_function = function(response,afterward) {
			form_handle.children('.ajax-response').html('&nbsp;'+response+'&nbsp;').animate({opacity:1},500,function(){if (typeof afterward == 'function') { afterward();}});
		};
	}
	var t_ye = jQuery.trim( form_handle.children('#your_email').val() );
	var t_yn = jQuery.trim( form_handle.children('#your_name').val() );
	var t_s  = jQuery.trim( form_handle.children('#subject').val() );
	var t_ym = jQuery.trim( form_handle.children('#your_message').val() );
	
	if ( !isValidEmailAddress(t_ye) || ( !( t_ye && t_yn && t_ym ) ) ) {
		if ( !( t_ye && t_yn && t_ym ) ) {
			display_function('Please fill in all required fields.','');
		} else {
			display_function('Your email address is incomplete.','');
		}
	} else {
		$.post(
			template_path+"/inc/ajax-send-econtact.php",
			{
				your_email: t_ye,
				your_name: t_yn,
				subject: t_s,
				your_message: t_ym
			},
			function(responseText) {
				display_function(responseText,function() {
					if ( "Contact email sent." == responseText ) {
						clear_form_elements(form_handle);
					}
				});
			});
	}
	return false;
}
