jQuery(function(){
	var $ = jQuery;
	// Navigation
	$("#header .nav ul li").hoverIntent({ 
		timeout: 500,      
		over: function(){
			if($("ul:first",this).is(':hidden')){
				$("ul:first",this).show();
				if($("#header .nav li.current-menu-item ul, #header .nav li.current-menu-parent ul, #header .nav li.current-menu-ancestor ul").is(':visible')){
					$("#header .nav li.current-menu-item ul, #header .nav li.current-menu-parent ul, #header .nav li.current-menu-ancestor ul").hide();
				};
			};
		},
		out: function(){
			if($("ul:first",this).is(':visible')){
				$("ul:first",this).hide();
				if($("#header .nav li.current-menu-item ul, #header .nav li.current-menu-parent ul, #header .nav li.current-menu-ancestor ul").is(':hidden')){
					$("#header .nav li.current-menu-item ul, #header .nav li.current-menu-parent ul, #header .nav li.current-menu-ancestor ul").show();
				};
			};
		}
	});	
	// Mission Toggle
	$('#mission-toggle a').toggle(function(){
		$('#mission').slideDown();
		$(this).addClass('active');
		$(this).text('Hide Panel');
		$('#wp-admin-bar').animate({
			top: '+=100'
		});
	}, function(){
		$('#mission').slideUp();
		$(this).removeClass('active');
		$(this).text('View Our Mission');
		$('#wp-admin-bar').animate({
			top: '-=100'
		});
	});
	// Slideshow
	$('#slideshow .slides').cycle({
		pager: '.slideshow-page',
		prev: '#slideshow .prev',
		next: '#slideshow .next',
		timeout: 8000
	});
	// Category Navigation
	$('.category-nav a').click(function(){
		$('.category-nav a').removeClass('active');
		$(this).addClass('active');
		$('.post-listing').hide();
		var listing = $(this).parent('li').attr('class');
		$('.posts-'+listing).show();
		return false;
	});
	// Input Default Value
	$("input[type=text]").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	// Get Legislators on Epic Fail Page
	$('#selEpicFailState').change(function(){
		$.ajax({
			type: "GET",
			url: "/advocacy/get-senators",
			dataType: "json",
			data: ({
				state : $(this).val()
			}),
			success: function(data){
				var html = '';
				for (var i = 0; i < data.length; i++) 
				{
					html += '<div class="legislator">';
					html += '    <div class="info">';
					html += '    <h3>' + data[i].title + ' ' + data[i].firstname + ' ' + data[i].lastname + '</h3>';
					html += '    <p>' + data[i].address + '<br />' + data[i].city + ', ' + data[i].state + ' ' + data[i].zip + '</p>';
					html += '    <p class="right">';
					if (data[i].facebook_id != '')
					{
						html += '	<a href="http://facebook.com/' + data[i].facebook_id + '" target="_new">Facebook</a>';
					}
					else
					{
						html += '	No Facebook';
					}
					html += '    <br />';
					if (data[i].twitter_id != '')
					{
						html += '	<a href="http://twitter.com/' + data[i].twitter_id + '" target="_new">Twitter</a>';
					}
					else
					{
						html += '	No Twitter';
					}
					html += '    </p>';
					html += '    <p>';
					if (data[i].phone)
						html += data[i].phone + ' (P)<br />';
					if (data[i].fax)
						html += data[i].fax + ' (F)';
					html += '    </p>';
					html += '    </div>';
					html += '</div><br /><br />';
				}
				$('#EpicFailLegislators').html(html);
			}
		});
	});
});

