/* default template js */
$(document).ready(function()
{
	/* init dropdown menu */
	$('#nav').droppy({speed: 1});
	
	/* prefill search button */
	if ($('#searchterm').val()=='') 
	{ 
		input_show_default('#searchterm', 'Search..'); 
	}
		
	/*
	$(".modal").click(function () 
	{ 
		var url = $(this).attr("href");
		$("#temp").load(url).dialog({modal:true, position: ['center','center'], title: 'Back to School Materials', width: 650, height:500}); 
		//alert(url);
		return false;
    });
	*/
	
	/* autoresizing height */
	if(!$("form#take_action_form").length && !$("#special-home").length)
	{

		/* autoresizing height */
		if($(".left_col").length)
		{
			var divHeight = $(".left_col").outerHeight();
			/* also need to allow for 10px margin of top and bottom of main and 8px each for top and bottom cells */
			divHeight = parseInt(divHeight) - 36;
		}	
		if($(".main").length)
		{
			var mainHeight = $(".main").outerHeight();
		}
		if(mainHeight && divHeight)
		{
			if(mainHeight < divHeight)
			{
				$(".main").css("height", divHeight);
			}
		}
	}
	
	
	/* activate tabs */
	if( $(".tabs").length )
	{
		/* activate tabs */
		if( $("#videowrapper").length )
		{
			$(".tabs").removeClass("ui-corner-all").addClass("ui-corner-top");
			var tab = $('#tabopen').val();
			$(".tabs").tabs({ selected: tab });
			var cat = $('#catopen').val();
			if(cat!=0)
			{
				$('span.title').hide();
				$('#cat_0'+cat).show();
				$('#cat_1'+cat).show();
				$('#cat_2'+cat).show();
				$('.fv_thumb').hide();
				$('.cat_'+cat).show();
				$('#c_0'+cat).css('color','#005474').css('font-weight','bold');
				$('#c_1'+cat).css('color','#005474').css('font-weight','bold');
				$('#c_2'+cat).css('color','#005474').css('font-weight','bold');
				$('.mediathumbs .cat_2').show();
			}
			else
			{
				$('#cat_02').hide();
				$('#cat_12').hide();
				$('#cat_21').hide();
				$('.cat_2').hide();
				$('#c_01').css('color','#005474').css('font-weight','bold');
				$('#c_11').css('color','#005474').css('font-weight','bold');
				$('#c_21').css('color','#005474').css('font-weight','bold');
				$('.mediathumbs .cat_2').show();
			}
			$('.mediathumbs .cat_2').show();
		}
		else
		{
			$(".tabs").tabs();
		}

		/* remember selected tab - requires jquery.cookie.js file */
		var cookie = $('.tabs').tabs('option', 'cookie');
		$('.tabs').tabs('option', 'cookie', { expires: 30 });
		
	}
		
	/* activate accordion */
	if( $(".accordion").length ) 
	{ 
		$(function() 
		{
			$(".accordion").accordion({ active: 0, navigation: true, autoHeight: true, collapsible: true }); 
		});
	}
	
	/* home page layout - image cycle */
	if( $("#homepage_logo").length )
	{

		var ver = getInternetExplorerVersion();
		            
		if(jQuery.support.leadingWhitespace==true && jQuery.support.scriptEval==true || ver >= 8.0)
		{
			$('#homepage_logo').cycle({ 
				fx:    'fade', 
				timeout: 6000,
				continuous: 0,
				speed: 2000,
				type: 'sequence',
				startingSlide: 0
			});		
			
			$("img.play1").click( function() { $('#homepage_logo').cycle({fx:'fade',startingSlide:0}); });	
			$("img.play2").click( function() { $('#homepage_logo').cycle({fx:'fade',startingSlide:1}); });	
			$("img.play3").click( function() { $('#homepage_logo').cycle({fx:'fade',startingSlide:2}); });	
			$('.playerbuttons img', this).hover(show_play, hide_play);	
	
			$("img.pausebutton").click(
					function()
					{
						var imgid = $(this).attr("id");
						slide = parseInt(imgid.substring(5));
						$('img.pausebutton').css("display","none");
						$('img.startbutton').css("display","inline");
						$('#homepage_logo').cycle({fx:'pause', startingSlide:slide});
					}
			);
	
			$("img.startbutton").click(
					function()
					{
						var imgid = $(this).attr("id");
						slide = parseInt(imgid.substring(5)) + 1;
						$('img.startbutton').css("display","none");
						$('img.pausebutton').css("display","inline");
						$('#homepage_logo').cycle({fx:'fade', startingSlide:slide});
					}
			);
		}
		else
		{
			$('.playerbuttons img').hide();
		}
		
	}	
	
	/**/
	if( $("#view_newsletter").length )
	{
		$("#view_newsletter").change(function() 
		{
			var newsletter_num = $("#view_newsletter option:selected").val();
			var newsletter = $("#view_newsletter option:selected").text();
			var html = '<br/><a title="Preview: '+newsletter+'" onclick="openWindow(\'/spread-the-truth/'+newsletter_num+'\', \'newsletter\', 800, 850,\'yes\')" href="javascript:void(0);"><u>click to see preview</u></a>';
			$("#preview_newsletter").html("<img src='/_data/newsletters/"+newsletter_num+"/preview.jpg' />"+html);
			//$("#preview_title").html(newsletter);
		}).trigger('change');
	}
	
	

	/**/
	if( $("div#lawmakers").length )
	{
		$("input#zipcode").keyup(function() 
		{
		    if (String(this.value)!=(Number(this.value))) 
		    {
		      // Case of error
		      this.value = "";
		    }
		});
	}
	/**/
	if( $("input#zip5").length )
	{
	
		$("input#zip5").focus(function() 
		{
			//$(this).val('');
		});
	}
	
	/**/
	if( $("input#zip4").length )
	{
	
		$("input#zip4").focus(function() 
		{
			//$(this).val('');
		});
	}
	
	if($('#theTimer').length)
	{
		text = $('#current_time').attr('value');
		$('#theTimer').countdown({
			until: +text,
			onExpiry: changePage,
			layout: '<div class="countdown_section">' +
			'<div style="float:left"><span class="digits">{hnn}</span></div><div style="float:left">:</div>' +
			   '<div style="float:left"><span class="digits">{mnn}</span></div><div style="float:left">:</div>' +
			   '<div style="float:left"><span class="digits">{snn}</span></div></div>'
		}); 
	}

});

