$(document).ready(function () {
	$("#leftColumn").css("overflow", "hidden");
	
	/*	Destination image shuffle
	===============================================================*/
		$(".position2").fadeTo(50,.65);
		$(".position3").fadeTo(50,.65);
		$(".position4").fadeTo(50,.65);
		$(".position5").fadeTo(50,.65);
		
		if (BrowserDetect.browser == "Explorer") {
			$("img", "#rotatingImages").css({
				borderRight: "2px solid #ccc",
				borderBottom: "2px solid #999"								   
			});
		} else {
			$("#image1").css({
				borderRight: "2px solid #ccc",
				borderBottom: "2px solid #999"				 
			});	
		}
		
		$(document).everyTime(5000, function () {
			for (var i = 1; i <= blockPositions.length; i++) {
				var currentDiv = "#image"+i;
				
				var currentPosition = $( currentDiv ).attr("class");
				currentPosition = currentPosition.replace(/position/, "");
				
				if (currentPosition == 1) {
					currentPosition = 5;
				} else {
					currentPosition--;
				}
				
				var currentIndex = currentPosition - 1;
				
				var newPosition = blockPositions[currentIndex];
				
				$(currentDiv).removeClass();
				$(currentDiv).addClass("position"+currentPosition);
				
				if (BrowserDetect.browser != "Explorer") {
					$("#image1").css({
						borderRight: "2px solid #ccc",
						borderBottom: "2px solid #ccc"				 
					});
					
					if (currentPosition == 1) {
						$(currentDiv).css("z-index", "10");
						$(currentDiv).animate( {
							top: "53px",
							left: "12px"					
						}, 350).animate( {
									top: newPosition[0]+"px",
									left: newPosition[1]+"px",
									height: newPosition[3]+"px",
									width: newPosition[4]+"px",
									opacity: newPosition[5],
									borderRight: "2px solid #ccc",
									borderBottom: "2px solid #999"
						}, 650);
					} else {
						$(currentDiv).fadeTo("fast", .25);
						$(currentDiv).animate( {
							top: newPosition[0]+"px",
							left: newPosition[1]+"px",
							height: newPosition[3]+"px",
							width: newPosition[4]+"px",
							opacity: newPosition[5],
							borderRight: "0px",
							borderBottom: "0px"
						}, 1000);
						$(currentDiv).css("z-index", newPosition[2]);
					}
				} else {
					$(currentDiv).fadeTo("fast", .25);
						$(currentDiv).animate( {
							top: newPosition[0]+"px",
							left: newPosition[1]+"px",
							height: newPosition[3]+"px",
							width: newPosition[4]+"px",
							opacity: newPosition[5]
						}, 1000);
					$(currentDiv).css("z-index", newPosition[2]);	
				}
			}
		});
		
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6") {
			$("#leftColumn").css("overflow", "scroll");	
		} else {
			$("#expandableDestinations h2").css({
				backgroundImage: "url('_presentation/images/background_Expandable.gif')",
				cursor: "pointer"
			});
			
			$("#expandableAreas h2").css({
				backgroundImage: "url('_presentation/images/background_Expandable.gif')",
				cursor: "pointer"
			});
			
			/*	Expandable areas (Destinations)
			===============================================================*/
				$('#expandableDestinations p').hide();
				$("#locator").hide();
				
				$('#expandableDestinations h2').click(
					function() {	
						var checkElement = $(this).next("p");
						var currentOffset = $(this).css("background-position");
						
						var destination = $("img" ,this).attr("src").replace(/_presentation\/images\/flags\//, "").replace(/\.png/, "");
						
						if (destination == "CzechRepublic") {
							$("#formHeader").attr("src", "_presentation/images/locationHeaders/in"+destination+".png");	
							$("#formHeader").attr("height", 61);
							$("#formHeader").css("width", "211px");
							$("#formHeader").css("margin", "15px auto auto 90px");
							$("#formHeader").attr("alt", "Picture yourself in the Czech Republic");
						} else {
							$("#formHeader").attr("height", 35);	
							$("#formHeader").css("width", "333px");
							$("#formHeader").css("margin", "41px auto auto 30px");
							$("#formHeader").attr("src", "_presentation/images/locationHeaders/in"+destination+".png");
							
							switch (destination) {
								case "CostaRica":
									$("#formHeader").attr("alt", "Picture yourself in Costa Rica");
								break;
								
								case "SouthAfrica":
									$("#formHeader").attr("alt", "Picture yourself in South Africa");
								break;
								
								default:
									$("#formHeader").attr("alt", "Picture yourself in "+destination);	
								break;
							}
						}
						
						$("#rotatingImages").fadeOut("normal", imageSwap);
						
						function imageSwap (switchDestination) {
							for (var i=0; i<5; i++) {
								var currentImage = "#image"+(i+1);
								if (switchDestination == "General") {
									destination = "General";
								}
								$(currentImage).attr("src", "_presentation/images/locationImages/"+destination+"/"+locationImages[destination][i]);
								
								switch (destination) {
									case "General":
										$(currentImage).attr("alt", "CEA Students");
									break;
									
									case "CostaRica":
										$(currentImage).attr("alt", "CEA Students in Costa Rica");
									break;
									
									case "CzechRepublic":
										$(currentImage).attr("alt", "CEA Students in the Czech Republic");
									break;
									
									case "SouthAfrica":
										$(currentImage).attr("alt", "CEA Students in South Africa");
									break;
									
									default:
										$(currentImage).attr("alt", "CEA Students in "+destination);
									break;
								}
							}
						}
						
						$("#rotatingImages").fadeIn("normal");
						
						$("img", "#flags").each(function () {
							$(this).attr("src", "_presentation/images/flags/"+$(this).attr("id")+"_Fade.png");
						});
						
						$("#"+destination).attr("src", $("#"+destination).attr("src").replace(/_Fade\.png/, ".png"));
						
						$('#expandableDestinations p:visible').slideUp('normal');
						$('#expandableDestinations p:visible').prev().css("background-position", "0px 0px");	
						
						$(this).css("background-position", "-447px 0px");	
						
						if (checkElement.is(':visible')) {
							$("#formHeader").attr("height", 35);	
							$("#formHeader").css("width", "333px");
							$("#formHeader").css("margin", "41px auto auto 30px");
							$("#formHeader").attr("src", "_presentation/images/locationHeaders/formHeader_general.png");
							checkElement.slideUp('normal');
							$(this).css("background-position", "0px 0px");
							$("img", "#flags").each(function () {
								$(this).attr("src", "_presentation/images/flags/"+$(this).attr("id")+".png");
							});
							imageSwap("General");
							$("#locator").fadeOut("normal").hide();
							moveLocator();
						} else {
							checkElement.slideDown('normal');
						}
						
						if ($("#locator").is(":hidden") && destination != "General") {
							$("#locator").fadeIn("normal", moveLocator);
						}
						
						function moveLocator() {
							$("#horizontalLine").animate( { 
									top: locationCoords[destination][0]+"px"
							}, 1000);
		
							$("#verticalLine").animate( { 
									right: locationCoords[destination][1]+"px"
							}, 1000);
							
							$("#redDot").animate( { 
									top: (locationCoords[destination][0] - 2)+"px",
									right: (locationCoords[destination][1] - 3)+"px"
							}, 1000);
						}
						
						return false;
					}
				);
				
				if ($.url.param("destination")) {
					var selector = "#"+$.url.param("destination")+"Div h2";
					$(selector).trigger("click");
				}
				
			/*	Expandable areas (Fields of Study)
			===============================================================*/
				$('#expandableAreas p').hide();
				
				$('#expandableAreas h2').click(
					function() {
						var checkElement = $(this).next();
						var currentOffset = $(this).css("background-position");
													
						$('#expandableAreas p:visible').slideUp('normal');
						$('#expandableAreas p:visible').prev().css("background-position", "0px 0px");	
						
						$(this).css("background-position", "-447px 0px");	
						
						if (checkElement.is(':visible')) {
							checkElement.slideUp('normal');
							$(this).css("background-position", "0px 0px");
						} else {
							checkElement.slideDown('normal');
						}
							
						return false;
					}
				);
		}
		
	/*	Form magic
	===============================================================*/
		function formMagic () {
			var year_in_school = $("select#year_in_school").val();
			var field_of_study = $("select#field_of_study").val();
			var destination_of_interest = $("select#destination_of_interest").val();
			var session_of_interest = $("select#session_of_interest").val();
			
			var getString = "?year_in_school="+year_in_school+"&field_of_study="+field_of_study+"&destination_of_interest="+destination_of_interest+"&session_of_interest="+session_of_interest;
			
			var formURL = "_includes/formStep2.php"+getString;
			
			$("#formStep2 #referFriendContainer #referFriend").hide();
			
			//  Build the overlay and the formContainer
			$('body').append('<div id="overlay"></div>');
			$('#overlay').css('height',$(document).height());
			
			$('body').append('<div id="formStep2"></div>');
			$('#formStep2').css('opacity',0)
			
			//  Get the form contents
			$.get(formURL, function(responseText) {
				$('#formStep2').html(responseText);										  
			});
			
			//  Fade in overlay and then the form
			$('#overlay').css('opacity',0).fadeTo(50, .5, function () {
				$('#formStep2').fadeTo(250, 1);
			});
			
			//  Center the div
			var scrollPos = getScroll();
			
			function getScroll () {
				scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
				scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
				return {scrollTop:scrollTop,scrollLeft:scrollLeft};
			};
			
			if($.browser.opera) {
				windowHeight = window.innerHeight;
				windowWidth = window.innerWidth;
			}else{
				windowHeight = $(window).height();
				windowWidth = $(window).width();
			};

			projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - ($('#formStep2').height()/2);
			if(projectedTop < 0) {
				projectedTop = 10;
				_followScroll = false;
			}else{
				_followScroll = true;
			};

			$('#formStep2').css({
				'top': projectedTop,
				'left': ($(window).width()/2) + scrollPos['scrollLeft'] - ($('#formStep2').width()/2)
			});
		}
		
		if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6") {
			// Do nothing and let step one submit like normal	
		} else {
			//  Set up links within expandable areas
			$("a", "#expandableAreas p, #expandableDestinations p").click(function () {
				formMagic();
				
				return false;
			});
			
			//  Set up 'Request More Information' link
			if ($('body').attr("id") != "MoreInfo") {
				$("#requestLink").click(function () {
					formMagic();
				
					return false;
				});
			}
			
			//  Open form
			$("#goToStep2").click(function () {
				formMagic();
				
				return false;
			});
		}
		
	/*	Standalone Form
	===============================================================*/
		$("#partOne").append('<img class="png" id="showPartTwo" src="_presentation/images/btn_GoToStep2.png" height="38" width="138" /><br /><p class="footerInfo"><span>*</span> Required Information<br /><br />Please know that your privacy is very important to us. <br />Your information will not be sold or shared for any reason.<br /><br /><strong>Step 1 of 2</strong></p>');
		$("#stepCounter").append('<br /><br /><strong>Step 2 of 2</strong>');
		
		$("#referFriend").hide();
		$("#partTwo").hide();
		
		$("#submitBtn").click(function () {
			var validForm = validate("step2");
			if (validForm) {
				$("#standaloneForm").submit();					
			}
			return false;
		});
		
		$("#institution").attr("disabled", "disabled");
		
		$("#state_of_institution").change(function () {
			var currentSelection = $(this).val();
						
			var pageToGet = "_includes/institutionDropDown.php?state="+currentSelection;
						
			$("#institution").attr("disabled", "false");
						
			$.get(pageToGet, function(responseText) {
				$('#institutionDropDown').html(responseText);										  
			});
		});
		
		$("#showPartTwo").click(function () {
			validate("step1");
			if (numErrors == 9) {
				$("#partOne").hide();
				$("#partTwo").show();
			}
			numErrors = 0;
		});
		
		
		$("#mobile_checkbox").click(function () {
			var currentState = $(this).attr("src").replace(/_presentation\/images\//, "").replace(/\.gif/, "");
			
			if (currentState == "checkBox") {
				$(this).attr("src", "_presentation/images/checkBox_checked.gif");
				$("#mobile_text").attr("value", "yes");
			} else {
				$(this).attr("src", "_presentation/images/checkBox.gif");
				$("#mobile_text").attr("value", "no");
			}
		});
		
		$("#checkBox").click(function () {
			var currentState = $(this).attr("src").replace(/_presentation\/images\//, "").replace(/\.gif/, "");
			
			if (currentState == "checkBox") {
				$(this).attr("src", "_presentation/images/checkBox_checked.gif");
				$("#referFriend").slideDown("normal");
			} else {
				$(this).attr("src", "_presentation/images/checkBox.gif");
				$("#referFriend").slideUp("normal");
			}
		});
});