// YMCA JavaScript




/*
* runs once the page is loaded
*/
$(document).ready(
     function(){
          //alert( 'here' );
          
          
          //initialize hidden sitemap, deprecated
          //initXMap();
          
          //homepage news,events,facebook accordion
          initHPWidget();

          //initialize search box
          initHeaderSearch();
          
          //initialize the HP footer carousel of callout graphics
          //initHPCarousel();
          
          
          //init find your Y select
          initFindYourY();
          
          
          //init event location dropdown on HP
          initHPEventFilters()

     }
 );

 /* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */

function initHPEventFilters() {

	$("#evlocbutton").click(clickHPEventByLocButton);
	$("#evlocaquabutton").click(clickHPEventByLocAquaticButton);
	$("#evloclandbutton").click(clickHPEventByLocLandButton);

}

function clickHPEventByLocButton( e ) {
	 var location_id = parseInt( $("select#evloc").val() );
	 
	 //if( location_id > 0 ) {
	 	  var URL = '/fm/events/index/?a=0&l='+location_id;
	       window.location = URL;
	 //}
}

function clickHPEventByLocAquaticButton( e ) {
	 var URL = $("select#evlocaqua").val();

	 //var URL = '/fm/events/index/?a=34&l='+location_id;
	 if( URL != '' ) {
      	window.location = URL;
      }

}

function clickHPEventByLocLandButton( e ) {
	var URL = $("select#evlocland").val();

	//var URL = '/fm/events/index/?a=35&l='+location_id;
	if( URL != '' ) {
		window.location = URL;
	}

}

/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */

/**
 * initialize the events for find your Y select
 */ 
function  initFindYourY() {

	$('#xfindysel').change( findYSelectChange );
}

/**
 * triggered onChange of find your Y select
 * @return none 
 */ 
function  findYSelectChange() {
	//alert('here');
	
	var URL = $('#xfindysel').val();
	
	if( URL != '' ) {
	 	window.location = URL;
	}
}

/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */

/**
 *   initialize hidden sitemap
 */ 
function initXMap() {

  $(".xnavplusicon").hover(function () {
     $("#xmap").slideDown("slow");
     $(".xnavplusicon").unbind('mouseenter mouseleave');
      $(".xnavplusicon").addClass('xnavplusminus');
     
     $(".xnavplusicon").click(function () {
        $("#xmap").slideToggle("slow");
        if( $(this).hasClass('xnavplusminus') ) {
          $(".xnavplusicon").removeClass('xnavplusminus');
        }else{
          $(".xnavplusicon").addClass('xnavplusminus');
        }
  	});
     
  }, null );

}

/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */

var hpw_index = 0;
var hpw_playing = true;
var hpw_auto_click = false;

/**
 *   initialize hidden homepage news/events/facebook widget accordion
 */ 
function initHPWidget() {

     $('.hpstatus-widget .hpwitem').each( setupHPWidgetItem );

     setTimeout("nextHPWidgetEvent()",1000);

}

/**
 * timer, next item
 */
function nextHPWidgetEvent() {
     //alert('Next! '+hpw_index);
     
     if( hpw_playing ) {
       var item_id = "hpwitem_"+hpw_index;
       
       hpw_auto_click = true; //only way to diferenicate "real" clicks from auto clicks.
       $('div#'+ item_id +' .hpw-icon').click();
       hpw_auto_click = false;
       
       hpw_index++;
       if( hpw_index == 6 ) hpw_index = 0; 
       
          setTimeout("nextHPWidgetEvent()",8000);
     }
} 

/**
 *  setups the actions on each item in HP widget
 */ 
function setupHPWidgetItem( index ) {
      //alert(index + ': ' + $(this).text());
      
      $(this).find('.hpw-icon').click( onClickHPWidgetIcon );
      
}



function onClickHPWidgetIcon( e ) {

   if( !hpw_auto_click )  hpw_playing = false;

   //alert('clicked me');
   $(this).parent().parent().find('.hpwitem').each( hideEachIcon );
   $(this).parent().find('.hpw-cnt').show( 'slow' );
   
   
   if($(this).hasClass('wnews')) $(this).addClass('wnewsa');
   if($(this).hasClass('wtwitter')) $(this).addClass('wtwittera');
   if($(this).hasClass('wevents')) $(this).addClass('weventsa');
   if($(this).hasClass('wfacebook')) $(this).addClass('wfacebooka');
   
}

function hideEachIcon( index ) {

     $(this).find('.hpw-cnt').hide( 'slow' );
     
     var thisicon = $(this).find('.hpw-icon');
     
     if( thisicon.hasClass('wnewsa') ) thisicon.removeClass('wnewsa');
     if( thisicon.hasClass('wtwittera') ) thisicon.removeClass('wtwittera');
     if( thisicon.hasClass('weventsa') ) thisicon.removeClass('weventsa');
     if( thisicon.hasClass('wfacebooka') ) thisicon.removeClass('wfacebooka');
     

}
 
 
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */

/**
 *   initialize the HP footer carousel of callout graphics
 */ 
function initHPCarousel() {
      $("div.hpc-callouts").jCarouselLite({
        btnNext: ".hpcnext",
        btnPrev: ".hpcprev"
    });
}


/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */


function initHeaderSearch() {
     
     var searchBox =$('input#q')

     searchBox.focus(function(e){
          //clearMenus();
          //$(this).addClass("ahactive");
          if( $(this).attr('value') == 'Search' ){
               $(this).attr('value','');
          }
          
     });
     searchBox.blur(function(e){  
          //$(this).removeClass("ahactive");
          if( $(this).attr('value') == '' ){
               $(this).attr('value','Search');
          }  
     });  
     
     searchBox.keypress(function(e){
          if(e.which == 13){
               submitSearchBox(e);
          }
     });

     
     $('div.sbutton a').click( submitSearchBox );

}

/**
 * activate/submit search box
 */ 
function submitSearchBox(e){
     e.preventDefault();
     
     var searchBox =$('input#q');
     var query = searchBox.attr('value');
     query = escape( query );

     if( query != '' ) {
          window.location = '/fm/search/?query='+query;
     }

}


/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////////////////// */
