var slides_current = 0;
var slides_total = 0;
var slides_timer = false;
var slides_interval = 5000;
var slides_transition = 500;

var target_container = $('#slideshow');
var target_slides = $('#slideshow #slides');
var target_slides_nav = $('#slideshow #slides-nav');

function start_slideshow() {
	if( slides_timer == false ) {
		slides_timer = setInterval(function() {
			if( slides_current == slides_total ) {
				index = 0;
			} else {
				index = slides_current+1;
			}

			swap_slide(index);
		}, slides_interval);
	}
}

function stop_slideshow() {
	clearInterval(slides_timer);
	slides_timer = false;
}

function swap_slide( index ) {
	if( slides_current == index ) { return; }
	if( index < 0 ) { index = slides_total; }
	if( index > slides_total ) { index = 0; }

	// deactivate current slide
	target_slides_nav.find('li:eq(' + slides_current + ')').removeClass('active');
	target_slides.find('li:eq(' + slides_current + ')').fadeOut(function() {
		$(this).removeClass('active');
	});

	// activate target slide
	target_slides_nav.find('li:eq(' + index + ')').addClass('active');
	target_slides.find('li:eq(' + index + ')').fadeIn(function() {
		$(this).addClass('active');
	});

	slides_current = index;
}

$(document).ready(function() {

	// get number of slides
	slides_total = parseInt( target_slides.find('li').length - 1 );

	// get currently active slide
	slides_current = parseInt(target_slides.find('li.active').index());

	// start the show!
	start_slideshow();

	// hook pagination controls
	target_slides_nav.find('li').click(function(e) {
		e.preventDefault();
		stop_slideshow();
		swap_slide( $(this).index() );
		start_slideshow();
	});
});


// Content Tab Pages
$(document).ready(function() {

	$('#page-contact #content #content-tabs li a').click(function() {
		if( $(this).parent().hasClass('active') ) { return; }

		var page_index = $(this).parent().index();

		$(this).parent().siblings().removeClass('active');
		$(this).parent().addClass('active');

		$('#content div.page').fadeOut(250, function() {
			$(this).removeClass('active');
		});

		$('#content div.page:eq(' + page_index + ')').fadeIn(250, function() {
			$(this).addClass('active');
		});

		return false;
	});

	$('#page-contact #content div.page ul.page-numbers li a').click(function() {
		if( $(this).parent().hasClass('active') ) { return; }

		var contact_index = $(this).parent().index();

		$(this).parent().siblings().removeClass('active');
		$(this).parent().addClass('active');

		$(this).closest('.page').find('.contact').removeClass('active');
		$(this).closest('.page').find('.contact:eq(' + contact_index + ')').addClass('active');

		return false;
	});
	
	$('#page-contact #content div.page').each(function() {
		if($(this).find('.contact').length == 1) {
			$(this).find('p.page-controls').hide();
		} else {
			$(this).find('p.page-controls').find('a:eq(0)').hide();
		}
	});
	
	$('#page-contact #content div.page p.page-controls a').click(function() {
		var link_index = $(this).index();
		var contact_total = $(this).closest('.page').find('ul.page-numbers li').length - 1;
		var contact_index = $(this).closest('.page').find('ul.page-numbers li.active').index();

		if( link_index == 0 ) {
			contact_index = contact_index - 1;
		}

		if( link_index == 1 ) {
			contact_index = contact_index + 1;
		}

		if( contact_index > contact_total ) { contact_index = 0; }
		if( contact_index < 0 ) { contact_index = contact_total; }
		
		if( contact_index == 0 ) {
			$(this).closest('.page').find('p.page-controls a:eq(0)').hide();
		} else {
			$(this).closest('.page').find('p.page-controls a:eq(0)').show();
		}
		
		if( contact_index == contact_total ) {
			$(this).closest('.page').find('p.page-controls a:eq(1)').hide();
		} else {
			$(this).closest('.page').find('p.page-controls a:eq(1)').show();
		}

		$(this).closest('.page').find('.contact').removeClass('active');
		$(this).closest('.page').find('.contact:eq(' + contact_index + ')').addClass('active');

		$(this).closest('.page').find('ul.page-numbers li').removeClass('active');
		$(this).closest('.page').find('ul.page-numbers li:eq(' + contact_index + ')').addClass('active');

		return false;
	});
});

// Products Page
$(document).ready(function() {

	$('#page-products #content-tabs li').hover(
		function() { $(this).find('.sub-menu').show(); },
		function() { $(this).find('.sub-menu').hide(); }
	);

	$('#page-products #content .product .images-nav li a').click(function() {
		if( $(this).parent().hasClass('active') ) { return false; }

		var image_index = $(this).parent().index();

		$(this).closest('li').siblings().removeClass('active');
		$(this).closest('li').addClass('active');

		$(this).closest('.product-image').find('.images li').removeClass('active');
		$(this).closest('.product-image').find('.images li:eq(' + image_index + ')').addClass('active');

		return false;
	});

	$('#page-products #content #content-tabs li a').click(function() {
		if( $(this).parent().hasClass('active') ) { return false; }

		var page_index = $(this).parent().index();

		$(this).parent().siblings().removeClass('active');
		$(this).parent().addClass('active');

		$('#content div.page').removeClass('active');
		$('#content div.page:eq(' + page_index + ')').addClass('active');
		
		$('#content div.page:eq(' + page_index + ') .product').removeClass('active');
		$('#content div.page:eq(' + page_index + ') .product:eq(0)').addClass('active');
		
		// update quote
		var product_quote = $('div.page').eq(page_index).find('.product').find('.product-quote');
		var quote = product_quote.find('.quote').html();
		var attribute = product_quote.find('.attribute').html();
		
		$('#featured').find('.quote').html(quote);
		$('#featured').find('.attribute').html(attribute);
		
		return false;
	});

	$('#page-products #content #content-tabs ul.sub-menu li a').click(function() {
		//if( $(this).parent().hasClass('active') ) { return false; }

		var page_index = $(this).parent().parent().parent().index();
		var product_index = $(this).parent().index();
		
		$(this).parent().parent().parent().siblings().removeClass('active');
		$(this).parent().parent().parent().addClass('active');

		$(this).parent().siblings().removeClass('active');
		$(this).parent().addClass('active');

		$('#content div.page').removeClass('active');
		$('#content div.page:eq(' + page_index + ')').addClass('active');
		
		$('#content div.page:eq(' + page_index + ') .product').removeClass('active');
		$('#content div.page:eq(' + page_index + ') .product:eq(' + product_index + ')').addClass('active');
		
		// update quote
		var product_quote = $('div.page').eq(page_index).find('.product').eq(product_index).find('.product-quote');
		var quote = product_quote.find('.quote').html();
		var attribute = product_quote.find('.attribute').html();
		
		$('#featured').find('.quote').html(quote);
		$('#featured').find('.attribute').html(attribute);
		
		return false;
	});

});

// Shadowbox Setup
// Shadowbox.init({ skipSetup:true });
// $(document).ready(function() {
// 	Shadowbox.setup('.product ul.videos a');
// });

// Shadowbox.init();

// Quotes Slideshow
$(document).ready(function() {
	if( $('#featured .entry').length > 1 ) {
		$('#featured .entry:gt(0)').hide();
		setInterval(function() {
			$('#featured .entry:eq(0)').fadeOut().next().fadeIn().end().appendTo('#featured');
		}, 3000);
	}
});


