jQuery(document).ready(function() {

	// open external links in new window
	jQuery('a[rel=external]').attr('target', '_blank');
	
	// keep parent menu highlighted
	jQuery('.sub-menu').mouseenter(function() {
		jQuery(this).parent().addClass('subhover');
	});
	
	jQuery('.sub-menu').mouseleave(function() {
		jQuery(this).parent().removeClass('subhover');
	});
	
	// detect wpadminbar
	var wpa_skew = 0;
	if(jQuery('#wpadminbar').length) {
		wpa_skew = 29;
		jQuery('#wpadminbar').css({'borderBottom': '1px solid #000'});
		jQuery('#mailchimp-subscribe').animate({'margin-top': -180 + wpa_skew}, 'slow', 'linear');
		//jQuery('#login-button').animate({'margin-top': -6 + wpa_skew}, 'slow', 'linear');
		}
		
	// add 'read more' button to the end of sidebar blog posts
	jQuery('.yd_rp_excerpt a[rel=bookmark]').removeAttr('rel').addClass('button').text('Read More').wrap('<div />');
	
	// add 'read more' button to the end of sidebar news posts
	jQuery('#sidebar .cat-post-item .post-title').each(function() {
		href = jQuery(this).attr('href');
		jQuery(this).parent().children().last().append("<div><a class='button news-button' alt='Read More...' href='" + href + "'>Read More</a></div");
	});
	
	// add 'all events' button to the end of sidebar events
	jQuery('[title="All Events"]').addClass('button').addClass('event-button');
	
	// more info on wellness center page
	var idc = 0;
	jQuery('.wellness #main h3').each(function() {
		aid = 'svc-' + ++idc;
		jQuery(this).wrapInner('<a href="#' + aid + '" id="' + aid + '"> </a>');
	});
	
	jQuery('.wellness #main h3 a').click(function() {
		p = jQuery(this).parent();
		jQuery(this).blur();
		if(jQuery(p).hasClass('unfolded')) {
			jQuery(p).removeClass('unfolded').next().hide('slow');
		} else {
			jQuery(p).addClass('unfolded').next().show('slow', function() {
				var window_bottom = jQuery(window).scrollTop() + jQuery(window).height();
				var content_bottom = Math.floor(jQuery(this).offset().top + jQuery(this).height());
				if(content_bottom > window_bottom) {
					jQuery.scrollTo('+=' + (content_bottom - window_bottom) + 'px', {duration:1000});
				}				
			});
		}
		return false;
	});
	
	// make <br /> tags after images on home and faculty pages go away
	jQuery('.home .alignleft + br').each(function() {
		jQuery(this).replaceWith('<span class="nc"> </span>');
	});
	jQuery('.faculty .alignleft + br').each(function() {
		jQuery(this).replaceWith('<span class="nc"> </span>');
	});
	
	// --- begin mailchimp subscribe form sliding ---
	jQuery('#mailchimp-tab').click(function() {				
		if(jQuery(this).hasClass('show')) {
			jQuery('#mailchimp-wrapper').addClass('shadow');
			jQuery(this).removeClass('show');
			jQuery(this).addClass('moving');
			jQuery('#login-button').animate({'margin-top': -50}, 'fast', 'linear');
			jQuery('#social-icons').animate({'margin-top': -50}, 'fast', 'linear');
			jQuery('#mailchimp-subscribe').animate({'margin-top': 0 + wpa_skew}, 'slow', 'linear', function() {					
				jQuery('#mailchimp-tab').removeClass('moving');
				jQuery('#mailchimp-tab').addClass('hide');
				return false;
			});							
		} else {
			jQuery(this).removeClass('hide');
			jQuery(this).addClass('moving');
			jQuery('#mailchimp-subscribe').animate({'margin-top': -180 + wpa_skew}, 'slow', 'linear', function() {
				jQuery('#mailchimp-wrapper').removeClass('shadow');
				jQuery('#mailchimp-tab').removeClass('moving');
				jQuery('#mailchimp-tab').addClass('show');
				jQuery('#login-button').animate({'margin-top': 0}, 'slow', 'linear');
				jQuery('#social-icons').animate({'margin-top': 0}, 'slow', 'linear');					
				return false;
			});
		}
	});
	
	jQuery('#mailchimp-tab-hide').click(function() {
		jQuery('#mailchimp-wrapper').removeClass('shadow');
		jQuery('#mailchimp-subscribe').animate({'margin-top': -140 + wpa_skew}, 'slow', 'linear', function() {
			return false;
		})
	});
	// --- end mailchimp subscribe form sliding ---
	
	// --- begin mailchimp subscribe form validation and submission ---
	var options = {
		onkeyup: function() {},
		onfocusout: function() {},
		onblur: function() {},
		showErrors: function(map, list) {
			if(list[0]) {
				jQuery('#mce-error-response').hide();
				jQuery('#mce-success-response').hide();
				jQuery('#mce-error-response').html(list[0].message).show();
			}
		}
	};

	var mce_validator = jQuery("#mc-embedded-subscribe-form").validate(options);
	var mc_inptext = 'Your Email Address';

  options = {
		url: 'http://saiayurvediccollege.us2.list-manage.com/subscribe/post-json?u=2b42eb1798fd71ff383fc7484&id=30f21f3d36&c=?',
		type: 'GET',
		dataType: 'json',
		contentType: "application/json; charset=utf-8",

    beforeSubmit: function() {
				if(jQuery('#mce-EMAIL').attr('value') == mc_inptext) {
					jQuery('#mce-EMAIL').attr('value', '');
				}
				jQuery('#mce-error-response').hide();
				jQuery('#mce-success-response').hide();           
        return mce_validator.form();
    },

		success: function(r) {
			jQuery('#mce-error-response').hide();
			jQuery('#mce-success-response').hide();
			if(r.result == 'error') {
				jQuery('#mce-error-response').html(r.msg).show();
			} else {
				jQuery('#mce-success-response').html(r.msg).show();							
			}
		}
  };

	jQuery('#mc-embedded-subscribe-form').ajaxForm(options);

	jQuery('#mce-EMAIL').focus(function() {
		if(jQuery(this).attr('value') == mc_inptext) {
			jQuery(this).attr('value', '');
		}
	});

	jQuery('#mce-EMAIL').blur(function() {
		if(jQuery(this).attr('value') == '') {
			jQuery(this).attr('value', mc_inptext);
		}					
	});

	jQuery('#mce-EMAIL').blur();
	// --- end mailchimp subscribe form validation and submission ---
});
