willow.bgcount = "";

willow.ready(function($){
	//emergency bulletin
	$('#Form1').bulletin({data:[{type:"announcement",id:3828}]});
	
	$(".notIE #foxcroft_logo_center_relative").center();

	// Podium Search 
	$('#search, #applysearch, #expsearch, #followsearch, #supportsearch, #connectsearch').pdSearch({showButton:true, showButtonClass:'searchButton', showButtonText:''}); 
	$(".searchButton").css("margin","0");
	
	
	willow.getMenu("140197|140198|140199|140201|140202|140203",function(data){
		//building L2 dropdown nav
		var L2Menu = data.menu;
		for(var i = 0; i < L2Menu.length; i++){	
			var menuString = "";
			var L1id = L2Menu[i].id;
			if(L2Menu[i].L2.length > 0){
				menuString+="<ul class='L2'>";
				if(L2Menu[i].L2.parentid = L1id){
				for(var j = 0; j < L2Menu[i].L2.length; j++){
					//following if statement is to check for and apply first and last class to the first and last links for styling
					
					if(L2Menu[i].L2[j].target == 1){
						if(j==0){menuString+="<li class='L2item first'><a class='L2link' href='"+L2Menu[i].L2[j].url+"' target='_blank'><span>"+L2Menu[i].L2[j].title+"</span></a></li>";}
						else if(j==L2Menu[i].L2.length-1){menuString+="<li class='L2item last'><a class='L2link' href='"+L2Menu[i].L2[j].url+"' target='_blank'><span>"+L2Menu[i].L2[j].title+"</span></a></li>";}
						else{menuString+="<li class='L2item'><a class='L2link' href='"+L2Menu[i].L2[j].url+"' target='_blank'><span>"+L2Menu[i].L2[j].title+"</span></a></li>";}
					}else{
						if(j==0){menuString+="<li class='L2item first'><a class='L2link' href='"+L2Menu[i].L2[j].url+"'><span>"+L2Menu[i].L2[j].title+"</span></a></li>";}
						else if(j==L2Menu[i].L2.length-1){menuString+="<li class='L2item last'><a class='L2link' href='"+L2Menu[i].L2[j].url+"'><span>"+L2Menu[i].L2[j].title+"</span></a></li>";}
						else{menuString+="<li class='L2item'><a class='L2link' href='"+L2Menu[i].L2[j].url+"'><span>"+L2Menu[i].L2[j].title+"</span></a></li>";}
					}
					}
				}
				menuString+="</ul>";
			}
			menuString+="</li>";
			$(".L2wrap#L2_"+L1id+"").append(menuString);
		}
	});
	

	
	//center photo	
	$('#photo_wrapper').cycle({
		fx:     'fade',
        speed:  'slow',
        timeout: 4000,
		after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
			willow.bgcount = options.currSlide;
		}
    });	
		
	//Experience Photo Gal
	$('#expPhotos').cycle({
        speed:  'slow',
        timeout: 4000,
		prev:    '#back',
        next:    '#next',
        pager:  '#nav',
		slideExpr: 'img',
		fx: "scrollHorz",
		after:     function() {
            $('#caption').html(this.alt);
        }
    });
	
	//get facebook
	$.getJSON("/data/json.asp?type=fb",function(response,txt,xhr) {
        $.getJSON("https://graph.facebook.com/FoxcroftSchool/feed?callback=?",options={access_token:response.access_token,limit:3},function(data,txt,xhr) {
			
			//loop through and within data array's retrieve the message and updated_time variable.
			datetime = new Date();
			$.each(data.data,function(i,fb){
			
				//get post day and time
				datetime.setISO8601(fb.updated_time); //set facebook time to ISO8601
				d = datetime.toLocaleString(); //convert date object to a string using locale conventions
				postDayTime = facebookDate(d); //format date ex. Monday at 1:00am 
			
				if (fb.message) {
					wallpost = fb.message; //facebook post
					
					//get post day and time
					//datetime.setISO8601(fb.updated_time); //set facebook time to ISO8601
					//d = datetime.toLocaleString(); //convert date object to a string using locale conventions
					//postDayTime = facebookDate(d); //format date ex. Monday at 1:00am 
					
					//wall post
					$(".facebook_message").eq(i).html(wallpost);
					//$(".facebook_message_date").eq(i).html(postDayTime);
				}
				//wall photo or album photo
				if(fb.picture){
					$(".fbPost").eq(i).prepend("<img class='fbImg' src='"+fb.picture+"' border='0' />");
				}
				// date and time
				$(".facebook_message_date").eq(i).html(postDayTime);
			});
        });
    });

	
	//below hover functions are for the dropdowns - the show/hide the dropdown box, apply/remove the on class for the L1 and call the function to build the news and links
	var DiscDropdownRelative = $("#DiscDropdownRelative"),
		ApplyDropdownRelative = $("#ApplyDropdownRelative"),
		ExperienceDropdownRelative = $("#ExperienceDropdownRelative"),
		FollowDropdownRelative = $("#FollowDropdownRelative"),
		SupportDropdownRelative = $("#SupportDropdownRelative"),
		ConnectDropdownRelative = $("#ConnectDropdownRelative"),
		centerLogo = $(".notIE #foxcroft_logo_center_relative"),
		photoWrapper = $('#photo_wrapper');

	$(".aL1").bind("mouseenter", function(e){ // On hover of L1 items
			var $this = $(this),
				$thisLi = $this.parent(".liL1"),
				$thisDD = $this.siblings(".dd_holder"),
				$ddContainers = $(".dd_holder"),
				idx = parseInt($this.attr("idx"));
				
			$thisDD.removeClass("hideDD");
			$(".liL1").removeClass("on");
			//add class to a tag
			$this.addClass("largeA");
			//add class to L1 parent
			$thisLi.addClass("hover");
			
			centerLogo.css("display","none");
			photoWrapper.cycle('pause');		
			flash_logoOff();	
	});
	
	$(".liL1").bind("mouseleave", function(e){ 
		$(".aL1").removeClass("largeA");
		$(this).removeClass("hover");
		$(".dd_holder").addClass("hideDD");
		
		centerLogo.css("display","block");
		photoWrapper.cycle('resume');
		flash_logoOn();
	});
	

		
	//Meet rollovers
	$(".meetContainer img").hover(
		function(){
			var group_id = $(this).attr("group_id");
			//$(this).css("display","none")
			//$('#over_'+group_id).css("display","block")	
			$('#over_'+group_id).css("display","block");
			$('#main_'+group_id).css("display","none");
			
			
		},
		function(){
			var group_id = $(this).attr("group_id");
			//$(this).css("display","none");
			$("#main_" + group_id).css("display","block");
			$('#over_'+group_id).css("display","none");
		}
	);
		
		
		
	//window width minus L1 nav, borders. It should push the footer off the screen
	var theWindow = $(window),
		centerImgWrapper = $("#photo_wrapper"),
		$centerImg = $("#photo_wrapper .img"),
		//windowWidth = theWindow.width() - 36,
		//windowHeight = theWindow.height() - 168,
		windowWidth = theWindow.width() - 54,
		windowHeight = theWindow.height() - 99,
		aspectRatio = windowWidth / windowHeight;
		
		resizeImg(centerImgWrapper, windowWidth, windowHeight);
		resizeImg($centerImg, windowWidth, windowHeight);
	
	theWindow.resize(function() {
		
		$('#photo_wrapper').cycle('destroy'); // needs to happen. BG image sizes get messed up if you dont.
		
		var h = theWindow.height() - 99,
			//h = theWindow.height() - 168,
			//w = theWindow.width() - 36;
			w = theWindow.width() - 36;
		resizeImg(centerImgWrapper, w, h);
		resizeImg($centerImg, w, h);
		$(".notIE #foxcroft_logo_center_relative").center();
		
		//center photo cycle reinitialization	
		$('#photo_wrapper').cycle({
			fx:     'fade',
			speed:  'slow',
			timeout: 4000,
			startingSlide: willow.bgcount,
			after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				willow.bgcount = options.currSlide;
			}
		});
		
	});
		
	function resizeImg($centerImg, windowWidth, windowHeight) {
		$centerImg.width(windowWidth).height(windowHeight);
	}
	
	$("#non_flash_content").css("display","block");
});	


