jQuery(document).ready(function() {

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ Inits ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	initTabSet();
	initMore();
	initMPH();
	initExpander();
	initNewsletter();
	//initNav();

});

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ Functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ MPH ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function initMPH()
{

	var width = jQuery('.widget_slider').width();
	var height = jQuery('.widget_slider').height();

	var maxHeight = 0;


	//selestiConsole(height);
	//selestiConsole(maxHeight);
	//SAS - multiply by 1000 for milliseconds, multiply by 2 because jquery.anythingslider.js line ~702 has the delay/2 ...
	var transition_time = jQuery('.widget_mph_transition_time').val()*2000;

	if(jQuery('.widget_slider').length>0){
		jQuery('.widget_slider').show();
		jQuery('.widget_slider li').each(function(index,element){
			if(jQuery(element).height() > maxHeight){
				maxHeight = jQuery(element).height();
			}
		});
		jQuery('.widget_slider').anythingSlider({
			"width": width,
			"height": maxHeight,
			toggleControls: true,
			startStopped: false,
			buildNavigation: false,
			startText: '',
			stopText: '',
			delay: transition_time,
			autoPlay: true,
			autoPlayLocked: true,
			pauseOnHover: true
		});
	}


}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ Content Expander ~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function initExpander(){
	jQuery('.widget.list.content_expander .expander_li a.list_item_heading').click(function(e){

		if(!jQuery(this).parent('li.expander_li').hasClass('selected')){
			e.preventDefault();

			jQuery(this).parents('ul.content_expander_ul').find('li.expander_li .list_item_body').slideUp();
			jQuery(this).parents('ul.content_expander_ul').find('li.expander_li').removeClass('selected');

			jQuery(this).parent('li.expander_li').children('.list_item_body').slideToggle();
			jQuery(this).parent('li.expander_li').toggleClass('selected');
		}
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ More Articles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function initMore(){
	jQuery('.widget.related_articles .more').click(function(){
		var more = jQuery(this);
		if(!more.data('original_text')){
			more.data('original_text',more.text());
		}

		more.data('less_text','Less');
		jQuery(this).parent().find('.extra').slideToggle('slow',function(){

			more.toggleClass('open');
			if(more.hasClass('open')){
				more.text(more.data('less_text'));
			}else{
				more.text(more.data('original_text'));
			}
		});

	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ Tab Sets ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function initTabSet()
{
	jQuery('.content_block_tabs').tabs();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ NEWSLETTER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function initNewsletter()
{
	jQuery('.newsletter_widget .newsletter_email').focus(function(){
		jQuery(this).data('placeholder',jQuery(this).val());
		jQuery(this).val('');
	});

	jQuery('.newsletter_widget .newsletter_email').focusout(function(){
		if(jQuery(this).val() == ''){
			jQuery(this).val(jQuery(this).data('placeholder'));
		}
	});

	jQuery('.newsletter_submit').live('click', function(e) {
		e.preventDefault();
		that = jQuery(this);
		that.hide();
		that.parent('form').append('<img src="/images/loader.gif" class="loading" alt="Loading" title="Loading" />');

		jQuery.ajax({
			'url':'/newsletter/sign_up',
			'data':'newsletter_email='+that.prev('.newsletter_email').val(),
			'dataType':'json',
			'success': function(data) {
				that.prev('.newsletter_email').removeClass('error');
				if (data && data.error) {
					that.parents('.formContainer').children('.errorDiv').html(data.error);
					that.prev('.newsletter_email').addClass('error');
					that.parent('form').children('.loading').hide();
					that.show();
				}
				if (data && data.success) {
					that.parent('form').parents('.formContainer').html(data.success);
				}
			}
		});
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ Section Navigation Widget ~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function initNav(){
	$(".sections_nav .slide_control").unbind('click');
	$(".sections_nav .slide_control").click(function(){
		var container = $(this).parent();
		container.children('ul').slideToggle('slow',function(){
			container.toggleClass('section_open');
			container.toggleClass('section_closed');
		});

	});
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~ Blank so you can cp ~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
