// JavaScript Document

function mainPics_init(resetFunction,positionArray,activeItem) {
	if (resetFunction) {
		positionArray = new Array("#mainPic_ManAtPicture","#mainPic_GirlAtWindow","#mainPic_BoyOutside","#mainPic_trumpet","#mainPic_GirlInWoods");
	} else {
		$.each(positionArray,function( intIndex, objValue ) {
			if ( "#"+$(activeItem).attr("id")==objValue) {
				
				positionArray[intIndex]=positionArray[0];
				positionArray[0]="#"+$(activeItem).attr("id");
			};
		});
	}
	
	$.each(positionArray,function( intIndex, objValue ) {
		if (intIndex==0) {
			$(objValue).attr("class","mainPic_active");	
		} else {
			mainPic_events( objValue, intIndex, positionArray );
		}
	});
}

function mainPic_events(itemObject,position,positionArray) {
	var setActive = false;
	
	mainPic_positioning(itemObject,position);
	mainPic_setBackgroundPosition(itemObject);
	
	$(itemObject)
		.mouseover(function() {
				$(this).attr("class","mainPic_hover");
		})
		.mouseout(function() {
				$(this).attr("class","mainPic");
		})
		.click(function() {
				$(positionArray[0]).attr("class","mainPic");
				
				$(this).animate( {"top":"90px","left":"19px","backgroundPosition":"0px 0px"},{duration:300},"easeOutBounce",function() { $(this).switchClass("mainPic_hover","mainPic_active",1000,"easeOutBounce") } );
				removeEvents( $(this) );
				
				mainPics_init( false, positionArray, $(this) );
		})
}
function mainPic_positioning(itemObject,position) {
		switch (position) {
			case 0 :
				$(itemObject).animate( {"top":"90px","left":"0px"},{duration:500} );	
			    break;
			case 1 :
				$(itemObject).animate( {"top":"90px","left":"834px"},{duration:500} );	
                break;
			case 2 :
				$(itemObject).animate( {"top":"145px","left":"834px"},{duration:500} );
			    break;
			case 3 :
				$(itemObject).animate( {"top":"200px","left":"834px"},{duration:500} );
			    break;
			case 4 :
				$(itemObject).animate( {"top":"255px","left":"834px"},{duration:500} );
			    break;
		}
}
function mainPic_setBackgroundPosition(itemObject) {
	
	if ( $(itemObject).attr("class")!="mainPic_active" ) {
		
		switch 	( $(itemObject).attr("id") ) {
			case "mainPic_ManAtPicture" :
				$(itemObject).css("backgroundPosition","-57px -55px");
			break;
			case "mainPic_GirlAtWindow" :
				 $(itemObject).css("backgroundPosition","-155px -85px");
			break;
			case "mainPic_BoyOutside" :
				$(itemObject).css("backgroundPosition","-700px -150px");
			break;
			case "mainPic_trumpet" :
				$(itemObject).css("backgroundPosition","-160px -75px");
			break;
			case "mainPic_GirlInWoods" :
				$(itemObject).css("backgroundPosition","-119px -40px");
			break;
		}
	}
}
   