/*jscript date functions*/
function facebookDate(d) {

	var d=new Date(d);
	var theDay = d.getDay();
	var theHours = d.getHours(); 
	var theMins = d.getMinutes(); 
	
	switch (theDay)
	{
	case 1:
		var theDay = "Monday";
		break;
	case 2:
		var theDay = "Tuesday";
		break;
	case 3:
		var theDay = "Wednesday";
		break;
	case 4:
		var theDay = "Thursday";
		break;
	case 5:
		var theDay = "Friday";
		break;
	case 6:
		var theDay = "Saturday";
		break;
	case 0:
		var theDay = "Sunday";
		break;
	default:
		var theDay = "Sunday";
	}
	
	
	var suffix = "am";
	if (theHours >= 12) {
		suffix = "pm";
		theHours = theHours - 12;
	}
	if (theHours == 0) {
		theHours = 12;
	}

	if (theMins < 10) {
		theMins = "0" + theMins;
	}
	return theDay + " at " + theHours + ":" + theMins + " " + suffix;
}

/*
Function that converts the datetime JSON string from facebook from an IETF RFC 3339 datetime to a datetime string Javascript understands as a Date object
Searched google for answering this problem and found this function
http://dansnetwork.com/2008/11/01/javascript-iso8601rfc3339-date-parser/
January 6, 2011
*/

Date.prototype.setISO8601 = function(dString){
		var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

        if (dString.toString().match(new RegExp(regexp))) {
                var d = dString.match(new RegExp(regexp));
                var offset = 0;

                this.setUTCDate(1);
                this.setUTCFullYear(parseInt(d[1],10));
                this.setUTCMonth(parseInt(d[3],10) - 1);
                this.setUTCDate(parseInt(d[5],10));
                this.setUTCHours(parseInt(d[7],10));
                this.setUTCMinutes(parseInt(d[9],10));
                this.setUTCSeconds(parseInt(d[11],10));
                if (d[12])
                        this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
                else
                        this.setUTCMilliseconds(0);
                if (d[13] != 'Z') {
                        offset = (d[15] * 60) + parseInt(d[17],10);
                        offset *= ((d[14] == '-') ? -1 : 1);
                        this.setTime(this.getTime() - offset * 60 * 1000);
                }
        }
        else {
                this.setTime(Date.parse(dString));
        }
        return this;
};

jQuery.fn.center = function () {
    this.css("position","absolute");
	//add 249px to the top so the logo is postioned almost to the bottom of the photo
    //this.css("top", (($(window).height() - this.outerHeight()) / 2) + 249 + $(window).scrollTop() + "px");
    this.css("top", (($(window).height() - this.outerHeight()) ) - 80 + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

