// ANGA v2 Javascript



/**
 * Default onLoad Actions / Setup
 */ 
$(document).ready(function() {

     //initialize header search functionality
     initHeaderSearch();
     
     //MENU
     $('.xnav a').hover(handlerNavOver, handlerNavOut);
     $('div.ql-pop').each(initDropMenu);
     $('div.aheader').hover( handlerAHeaderOver, handlerAHeaderOut );
     
    
     if( $('#hptopblock') ) $('#hptopblock').tabs();
     if( $('#hplocalblock') ) $('#hplocalblock').tabs();
     if( $('#hpsportsblock') ) $('#hpsportsblock').tabs();
     
          
     
     //$('.nupdated')
     $('.hpfeatured .nupdated .nimg').each( eachUpdatedImageReplace );
     
     $('#submitSurveyButton').click( submitSurvey );
     
     
     //fix dropdown menu for variable height
     /*
     if( $('img#aspecialimg') ) {
          var imageHeight = $('img#aspecialimg').height();
          var topPos = $('.ql-struct').css('top');
          var position = $('.ql-struct').position();
          $('.ql-struct').css('top', (position.top+imageHeight)+'px');   
     }
     */
     
     
     
     
     //Carousel Ads
     initAddCarousel();
     
     //HEADER FADE IN/OUT
     setTimeout(initRadioPopupPlayers, 500);
     
     
     setTimeout(initMarquee, 1200);

});

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

function initMarquee() {
  //HOMEPAGE     
  var mar_div = $('div.marquee');
  
   if( mar_div ){
        mar_div.marquee();
        mar_div.show();        
	}
}


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

function initRadioPopupPlayers() {

     //radio popup players, just add CSS class
     $('a.openWDUN550').click( openWDUN550 );
     $('a.openWDUN1029').click( openWDUN1029 );
     $('a.openESNP1240').click( openESNP1240 );
     
}


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

/**
 * this function replaces the standard IMG tag
 * with background-image / src swap with "Updated" tag
 *  
 * @return none    
 */ 
function eachUpdatedImageReplace(index){
     var nimg = $(this).find('img').attr('src');
     $(this).css("background-image", "url("+ nimg +")");
     $(this).find('img').attr('src','/images/v3/images/updated-320x240.png');
}

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

function fadeInHP() { $('#ihome').fadeIn('slow'); setTimeout(fadeOutHP, 5000); }
function fadeOutHP() { $('#ihome').fadeOut('slow'); setTimeout(fadeInFacebook, 1000); }
function fadeInFacebook() { $('#ifacebook').fadeIn('slow'); setTimeout(fadeoutFacebook, 5000); }
function fadeoutFacebook() { $('#ifacebook').fadeOut('slow'); setTimeout(fadeInNewsroom, 1000); }
function fadeInNewsroom() { $('#inewsroom').fadeIn('slow'); setTimeout(fadeoutNewsroom, 5000); }
function fadeoutNewsroom() { $('#inewsroom').fadeOut('slow'); setTimeout(fadeInHP, 1000); }



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

/**
 *  handles the mouse rollover event
 */ 
function handlerAHeaderOver() {
     clearMenus();     
}

/**
 *  handles the mouse rollover event
 */ 
function handlerAHeaderOut() {

}

/**
 *  handles the mouse rollover event
 */ 
function handlerNavOver() {

     //clear drop menu all
     clearMenus();
     
     //if news dropmenu
     
     var thisId = $(this).attr('id');
     $(this).addClass('xnactive');
     
     if( thisId != '' ) {

          thisId = '#'+thisId;          
          var menuId = thisId+'-menu';
          
          //add orange active class
          if( thisId == '#xnews' ) $(this).addClass('xnactiveorg');
          if( thisId == '#xsports' ) $(this).addClass('xnactivegreen');
          if( thisId == '#media' ) $(this).addClass('xnactiveblue');
          if( thisId == '#xclassifieds' ) activateClassifiedsMenu();
          //if( thisId == '#xextra' ) activateExtraMenu();
          
          
          
          //if menu exists, show it
          if( $( menuId ).length ) {
              $( menuId ).css('display','block');          
          }
          
     }

}

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

/**
 *  activate classifieds drop menu, get featured classifieds
 */