function showContent(parentID,subID) {
	var contentID = "";	
	if ( subID == "houstonConf" || subID == "video" ) {
		parentID = "confInfo"; 
	} else if ( subID == "laConf" ) {
		parentID = "media";
	};
	// Hide the main content
	$("#mainContent").hide("fast");
	
	if (parentID=="#topNavMenu") {
		contentID = subID;
		if (subID=="about" || subID=="conf") {
			subID = $("#topSubNav_"+subID).children("li:first").attr("id");
			contentID = subID;
		} 
	} else {
		contentID = parentID;
		if (parentID.indexOf("_")>0) {
				contentID = subID;
		}
	}
		
	var contentURL = "content/" + contentID + ".html";
	var defaultEase = "easeOutBack";
	// Set page name and the height of the page
	switch (contentID) {
		case "home" :
			document.title = "Wheatstone Academy";
			$("#body").animate( {"height":"600px"},100,defaultEase );
			$("div").data("pageTracking", contentID);
			break;
	//About Sub Nav Begin	
		case "mission" :
			document.title = "Wheatstone Academy: The Mission";
			$("#body").animate( {"height":"1610px"},100,defaultEase );
			$("div").data("pageTracking", "About/" + contentID);
			break;
		case "story" :
			document.title = "Wheatstone Academy: The Story";
			$("#body").animate( {"height":"700px"},100,defaultEase );
			$("div").data("pageTracking", "About/" + contentID);
			break;	
		case "facultyStaff" :
			document.title = "Wheatstone Academy: The Faculty and Staff";
			$("#body").animate( {"height":"1300px"},100,defaultEase );
			$("div").data("pageTracking", "About/" + contentID);
			break;				
		case "contact" :
			document.title = "Wheatstone Academy: Contact Us";
			$("#body").animate( {"height":"525px"},100,defaultEase );
			$("div").data("pageTracking", "About/" + contentID);
			break;
	// About Sub Nav End	
		
	// Conference Sub Nav Begin
		case "confInfo" :
			document.title = "Wheatstone Academy: Conference Info";
			$("#body").animate( {"height":"1345px"},100,defaultEase );
			$("div").data("pageTracking", "Conference/" + contentID);
			break;
		case "who" :
			document.title = "Wheatstone Academy: Who Should Come?";
			$("#body").animate( {"height":"975px"},100,defaultEase );
			$("div").data("pageTracking", "Conference/" + contentID);
			break;
		case "housing" :
			document.title = "Wheatstone Academy: Housing Info";
			$("#body").animate( {"height":"975px"},100,defaultEase );
			$("div").data("pageTracking", "Conference/" + contentID);
			break;	
		case "parents" :
			document.title = "Wheatstone Academy: Parent Info";
			$("#body").animate( {"height":"950px"},100,defaultEase);
			$("div").data("pageTracking", "Conference/" + contentID);
			break;
		case "payments" :
			document.title = "Wheatstone Academy: Payment Info";
			$("#body").animate( {"height":"1550px"},100,defaultEase);
			$("div").data("pageTracking", "Conference/" + contentID);
			break;
		case "raleigh" :
			document.title = "Wheatstone Academy: Raleigh, North Carolina";
			$("#body").animate( {"height":"2230px"},100,defaultEase);
			$("div").data("pageTracking", "Conference/" + contentID);
			break;
	// Conference Sub Nav Ends
	
		case "alumni" :
			document.title = "Wheatstone Academy: Alumni";
		
			$("#body").animate( {"height":"875px"},100,defaultEase, function() {
					$("#leftColumn").animate( {"width":"864px"},100,defaultEase, function () {
							$("#rightColumn").hide("fast");		
						})																  
				});	
			$("div").data("pageTracking", contentID);
			break;
		case "blog" :
			document.title = "Wheatstone Academy: Blog";
			$("#body").animate( {"height":"575px"},100,defaultEase);
			contentURL = "content/home.html";
			generateNav("#topNavMenu","li");
			GB_showPage("Wheatsone Academy Blog", "http://www.wheatstoneforum.com")
			$("div").data("pageTracking", contentID);
			break;			
        case "local":
            document.title = "Wheatstone Academy: Local Events";
            $("#body").animate( {"height":"1380px"},100,defaultEase);
            break;
		case "faqs" :
			document.title = "Wheatstone Academy: FAQs";
			$("#body").animate( {"height":"1875px"},100,defaultEase);
			$("div").data("pageTracking", contentID);
			break;
		case "donate" :
			document.title = "Wheatstone Academy: Donate";
			$("#body").animate( {"height":"1100px"},100,defaultEase);
			$("div").data("pageTracking", contentID);
			break;
		case "youthWorkers" :
			document.title = "Wheatstone Academy: Privacy Policy";
			$("#body").animate( {"height":"745px"},100,defaultEase);
			$("div").data("pageTracking", contentID);
			break;	
		case "advocates" :
			document.title = "Wheatstone Academy: Advocates";
			$("#body").animate( {"height":"645px"},100,defaultEase);
			$("div").data("pageTracking", contentID);
			break;				
		case "media" :
			document.title = "Wheatstone Academy: Media and Press Info";
			$("#body").animate( {"height":"1800px"},100,defaultEase);
			$("div").data("pageTracking", contentID);
			break;
		case "privacy" :
			document.title = "Wheatstone Academy: Privacy Policy";
			$("#body").animate( {"height":"945px"},100,defaultEase);
			$("div").data("pageTracking", contentID);
			break;
		default:	
			document.title = "Wheatstone Academy";
			$("#body").animate( {"height":"575px"},100,defaultEase);
			break;
	}
	
	
	
	$("#mainContent").load(contentURL,"",function() { $("#mainContent").show("slow") } );
	
	if ( $("#rightColumn").css("display")=="none" ) {
		$("#leftColumn").animate( {"width":"460px"},100,defaultEase, function() {
					$("#rightColumn").show("fast");																   
			});
	}
}

function resetSubNav() {
	$("#topSubNav_about")
		.animate({"top":"-30px"},100)
		.children("li").each( function() { $(this).stop().animate({"top":"7px"},200) });
	
	$("#topSubNav_conf")
		.animate({"top":"-30px"},100)
		.children("li").each( function() { $(this).stop().animate({"top":"7px"},200) });
}
function resetSubNavItems(parent,child) {
	$(parent).children("li").each( function() { 
		$(this).stop().animate({"top":"7px"},{duration:200,queue:true}); 
	});
}

