/**
 * @author Dave Shepard
 */

var slideShow = {
	delay: 5000,
	container: 'gallery_container',
	transition: function(){
		if(slideShow.elements[slideShow.current] == slideShow.elements[0]) {
			// If this is the last element
			$(slideShow.elements[slideShow.last]).fadeIn("slow",function(){
				slideShow.elements.show();
				slideShow.current = slideShow.last;
				setTimeout("slideShow.transition()",slideShow.delay);
			});
		} else {
			// If this is not the last element
			$(slideShow.elements[slideShow.current]).fadeOut("slow",function(){
				slideShow.current--;
				setTimeout("slideShow.transition()",slideShow.delay);
			});
		}
	}
}

var teamSlider = {
	delay: 3000,
	currentSlide: 1,
	nextSlide: 0,
	total: 0,
	next: function(){
		
		///// Update to reveal first slide again after the 2nd one shows. Do not hide slide 1
		if(teamSlider.currentSlide < teamSlider.total){
			teamSlider.nextSlide = teamSlider.currentSlide + 1;	
		}else{
			teamSlider.nextSlide = 1;	
		}
		$('li.teamSlide').removeClass("active");
		$('#slide_'+teamSlider.nextSlide).addClass("active").fadeIn("slow",function(){
			$('#slide_'+teamSlider.currentSlide).hide();
			teamSlider.currentSlide = teamSlider.nextSlide;
			$('#team_slides li.active').animate({
				opacity: 1.0
				},teamSlider.delay,function(){
					teamSlider.next();
				}
			);
		});
		
	},
	init: function(){
		if($('#team_slides').length){
			teamSlider.total = $('li.teamSlide').length;
			$('#team_slides li.active').animate({
				opacity: 1.0
			},teamSlider.delay,function(){
				teamSlider.next();
			});
		}
	}
}

$(document).ready(function(){
	if($('#'+slideShow.container)) {
		slideShow.elements = $('#'+slideShow.container+' img');
		slideShow.last = slideShow.elements.length-1;
		slideShow.current = slideShow.last;
		setTimeout("slideShow.transition()",slideShow.delay);
	}

	$('ul.sf-menu').superfish({
        delay:       250,                            // one quarter second delay on mouseout 
        animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
        speed:       'fast',                          // faster animation speed 
        autoArrows:  false,                           // disable generation of arrow mark-up 
        dropShadows: false                            // disable drop shadows 
    });
	
	if($('#plans a').length > 0) {
		$('#plans a').click(function(){$('applet').hide();});
	}
	
	$('form.formValidate').validate();
	
	if($('#contact_form')) {
		$('#contact_form').validate();
	}
	
	if($('#home_page_background_1')) {
		$('#home_page_content_shadow').css('opacity',0);
		homePage.init();
	}
	teamSlider.init();
	//$.localScroll();
});


var homePage = {
	// [str] IDs of background images
	bgObjects: [
		'home_page_background_1',
		'home_page_background_2',
		'home_page_background_3'
	],
	// [int] Delay between each background image
	bgDelay: 5,
	// [int] Duration of cross-fade for background image
	bgDuration: 1,
	// [str] IDs of objects to animate
	objects: [
		'home_page_small_image_1',
		'home_page_small_image_2',
		'home_page_small_image_3',
		'home_page_small_image_4',
		'home_page_small_image_5',
		'home_page_small_image_6'
	],
	// [int] Start positions of objects (in pixels)
	startPos: [
		{x:537,y:82},
		{x:590,y:116},
		{x:569,y:97},
		{x:522,y:0},
		{x:597,y:98},
		{x:609,y:16}
	],
	// [int] End positions of objects (in pixels)
	endPos: [
		{x:119,y:82},
		{x:312,y:116},
		{x:0,y:97},
		{x:269,y:0},
		{x:219,y:98},
		{x:175,y:16}
	],
	// [int] Delay in seconds of each animated object from initialization of animator
	delays: [0,0.6,0.8,0.9,1.4,1.5],
	duration: 0.75,						// [int] Duration of animations in seconds
	easing: 'easeOutSine',				// [str] Easing value, see jquery.easing.1.2.js for easing options
	animateSmallImages: function(){
		for(var i=0;i<this.objects.length;i++){
			$('#'+this.objects[i])
				// Set initial position "off screen"
				.css({
					left: this.startPos[i].x,
					top: this.startPos[i].y
				})
					// Use opacity animation to simulate a delay
					.animate({
						'opacity': 1
					},{ duration: this.delays[i]*1000 })
						// Animate image
						.animate({
							left: this.endPos[i].x+'px'
						},{
							duration: this.duration*1000,
							'easing': this.easing
						});
		}
	},
	bgCurrent: 0,						// [int] Current Background - used internally
	initBackgroundObjects: function(){
		this.bgObjects.reverse();
		this.bgObjects.pop();
		this.bgObjects.push(this.bgObjects[0]);
	},
	crossFadeBackground: function(){
		var endOpacity = (this.bgCurrent == this.bgObjects.length-1 ? 1 : 0);
		
		$('#home_page_content_shadow')
			.animate({
				left: 0
			},{ duration: this.bgDelay*1000 })
				.animate({
					'opacity': endOpacity == 1 ? 0 : 0.4
				}, this.bgDuration*1000);

		$('#'+this.bgObjects[this.bgCurrent])
			.animate({
				left: 0
			},{ duration: this.bgDelay*1000 })
				.animate({
					'opacity': endOpacity
				},
					this.bgDuration*1000,
					'linear',
					function(){
						if(homePage.bgCurrent == homePage.bgObjects.length-1) {
							$(homePage.bgObjects.join(",#").substr(1)).css('opacity',1);
							homePage.bgCurrent = 0;
						} else {
							homePage.bgCurrent++;
						}
						homePage.crossFadeBackground();
					}
				)
	},
	init: function(){
		this.initBackgroundObjects();
		this.crossFadeBackground();
		this.animateSmallImages();
	}
}
