

(function( $ ){



/*
	$(function(){
	  
	  // Bind an event to window.onhashchange that, when the hash changes, gets the
	  // hash and adds the class "selected" to any matching nav link.
	  $(window).hashchange( function(){
	    var hash = location.hash;
	    
	    // Set the page title based on the hash.
	    //document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
	    
	    // Iterate over all nav links, setting the "selected" class as-appropriate.
	    $('#nav a').each(function(){
	      var that = $(this);
	      that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
	    });
	  })
	  
	  // Since the event is only triggered when the hash changes, we need to trigger
	  // the event now, to handle the hash the page may have loaded with.
	  $(window).hashchange();
	  
	});
*/




		$('a').smoothScroll();

		var playButton = $('#playSong');
		var downLoadButton = $('#downloadSong');
		
		if(downLoadButton.length > 0 ) {
			
			var s = CI.track_name;
			var name = s.replace(/[\s+-]/g, '-').replace(/[^\w-]/g, '').toLowerCase();
			//console.log(s);
			
			newLink = 'http://wvmusic.bandcamp.com/track/' + name + '?action=download';
			downLoadButton.attr('href', newLink );
			

		}
		




		//Calculate the height of <header>
        //Use outerHeight() instead of height() if have padding
        var aboveHeight = $('.container.mainNav').outerHeight() + $('.container.branding').outerHeight();
        var navHeight = $('.container.makeFixed').outerHeight();
        
        var changePoints = Array;
        var elems = Array;
        
        function setPoints() {
	 		$('.episodeNav > li > a').each(function(index) {
	 			var $$ = $(this);
	 			var h = $$.attr('href');
	 			var hash = h.split("#");
	 			
	 			
	 			//console.log(hash[1]);
	 			
	 			var theAnchor = $('a[name="#' + hash[1] + '"]');
	 			elems[index] = $$;
	 			changePoints[index] = theAnchor.offset().top;
	 			//console.log(changePoints[index]);
	 		
	 		});
	 		//console.log("setting points");
        }
        setPoints();
        
        
        
        
        // Image gallery on new episode pages
        $('.editorial').find('img').each( function() {
			var $$ = $(this);
			var parent = $$.parent('a');
			var src = $$.attr('src');
			parent.attr('href', src);
			
			//console.log(parent);
			
			
			
			$$.fancybox({
				padding: 10,
				autoSize: false,
				

				openEffect : 'elastic',
				openSpeed  : 150,

				closeEffect : 'elastic',
				closeSpeed  : 150,

				closeClick : true,
				title : null
			});
        });
        
        
					
					
					
						
 
		//when scroll
        $(window).scroll(function(){
        
			var scrollTop = $(this).scrollTop(); 
			
			for (thePoint in changePoints) {
				var thisElem = $('.episodeNav > li:eq(' + thePoint + ')');
				var pt = changePoints[thePoint];
				
				if (scrollTop >= pt ){
					thisElem.addClass('active');
					//thisElem.siblings().removeClass('active');
					
				} else {
					thisElem.removeClass('active');
				}
			
			}
			    
        	
       		//if scrolled down more than the headerÕs height
            if (scrollTop > aboveHeight){
	        	// if yes, add ÒfixedÓ class to the <nav>
	        	// add padding top to the #content
                //(value is same as the height of the nav)
                $('.container.makeFixed').addClass('scrollpop').css('top', 0).next()
                .css('margin-top',navHeight);
 
            } else {
	        	// when scroll up or less than aboveHeight,
                //remove the ÒfixedÓ class, and the padding-top
                $('.container.makeFixed').removeClass('scrollpop').next()
                .css('margin-top', 0);
            }
        });

		
		$.ajax({
		  url: CI.base_url + 'ajax/ajax_gallery/' + CI.artist_id + '/A',
		  dataType: 'text',
		  type: 'GET',
		  success: function(data) {
		  		//console.log(data);
		  		
		  },
		  onComplete: function(data) {
		  	//console.log(data);
		  		
		  },
		  error: function(data) {
		    	$('#gallery_wrapper_artist').remove();
			},
		  statusCode: {

		    500: function() {
		    	$('#gallery_wrapper_artist').remove();
		      },
		    200: function(data) {
		        if(data != '') {
		       	 	$('#gallery_wrapper_artist').find('p').remove();
		       	 	$('#gallery_wrapper_artist').append(data);
		        	setPoints();
		        	$('#gallery_wrapper_artist').find('a').fancybox({
						padding: 10,
		
						openEffect : 'elastic',
						openSpeed  : 150,
		
						closeEffect : 'elastic',
						closeSpeed  : 150,
		
						closeClick : true,
						title : null
					});
		        } else {
		        	$('#gallery_wrapper_artist').remove();
		        }
		        
		      },
				}
				
		});
		
		
		$.ajax({
		  url: CI.base_url + 'ajax/ajax_gallery/' + CI.artist_id + '/G',
		  dataType: 'text',
		  type: 'GET',
		  success: function(data) {
		  		//console.log(data);
		  		
		  },
		  onComplete: function() {
		  		
		  },
		  error: function(data) {
		    	$('#gallery_wrapper_gear').remove();
			},
		  statusCode: {

		    500: function() {
		    	$('#gallery_wrapper_gear').remove();
		      },
		    200: function(data) {
		        if(data != '') { 
		       	 	$('#gallery_wrapper_gear').find('p').remove();
		       	 	$('#gallery_wrapper_gear').append(data);
		        	setPoints();
		        	$('#gallery_wrapper_gear').find('a').fancybox({
						padding: 10,
		
						openEffect : 'elastic',
						openSpeed  : 150,
		
						closeEffect : 'elastic',
						closeSpeed  : 150,
		
						closeClick : true,
						title : null
					});
		        } else {
		        	$('#gallery_wrapper_gear').remove();
		        }
		        
		      },
				}
				
		});
				



		// Newsletter form
		$('#newsletterForm').each(function() {
			var $$ = $(this);
			var subButton = $$.find('.submit_button');
				
			subButton.click(function(e) {
				var $email = $("input#email").val();  
				var $zip = $("input#zip").val(); 

				e.preventDefault();
				$.post(CI.base_url + 'ajax/submit_contact', { email : $email, zip: $zip }, function(data) {
					//console.log(data);
					$('#newsletterResponse').html(data);
				});
			});
		});
	
		
					
			


})( jQuery );
