var subnav = activateNav('.subnav', true);

if((subnav == 'reviews.php') || (subnav == 'reviews.php#')){
	var count = 0;
	$('#arrow_next').click(function() {
		count = slideNext('#reviews_window', count, 3);
		return false;
	});
	$('#arrow_prev').click(function() {
		count = slidePrev('#reviews_window', count, 3);
		return false;
	});
}
//(subnav == 'performers.php') || (subnav == 'performers.php#') || (subnav == "performers.php?tour=1") || (subnav == "performers.php?tour=1#")

if(subnav.match(/performers/)) {
	/*$(document).ready(function() {
		iWidth	= $slider.children(".performers_group a:first-child").children("a").width() + 4;
		$a		= $slider.children(".performers_group").children("a");
		num_a	= $a.length;
		pWidth	= num_a * iWidth;
		$slider.css({ width: pWidth });
	});*/
	performerSlide = 0;
	var slideCount = $('.performers_group').children().length;
	$('.performers_controls').click(function(clicked) {
		var triggers = $('.performers_controls').children();
		
		// make sure you're clicking the anchor or the image - id is on anchor
		if(clicked.target.tagName == "IMG" || clicked.target.parentNode.tagName == "A") {			
			var targetID = clicked.target.tagName == 'IMG' ? clicked.target.parentNode.id : clicked.target.id;
		} else {
			return false;
		}
		
		if(targetID == 'arrow_right' && (performerSlide + 1) < slideCount) {
			// if I could express screaming at this stupid slider through comments, i would.
			// instead, enjoy this hack. -mway 30 oct 2008
			$children	= $('.performers_group').children();
			$slider		= $(".performers_slider");
			margin		= 4;
			// get the width of each element including its pseudomargins (because apparently we use whitespace to space elements)
			iWidth		= $slider.children(".performers_group a:first-child").children("a").width() + margin;
			// get the last headshot in the slider
			$last		= $($children.get($children.length - 1));
			// get the position of the last headshot minus its right pseudomargin relative to the wrapper
			last_offset	= $last.offset().left + iWidth - $(".performers_list_wrapper").width() - (margin - 1);
			// get the slider offset
			slide_offset	= $(".performers_slider").offset().left;
			// they should be equal, but with how this slider works, who knows whether they'll always be, so...
			if(Math.abs(last_offset - slide_offset) < 10) { return; }
			
			
			performerSlide = performerSlide + 1;
			jumpSlide('.performers_slider', performerSlide, 'horizontal');
		} 
		else if(targetID == 'arrow_left' && (performerSlide - 1) >= 0) {
			performerSlide = performerSlide - 1;
			jumpSlide('.performers_slider', performerSlide, 'horizontal');
		}
		
		clicked.preventDefault();
	});
	
	
	$('.performers_group').click(function(clicked) {
		var triggers = $('.performers_group').children();
		var targetIndex = null;

		// make sure you're clicking the anchor or the image - id is on anchor
		if(clicked.target.tagName == "IMG" || clicked.target.parentNode.tagName == "A") {
			var targetID = clicked.target.tagName == 'IMG' ? clicked.target.parentNode.id : clicked.target.id;
		} else {
			return false;
		}

		// ok this is kind of stupid but necessary. We are going to loop 
		// through the li items to find the index of the array item by
		// compairing IDs.
		for(x = 0; x < triggers.length; x++) {
			if(triggers[x].id == targetID) targetIndex = x; 
		}

		// move slide
		jumpSlide('.performer_headshot', targetIndex, 'horizontal');
		jumpSlide('.performer_details', targetIndex, 'vertical');

		// change classnames
		triggers.removeClass('selected');
		$(triggers[targetIndex]).addClass('selected');

		// prevent click return
		clicked.preventDefault();
	});
	
}

/**
 *
 *
 */
