

//Page turner and Genre loader
$(document).ready(function() {

// Fix header menu to top screen on scroll 
    var theLoc = $('.menu-primary-menu-container').position().top;
    $(window).scroll(function() {
        if(theLoc >= $(window).scrollTop()) {
            if($('.menu-primary-menu-container').hasClass('fixed')) {
                $('.menu-primary-menu-container').removeClass('fixed');
            }
        } else { 
            if(!$('.menu-primary-menu-container').hasClass('fixed')) {
                $('.menu-primary-menu-container').addClass('fixed');
            }
        }
    });
    
    
    //Review page pagination
 
    $('#postPagination a').live('click', function(e){
        e.preventDefault();
        
        var link = $(this).attr('href');
        $('.ajax_class').fadeOut(500).load(link + ' #ajax_wrap', function(){ $('.ajax_class').fadeIn(500); });
 
    });
    
    //downloads append
    
    var showp = 12;
    $('#specialpostPagination a').live('click', function(e){
    	e.preventDefault();
		showp = showp+12;
    	var link = $(this).attr('href');
    	$('.ajax_class').fadeOut(500).load(link + '?showp=' + showp + ' #ajax_wrap', function(){ $('.ajax_class').fadeIn(500); });
    	$('html, body').animate({scrollTop: $(document).height()}, 'slow');
 	});


    
    $('.genre_cat a').live('click', function(e){
		e.preventDefault();
		var link = $(this).attr('href');
        $('.ajax_class').fadeOut(500).load(link + ' #ajax_wrap', function(){ $('.ajax_class').fadeIn(500); });

	});
	
	
	$(".video_image, .video_image2, .med_image, .review_image_size, .features_box, .album_pic4, .review_image_size2")
		.live('hover', function(e){
		e.preventDefault();
    		$(this).children(":first").stop(true, true).fadeToggle(400, "linear");
			}, function() {
    		$(this).children(":first").stop(true, true).fadeToggle(400, "linear");
			});
			
			$('.features_box').show();
	});

//Front page hovers	
$(function() {
	$('.podcast_bkground img').animate({
		"opacity" : 1
	});
	$('.podcast').hover(function() {
		$('.podcast_bkground img').stop().animate({ "opacity" : 0 } , 1500 );	
		},function() {
			$('.podcast_bkground img').stop().animate({ "opacity" : 1 } , 1500 );
		});
		
	$('.album_cover').animate({ "opacity" : 0 });
	$('.banner').hover(function(){
		$('.album_cover').stop().animate({ "opacity" : 1 } , 1500 );	
		},function(){
			$('.album_cover').stop().animate({ "opacity" : 0 } , 1500 );
	});
	$('#speaker_image').animate({ "opacity" : 1 });
	$('.banner').hover(function(){
		$('#speaker_image').stop().animate({ "opacity" : 0 } , 1500 );	
		},function(){
			$('#speaker_image').stop().animate({ "opacity" : 1 } , 1500 );
	});

//Lightbox function
    $(function() {
        $('#litebox a').lightBox();
    });


//About Page Images
$(function() {
	var actTab;
	$('div[box]').hover(function() {
	  $('.content'+actTab).hide();
	  actTab = $(this).attr("box");
	  $('#bio'+actTab).stop(true, true).show();
	}, function() {
		$('div.content').stop(true, true).hide();
	});
});


//Dropdown Menu
	//$('.menu ul').hide();	   
$('.menu li').hover(
    function () {
    
        //show its submenu
        $('ul', this).show();
        },function () {
        
        //hide its submenu
        $('ul', this).hide();        
        }
    );
});

  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  function checkEmail(email) { 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = $("#" + email).val();
    return pattern.test(emailVal);
  }

 $(function() {
    $('#subForm').submit(function(e) {
      e.preventDefault();
            
      // Grab form action
      var formAction = this.action;
      
      // Hacking together id for email field
      // Replace the xxxxx below:
      // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
      var id = "qhjkdj";
      var emailId = id + "-" + id;
      
      // Validate email address with regex
      if (!checkEmail(emailId)) {
        alert("Please enter a valid email address");
        return;
      }
      
      // Serialize form values to be submitted with POST
      var str = $(this).serialize();
      
      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>
      
      // Submit the form via ajax
      $.ajax({
        url: "wp-content/themes/beatnik/scripts/proxy.php",
        type: "POST",
        data: serialized,
        dataType: 'html',
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
            alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to the Beatnik Online mailing list.');
          }
          else
          {
            var $confirmation = $('#confirmation');
            $("#theForm").hide(); // If successfully submitted hides the form
            $confirmation.slideDown("slow");  // Shows "Thanks for subscribing" div
            $confirmation.prop('tabIndex', -1);
            $confirmation.focus(); // For screen reader accessibility
            // Fire off Google Analytics fake pageview
            //var pageTracker = _gat._getTracker("UA-XXXXX-X");
            //pageTracker._trackPageview("/newsletter_signup");
          }
        }
      });
    });
  });
  
  
//Back to top scroll
$(function() {
	$('#bck_to_top a').bind('click',function(event){
	var $anchor = $(this);

	$('html, body').stop().animate({
	scrollTop: $($anchor.attr('href')).offset().top
	}, 1000, 'easeInExpo');

	event.preventDefault();
	});
});

//Disappearing button
$(function () { // run this code on page load (AKA DOM load)
 
	/* set variables locally for increased performance */
	var scroll_timer;
	var displayed = false;
	var $topBtn = $('#bck_to_top a');
	var $window = $(window);
	var top = $(document.body).children(0).position().top;
 
	/* react to scroll event on window */
	$window.scroll(function () {
		window.clearTimeout(scroll_timer);
		scroll_timer = window.setTimeout(function () { // use a timer for performance
			if($window.scrollTop() <= top) // hide if at the top of the page
			{
				displayed = false;
				$topBtn.fadeOut(500);
			}
			else if(displayed == false) // show if scrolling down
			{
				displayed = true;
				$topBtn.stop(true, true).show().click(function () { $topBtn.fadeOut(500); });
			}
		}, 100);
	});
});
