$(document).ready(function() {
	prepare_testimonials();
	prepare_hover_menus();
	prepare_links();
	prepare_request_information_form();
	prepare_video_wmode();
});

function prepare_video_wmode() {
	$('embed').attr('wmode', 'opaque'); 
	$('object').each (function() {
		var param= $(this).children('param[name="wmode"]');
		
		if (param.length == 0) {
			elem = $('<param name="wmode" value="opaque" />');
			$(this).append(elem);
		}
		else {
			param.attr("value", "opaque");
		}
	});
}

function prepare_testimonials() {
	var body_id = $('body').attr('id');
	if (body_id != 'page-crop-insurance') {
		return false;
	}
	var testimonials = $('#testimonials .quote');

	testimonials.click(function() {
		if ($(this).attr('class') == 'main') {
			return false;
		}
		var current_main = $('.quote.main');
		var new_main = $(this);

		// switch main class from old testimonial to new testimonial
		current_main.removeClass('main').addClass('secondary')
		new_main.removeClass('secondary').addClass('main');

		// fade out testimonials
		current_main.fadeToggle('fast');
		new_main.fadeToggle('fast');

		// clear second and third classes
		testimonials.removeClass('second third');

		// added second & third classes to position secondary testinials
		var second_testimonial = true;
		$('#testimonials .quote.secondary').each(function() {
			$(this).addClass((second_testimonial) ? 'second' : 'third');
			second_testimonial = false;
		});

		// fade in testimonials
		current_main.fadeToggle('fast');
		new_main.fadeToggle('fast');
	});
}

function prepare_hover_menus() {
	// ensure the page contains a hover menu
	if (!$('ul.hover_menu')) {
		return;
	}

	// hide all links and set the appropriate class
	$('ul.hover_menu ul.sub-menu').hide().addClass('hidden').each(function() {
		// add convenience class
		var container = $(this).parent('li');
		container.addClass('parent-item');

		// insert span to hold triangle
		var first_li = $(this).children(':first-child');
		first_li.prepend('<span></span>');
	});

	// add a mouseover trigger to the elements
	$(".parent-item > a, .hidden").bind("mouseover", function() {
		// get container
		var container = $(this).parent('li');

		if (container.attr('timer')) {
			// clear any existing timeout in case mouseout then mouseover
			clearTimeout(container.attr('timer'));
			container.removeAttr('timer');
		}

		if (!container.attr('on')) {
			// set state attributes
			container.attr('on', true).addClass("active");
			container.children('.hidden').show();
		}
	}).bind("mouseout", function() {
		// get parent container
		var container = $(this).parent('li');

		// set timeout to hide menu with delay
		container.attr('timer', setTimeout(function() {
			hide_hover_menus(container);
		}, 100));
	});
}

function hide_hover_menus(e) {
	// stop the executer
	clearTimeout(e.attr('timer'));

	// remove state attributes
	e.removeAttr('timer').removeAttr('on');

	// hide menu
	$('.hidden', e).hide();
	e.removeClass("active");
}

function prepare_links() {
	// add pdf icon for any pdf links with text and open in a new window
	$('a[href$=".pdf"]').filter(function() {
		t = $(this).text();
		return t && $.trim(t) != "";
	}).addClass("icon_pdf").click(function() {
		var href = $(this).attr('href');
		window.open(href, 'pdfWindow');
		return false;
	});

	// add external icon to any link that does not contain the baseURL
	$('a[href^="http://"], a[href^="https://"]').filter(function() {
		return $(this).attr('href').indexOf('e-farmcredit.') == -1 && $(this).attr('class').indexOf('icon') == -1;
	}).addClass("icon_external").click(function() {
		external_warning(this);
		return false;
	});
}

function external_warning(link) {
	var html_height = $(document).height();
	var html_width = $('html').width();
	var dialog_width = 518;
	
	var dialog = $('<div id="modal_win" style="width:' + dialog_width + 'px;"><div class="logomod"><div class="modal_close"></div></div><p class="alert">You are leaving the Farm Credit Services of Mid-America website. Farm Credit is not responsible for content or security within the external site. The content of this site does not reflect the endorsement of FCS for any products or services that may be offered. To return to the FCS site, close this browser window.</p><p><a href="' + $(link).attr('href') + '" class="btn">Continue</a></p></div>').css({
		zIndex: 99,
		position: 'fixed',
		top: 100,
		left: (html_width/2)-(dialog_width/2)
	});
		$('body').append(dialog);
	
	var overlay = $('<div></div>').css({
		position: "absolute",
		left: 0,
		top: 0,
		height: html_height,
		width: html_width,
		zIndex: '9',
		background: '#000000',
		opacity: '0.5'
	});
	$('body').append(overlay);
	
	// follow link
	$('#modal_win .btn').focus().click(function() {
		window.open($(this).attr('href'), 'externalWindow');
		close_dialog();
		
		return false;
	});
	
	// close overlay
	overlay.click(function() {
		close_dialog();
	});
	$('.modal_close').click(function() {
		close_dialog();
	});
	
	close_dialog = function() {
		overlay.hide().remove();
		dialog.hide().remove();
	}
}

function prepare_request_information_form() {
	var request_information_form = $("#request_information_form");

	if (request_information_form.length == 0) {
		return false;
	}

	// clone the dependent select
	$('#00N60000002BxXo').clone().attr('id', 'county_clone').hide().insertAfter('#00N60000002BxXo');

	// bind the event
	$('#state').change(function() {
		// reset the dependent select
		$('#00N60000002BxXo').val("");
		$('#00N60000002BxXo optgroup').remove();
		
		// copy the optgroup that matches the parent value
		$('#county_clone optgroup[label="' + $(this).val() + ' Counties"]').clone().appendTo('#00N60000002BxXo');
	});

	// set validation rules using jQuery Validate Plugin	
	request_information_form.validate({
		rules: {
			first_name: {
				required: true
			},
			last_name: {
				required: true
			},
			phone: {
				required: true
			},
			street: {
				required: true
			},
			city: {
				required: true
			},
			"state": {
				required: true
			},
			"00N60000002BxXo": {
				required: true
			},
			zip: {
				required: true
			},
			"00N60000002Buyf": {
				required: true
			}
		},
		messages: {
			"state": "Please select a State.",
			"00N60000002BxXo": "Please select a County.",
			"00N60000002Buyf": "Please select a Product Interest."
		}
	});
}