function attachNavEvents(parent, childType, itemObject) {		

	$(itemObject)
         /*.mouseover(function() {
			if (parent=="#newsAndEventsItems") {
				// News and Events Animation
				$(this).stop().animate({ "backgroundColor":"#ebe0ea" },{ duration:1000,queue:true });
			} else {
				// Main TopNav, Top SubNav, and BottomNav Animation
				$(this).stop().animate({"top":"10px"},200,"easeOutBack");
			}
		})
		.mouseout(function() {
			if (parent=="#newsAndEventsItems") {
				// News and Events Animation
				$(this).stop().animate({ "backgroundColor":"transparent" },{ duration:1000,queue:true });
			} else {
				// Main TopNav, Top SubNav, and BottomNav Animation
				$(this).stop().animate({"top":"7px"},500,"easeOutBack");
			}
		})*/
		.click(function() {
			
			var subNavID = "#topSubNav_" + $(this).attr("id");

			if (parent=="#topNavMenu") {
				$(this).animate({"top":"7px"},200 );
				showSubNav( parent, childType, $(this).attr("id"), subNavID );
			
			} else if (parent=="#newsAndEventsItems") {
				showSubNav( "#topNavMenu", "li", "conf", "#topSubNav_conf", "confInfo", false);
			} else {
				// Should only fire if clicked on subnav item
				resetSubNavItems(parent,subNavID);
				generateNav(parent,childType,$(this).attr("id") );
			}
		  	showContent( parent, $(this).attr("id") );
		});
}
function showSubNav(parent, childType, activeID, subNavID, activeSubID,content) {
	// Generate Nav with Active Item
	generateNav( parent, childType, activeID );
	resetSubNav();
	
	// Move SubNav Down Into View
//	resetSubNavItems(parent,subNavID);
	$(subNavID).animate({ "top":"0px" }, { duration: 300, queue: true } );
	// Generate SubNav with Active Item if available
	generateNav(subNavID, "li", activeSubID);
	if (content) {
		showContent( parent, activeSubID );	
	}
}
function removeEvents(itemObject) {
	$(itemObject)
		.unbind('mouseover')
		.unbind('mouseout')
		.unbind('click');
}
function generateNav(parent, childType, activeItemID) {
	// Set active element's ID
	var activeID = "";
	if ( typeof(activeItemID) == "undefined" ) {
		// Set default active element to the first element. Usually home
	    activeID = $(parent).children(childType+":first-child").attr("id");
	} else {
		activeID = activeItemID;
	} // end if
	// Loop through each parents's childType elements 
	$(parent).children(childType).each(
		function() {
			if ( $(this).attr("id") == activeID ) {
				// Set the active element's class
			    $(this).attr("class", "active");
				removeEvents($(this));
			} else {
				// Remove any previous active class
				$(this).removeAttr("class");
				
				// Add item events
				attachNavEvents( parent, childType, $(this) );
			} // end if
		} // end function
	) // end .each
} // end generateNav function
function viewPDF() {

	selectedValue = $("#view_mentor option:selected").val();
	i = selectedValue.indexOf(".");
	fName = selectedValue.substring(i+1,selectedValue.length);
	lName = selectedValue.substring(0,i);
	if (selectedValue!="Select") {
		GB_showPage(fName+" "+lName+"'s Mentor Information","/staff/"+selectedValue+".pdf");
	}
}
function eventListings(location, month) {
	$.get("/content/culturalEventInfo.php",{ l: location, d: month }, function(data) {
		if (data) {
			if ( location==2 ) { 
				var l = "houston";
			} else {
				var l = "la";
			}
			$("#"+l+"_culturalEvents").html(data);
			generateEventActions( "#"+l+"_culturalEvents" );
		} else {
			$("#"+l+"_culturalEvents").html("<li>No events for this month.</li>");	
		}
	});	
}
function getAllEventListings(month) {
	eventListings(1,month);
	eventListings(2,month);
}
function generateEventActions(id) {
	$(id).children("li").each( function() {
		$(this).click(function() {
			$.get("/content/culturalEventInfo.php",{ id: $(this).attr("id") }, function(data) {
				$("#eventDescriptions").show("fast");
				$("#eventDescriptionContent").html(data);
			});
		});											   
	});
}
function trackMonth(year, month, date, inst) { 
	var d = new Date();
	var c = new Date();
	
	d.setMonth(month-1);
	
	if ( d.getMonth() != c.getMonth() ) {
		d.setDate(0);
	} 
	var t = Date.parse(d).toString();
	//alert( "Current Month: " + c.getMonth() + " and Calendar Month: " + d.getMonth() );
	getAllEventListings( t.substring( 0,(t.length-3) ) );
}


$(function(){
    $(".mainPic").click(
        function(event){
            event.preventDefault();
            switch($(this).attr('id')){
                case 'mainPic_ManAtPicture':
                    $("#register").trigger('click');
                    break;
                case 'mainPic_trumpet':
                    $("#alumni_c").trigger('click');
                    break;
            }
        }
    );
    $("#mainPic_ManAtPicture.mainPic_active").click(
        function(event){
            event.preventDefault();
            if($(this).hasClass('mainPic_active')){
                $("#register").trigger('click');
                return false;
            }
        }
    );
    $("#mainPic_trumpet.mainPic_active").live('click',
        function(event){
            event.preventDefault();
            if($(this).hasClass('mainPic_active')){
                $("#alumni_c").trigger('click');
                return false;
            }
        }
    );
});