function slideNext(slideWindow, count, visible) {
	var slideWindow = $(slideWindow);
	var slider = slideWindow.children('div');
	var slideOffset = slider.position().top;
	var slides = slider.children('div');
	
	// prevent off-track scrolling
	var trackCount = parseInt(count) + parseInt(visible);
	if(trackCount > slides.length) return count;
	
	// offset slider
	var newHeight = 0;
	var currentSlide = $(slides[count]); // 0 index
	var nextSlide = $(slides[count+1]);
	var currentSlideHeight = currentSlide.outerHeight();
	var newOffset = nextSlide.position().top;
	slider.animate({top: -newOffset+'px'}, {'easing': 'swing', 'duration': 350});

	// calculates height for next visible slides
	// for(x = count; x < (count + visible); x++) {
		// newHeight = newHeight + $(slides[x]).outerHeight();
	// }
	// slideWindow.animate({height: newHeight+'px'}, {'easing': 'swing', 'duration': 350});
	
	return count + 1;
}


/**
 *
 *
 */
function slidePrev(slideWindow, count, visible) {
	var slideWindow = $(slideWindow);
	var slider = slideWindow.children('div');
	var slideOffset = slider.position().top;
	var slides = slider.children('div');

	// prevent off-track scrolling
	var trackCount = parseInt(count) + parseInt(visible);
	if(trackCount == 0) return count;

	// offset slider
	var newHeight = 0;
	var currentSlide = $(slides[count]); // 0 index 
	var previousSlide = $(slides[count-1]);
	var currentSlideHeight = currentSlide.outerHeight();
	var newOffset = previousSlide.position().top;
	if(newOffset<0) return count;
	slider.animate({top: -newOffset+'px'}, {'easing': 'swing', 'duration': 350});

	// calculates height for next visible slides
	// for(x = count; x < (count + visible); x++) {
		// newHeight = newHeight - $(slides[x]).outerHeight();
	// }
	// slideWindow.animate({height: newHeight+'px'}, {'easing': 'swing', 'duration': 350});

	return count - 1;
}


/**
 *
 *
 */
function activateNav(nav, direct) {
	// split url and get page (or lack of page - index)
	var url = document.location.href;
	var urlSplit = new Array();
	urlSplit = url.split('/');
	var page = urlSplit[urlSplit.length-1];

	// catch for root (index)
	if(page == '') page = 'index';

	// detect location of nav items and classify right one
	if(direct) {
		$(nav + ' a[href*='+page+']').addClass('active');
	} else {
		$(nav + ' a[href*='+page+']').parent().addClass('active');
	}
	return page;
}

/**
 * jumpSlide @function
 * 
 * @param {String} slideWindow :: string of 
 * @param {int} slideTarget :: 0-based index integer of the slide to slide to.
 * @param {String} direction :: axis for slide
 */
function jumpSlide(slideWindow, slideTarget, direction) {
	var slider = $(slideWindow).children(0);
	var slide = slider.children()[slideTarget];
	
	// get slide offset - left for horiz, top for vert
	var slideLeft = slide.offsetLeft;
	var slideTop = slide.offsetTop;
	
    if(direction == 'horizontal') slider.animate({left: -slideLeft}, {'easing': 'swing', 'duration': 350});
    if(direction == 'vertical') slider.animate({top: -slideTop}, {'easing': 'swing', 'duration': 350});
}

function videoEmail(url){
	window.open('/sendEmail.php?url='+url,'email','width=408,height=335');
}

function studyGuide(url){
	window.open('/studyguide/'+url+'.php','studyguide','width=700,height=500,scrollbars=1');
}

function privacyPop(){
	window.open('/privacy.php','privacy','width=700,height=500,scrollbars=1');
}

function news(id){
	window.open('/popup.php?id=' + id,'privacy','width=700,height=700,scrollbars=1');
}

function contactPop() {
	window.open('/faq.php','privacy','width=440,height=500,scrollbars=1');
}

function nySeatingChart() {
	window.open('/images/seatingchart.jpg','privacy','width=330,height=680,scrollbars=1');
}