/*********************
Bob Garfield JS V1
*********************/

$(document).ready(function()
{
	var videoSrc;
	
	function myCarousel_initCallback(carousel, state)
	{
		$('a#videoNext').click(function(){
			carousel.next();
			$('a#videoPrevious').removeClass('disabled');
			$(this).addClass('disabled');
		});
		
		$('a#videoPrevious').click(function(){
			carousel.prev();
			$('a#videoNext').removeClass('disabled');
			$(this).addClass('disabled');
		});		
	}
	
	/* Video Callouts */
	$('#videoCallouts').jcarousel({
		scroll: 1,
		visible: 4,
		initCallback: myCarousel_initCallback
	});
	
	
	/* Navigation List Active State*/
	$('div#navigation ul li').hover(function(e){
		$(this).addClass('active');
	}, function(e){
		$(this).removeClass('active');
	});
	
	/*
	if($('div').hasClass('scroll-pane'))
	{
		$('.scroll-pane').jScrollPane();
	}
	*/
	
	if($('body').attr('id') == "video")
	{	
		var videoName;
		videoName = window.location.search.substring(1);
		videoName = videoName.split("=");		
		
		videoName = videoName[1];
		if(videoName == undefined)
		{
			videoName = "video1";
		}
		getDynamicVideo(videoName);
		
		$('ul#videoCallouts a.dynamicVideo').click(function(){
			videoName = $(this).attr('title');
			getDynamicVideo(videoName);
		});
	}
	
	function getDynamicVideo(videoName)
	{
		var flashvars = {};
		var params = {wmode:"transparent"};
		var attributes = {};
		var videoSrc;

		switch(videoName)
		{
			case "video1":
			videoSrc = "http://vimeo.com/moogaloop.swf?clip_id=6873200&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1&autoplay=1";
			break;
			
			case "video2":
			videoSrc = "http://www.youtube.com/v/IXG8zaB4eGw&hl=en_US&fs=1&autoplay=1";
			break;
			
			case "video3":
			videoSrc = "http://www.youtube.com/v/ImV16Fez_P4&hl=en_US&fs=1&autoplay=1";
			break;
			
			case "video4":
			videoSrc = "http://www.youtube.com/v/6aair22dNz8&hl=en_US&fs=1&autoplay=1"
			break;
			
			case "video5":
			videoSrc = "http://www.youtube.com/v/1a2R8wKfmHM&hl=en_US&fs=1&autoplay=1";
			break;
			
			default:
			videoSrc = "http://vimeo.com/moogaloop.swf?clip_id=6873200&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1&autoplay=1";
		}
		
		function hideLoader(e)
		{
			$('#loadingImage').hide();
		}
		
		swfobject.embedSWF(videoSrc, "bobVideo", "639", "380", "9.0.0", videoSrc, flashvars, params, attributes, hideLoader);
	}
	
	$('input#submitBtn').click(submitForm);
	
	function submitForm()
	{	
		var firstName = $('#firstName').val();
		var lastName = $('#lastName').val();
		var email = $('#email').val();
		var phone = $('#phone').val();
		var comments = $('#comments').val();
		var mediaContact = $('#mediaType :selected').val();
		
		var regexPhone = new RegExp("^(\\(?[0-9]{3}\\)?)?\\-?[0-9]{3}\\-?[0-9]{4}$");
		var regexEmail = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
		
		var pass;
		
		if(firstName == "")
		{
			$('#firstNameError .error').show();
			pass = false;
		}
		else
		{
			$('#firstNameError .error').hide();
			pass = true;
		}
		
		if(lastName == "")
		{
			$('#lastNameError .error').show();
			pass = false;
		}
		else
		{
			$('#lastNameError .error').hide();
			pass = true;
		}
		
		if(email != "" && regexEmail.test(email))
		{
			$('#emailError .error').hide();			
			pass = true;
		}
		else
		{
			$('#emailError .error').show();
			pass = false;
		}
		
		if(phone != "")
		{
			if(regexPhone.test(phone))
			{
				$('#phoneError .error').hide();
				pass = true;
			}
			else
			{
				$('#phoneError .error').show();
				pass = false;
			}
		}

		if(pass == true)
		{
			document.contactBob.action ="contactForm.php"
		}
		else
		{

			return false;
		}
		
	}
	
	theRotator();
	
});

function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});

	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};
