var interval;
var clientInterval;
var projectImagesInterval;
var selectedProjectImage;

function slideSwitch() {
	var text = $('#banner .cont').find('.active').attr('alt');
	$('#banner .banner-title').html(text);
    $('#banner .cont div.active').animate({
		"opacity": 0.0
	},500,function(){
		$(this).removeClass('active');
	}).next('div').addClass('active').animate({
		"opacity": 1.0
	},500).prev().appendTo('#banner .cont');
}
$(document).ready(function(){

// Stop animation when tab isn't active - stops weird errors
	$(window).focus(function() {
		$.fx.off = false;
	});
	$(window).blur(function() {
		$.fx.off = true;
	});
	$(function() {
		interval = setInterval( "slideSwitch()", 5000 );
	});
// Login dropdown
	$('#login-tab').click(function(){
		if ( $('#login-form').css('height') == '5px' ){
			$('#login-form').animate({'height': '200px'});
		} else {
			$('#login-form').animate({'height': '5px'});
		}
	});
	
// Clients slider
	var $clients = $('#clients .client');
	// Start with a random one
	var currentClient = Math.floor(Math.random() * $('#clients .client').length);
	$($('#clients .client')[currentClient]).show();
	// Set an interval to switch to the next one
	clientInterval = setInterval(function(){
		$($('#clients .client')[currentClient]).fadeOut(1000);
		
		/* Commented out: Code for random slide
		var newClient = currentClient;
		while (newClient == currentClient) {
			newClient = Math.floor(Math.random() * $('#clients .client').length);
		}
		currentClient = newClient;*/
		
		// Get next image index
		currentClient = (currentClient == $('#clients .client').length-1)
			? 0
			: currentClient + 1;
		
		$($('#clients .client')[currentClient]).fadeIn(1000);
	}, 5000);

// Project images slider
	var $projectImagesSlider = $('#latest-project-photos');
	var $projectImages = $('#latest-project-photos img');
	var $projectText = $('.latest-project-info .slide-text');
	var $projectImagesDots = $('#latest-project-photos .slider-dots li');
	// Get active slide
	selectedProjectImage = $('#latest-project-photos img').index( $('#latest-project-photos img').filter('.active') );
	// Set active dot
	$('#latest-project-photos .slider-dots li').eq(selectedProjectImage).addClass('active');
	// Set an interval to switch to the next image
	function swapToNextProjectImage(){
		$($('#latest-project-photos img')[selectedProjectImage]).fadeOut(800);
		$($('.latest-project-info .slide-text')[selectedProjectImage]).fadeOut(800);
		$('#latest-project-photos .slider-dots li').eq(selectedProjectImage).removeClass('active');
		
		// Get next image index
		selectedProjectImage = (selectedProjectImage == $('#latest-project-photos img').length-1)
			? 0
			: selectedProjectImage + 1;
		
		$($('#latest-project-photos img')[selectedProjectImage]).fadeIn(800);
		$($('.latest-project-info .slide-text')[selectedProjectImage]).fadeIn(800);
		$('#latest-project-photos .slider-dots li').eq(selectedProjectImage).addClass('active');
	}
	projectImagesInterval = setInterval(swapToNextProjectImage, 8000);
	// Change image when user clicks a dot
	$('#latest-project-photos .slider-dots li').live("click",function(){
		var index = $(this).index();
		
		// Do nothing if this is the current slide
		if (index == selectedProjectImage) {
			return;
		}
	
		// Clear current interval
		clearInterval(projectImagesInterval);
		
		// Hide previous text and image
		$('#latest-project-photos .slider-dots li').eq(selectedProjectImage).removeClass('active');
		$($('#latest-project-photos img')[selectedProjectImage]).stop(true, true).fadeOut(800);
		$($('.latest-project-info .slide-text')[selectedProjectImage]).stop(true, true).fadeOut(800);
		
		// Get next image index
		selectedProjectImage = index;
		
		// Show new text and image
		$($('#latest-project-photos img')[selectedProjectImage]).stop(true, true).fadeIn(800);
		$($('.latest-project-info .slide-text')[selectedProjectImage]).stop(true, true).fadeIn(800);
		$('#latest-project-photos .slider-dots li').eq(selectedProjectImage).addClass('active');
		
		// Set interval again
		projectImagesInterval = setInterval(swapToNextProjectImage, 8000);
	});
	
// Dropdown text
	$('.dropTitle').click(function(){
		var wasActive = false;
		
		if ($(this).hasClass('actives')){
			wasActive = true;
		}
		$('.actives').children('.expand').attr('src', '/themes/mackinnon/img/plus.png');
		$('.actives').removeClass('actives').siblings('.dropCont').slideUp();
		
		if (!wasActive) {
			$(this).addClass('actives');
			$(this).siblings('.dropCont').slideToggle();
			$(this).children('.expand').attr('src', '/themes/mackinnon/img/minus.png');
		}
	});
	
});
