/* Author: Matter LLC

*/
var arrowStart   = parseInt($('.highlight-arrow').css('top'));	//get the starting position of the highlight arrow from css
var arrowSpace   = 44;                                        	//arrow should move this much each time
var delayRegular = 6500;                                      	//the delay between transistions
var delayInitial = 3000;                                      	//the initial delay for the first transition after the page loads
var delayRestart = 10000;                                     	//the delay before restart after a user clicks an artist n

$(document).ready(function() {

	//change the style of the artist menu
	$('#artist-nav').customStyle();

	// Auto submit the artist menu list in the global nav
	$(function() {
		$("#artist-nav").change(function() {
			if ($(this).val()) {
				window.open($(this).val(), '_self');
				// $("#artist-nav-form").submit();
			}
		});
	});



	//Roster slideshow
	$('.category-roster').nextAll().addClass('off').toggle();   	//Hide all but the first roster entry
	$('.roster-overlay p').nextAll('p').toggle();               	//Hide all but the first sub-links
	$('.roster-overlay h2').first().css('padding', '0');        	//Remove the padding from the first h2 link
	$('.roster-overlay h2 a').first().addClass('roster-active');	//Make the first roster artists active by default
	var rosterList = $('.roster-overlay h2 a');                 	//create an array of all the artists in the roster





	//Add a position to the alt value to each roster artist, e.g., 0, p1
	$('.roster-overlay h2 a').each(function(i) {
		var pos = 'pos-'+i;
		$(this).attr('alt', i);
		$(this).addClass(pos);
		i++;
	});

	//start the slideshow
	//use the setTimeout to start the show after an initial delay
	var autoShowTimeoutId = setTimeout(function() {
		autoShow(rosterList, autoShowTimeoutId);
	},delayInitial);

	//respond to a click to an artist name
	$('.roster-overlay h2 a').click(function(e) {
		e.preventDefault();
		clearTimeout(autoShowTimeoutId);           		//stop the autoshow
		clickArtist.call(this, rosterList);        		//call the function to select the clicked artist
		autoShowTimeoutId = setTimeout(function() {		//restart the autoshow
		    autoShow(rosterList, autoShowTimeoutId);
		},delayRestart);
	});

	///////////////////////////////////////////////////////
	//NEWS pages
	///////////////////////////////////////////////////////

	//turn News global nav on for tag and single post pages
	if ($('body').hasClass('tag') || $('body').hasClass('single-post') ) {
		$('.page-item-7').addClass('current_page_item');
	};


	///////////////////////////////////////////////////////
	//mailing list code
	///////////////////////////////////////////////////////
	$('.subscribe').click(function(e) {
		e.preventDefault();
		$('#newsletter').toggle();
		var height = document.body.clientHeight;	//get the height of the window
	    height = height + 'px';
	    $('#newsletter').css('height', height);		//make the height of the background image the same as the window
	});

	$('#cancelnewsletter').click(function(e) {
		e.preventDefault();
		$(':input','#subForm')
			.not(':button, :submit, :reset, :hidden')
			.val('')
			.removeAttr('checked')
			.removeAttr('selected');	//clear the form when cancel is clicked
		$('#newsletter').toggle();
	});

	///////////////////////////////////////////////////////
	//Artist Pages
	///////////////////////////////////////////////////////

	var artistAttribute;

	$('.photos').hide();
	$('.photos-button').click(function(e) {
		e.preventDefault();
		artistAttribute = "."+$(this).attr('class').split('-')[0];
		pressKit.call(this, artistAttribute);
	});

	$('.bio').hide();
	$('.bio-button').click(function(e) {
		e.preventDefault();
		artistAttribute = "."+$(this).attr('class').split('-')[0];
		pressKit.call(this, artistAttribute);
	});

	$('.artist-video').hide();
	$('.video-button').click(function(e) {
		e.preventDefault();
		artistAttribute = ".artist-"+$(this).attr('class').split('-')[0];
		pressKit.call(this, artistAttribute);
	});

	$('.download').hide();
	$('.download-button').click(function(e) {
		e.preventDefault();
		artistAttribute = "."+$(this).attr('class').split('-')[0];
		pressKit.call(this, artistAttribute);
	});

	// dynamically change the page background image based on the page title
	if ( $('#background').hasClass('artist-page') ) {	//check to make sure you are on an aritst page

		var fi = document.title.split(' ')[0].slice(0,1).toLowerCase();	//get the first name initial of the artist from the page title
		var li = document.title.split(' ')[1].slice(0,1).toLowerCase();	//get the last name initial of the artist from the page title
		var ai = fi+li;                                                	//combine the two initials
		$('#background').addClass('background-'+ai);                   	//add the background image to the page
	};


	// var artist = {};	//create the artist object
	// $('.roster-overlay h2 a').each(function(index) {
	//	var fn = $(this).html().split(' ')[0];     	//get the artist first name
	//	var ln = $(this).html().split(' ')[1];     	//get the last name
	//	var wn = $(this).html();                   	//get the whole name
	//	artist[fn] = { first:fn, last:ln, full:wn};	//define the artist object in artist.firstname notation, to access: artist.James.first would return James
	// });

	// var artist = {	Andres:   	"background-ac",
	//               	James:    	"background-jc",
	//               	William:  	"background-wm",
	//               	Jane:     	"background-jm",
	//               	Rob:      	"background-rm",
	//               	Madeleine:	"background-mp",
	//               	Roberto:  	"background-rs",
	//               	Sam:      	"background-sy",
	//              };

	// if (document.title.indexOf('Andres')>=0 && $('#background').hasClass('artist-page')) {
	//	$('#background').addClass('background-ac');
	// };

	// if (document.title.indexOf('James')>=0 && $('#background').hasClass('artist-page') ) {
	// 	$('#background').addClass('background-jc');
	// };

	// if (document.title.indexOf('William')>=0 && $('#background').hasClass('artist-page')) {
	// 	$('#background').addClass('background-wm');
	// };

	// if (document.title.indexOf('Jane')>=0 && $('#background').hasClass('artist-page')) {
	// 	$('#background').addClass('background-jm');
	// };

	// if (document.title.indexOf('Rob')>=0 && $('#background').hasClass('artist-page')) {
	// 	$('#background').addClass('background-rm');
	// };

	// if (document.title.indexOf('Madeleine')>=0 && $('#background').hasClass('artist-page')) {
	// 	$('#background').addClass('background-mp');
	// };

	// if (document.title.indexOf('Roberto')>=0 && $('#background').hasClass('artist-page')) {
	// 	$('#background').addClass('background-rs');
	// };

	// if (document.title.indexOf('Sam')>=0 && $('#background').hasClass('artist-page')) {
	// 	$('#background').addClass('background-sy');
	// };





});

