 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 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);

    }
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;



}
SqueezeBox.parsers.swf = function(preset) {
	return (preset || this.url.test(/\.swf/)) ? this.url : false;
};

SqueezeBox.handlers.swf = function(url) {
	var size = this.options.size;
	return new Swiff(url, {
		id: 'sbox-swf',
		width: size.x,
		height: size.y
	});
};

window.addEvent('domready',function(){
     if($chk($('contact_info'))){
         $('contact_info').addEvent('click', function(event) {

                    return false;
                });
     }
	var loc = window.location.toString().split('/');

	if(loc[3]=="")
	{
		setnavtab("home");
	} else {
		setnavtab(loc[3]);
	}

	var navs	= $$('a.topnav');

	navs.each(function(e,idx)
	{

		e.addEvent('mouseenter', function(f){

			var active	= e.get('class').split(' ');
			console.log(active[0]);

			setnavtab(active[0]);

		});

	});

	function setnavtab(page)
	{
		switch(page)
		{

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

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

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

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

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

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

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

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

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

		}
	}

	if($chk($('moostick')))
	{
		var moostick = new Moostick($('moostick'));
		moostick.interval = 12000;
		moostick.startTick();
	}

	var Toggler	= $$('img.toggleWidget');
	var Sst		= $$('.homeSubSpotTitle');
	var Snippets = $$('.teaser');
	var RCTeaser = $$('.redcross_teaser');

    var scroll = new Fx.Scroll(window, {
	wait: false,
	duration: 1500,
	offset: {'x': -200, 'y': -50},
	transition: Fx.Transitions.Quad.easeInOut
    });

	if(Toggler.length>0)
	{
		Toggler.each(function(e, idx)
		{
			var snindex	= Snippets[idx];
			var snippet = new Fx.Slide(snindex);

			var viewerheight = snindex.getStyle('height').toInt();
			snindex.setStyle('height', viewerheight + 46);

            if($chk($('spotlight'))){
                if (getparam('s') != idx+1){
                    snippet.hide();
                }else if(getparam('s') == idx+1){
                    Toggler[idx].set('src','/images/template/circle_arrow_down.gif');
                    scroll.toElement(snindex);
                }
            }
			tagopener(e,idx,snippet,snindex);
			tagopener(Sst[idx],idx,snippet,snindex);

		});
	}
   


    function getparam( 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 results[1];
    }
	function tagopener(e, idx, snippet, snindex)
	{
		$(e).addEvent('click',function(f){
			f	= new Event(f).stop();
			snippet.toggle();
			if(!Toggler[idx].get('src').contains('down'))
			{
				Toggler[idx].set('src','/images/template/circle_arrow_down.gif');
				RCTeaser.setStyle('display','none');
			} else {
				Toggler[idx].set('src','/images/template/circle_arrow.gif');
				RCTeaser.setStyle('display','block');
			}
		});
	}

	var account	= $$('div.atStart');

	SqueezeBox.assign($$('a.videolink'), {
		parse: 'rel'
	});

	if($chk($('carousel-wrapper'))){

		$('carouselNavPrev').setStyles({'opacity': '0'});

		var caritems	= $$('img.carouselItem');
		var carlen		= caritems.length;
		var cardir		= 'ltr';

		$('carousel-inner').setStyle('width',(carlen * 170));

		if(carlen==4)
		{
			$('carouselNavNext').setStyles({'opacity': '0'});
		} else {

			var currcaro	= 0;
			var carlimit	= carlen - 3;

			var scroll = new Fx.Scroll('carousel-wrapper', {
				wait: false,
				duration: 200,
				offset: {'x': 0, 'y': 0},
				transition: Fx.Transitions.Quad.easeInOut
			});

			var periodical;

			var fx = function() {
				if(currcaro + 1 < carlimit && cardir=='ltr')
				{
					currcaro++
					scroll.toElement($('CarouselItemImage-' + currcaro));
					if($('carouselNavPrev').getStyle('opacity')<1)
					{
						$('carouselNavPrev').setStyles({'opacity': 1});
					}
					if(currcaro > -1)
					{
						if(currcaro - 1 < -1)
						{
							$('carouselNavPrev').setStyles({'opacity': '0'});
						}
					}
					if(currcaro + 1 == carlimit)
					{
						cardir	= 'rtl';
						$('carouselNavNext').setStyles({'opacity': '0'});
					}
				} else {
					currcaro--;
					scroll.toElement($('CarouselItemImage-' + currcaro));
					if(currcaro + 1 < carlimit && $('carouselNavNext').getStyle('opacity')<1)
					{
						$('carouselNavNext').setStyles({'opacity': 1});
					}
					if(currcaro - 1 < 0)
					{
						$('carouselNavPrev').setStyles({'opacity': '0'});
						cardir	= 'ltr';
					}
				}
			}

			fx();
			periodical = fx.periodical(10000);


			$('carouselNavNext').addEvent('click', function(event) {
				$clear(periodical);
				event = new Event(event).stop();
				if(currcaro + 1 < carlimit)
				{
					currcaro++
					scroll.toElement($('CarouselItemImage-' + currcaro));
					if($('carouselNavPrev').getStyle('opacity')<1)
					{
						$('carouselNavPrev').setStyles({'opacity': 1});
					}
				}
				if(currcaro > -1)
				{
					if(currcaro - 1 < 0)
					{
						$('carouselNavPrev').setStyles({'opacity': '0'});
					}
				}
				if(currcaro + 1 == carlimit)
				{
					$('carouselNavNext').setStyles({'opacity': '0'});
				}
			});

			$('carouselNavPrev').addEvent('click', function(event) {
				$clear(periodical);
				event = new Event(event).stop();
				if(currcaro - 1 > -1)
				{
					currcaro--;
					scroll.toElement($('CarouselItemImage-' + currcaro));
				}
				if(currcaro + 1 < carlimit && $('carouselNavNext').getStyle('opacity')<1)
				{
					$('carouselNavNext').setStyles({'opacity': 1});
				}
				if(currcaro - 1 < 0)
				{
					$('carouselNavPrev').setStyles({'opacity': '0'});
				}
			});
		}
	}

	if($chk($('snippets')))
	{
		var items	= $$('.snippet_item');

		items.each(function(e,idx){
			if(e.getStyle('height').toInt()<40)
			{
				e.setStyle('height','40px');
			}
		});
	}

	if($chk($('billboard')))
	{
		if($('billboard').getProperty('rel')!='')
		{
			$('billboard').setStyles({
				'background-image': "url('/images/billboards/img/" + $('billboard').getProperty('rel') + "')",
				'background-repeat': 'no-repeat',
				'background-position': '0px 0px'
			});
		}
	}

	if($chk($('email')))
	{
		$('email').addEvent('focus', function(){
			if(this.value=='Enter your e-mail')
			{
				this.value = '';
			}
		});

		$('email').addEvent('blur', function(){
			if(this.value=='')
			{
				this.value = 'Enter your e-mail';
			}
		});
	}

	if($chk($('signup_submit')))
	{
		$('signup_submit').addEvent('click', function(){
			var addr = $('email').get('value');
			if(addr.length>0)
			{
				var post = Hash.toQueryString({email: addr});

				new Request({
					url: '/action/verifyemail/',
					method: 'post',
					data: post,
					onComplete: function() {
						var resp	= this.response.text;

						switch(resp)
						{
							case "true":
								window.open('https://secure2.convio.net/mmm/site/Advocacy?cmd=display&page=UserAction&id=811','_self');
							break;

							case "false":
								alert('Invalid e-mail address');
							break;

							case "empty":
								alert('Nothing entered.');
							break;

							case "dupe":
								window.open('https://secure2.convio.net/mmm/site/Advocacy?cmd=display&page=UserAction&id=811','_self');
							break;
						}
					}
				}).send();
			}
		});
	}

	if($chk($('searchsubmit')))
	{
		$('searchsubmit').addEvent('click', function(e){
			console.log('clicked!');
			var q = $('q').get('value');
			console.log(q);
			e = new Event(e).stop();
			window.open('http://www.bradycampaign.org/search/?q=' + q, '_self');
		});

	}

	if($chk($('MMMselectstateDD')))
	{
		$('MMMselectstateDD').addEvent('change', function(){
			if($('MMMselectstateDD').get('value')!="null")
			{
				window.open('http://www.bradycampaign.org/chapters/' + $('MMMselectstateDD').get('value'), "_self");
			} else {
				return false;
			}
		});
	}

    if($chk($('selectstateDD')))
	{
		$('selectstateDD').addEvent('change', function(){
			if($('selectstateDD').get('value')!="null")
			{
				window.open('http://www.bradycampaign.org/stategunlaws/scorecard/' + $('selectstateDD').get('value'), "_self");
			} else {
				return false;
			}
		});
	}

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

	if($chk($('site_title')))
	{
		$('site_title').addEvent('click', function(){
			window.open('/', "_self");
		});
	}

	if($chk($('GScounter')))
	{
		$('GScounter').addEvent('click', function(){
			window.open('http://www.bradycampaign.org/facts/gunviolence/', '_self');
		});
	}

});