

function loadCities (state) {
	//alert (state);
	$("#bar-expanded-find-stations-cities").load("/progs/loadCities.php?state="+state);
	$("#bar-expanded-find-stations-states").fadeTo(500, 0.0).hide();
	$("#bar-expanded-find-stations-cities").fadeTo(500, 1.0).show();
}

function loadStates () {
	//back to states
	$("#bar-expanded-find-stations-cities").fadeTo(500, 0.0).hide();
	$("#bar-expanded-find-stations-states").fadeTo(500, 1.0).show();
}

function toggleExpandABarDrawer(isStations){
	if(isStations) {
		$('#bar-expanded-buyers-guide').hide();
		$('#bar-expanded-find-stations').show();
	} else {
		$('#bar-expanded-find-stations').hide();
		$('#bar-expanded-buyers-guide').show();
	}
}

function expandABar () {
	if ($('.bar-big-button').length > 1) {
		expandABarBGSubCat();
		
		// does a less SEO-SCARY bar-expanded... it doesn't load the page as DISPLAY:NONE on an important nav element right off the bat
		$('#bar-expanded.loading').removeClass('loading').addClass('loaded');
		
		$('.bar-big-button a').click(function(expandABarButt){
			expandABarButt.preventDefault();
			$(this).blur(); // FF shows a dotted outline around a button, this takes it away
			
			$('.bar-big-button a').removeClass('unfurled'); // button stylings
			
			var rollOut = true;
			var rollOutStations = true;
			
			if( $(this).attr('href') == '/stations' ) {
				if( $('#bar-expanded-find-stations:visible').length ) rollOut = false;		
				$('#bar-expanded:visible').slideUp(200, function(){
					toggleExpandABarDrawer(true);
				});
			} else {
				if( $('#bar-expanded-buyers-guide:visible').length ) rollOut = false;		
				$('#bar-expanded:visible').slideUp(200, function(){
					toggleExpandABarDrawer(false);
				});
				rollOutStations = false;
			}
			
			if( $('#bar-expanded:hidden').length ){ toggleExpandABarDrawer(rollOutStations); } // needed for a click when the button is already folded in
			
			if(rollOut) {
				$('#bar-expanded').delay(200).slideDown(200);
				$(this).addClass('unfurled'); //button styling	
			} 
			
		});
	}
	
	//initialize state links if they exist
	if ($('.states').length) {
		$('.states a').click(function(stuff) {
			stuff.preventDefault();
			loadCities (this.rel);
		})
	}

	//init search
	searchMe ();
	
	//buyer's guide stuff
	if( $('ul.secondCat:visible').length ) $('ul.topCat').height(40);

}

function expandABarBGSubCat(){
	$('#bar-expanded-buyers-guide ul.topCat li a').click(function(clicky){
		// if it is not one of the BG subpages AND it has sub categories, PREVENT DEFAULT
		if( ($('.loadedBG').length == 0 && $(this).siblings('ul.secondCat').length) ) clicky.preventDefault(); // if there are subcategories, prevent default behavior, otherwise click-through
		
		$(this).blur().parent().siblings().removeClass('selected');
		$(this).parent().addClass('selected');
		if( $('ul.secondCat:visible').length ) $('ul.topCat').animate({'height': 40}, 100);
	});	
}

function searchMe() {

	var searchTerms = new Array ("Try: Garmin", "Try: 90210", "Try: Oakland, CA", "Try: Portable"); //search terms array
	var x = -1; //count var
	var on = true; //true = animation continues
	
	//init stored value, puts search query term in there if exists
	if ($('#bar-search-field').val() == "Search Me") {
		storedInput = "";
	} else {
		storedInput = $('#bar-search-field').val();
	}
	
	//click to disable animation
	$('#bar-search-field').focus (function () {
		if (storedInput == '') {
			$(this).val('');
		} else {
			$(this).val(storedInput);
		}
		on=false;
 	});
 	
 	//blur to enable animation
 	$('#bar-search-field').blur (function () {
		storedInput = $(this).val();
		on=true;
 	});

   	window.setTimeout(searchMeAnimate, 5000);
	
    function searchMeAnimate() {
    
    	//reset x
    	x++;
		if (x == searchTerms.length) { x = 0; }
		
		//testing
		//console.log("x = " + x + " (" + searchTerms[x] + ")");
		
		//animation if not being used
		if (on == true) {
			$('#bar-search-field').animate({ color: '#fde6c6' }, 500, function () {
				$(this).val(searchTerms[x]).animate({ color: '#f99d1c' }, 500);
			});
		}
		
		//refire animation
        if (x < searchTerms.length) {
            window.setTimeout(searchMeAnimate, 5000);
		}
    }
}
	
function buyersGuideSaveForLater(){
	$('.savedCheckBox input').click(function(){
		var postObject = this.checked ? { 'add' : this.value } : { 'remove' : this.value };
		$.post('/progs/saveRadios.php', postObject, function(data){
			$('.savedCheckBox #memo'+data.id).text(data.action).fadeIn(100);
			$('#savedRadiosButton:hidden').fadeIn(200);
			window.setTimeout("$('.savedCheckBox #memo"+data.id+"').fadeOut(200)",1000);
		}, 'json');
	});
}
function searchExpand(collapse){
	if(collapse == 1) {
		$('.hiddenStationRows').fadeTo(0, 1.0).hide();
		$('.showStationRowsButton').hide();
		$('.hideStationRowsButton').show();
	}
	else {
		$('.hiddenStationRows').fadeTo(500, 1.0).show();
		$('.hideStationRowsButton').hide();
		$('.showStationRowsButton').show();
	}
	return void(0);
}