function pressKit(artistAttribute) {
	$(artistAttribute).slideToggle();
	if ( $(this).hasClass('artist-button-on') ) {
		$(this).removeClass('artist-button-on');	//turns the button bar off
		$(this).find('p').toggle();             	//changes the button bar instruction text to the off state
	} else{
		$(this).addClass('artist-button-on');	//turns the button bar on
		$(this).find('p').toggle();          	//changes the button bar text to "close"
	};

}


function changeArtist(nextPositionClass) {
	$('.roster-overlay h2 a').each(function(i) {
		$(this).removeClass('roster-active');
		$('.roster-overlay p').slideUp();
	});
	$(nextPositionClass).addClass('roster-active');  //might be smarter to keep this here and remove it from autoShow()
	$(nextPositionClass).parent().next('p').slideDown();
}

function changeImage(sectionActive) {
	$('section.category-roster').each(function (i) {
		$(this).addClass('off').fadeOut(1000, "linear");
	});
	$(sectionActive).removeClass('off').fadeIn(1000, "linear");
}

function autoShow(rosterList, autoShowTimeoutId) {
	if ($('.roster-overlay h2 a').hasClass('roster-active')) {
		var currentPosition         = $('.roster-active').attr('alt');					//get the current position from alt, e.g., 0, 1
		var currentPositionClass    = 'pos-'+currentPosition;
		var nextPosition            = parseInt(currentPosition)+1;
		var nextPositionClass       = '.pos-'+nextPosition;
		var currentlyActive         = $('.roster-active').attr('class').split(' ')[0];	//get the first class listed on the active roster
		var currentlyActiveSelector = '.'+currentlyActive;
		// clearTimeout(autoShowTimeoutId);



		$('.roster-active').removeClass('roster-active');
		$(nextPositionClass).addClass('roster-active');


		if (nextPosition == rosterList.length) {	//reset the loop and start at the beginning of the roster list
		    nextPositionClass = '.pos-0';
			var sectionActive = 'section.'+$(nextPositionClass).attr('class').split(' ')[0];

			changeArtist(nextPositionClass);
			$('.highlight-arrow').animate({top: arrowStart}, 'fast', function() {
				autoShowTimeoutId = setTimeout(function() {
					autoShow(rosterList);
				},delayRegular);
			});

			if ( $(sectionActive).hasClass('off') ) {
				changeImage(sectionActive);
			};

		} else {
			var sectionActive = 'section.'+$(nextPositionClass).attr('class').split(' ')[0];

 			changeArtist(nextPositionClass);
 			$('.highlight-arrow').animate({top: '+='+arrowSpace}, '500', function() {
 				autoShowTimeoutId = setTimeout(function() {
 				    autoShow(rosterList);	//use a callback to itself to keep the animation running
 				},delayRegular);         	//the delay between animations
 			});
 			if ( $(sectionActive).hasClass('off') ) {
 				changeImage(sectionActive);
 			};
 		};
 	};
 	$('.roster-overlay h2 a').click(function(e) {
 	    e.preventDefault();
 		clearTimeout(autoShowTimeoutId);			//used to stop the current running function, (not sure why but it is needed twice)
 	});
 	return autoShowTimeoutId;
}

function clickArtist(rosterList) {
	// Respond to clicking the Artist Name by showing the selected artist and hiding all the rest
	var rosterActive    = $(this).attr('class').split(' ')[0];
	var sectionActive   = 'section.'+rosterActive;
	var currentPosition = $(this).attr('alt');	//Get the current position of the clicked item


	//Highlight the selected roster Artist and show/hide the press kit links
	if ( !$(this).hasClass('roster-active') ) {
		$('.roster-overlay h2 a').each(function(i) {
			$(this).removeClass('roster-active');
			$('.roster-overlay p').slideUp();
		});
		$(this).addClass('roster-active');
		$(this).parent().next('p').slideDown();

		//move the selected arrow to the selected item

		// var moveTo = (parseInt(currentPosition)+1)*arrowSpace;
		var moveTo = (parseInt(currentPosition)*arrowSpace)+29;
		$('.highlight-arrow').animate({top: moveTo}, 'fast');
	};

	//Fade in and out the roster image and description
	if ( $(sectionActive).hasClass('off') ) {
		changeImage(sectionActive);
	};
}

