$(document).ready(function() {
	carousel.init();
}); //end document.ready()

var carousel = {
	
	newCarousel:null,
	delay:null,
	busy:false,
	menuBusy:false,
	IDs:new Array("clients", "case_studies", "work", "people", "contact"),
	directionsService:null,
	directionsDisplay:null,
	latlng:null,
	
	init:function() {
		$('#orbit_container').css("display", "block");
		$('#javascript_error').css("display", "none");
		if (navigator.appName.toLowerCase().indexOf("microsoft") < 0)
			$('#star_burst').css("visibility", "visible");
		carousel.setUpCarousel();
		carousel.getHash();
	}, //end init
	
	setUpCarousel:function() {
		carousel.newCarousel = $('#carousel').carousel({
			width:800,
			height:350,
			itemWidth:300,
			itemHeight:200,
			horizontalRadius:320,
			verticalRadius:140,
			resize:false,
			mouseScroll:false,
			mouseDrag:false,
			scaleRatio:0.4,
			scrollbar:false,
			tooltip:true,
			mouseWheel:false,
			mouseWheelReverse:false,
			autoScroll:false,
			autoScrollDelay:0,
			autoScrollDirection:'previous',
			scrollSpeed:carousel.scrollSpeed(),
			itemClick:carousel.clicked,
			itemMouseOver:carousel.pause,
			itemMouseOut:carousel.play,
			continuousScroll:true,
			continuousScrollSpeed:carousel.continuousScrollSpeed(),
			scrollComplete:carousel.completed,
			angle:1
		}); //end carousel
	}, //end setUpCarousel
	
	scrollSpeed:function() {
		if (carousel.isiPad()) {
			return 150;
		} else {
			return 25;
		} //end if
	}, //end speed
	
	continuousScrollSpeed:function() {
		if (carousel.isiPad()) {
			return 5;
		} else {
			return 40;
		} //end if
	}, //end speed
	
	orbitContainerMoveSpeed:function() {
		if (carousel.isiPad()) {
			return 300;
		} else {
			return 1000;
		} //end if
	}, //end speed
	
	showPlanetsSpeed:function() {
		if (carousel.isiPad()) {
			return 500;
		} else {
			return 2000;
		} //end if
	}, //end showPlanetsSpeed
	
	isiPad:function() {
		var ua = navigator.userAgent;
		var isiPad = /iPad/i.test(ua); // || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
		return isiPad;
	}, //end isIpad
	
	clicked:function(e) {
		if (carousel.menuBusy == true)
			return false;
		carousel.menuBusy = true;
		carousel.busy = true;
		carousel.showPage(this.id);
		carousel.hidePlanets(this.id);
		carousel.changeImg(this);
	}, //end clicked
	
	changeImg:function(obj) {
		$(obj).attr("src", "images/nav_" + obj.id + "_on.png");
	}, //end changeImg
	
	hidePlanets:function(id) {
		$('#' + id).show(0);
		$('.tooltip').css("display", "none");
		for (i = 0; i < carousel.IDs.length; i++) {
			if (carousel.IDs[i] != id) {
				elm = $('#' + carousel.IDs[i]);
				//$(elm).delay(1000).hide(100)
				$(elm).animate({
					opacity:'0'
				}, {
					duration:300
				}); //end animate
				$(elm).delay(1000).hide(0);
			} else {
				elm = $('#' + carousel.IDs[i]);
				$(elm).animate({
					opacity:'1'
				}, {
					duration:300
				}); //end animate
				$(elm).delay(1000).show(0);
			} //end if
		} //end for
		
		$('#planet_launch').animate({
			opacity:'0'
		}, {
			duration:500	
		}); //end animate
		$('#planet_launch').delay(1000).hide(0);
	}, //end hidePlanets
		
	showPlanets:function() {
		for (i = 0; i < carousel.IDs.length; i++) {
			$(document.getElementById(carousel.IDs[i])).show();
			$(document.getElementById(carousel.IDs[i])).animate({
				opacity:'1'
			}, {
			  duration:carousel.showPlanetsSpeed(),
			  complete:function() {
				  $('.tooltip').css("display", "inline");
			  } //end complete
			}); //end animate
		} //end for
		
		$('#planet_launch').show(0);
		$('#planet_launch').animate({
			opacity:'1'
		}, {
			duration:1000	
		}); //end animate
	}, //end showPlanets
	
	pause:function() {
		if (carousel.busy == false) {
			$(this).attr("src", "images/nav_" + this.id + "_on.png");
			carousel.newCarousel.stopContinuousScroll();
		} //end if
	}, //end pause
	
	play:function() {
		if (carousel.busy == false) {
			$(this).attr("src", "images/nav_" + this.id + ".png");
			carousel.newCarousel.startContinuousScroll();
		} //end if
	}, //end pause
	
	completed:function() {
		
		$('#orbit_container').animate({
			left:'-110px',
			top:'-150px'
		}, {
			duration:carousel.orbitContainerMoveSpeed(),
			easing:"easeInOutQuint",
			complete:function() {
				carousel.menuBusy = false;
			} //end complete
		}); //end animate
				
		$('#mission_statement').animate({
			opacity:'0'
		}, {
			duration:500
		});
		
		if (navigator.appName.toLowerCase().indexOf("microsoft") >= 0) {
			$('.arrow_container').animate({
				opacity:'0'
			}, {
				duration:500
			});
		} //end if
		
		if (navigator.appName.toLowerCase().indexOf("microsoft") < 0) {
			$('#star_burst').animate({
				'opacity':'1'
			}, {
				duration:2000
			});
		} //end if
	}, //end completed
	
	scrollToItem:function(index) {
		if (carousel.menuBusy == true)
			return false;
		
		carousel.menuBusy = true;
		
		var section = null;
		switch (index) {
			case 0:
				section = 'clients';
				break;
			case 1:
				section = 'case_studies';
				break;
			case 2:
				section = 'work';
				break;
			case 3:
				section = 'people';
				break;
			case 4:
				section = 'contact';
				break;
		} //end switch
		
		carousel.changeImg(document.getElementById(section));
		carousel.showPage(section);
		carousel.hidePlanets(section);
		carousel.busy = true;
		carousel.newCarousel.scrollToItem(index);
	}, //end scrollToItem
	
	resetCarousel:function() {
		window.location.hash = "";
		carousel.showPlanets();
		carousel.resetImages();
		
		$('#orbit_container').animate({
			left:'220px',
			top:'220px'
		}, {
			duration:1000,
			easing:"easeInOutQuint"
		}); //end animate
				
		$('#page_container').animate({
				height:'0px'
		}, {
			duration:500,
			complete:function() {
				$('#page_container').html("");
			} //end complete
		}); //end animate
			
		$('#bottom_seo_copy').animate({
			opacity:'1'
		}, {
			duration:1000
		});
		
		$('#home_address').animate({
			opacity:'1'
		}, {
			duration:1000
		});
		
		setTimeout(function() {
			$('#mission_statement').animate({
				'opacity':'1'
			}, {
				duration:500	
			})
		}, 500);
		
		if (navigator.appName.toLowerCase().indexOf("microsoft") >= 0) {
			setTimeout(function() {
				$('.arrow_container').animate({
					'opacity':'1'
				}, {
					duration:500	
				})
			}, 500);
		} //end if
		
		if (navigator.appName.toLowerCase().indexOf("microsoft") < 0) {
			$('#star_burst').animate({
				'opacity':'0'
			}, {
				duration:1000	
			});
		} //end if
				
		carousel.busy = false;
		setTimeout(function() {carousel.play();}, 300);
	}, //resetCarousel
	
	resetImages:function() {
		for (i = 0; i < carousel.IDs.length; i++) {
			$(document.getElementById(carousel.IDs[i])).attr("src", "images/nav_" + carousel.IDs[i] + ".png");
		} //end for
	}, //end resetImages
	
	showPage:function(page) {
		carousel.setCurrentPage(page);
		
		var height = '';
		switch (page) {
			case 'clients':
				height = '650px';
				break;
			case 'case_studies':
				height = '1200px';
				break;
			case 'work':
				height = '1850px';
				break;
			case 'people':
				height = '2200px';
				break;
			case 'contact':
				height = '700px';
				break;
			default:
				height = '2200px';
		} //end switch
 
		window.location.hash = page;
		
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
			var ieversion = new Number(RegExp.$1); // capture x.x portion and store as a number
			if (ieversion <= 7) {
				var containerHeight = Number(parseInt(height.replace("px", "")) + 300) + "px";
				$('#container').css("height", containerHeight);
			} //end else if
		} //end if
		
		$.ajax({
			beforeSend:function() {
				$('#page_container').animate({
					height:'0px'
				}, {
					duration:300,
					complete:function() { $('#page_container').html(""); }	
				});
			}, //end beforeSend
			url:'sections/' + page + '.html',  
			complete:function(data){
				setTimeout(function() {$('#page_container').append(data.responseText); }, 1000);
			} //end complete
		}); //end ajax
		
		setTimeout(function() {
			$('#page_container').animate({
				height:height
			}, {
				duration:2000,
				easing:"easeInOutQuint",
				complete:function() {
					if (page == "work")
						carousel.prettyPhoto();
					if (page == "contact")
						carousel.map();
				} //end complete
			}); //end animate
		}, 1000); //end setTimeout
		
		$('#bottom_seo_copy').animate({
			opacity:'0'
		}, {
			duration:1000
		}); //end animate
		
		$('#home_address').animate({
			opacity:'0'
		}, {
			duration:1000
		});
	}, //end showPage
	
	setCurrentPage:function(page) {
		for (i = 0; i < carousel.IDs.length; i++) {
			if (page == carousel.IDs[i]) {
				document.getElementById("nav_" + page).className = "current_page";
			} else {
				document.getElementById("nav_" + carousel.IDs[i]).className = "nav_text";
			} //end if
		} //end for
	}, //end setCurrentPage
	
	prettyPhoto:function() {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animation_speed:'normal', /* fast/slow/normal */
			slideshow:false, /* false OR interval time in ms */
			autoplay_slideshow:false, /* true/false */
			opacity:0.80, /* Value between 0 and 1 */
			show_title:true, /* true/false */
			allow_resize:true, /* Resize the photos bigger than viewport. true/false */
			default_width:720,
			default_height:480,
			counter_separator_label:'/', /* The separator for the gallery counter 1 "of" 2 */
			theme:'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			horizontal_padding:20, /* The padding on each side of the picture */
			hideflash:false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode:'opaque', /* Set the flash wmode attribute */
			autoplay:true, /* Automatically start videos:True/False */
			modal:false, /* If set to true, only the close button will close the window */
			deeplinking:false, /* Allow prettyPhoto to update the url to enable deeplinking. */
			overlay_gallery:false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
			keyboard_shortcuts:true, /* Set to false if you open forms inside prettyPhoto */
			changepicturecallback:function(){}, /* Called everytime an item is shown/changed */
			callback:function(){}, /* Called when prettyPhoto is closed */
			ie6_fallback:true,
			social_tools:false
		});
	}, //end prettyPhoto
	
	map:function() {
		carousel.latlng = new google.maps.LatLng(33.022148,-96.841985);
		carousel.directionsService = new google.maps.DirectionsService();
		carousel.directionsDisplay = new google.maps.DirectionsRenderer();
		
		var map = new google.maps.Map(document.getElementById('map'), {
			zoom:12,
			center:carousel.latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		
		carousel.directionsDisplay.setMap(map);
		
		var contentString = "<div class='map_pop_up'>Launch Agency</div>";
			
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		
		var marker = new google.maps.Marker({
			position:carousel.latlng,
			title:"Launch Agency"
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
		
		// To add the marker to the map, call setMap();
		marker.setMap(map);
	}, //end map
	
	directions:function() {
		var origin = document.getElementById('origin').value;
		carousel.directionsDisplay.setPanel(document.getElementById('panel'));
		
		var request = {
			origin:origin, 
			destination:carousel.latlng,
			travelMode:google.maps.DirectionsTravelMode.DRIVING
		};
		
		carousel.directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				getKeys(response);
				carousel.directionsDisplay.setDirections(response);
			}
		});
		
		var getKeys = function(response){
			var count = 0;
			var keys = [];
			for(var key in response){
				keys.push(key);
				count += key.length;
			}
			
			var extra = parseInt(count * 50);
			var curHeight = parseInt($('#page_container').css("height").replace("px", ""));
			var height = Number(curHeight + extra) + "px";
			$('#page_container').animate({
				height:height
			}, {
				duration:2000,
				easing:"easeInOutQuint"
			}); //end animate
		} //end getKeys

	}, //end directions
	
	setHash:function(page) {
		if (page)
			window.location.hash = page;
	}, //end setHash
	
	getHash:function() {
		if (window.location.hash) {
			var section = window.location.hash.replace("#", "");
			var index = null;
			
			if (carousel.testForNames() == true) {
				index = 3;
			} else {
				switch(section) {
					case 'clients':
					index = 0;
					break;
				case 'case_studies':
					index = 1;
					break;
				case 'work':
					index = 2;
					break;
				case 'people':
					index = 3;
					break;
				case 'contact':
					index = 4;
					break;
				} //end switch
			} //end if
			carousel.scrollToItem(index);
		} //end if
	}, //end getHash
	
	testForNames:function() {
		var names = new Array("diane", "david", "michael", "valerie", "jason", "alexandra", "jon", "alex", "richard");
		for (i = 0; i < names.length; i++) {
			if (window.location.hash.replace("#","") == names[i])
				return true;
		} //end for
	} //end testForNames
			
} //end carousel
