function activateClassifiedsMenu() {
     var ft_html = $('#xclfeatured').html();
     
     if( ft_html == '' ){
          $('#xclfeatured').html( renderLoadingHTML() );
          
          $('#xclfeatured').load( '/includes/html/classifieds/ajax/classifieds-drop-featured.php?p=1', function() {
               addClassifiedsPrevNextAction();
          });
     }
} 

/**
 *  adds the prev/next actions
 */ 
function addClassifiedsPrevNextAction(){
     var page = Number( $('input#xclpage').val() );
     //alert( page );
     
     //previous
     $('#xclprev').click(function(e) {
          e.preventDefault();
          var prev_page = page - 1;
          var prev_url = '/includes/html/classifieds/ajax/classifieds-drop-featured.php?p='+prev_page;
          $('#xclfeatured').html( renderLoadingHTML() );
          $('#xclfeatured').load( prev_url, function() {
               addClassifiedsPrevNextAction();
          });
     });
     
     //next
     $('#xclnext').click(function(e) {
          e.preventDefault();
          var next_page = page + 1;
          var next_url = '/includes/html/classifieds/ajax/classifieds-drop-featured.php?p='+next_page;
          $('#xclfeatured').html( renderLoadingHTML() );
          $('#xclfeatured').load( next_url, function() {
               addClassifiedsPrevNextAction();
          });
     });
}

/**
 * builds the HTML that show the loading gif
 */ 
function renderLoadingHTML(){
     var str = '';

     str = str + "<div class='clfloadicon'><img src='/img/classifieds/menu-loading.gif' alt='loading' /></div>";
     str = str + "<div class='clfloadtext'>loading...</div>";
     
     return str;
}

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

/**
 *  load extra columinist data
 */ 
function activateExtraMenu(){
     var ft_html = $('#xextrafeatured').html();
     
     if( ft_html == '' ){
          $('#xextrafeatured').html( renderLoadingHTML() );
          
          $('#xextrafeatured').load( '/includes/html/extra/ajax/featured-columnist.php' );
     }
}


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

/**
 *  handles the mouse rollout event
 */ 
function handlerNavOut() {

}

/**
 *  clears all the active drop menus
 */ 
function clearMenus() {

     $('div.xnav a').each(function(index) {
         $(this).removeClass('xnactive');
         $(this).removeClass('xnactiveorg');
         $(this).removeClass('xnactivegreen');
         $(this).removeClass('xnactiveblue');
     });

     $('div.ql-pop ').each(function(index) {
         $(this).css('display','none');
     });

}

/**
 * initalizes drop down menu
 */ 
function initDropMenu(){

     $(this).hover(handlerMenuOver, handlerMenuOut);
     
}

/**
 *  handles the menu rollout event
 */ 
function handlerMenuOver() { }

/**
 *  handles the menu rollout event
 */ 
function handlerMenuOut() {
      clearMenus();
}

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

function openWDUN550( e ) {
     e.preventDefault();
     //openPlayer('http://www.streamaudio.com/stations/player/pages/index.asp?Station=WDUN_AM');
     fncCenteredChildWindow( 'http://mgmt.firststreaming.com/listen/player.php?callsign=WDUN_AM', 375, 648,'no','no');
}

function openWDUN1029( e ) {
     e.preventDefault();
     //openPlayer('http://www.streamaudio.com/stations/player/pages/index.asp?Station=WMJE_FM');
     fncCenteredChildWindow( 'http://mgmt.firststreaming.com/listen/player.php?callsign=WDUN_FM', 375, 648,'no','no');
}

function openESNP1240( e ) {
     e.preventDefault();
     //fncCenteredChildWindow( 'http://mediasuite.multicastmedia.com/playerlive.php?e=lxe861s6&doResize=false', 275, 325,'no','no');
     fncCenteredChildWindow( 'http://www.streamaudio.com/player/player.aspx?optin=no&station=WDUN_IR', 275, 325,'no','no');
}


function launchCommentDisclaimer(){
	fncCenteredChildWindow('/access/etc/comment-disclaimer.php',250,300,'no','no');
}

