$(document).ready(function() {
	
	//Lose margin-right off of last inline list items
// Now done in CSS
//	$('.header .nav ul li').last().addClass('last-item');
//	$('.footer .nav ul li').last().addClass('last-item');
//	$('#pushes ul li').last().addClass('last-item');


	 // Define cases here where NOT to display the jQuery onpage popup
       var footeranimate = true;
       $.each($.browser, function(i, val) {
           if(i=="msie" && jQuery.browser.version.substr(0,1)=="8") {
               footeranimate = false;
           }
       });
	
	//Solutions page accordian
	$('ul.solutions li .expanded-content').hide();
	

	$('ul.solutions li>p a').click(function(){		
		if (footeranimate) {
			$('ul.solutions li .expanded-content').slideUp(500);
		} else {
			$('ul.solutions li .expanded-content').hide();
		}

		if ($(this).hasClass('expanded')) {
			$('ul.solutions li>p a').removeClass('expanded');
		} else {
			$('ul.solutions li>p a').removeClass('expanded');
			
			if (footeranimate) {
				$(this).parent().next().slideDown(500);
			} else {
				$(this).parent().next().show();
			}
				
			$(this).addClass('expanded');
		}
		if (!footeranimate) $('.footer').toggleClass('die8');

		return false;
	});

	//Lose right margin from every 5th photo on the team pages
//	$('ul.staff-photos li:nth-child(5n)').addClass('last-item');
	
	//Slideshow on homepage
	$('#slideshow ul').cycle({
		fx:'fade',
		speed: footeranimate ? 3000 : 0,
		random: 1
	});
	
	//Slideshow in sidebar
	$('ul.sidebar-slideshow').cycle({
		fx:'fade', 
		speed: 3500,
		random: 1
	});
	
	//Form default swap out
    $("input, textarea").focus(function () {
    	if (this.value == this.title) this.value = "";
    	if (this.value == this.title) this.value = "";
        $(this).css('color','#333');
    }).blur();
    $("input, textarea").blur(function () {
    	if (this.value == "") {
    		$(this).val(this.title).css('color','#999');
		}
    }).blur();


    //Validate the form
    $('form').submit(function(event) {
	 //   event.preventDefault();

		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var alert_error = false;

		// Loop over all fields that have an asterisk '*' in their title attibute
		// will work for ANY tag: input, textarea, select, etc.
		$(this).find('[title*="*"]').each(function() {

			// Check for the value being empty or being the title attribute (i.e. the default text)
			if ($(this).val() == "" || $(this).val() == $(this).attr('title')) {
				// Make a more readable field name ("Your name*" becomes "your name")
				var this_title = $(this).attr('title').toLowerCase().replace('*', '');
				alert_error = 'Please enter your '+$(this).attr('title');

			// Check email validity
			} else if ($(this).is('[title*=email]')) {
				if (!emailRegEx.test($(this).val())) {
					alert_error = 'Please enter a valid email address';
				}
			}

			// If an error has been found...
			if (alert_error) {
				alert(alert_error); // tell the user
				$(this).focus(); // focus the erroneous field
				return false; // break ...out of the each loop, no need to show more errors
			} 
		});

		// Stop form submitting if there are errors
		if (alert_error) { 
			return false;
		} else {
			ajax_get_results();
			return false;
		}
	});
	
	//Initialise overlay states
	$('#darkener').hide();
	$('.overlay-wrap').hide();
	$('.staff-overlay-wrap').hide();
	
	//Open overlay states
	$('#login a').click(function(){
		$('#darkener').fadeIn(500);
		$('.overlay-wrap').fadeIn(500);
	});
	
	$('ul.staff-photos li').click(function(){
		$('#darkener').fadeIn(500);
		$('.staff-overlay-wrap').fadeIn(500);
		
		//Populate popup
		$('.staff-overlay-wrap h2 .name').html($(this).find('.name').html());
		$('.staff-overlay-wrap h2 .position').html($(this).find('.position').html());
		$('.staff-overlay-wrap img.main-image').attr('src',$(this).find('img.main-image').attr('src'));
		$('.staff-overlay-wrap img.second-image').attr('src',$(this).find('img.second-image').attr('src'));
		$('.staff-overlay-wrap .biog').html($(this).find('.biog').html());
		
		return false;
	});
	
	//Close overlay states
	$('a#close, #darkener').click(function(event){
		$('#darkener').fadeOut(500);
		$('.overlay-wrap').fadeOut(500);
		$('.staff-overlay-wrap').fadeOut(500);
		return false;
	});
	$('#darkener *').click(function(event) {
	    event.stopPropagation();
	});
	
	// Tab selections
	if ($('body').hasClass('departments') || $('body').hasClass('operations')) {
		//alert('test');
		$('.staff-photos li').hide();
	}
	
	$('.staff-tabs li a').click(function(){
		$('.staff-tabs li').removeClass('selected');
		$(this).parent().addClass('selected');
		var classes_to_select = $(this).html().replace(/ /g, '.');
		if (footeranimate) {
			$('.staff-photos li').hide(500);
			$('.staff-photos li.'+classes_to_select).show(500);
		} else {
			$('.staff-photos li').hide();
			$('.staff-photos li.'+classes_to_select).show();
			$('.footer').toggleClass('die8');
		}
		return false;
	})
	$('body.departments .staff-tabs li:first a, body.operations .staff-tabs li:first a').click();
	
	//Login Form selection
	$('#music_login').hide();
	
	$('ul.staff-tabs li.music_selection a').click(function(){
		$('#cam_login').fadeOut(250,function(){
			$('#music_login').fadeIn(250);
		});
	});
	
	$('ul.staff-tabs li.cam_selection a').click(function(){
		$('#music_login').fadeOut(250,function(){
			$('#cam_login').fadeIn(250);
		});
	});
	
	
	//End jQuery sweetness

	
});