function changePage()
{
	$('#theTimer').css( 'display', 'none' );
	$('#timerLink').css( 'display', 'block' );
	$('#timer').css( 'margin-left', '120px' );
	$('#timer').css( 'width', '278px' );
}

function getpeople()
{

	window.location = "/special/contact-your-legislator/zip5/"+$("input#zip5").val()+"/zip4/"+$("input#zip4").val()+"/form_action/1";
	
//	$.ajax({
//		   type: "GET",
//		   url: "/special/searchlawmakers/zip5/"+$("input#zip5").val(),
//		   beforeSend: function(){
//
//			},
//		   success: function(msg){
//				 $("div#lawmakers").html(msg);
//				 $("div#thisee").show();
//				 $("div#send_message").show();
//				 $("div#space").hide();
//		}
//
//	});
}


/* player for home page layout - image cycle */
function show_play()
{
	var imgid = $(this).attr("id");
	var imgsrc = $(this).attr("src");
	if(imgid)
	{
		var isactive = imgid.match(/on/);
		if(isactive)return;
	}
	imgsrcON = imgsrc.replace(/off.gif$/ig, "on.gif"); // strip off extension
	$(this).attr("src", imgsrcON);
	
}
/* player for home page layout - image cycle */
function hide_play()
{
	var imgid = $(this).attr("id");
	var imgsrc = $(this).attr("src");
	if(imgid)
	{
		var isactive = imgid.match(/on/);
		if(isactive)return;
	}
	imgsrcON = imgsrc.replace(/on.gif$/ig, "off.gif"); // strip off extension
	$(this).attr("src", imgsrcON);
}

function category(id,type)
{
	$('.fv_thumb:not(.cat_'+id+')').hide();
	$('.cat_'+id).show();

	if(type==0)
	{
		$('.vidthumbs span.title').hide();
		$('#cat_0'+id).show();
		$('#c_0'+id).css('color','#005474').css('font-weight','bold');
		$('.category_list a:not(#c_0'+id+')').css('color','#429dc0').css('font-weight','normal');
	}
	else if(type==1)
	{
		$('.gamethumbs span.title').hide();
		$('#cat_1'+id).show();
		$('#c_1'+id).css('color','#005474').css('font-weight','bold');
		$('.category_list a:not(#c_1'+id+')').css('color','#429dc0').css('font-weight','normal');
	}
	else
	{
		$('.mediathumbs span.title').hide();
		$('#cat_2'+id).show();
		$('#c_2'+id).css('color','#005474').css('font-weight','bold');
		$('.category_list a:not(#c_2'+id+')').css('color','#429dc0').css('font-weight','normal');
	}
	$('.mediathumbs .cat_2').show();
	

}
function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
function openWindow(myFile,myName,myWidth, myHeight, r){
	var atts = 'menubar=0,scrollbars='+r+',width='+myWidth+',height='+myHeight;
	window.open(myFile,myName,atts);
	//swfWin.focus();
}
function setplus4(plus4)
{
	$('#zip4').val(plus4);
	
}