function fncCenteredChildWindow(URL,vHeight,vWidth,vScroll,vStatus) {
	if (screen.width) {
		var winl=(screen.width-vWidth)/2;
		var wint=(screen.height-vHeight)/2;
	} else { 
		winl=0;wint=0;
	}
	if(winl<0)winl=0;
	if(wint<0)wint=0;
	window.open(URL,'myWin','height='+vHeight+',width='+vWidth+',scrollbars='+vScroll+',status='+vStatus+',top='+wint+',left='+winl+',resizable=no')
}



/*
StreamAudio Player Open Script
*/

 function openPlayer(URL) {
     if(navigator.appVersion.indexOf("AOL") > -1)
      {
      //AOL
      var opt1 = 'width=472,height=257';
      var opt2 = 'width=472,height=257';
      } else if(navigator.platform.indexOf("Win") > -1)
      {
      //IE and NS Windows
      var opt1 = 'width=472,height=257,top=420,left=520,screenY=420,screenX=520';
      var opt2 = 'width=472,height=257,top=250,left=300,screenY=250,screenX=300';
      } else if(navigator.platform.indexOf("Mac") > -1)
      {
      //IE and NS Mac
      if(navigator.appName.indexOf("Microsoft") > -1)
      {
      var opt1 = 'width=455,height=240,top=420,left=520,screenY=420,screenX=520';
      var opt2 = 'width=455,height=240,top=250,left=300,screenY=250,screenX=300';
      } else if(navigator.appName.indexOf("Netscape") > -1)
      {
      var opt1 = 'width=472,height=257,top=420,left=520,screenY=420,screenX=520';
      var opt2 = 'width=472,height=257,top=250,left=300,screenY=250,screenX=300';
      }
      }
    if(screen.width >= 1000) {
          window.open(URL,'Streamaudio_Player',opt1);
     } else {
          window.open(URL,'Streamaudio_Player',opt2);
     }
}

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


function initHeaderSearch() {
     
     var searchBox =$('input#ahq');
     
     
     if(searchBox) {

	     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);
	          }
	     });
	
	     
	     $('#ahbutton').click( submitSearchBox );
     
     }

}

/**
 * activate/submit search box
 */ 
function submitSearchBox(e){

     var searchBox =$('input#ahq');
     var query = searchBox.attr('value');
     query = escape( query );

     if( query != '' ) {
          window.location = '/access/search.php?q='+query+'&m=access';
     }

}




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

function submitSurvey( e ) {

     e.preventDefault();
     
	//alert(document.getElementById("survey_ans").value);
	if(valRadio(document.surveyForm.survey_ans) != null){
		document.getElementById("surveyForm").submit();
	}else{
		alert("You must select one answer to submit the survey.");
	}
}

function valRadio(radioGroup) {
	//alert(radioGroup.value);
	//alert(radioGroup.length);
	var count = -1;
	for (var i=radioGroup.length-1; i > -1; i--) {
		//alert(radioGroup[i].checked);
		if (radioGroup[i].checked) {count = i; i = -1;}
	}
	if (count > -1) return radioGroup[count].value;
	else return null;
}



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

/**
 *
 */ 
function initAddCarousel() {

	// #adcar1
	var num_elements = $('#adcar1 div.adcarouselcnt > div').size();
	//alert( "ele: "+ num_elements );
	
	
	var slider = $('#adcar1').bxSlider({
		controls: false,
		auto: true,
		onAfterSlide: updateCarThumbs
	});
	
	$('#adcarprev').click(function() {
		slider.goToPreviousSlide();
		return false;
	});
	
	$('#adcarnext').click(function() {
		slider.goToNextSlide();
		return false;
	});
	
	$('.adcarthumbs a').click(function(){
            var thumbIndex = $('.adcarthumbs a').index(this);
            slider.goToSlide(thumbIndex);                      
            return false;
     });

	
}

function updateCarThumbs( currentSlideNumber, totalSlideQty, currentSlideHtmlObject ) {
     updateThumbsByIndex( currentSlideNumber );
}

function updateThumbsByIndex( sindex ){ 	
 	$('.adcarthumbs a img').attr('src','/img/2008/icon-circlewhite-featnews.gif');
 	var img = $('.adcarthumbs a img').get( sindex );
	img.src = '/img/2008/icon-circleblue-featnews.gif';
}




