$(document).ready(function(){


	// preload billboard images
	
	if($('img.ssimg').length > 0)
	{
		imgs = new Array();
		$('img.ssimg').each(function(i){
			imgs[i] = $(this).attr('src');
		});
	}

	/** Nav Stuff **/

	if($('#sublinks').length > 0) {

		$('#sublinks').children('ul').each(function(){
			$(this).css('display','none');
		});

		$('#mymenu').children('li').each(function(){
			$(this).children('a').mouseenter(function(){
				hideOthers();
				var layer = $(this).parent('li').attr('id') + '_m';
				$('#'+layer).css('display','block');
			});
		});

	}

	function hideOthers()
	{
		$('#sublinks').children('ul').each(function(){
			$(this).css('display','none');
		});
	}

	$('#sublinks').slideUp();

	$('#home_m').css('display','block');

	$('#nav').mouseenter(function(){
		$('#sublinks').slideDown();
	});

	$('#nav_hover_wrapper').mouseleave(function(){
		$('#sublinks').slideToggle();
//		resetTab();
	});
	
	$('ul#nav').children('li').children('a.parent').each(function(e,idx)
	{
		$(this).mouseenter(function(){
			var c = $(this).attr('class').split(' ');
			setnavtab(c[0]);
		});

	});
	
	$('#nav-outer').mouseleave(function(){
		resetTab();
	});

	function resetTab()
	{
		var loc = window.location.toString().split('/');

		if(loc[3]=='' || loc[3] == 'index')
		{
			setnavtab('home');
		} else {
			setnavtab(loc[3]);
		}
	}

	function setnavtab(page)
	{
		switch(page)
		{

			case "home":
				$('#nav').css(
					'background-position','0px -30px'
				);
			break;

			case "about":
				$('#nav').css(
					'background-position','0px -60px'
				);
			break;

			case "facts":
				$('#nav').css(
					'background-position','0px -90px'
				);
			break;

			case "legislation":
				$('#nav').css(
					'background-position','0px -120px'
				);
			break;

			case "stategunlaws":
				$('#nav').css(
					'background-position','0px -120px'
				);
			break;

			case "action":
				$('#nav').css(
					'background-position','0px -150px'
				);
			break;

			case "media":
				$('#nav').css(
					'background-position','0px -180px'
				);
			break;

			case "blog":
				$('#nav').css(
					'background-position','0px -210px'
				);
			break;

			case "donate":
				$('#nav').css(
					'background-position','0px -240px'
				);
			break;

		}
	}

	resetTab();

	/** End Nav **/

	/** Ticker **/

	$('.ticker-shell').vTicker({
		speed: 500,
		pause: 5000,
		animation: 'fade',
		mousePause: false,
		height: 0,
		direction: 'up'
	});

	/** Ticker **/

	/** Accordions **/
	
	function getParameterByName( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return "";
	  else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

	if($('img.toggleWidget').length > 0)
	{
		var teasers = $('.teaser');
		var sst = $('.homeSubSpotTitle');
		var iw = $('img.toggleWidget');

		$('.teaser').each(function(e,idx){
			$(this).slideUp();
		});

		$(iw).each(function(i){
			showoff($(this),i);
		});

		$(sst).each(function(i){
			showoff($(this),i);
		});
	}

	function showoff(el,i)
	{
		$(el).click(function(){
			if($(iw[i]).attr('src').indexOf('down')==-1)
			{
				$(iw[i]).attr('src','/images/template/circle_arrow_down.gif');
				$(teasers[i]).slideDown();
			} else {
				$(iw[i]).attr('src','/images/template/circle_arrow.gif');
				$(teasers[i]).slideUp();
			}
		});
	}

   /** Snippet Adjustment **/

	if($('#snippets').length > 0)
	{
		$('.snippet_item').each(function(){
			if(parseInt($(this).height())<40)
			{
				$(this).height('40');
			}
		});
	}

	/** Carousel **/
	
	if($('ul#mycarousel li').length > 4)
	{
		$('#mycarousel').jcarousel({
			scroll: 1,
			auto: 3,
			wrap: 'circular',
			buttonNextHTML: '<img src="/images/template/carousel_next.gif" alt="Next" width="11" height="129" hspace="0" vspace="20" border="0" align="right" />',
			buttonPrevHTML: '<img src="/images/template/carousel_previous.gif" alt="Previous" width="11" height="129" hspace="0" vspace="20" border="0" align="left" />'
		});
	} else {
		if($.browser.msie)
		{
			$('div.carousel').css({'width':'740px','margin-left': '45px'});
		} else {
			$('div.carousel').css({'width':'740px','margin-left': '6px'});
		}
	}

	/** Print pages **/

	function limitText(limitField, limitNum) {
		if (limitField.value.length > limitNum) {
			limitField.value = limitField.value.substring(0, limitNum);
		}
	}

	function printSelectionDynamic(node,title,spotlight,id){
		 // var spot_id = '_'+id;
		  var page_title = title;
		  var content=node.innerHTML
		  var spotlight_heading = spotlight;
		  var pwin=window.open('','print_content','width=800,height=600');
		  var TheForm;

		  TheForm = document.contact_info_form+id;

		  pwin.document.open();
		  pwin.document.write('<html><head><style type="text/css">body{font-size:11px;} #contact_info_form_'+id+'{display:none;} p{padding-top:5px; font-size:11px;} .boldtext{font-weight:bold;} #print_link{display:none;} #print_options{display:none;} #spacer{display:none;} @media print {#print_button{display:none;}</style></head><body  style="font-family:arial; padding-left:50px; padding-right:50px; padding-top:25px;padding-bottom:50px;">');
		  pwin.document.write('<center> <div id="print_button"><a href="#" onclick="window.print();window.document.close(); setTimeout(function(){window.close();},1000);" style="font-size:24px;">CLICK HERE TO PRINT</a></div></center><table width=100%><tr><td><img src="/images/template/bcam-logo-240.gif" width="240"></td><td style="padding-left:30px;"><span style="font-size:20px;">'+page_title.toUpperCase()+'<br>'+spotlight_heading.toUpperCase()+'</span></td></tr></table>');
		  pwin.document.write(content);

		  if($chk($('name_'+id))){
			  if(document.getElementById('name_'+id).value != null ){
				  pwin.document.write('<br><strong>Contact information:</strong>');
					pwin.document.write('<br>'+document.getElementById('name_'+id).value);

			  if($chk($('address_'+id))){
					if(document.getElementById('address_'+id).value != null ){
					pwin.document.write('<br>'+document.getElementById('address_'+id).value);
					}}
			 if($chk($('phone_'+id))){
					if(document.getElementById('phone_'+id).value != null ){
					pwin.document.write('<br>'+document.getElementById('phone_'+id).value);
				}
			 }
			  if($chk($('city_'+id))){
					if(document.getElementById('city_'+id).value != null ){
					pwin.document.write('<br>'+document.getElementById('city_'+id).value);
			  }
			  }
			  if($chk($('state_'+id))){
					if(document.getElementById('state_'+id).value != null ){
					pwin.document.write(', '+document.getElementById('state_'+id).value);
			  }
			  }
			  if($chk($('zip_'+id))){
					if(document.getElementById('zip_'+id).value != null ){
					pwin.document.write(' '+document.getElementById('zip_'+id).value);
			  }
			  }
			  if($chk($('notes_'+id))){
					if(document.getElementById('notes_'+id).value != null ){
					pwin.document.write('<br>NOTES: '+document.getElementById('notes_'+id).value);
					}}
		  }}
		  pwin.document.write('<center><span style="font-family:arial; font-size:10px;">1225 Eye Street, NW, Suite 1100, Washington, DC  20005 - (202) 898-0792  - FAX (202) 371-9615<br>www.bradycampaign.org</span></center>');
		  pwin.document.write('</body></html>');
	   //   pwin.document.close();

		 // setTimeout(function(){pwin.close();},1000);

		}
	
	function contactInfoStatic(node){
					// var height = node.parentNode.style.height;
					var height = node.parentNode.offsetHeight;
					var newHeight = height + 285;
					node.parentNode.style.height = newHeight +'px';

					var name = 'contact_info_form';

					document.getElementById('contact_info_form').style.display = 'block';

					document.getElementById('name').style.display = 'inline';
					document.getElementById('name_lbl').style.display = 'inline';

					document.getElementById('phone').style.display = 'inline';
					document.getElementById('phone_lbl').style.display = 'inline';

					document.getElementById('address').style.display = 'block';
					document.getElementById('address_lbl').style.display = 'block';

					document.getElementById('city').style.display = 'block';
					document.getElementById('city_lbl').style.display = 'inline';

					document.getElementById('state').style.visibility = 'visible';
					document.getElementById('state_lbl').style.display = 'inline';

					document.getElementById('zip').style.display = 'inline';
					document.getElementById('zip_lbl').style.display = 'inline';

					document.getElementById('notes').style.display = 'inline';
					document.getElementById('notes_lbl').style.display = 'inline';
					document.getElementById('print_link_static').style.display = 'inline';

				  //  TheForm.firstname.focus();
					return false;



	}
	function contactInfo(node,id){
		// var height = node.parentNode.style.height;

		var height = node.parentNode.offsetHeight;
		var newHeight = height + 285;
		node.parentNode.style.height = newHeight +'px';

		var name = 'contact_info_form_'+id;

		document.getElementById('contact_info_form_'+id).style.display = 'block';

		document.getElementById('name_'+id).style.display = 'inline';
		document.getElementById('name_lbl_'+id).style.display = 'inline';

		document.getElementById('phone_'+id).style.display = 'inline';
		document.getElementById('phone_lbl_'+id).style.display = 'inline';

		document.getElementById('address_'+id).style.display = 'block';
		document.getElementById('address_lbl_'+id).style.display = 'block';

		document.getElementById('city_'+id).style.display = 'block';
		document.getElementById('city_lbl_'+id).style.display = 'inline';

		document.getElementById('state_'+id).style.visibility = 'visible';
		document.getElementById('state_lbl_'+id).style.display = 'inline';

		document.getElementById('zip_'+id).style.display = 'inline';
		document.getElementById('zip_lbl_'+id).style.display = 'inline';

		document.getElementById('notes_'+id).style.display = 'inline';
		document.getElementById('notes_lbl_'+id).style.display = 'inline';

		document.getElementById('print_link_'+id).style.display = 'inline';


	  //  TheForm.firstname.focus();
		return false;
	}

	/** prettyPhoto **/

	$("a[rel^='prettyPhoto']").prettyPhoto();

	/** prettyPhoto **/

	if($('#MMMselectstateDD').length > 0)
	{
		$('#MMMselectstateDD').change(function(){
			if($('#MMMselectstateDD option:selected').val()!="null")
			{
				window.open('http://www.bradycampaign.org/chapters/' + $('#MMMselectstateDD').val(), "_self");
			} else {
				return false;
			}
		});
	}

    if($('#selectstateDD').length > 0)
	{
		$('#selectstateDD').change(function(){
			if($('#selectstateDD option:selected').val()!="null")
			{
				window.open('http://www.bradycampaign.org/stategunlaws/scorecard/' + $('#selectstateDD').val(), "_self");
			} else {
				return false;
			}
		});
	}
	
	if($('#selectstateDDmmx').length > 0)
	{
		$('#selectstateDD').change(function(){
			if($('#selectstateDD option:selected').val()!="null")
			{
				window.open('http://www.bradycampaign.org/stategunlaws/scorecardmmx/' + $('#selectstateDD').val(), "_self");
			} else {
				return false;
			}
		});
	}

	if($('#selectstateDD_state').length > 0)
	{

		$('#selectstateDD_state').change(function(){
			if($('#selectstateDD_state option:selected').val()!="null")
			{
				window.open('http://www.bradycampaign.org/stategunlaws/scorecard/' + $('#selectstateDD_state').val(), "_self");
			} else {
				return false;
			}
		});
	}

	if($('#site_title').length > 0)
	{
		$('site_title').change(function(){
			window.open('/', "_self");
		});
	}

	if($('#GScounter').length > 0)
	{
		$('#GScounter').click(function(){
			
			window.open('http://www.bradycampaign.org/facts?s=1', '_self');
		});
	}
	
	$('#nav li').hover(
		function () {
			$('ul', this).slideDown(100);

		}, 
		function () {
			$('ul', this).slideUp(100);			
		}
	);
		
	var s = getParameterByName('s');
	if(s > 0)
	{
		setTimeout(function(){$(iw[s-1]).attr('src','/images/template/circle_arrow_down.gif'); $(teasers[s-1]).slideDown();$('html, body').animate({scrollTop: $("#openspot-"+(s)).offset().top},2000);},1000);
	}

});

function printSelectionDynamic(node,title,spotlight){

		 // var spot_id = '_'+id;
		 id= '1';
		  var page_title = title;
		  var content=node.innerHTML
		  var spotlight_heading = spotlight;
		  var pwin=window.open('','print_content','width=800,height=600');
		  var TheForm;

		  TheForm = document.contact_info_form+id;

		  pwin.document.open();
		  pwin.document.write('<html><head><style type="text/css">body{font-size:11px;} #contact_info_form_'+id+'{display:none;} p{padding-top:5px; font-size:11px;} .boldtext{font-weight:bold;} #print_link{display:none;} #print_options{display:none;} #spacer{display:none;} @media print {#print_button{display:none;}</style></head><body  style="font-family:arial; padding-left:50px; padding-right:50px; padding-top:25px;padding-bottom:50px;">');
		  pwin.document.write('<center> <div id="print_button"><a href="#" onclick="window.print();window.document.close(); setTimeout(function(){window.close();},1000);" style="font-size:24px;">CLICK HERE TO PRINT</a></div></center><table width=100%><tr><td><img src="/images/template/bcam-logo-240.gif" width="240"></td><td style="padding-left:30px;"><span style="font-size:20px;">'+page_title.toUpperCase()+'<br>'+spotlight_heading.toUpperCase()+'</span></td></tr></table>');
		  pwin.document.write(content);

		  if($('#name_'+id).length > 0){
			  if(document.getElementById('name_'+id).value != null ){
				  pwin.document.write('<br><strong>Contact information:</strong>');
					pwin.document.write('<br>'+document.getElementById('name_'+id).value);

			  if($('address_'+id).length > 0){
					if(document.getElementById('address_'+id).value != null ){
					pwin.document.write('<br>'+document.getElementById('address_'+id).value);
					}}
			 if($('phone_'+id).length > 0){
					if(document.getElementById('phone_'+id).value != null ){
					pwin.document.write('<br>'+document.getElementById('phone_'+id).value);
				}
			 }
			  if($('city_'+id).length > 0){
					if(document.getElementById('city_'+id).value != null ){
					pwin.document.write('<br>'+document.getElementById('city_'+id).value);
			  }
			  }
			  if($('state_'+id).length > 0){
					if(document.getElementById('state_'+id).value != null ){
					pwin.document.write(', '+document.getElementById('state_'+id).value);
			  }
			  }
			  if($('zip_'+id).length > 0){
					if(document.getElementById('zip_'+id).value != null ){
					pwin.document.write(' '+document.getElementById('zip_'+id).value);
			  }
			  }
			  if($('notes_'+id).length > 0){
					if(document.getElementById('notes_'+id).value != null ){
					pwin.document.write('<br>NOTES: '+document.getElementById('notes_'+id).value);
					}}
		  }}
		  pwin.document.write('<center><span style="font-family:arial; font-size:10px;">1225 Eye Street, NW, Suite 1100, Washington, DC  20005 - (202) 898-0792  - FAX (202) 371-9615<br>www.bradycampaign.org</span></center>');
		  pwin.document.write('</body></html>');
	    pwin.document.close();

		 // setTimeout(function(){pwin.close();},1000);

		}

		function printSelection(node,title,spotlight){
		 // var spot_id = '_'+id;
		  var page_title = title;
		  var content=node.innerHTML
		  var spotlight_heading = spotlight;
		  var pwin=window.open('','print_content','width=800,height=600');
		  var TheForm;

		  TheForm = document.contact_info_form;
		  pwin.document.open();
		  pwin.document.write('<html><head><style type="text/css">body{font-size:11px;} #print_options{display:none;} #contact_info_form{display:none;} #print_link_static{display:none;} #print_link{display:none;} #print_options{display:none;} #spacer{display:none;} @media print {#print_button{display:none;}} </style></head><body  style="font-family:arial; padding-left:50px; padding-right:50px; padding-top:25px;padding-bottom:50px;">');
		  pwin.document.write('<center> <div id="print_button"><a href="#" onclick="window.print();window.document.close(); setTimeout(function(){window.close();},1000);" style="font-size:24px;">CLICK HERE TO PRINT</a></div></center><table width=100%><tr><td><img src="/images/template/brady_logo_large.jpg" width="250"></td><td style="padding-left:30px;"><span style="font-size:20px;">'+page_title.toUpperCase()+'<br>'+spotlight_heading.toUpperCase()+'</span></td></tr></table>');
		  pwin.document.write(content);
		  if(TheForm.name.value != '' && TheForm.name.value != null ){
			  pwin.document.write('<br><strong>Contact information:</strong>');
				pwin.document.write('<br>'+TheForm.name.value);

		  if(TheForm.address.value != '' && TheForm.address.value != null ){
				pwin.document.write('<br>'+TheForm.address.value );
		  }
		  if(TheForm.phone.value != '' && TheForm.city.value != null ){
				pwin.document.write('<br>'+TheForm.phone.value);
		  }
		  if(TheForm.city.value != '' && TheForm.city.value != null ){
				pwin.document.write('<br>'+TheForm.city.value);
		  }
		  if(TheForm.state.value != '' && TheForm.state.value != null ){
				pwin.document.write(', '+TheForm.state.value);
		  }
		  if(TheForm.zip.value != '' && TheForm.zip.value != null ){
				pwin.document.write(' '+TheForm.zip.value);
		  }
		  if(TheForm.notes.value != '' && TheForm.notes.value != null ){
				pwin.document.write('<br>NOTES: '+TheForm.notes.value);
		  }
		  }

		  pwin.document.write('<br><br><br><center><span style="font-family:arial; font-size:10px;">1225 Eye Street, NW, Suite 1100, Washington, DC  20005 - (202) 898-0792  - FAX (202) 371-9615<br>www.bradycampaign.org</span></center>');
		  pwin.document.write('</body></html>');

	      pwin.document.close();

		 // setTimeout(function(){pwin.close();},1000);

		}