function verticalSidebar () {

	//make copycopy min height of sidebar
	$('.copyCopy').css({'min-height': $('#sidebar-nut').css('height')})
	this.didScroll = true;
	
	//turn off for IE
	if (false) {
		$(window).scroll(function(){ this.didScroll = true; });
		
		function computeHeight(){ this.computedHeight = $('#bar-husk-sub').height()+$('#sub-header-shell').height()+$('#header').height()+10; }
		
		computeHeight(); // calculate once when this function runs
		$(window).load(function(){computeHeight(); $('.copyCopy').css({'min-height': $('#sidebar-nut').css('height')}); }); // calculate again when everything is loaded, as the fonts and whatever else sits in the header change things
		
		setInterval(function() { // this reduces the number of times this function runs, increasing the spryness of the whole operation
		    if ( this.didScroll ) {
		        this.didScroll = false;
		
				computeHeight();
				//attach sidebar to browser if over a certain height
				if ($(this).scrollTop() > this.computedHeight){
					$('#sidebar-husk').addClass('sliding');
				} else if ($(this).scrollTop() < computedHeight){
					$('#sidebar-husk').removeClass('sliding');
				}
			//console.log($(this).scrollTop());
			//console.log($('#bar-husk-sub').css('height'));
		    }
		}, 50);
	}
}

function whatIsItTabMenu(){
	$('#wihdrTabs>li>a').click(function(clickck){
		clickck.preventDefault();
		$('.whatIsBlocks:visible:not('+this.hash+')').slideUp(400);
		$(this.hash).slideDown(400);
	})
}

function advancedFeaturesScrollable(){
	$("#bafSlider").scrollable({speed: 500, vertical: true}).navigator({navi: '#bafMenu', history: true});
    tweakery = $("#bafSlider").data("scrollable");
    $('#bafMenu>a').click(function(){
		$(this).blur();
		$('#bafMenuArrow').animate({ 'top' : $(this).index()*38+93 }, 500); 
	});
	//$('#bafMenu>a[href=#features-iTunesTagging]').click(); // rewind to itunes tagging on load
}

$(document).ready(function(){

	//check for vertical side-bar, do vertical sidebar things
	if ($('#sidebar-nut').length) verticalSidebar();
	
	//do horizontal bar things
	expandABar ();
	
	//home images
	if ($('#home-feature .home-feature-image').length) {
		$('#home-feature').cycle({ fx: 'fade', speed: 1050, timeout: 6000, pauseOnPagerHover: false});
		$('.hdRadioBug').replaceWith('<img src="/i/logo-hd-bug.png" alt="HD Radio" />');  //for SEO on HD Radio
	}
	
	
	//buyer's guide
	if($('.savedCheckBox').length) buyersGuideSaveForLater();
	
	if($('#bgBrandsSubMenu').length) {
		$('#bgBrandsSubMenu').hide();
		$('li.brandsButton a').click(function(click){
			click.preventDefault();
			$(this).blur();
			$('#bgBrandsSubMenu').slideToggle(200, function(){ $('li.brandsButton a').toggleClass('unfurled') })
		});
	}
	//general hide / show classes
	$(".hidemejs").css({'display':'none'});
	$(".showmejs").css({'display':'block'});
	
	//what is hd radio section
	if($('.whatIsBlocks').length > 1) {
		whatIsItTabMenu();
		advancedFeaturesScrollable();
	}
	
	if($('#hdisPlayers').length == 1){

	    $("#hdisPlayerFM").jPlayer({
	    	ready: function () {
	    		$(this).jPlayer("setMedia", {
	    			mp3: "http://www.txcftp.com/CLOUDED/HDRadio/FMCountry.mp3"
	    		});
				$(this).jPlayer("load");
	    	},
	    	swfPath: "/assets/flash",
	    	//errorAlerts: true,
	    	supplied: "mp3",
	        solution: "flash, html",
			preload: 'auto',
	        cssSelectorAncestor: "#jp_interface_0"
	    }).bind($.jPlayer.event.play, function() {
	    	$(this).jPlayer("pauseOthers");
	    });
	      
	    $("#hdisPlayerAM").jPlayer({
	    	ready: function () {
	    		$(this).jPlayer("setMedia", {
					mp3: "http://www.txcftp.com/CLOUDED/HDRadio/AMSports.mp3"
	    		});
				$(this).jPlayer("load");
	    	},
	    	swfPath: "/assets/flash",
	    	//errorAlerts: true,
	        supplied: "mp3",
	        solution: "flash, html",
	        cssSelectorAncestor: "#jp_interface_1"
	    }).bind($.jPlayer.event.play, function() {
	    	$(this).jPlayer("pauseOthers");
	    });
		
		
	}
	
	
});

$(window).load(function(){
	// when images all load
});
