//Add browser specific class to body
$(document).ready(function() {
	if ($.browser.msie)
	{
		$("body").addClass("ie");
	}
});

//Location to image folder set as global var
var imgLocation = "/tl_files/support/images/";
var lastEffect = 0;

//Preload images (just ones needed for smooth navigation)
$(document).ready(function() {
	
	var preloadStr = "";
	var imgs = Array(
				"photo_blank.jpg",
				"students/jazlyn.jpg",
				"students/tyrell.jpg",
				"students/samantha.jpg",
				"students/philip.jpg",

				"btn_arrw_left_hover.gif", 
				"btn_arrw_middle_hover.gif", 
				"btn_arrw_right_hover2.gif",
				
				"btn_sm_left_hover.gif",
				"btn_sm_middle_hover.gif",
				"btn_sm_right_hover.gif",

				"menu_arrow_over.gif",
				"photo_blank.jpg",

				"sidebar/sidebar01.jpg",
				"sidebar/sidebar02.jpg",
				"sidebar/sidebar03.jpg",
				"sidebar/sidebar04.jpg",
				"sidebar/sidebar05.jpg"
			  );
	
	if (document.images) {
		for (imgIndex in imgs) {
			eval("img"+imgIndex+" = new Image()");
			eval("img"+imgIndex+".src = '"+(imgLocation+imgs[imgIndex])+"'");
		}
	} else {
		//just in case browser doesn't support document.images
		for (img in imgs) {
			preloadStr+=("<IMG SRC='"+imgLocation+imgs[img]+"' WIDTH='0' HEIGHT='0' CLASS='hidden'>");
		}

		preloadStr = ("<DIV STYLE='width:0px; height: 0px; visibility: hidden'>"+preloadStr+"</DIV>");
		$("div#footer").after(preloadStr);
	}
	
});


//Rollover buttons with click event
$(document).ready(function() {
	
	//Arrow Button
	var btnLeft   = "url("+imgLocation+"btn_arrw_left.gif)";
	var btnMiddle = "url("+imgLocation+"btn_arrw_middle.gif)";
	var btnRight  = "url("+imgLocation+"btn_arrw_right.gif)";
	
	//Arrow Button: Hover State
	var btnLeftHover   = "url("+imgLocation+"btn_arrw_left_hover.gif)";
	var btnMiddleHover = "url("+imgLocation+"btn_arrw_middle_hover.gif)";
	var btnRightHover  = "url("+imgLocation+"btn_arrw_right_hover2.gif)";

	//Small Arrow Button
	btnSmLeft = "url("+imgLocation+"btn_sm_left.gif)";
	btnSmMiddle = "url("+imgLocation+"btn_sm_middle.gif)";
	btnSmRight = "url("+imgLocation+"btn_sm_right.gif)";

	//Small Arrow Button: Hover State
	btnSmLeftHover   = "url("+imgLocation+"btn_sm_left_hover.gif)";
	btnSmMiddleHover = "url("+imgLocation+"btn_sm_middle_hover.gif)";
	btnSmRightHover  = "url("+imgLocation+"btn_sm_right_hover.gif)";

	//Small button rollover
		$small_button = $("div.small_button");

		$small_button.hover(
			function() { 
				$(this)
					.find(".btn_sm_left").css("background-image", btnSmLeftHover)
					.end()
					.find(".btn_sm_middle").css("background-image", btnSmMiddleHover)
					.find("a").css("color", "#003399")
					.end() //Cacnel last search "a"
					.end() //Cancel last search ".btn_sm_middle"
					.find(".btn_sm_right").css("background-image", btnSmRightHover);
			},
			function() { 
				$(this)
					.find(".btn_sm_left").css({"background-image": btnSmLeft,"margin": "0px"})
					.end()
					.find(".btn_sm_middle").css("background-image", btnSmMiddle)
					.find("a").css("color", "white")
					.end()	//Cancel last search "a"
					.end()	//Cancel last search ".btn_sm_middle"
					.find(".btn_sm_right").css("background-image", btnSmRight);
			}
		);
		
		$small_button.click(
			function(event) {
				$(this).find(".btn_sm_middle").trigger("click");
			}
		);
	
	//Arrow button
		$arrow_button = $("div.arrow_button");

		$arrow_button.hover(
			function() { 
				$(this)
					.find(".btn_arrow_left").css({"background-image": btnLeftHover})
					.end()
					.find(".btn_arrow_middle").css("background-image", btnMiddleHover)
					.find("a").css("color", "#003399")
					.end() //Cacnel last search "a"
					.end() //Cancel last search ".btn_sm_middle"
					.find(".btn_arrow_right").css("background-image", btnRightHover);
			},
			function() { 
				$(this)
					.find(".btn_arrow_left").css({"background-image": btnLeft})
					.end()
					.find(".btn_arrow_middle").css("background-image", btnMiddle)
					.find("a").css("color", "white")
					.end()	//Cancel last search "a"
					.end()	//Cancel last search ".btn_sm_middle"
					.find(".btn_arrow_right").css("background-image", btnRight);
			}
		);
});


/** Fading Homepage Modules **/
$(document).ready(function() {
	
	//Fixed issue in FF where window.setInterval kept firing twice due to having
	//more than one setInterval on the page. Used setTimeout insead.

	var timeout1=null;
	var blankPhotoUrl = "url(/tl_files/support/images/photo_blank.jpg)";

	function rotateHomepageModule() {
		var modules = [
			{	src: imgLocation + "students/jazlyn.jpg", 
				text: "\"HEAF offered a safe haven where my academic success could be a priority.\"",
				person: "Jazlyn, sophomore<BR/>Barnard College"
			},
			{	src: imgLocation + "students/tyrell.jpg", 
				text: "When I came to HEAF, I was unable to express my ideas with confidence, now I can help others using my skills and talents.",
				person: "<br/>Tyrell, 10th Grade<br/>Manhattan Center for Science and Mathematics"
			},
			{	src: imgLocation + "students/samantha.jpg", 
				text: "\" Through HEAF I have matured and taken on more responsibility.\"",
				person: "Samantha, 11th Grade<BR/>Bard High School � Early College"
			},
			{	src: imgLocation + "students/philip.jpg", 
				text: "\"In my experience with HEAF, their motto, 'Come to HEAF ... Go To College!' has become increasingly real to me.\"",
				person: "Philip, 12th Grade<BR/>Fiorello H. LaGuardia High School"
			}
		];

		var num = -1;
		var $module = $("img#ourWords");
		var $moduleText = $("div#ourWordsText");
		
		function rotateHomepageModule() {
			num++;
			if (num>=modules.length) num = 0;
			var module = modules[num];	
			
			var updateContent = function() { 
					
					setNewTimeout(); 
			}
			var setNewTimeout = function() {
					window.clearTimeout(timeout1);
					timeout1 = window.setTimeout(rotateHomepageModule, 1000);
			}

			var animate = function(actionIn, actionOut) {
					var photoExit = function() {
									eval("$module."+actionOut+"('slow', function() { updateContent() })");
									if(actionOut != "fadeOut") $module.hide();
								}
					$moduleText.text(module.text);
					$module.attr("src", module.src);
					eval("$module.css({'opacity': 1, 'visibility': 'visible'})."+actionIn+"('slow', function() { timeout1 = window.setTimeout(photoExit, 5000) })");
			}

			Effects = ["fade", "DropUpDn", "growShrink", "DropLtRt", "growPuff", "DropDnUp", "DropDnShrink", "DropRtLt", "fade"];
			
			var randomNumber = Math.round(Math.random() * (Effects.length-1));
			
			if (randomNumber == lastEffect) {

				var nextNumber = randomNumber;
				while (randomNumber == nextNumber) {
					nextNumber = Math.round(Math.random() * (Effects.length-1));
				}
				randomNumber = nextNumber;
			}
			lastEffect = randomNumber;

			randFunc = Effects[randomNumber];

			switch(randFunc) {
				case "fade":
					animate("fadeIn", "fadeOut");
					break;
				case "DropUpDn":
					animate("DropInUp","DropOutDown");
					break;
				case "DropRtLt":
					animate("DropInRight","DropOutLeft");
					break;
				case "growPuff":
						if (!$.browser.msie) {
							animate("Grow","Puff");
						} else {
							animate("Grow","fadeOut");
						}
					break;
				case "DropDnUp":
					animate("DropInDown","DropOutUp");
					break;
				case "DropLtRt":
					animate("DropInLeft","DropOutRight");
					break;
				case "DropDnShrink":
					animate("DropInDown", "Shrink");
					break;
				case "growShrink":
					animate("Grow", "Shrink");
					break;
			}
		}
		
		//run only if objects exist
		if ($module.size() > 0 ) {
			$("div.content_column_top_middle > span").css("background", "none");
			
			//first run
			$moduleText.fadeIn("slow");
			timeout1 = window.setTimeout(rotateHomepageModule, 500);
		}
	}


	rotateHomepageModule();
});

/** Custom JQuery Accordion for TYPOlight **/
$(document).ready(function() {
		if( $("div.accordion").size() > 0) {
			$("div.accordion").hide();

			$("div.toggler")
			.css("margin-top","0px")
			.click( function() {
				if ($(this).is(".opened")) {
					if($.browser.msie) {
						$(this).next().hide("normal", function() { $(this).prev().removeClass("opened")});
					} else {
						$(this).next().slideUp("normal", function() { $(this).prev().removeClass("opened")});
					}
				} else {
					if($.browser.msie) {
						$(this).next().show("slow", function() { $(this).prev().addClass("opened")});
					} else {
						$(this).next().slideDown("slow", function() { $(this).prev().addClass("opened")});
					}
				}
			})
			.mouseover(function() {	$(this).addClass("accordion_over") })
			.mouseout(function() { $(this).removeClass("accordion_over") });
		}
});	//end document.ready


/** External Links **/
$(document).ready(function() {
	$("a").each(function() {
		//var baseURL = $("BASE").attr("href")	Taken out for safer match
		var baseURL = "heaf.org";
		if ($(this).children("img").size() > 0) return true;
		//var popupImg = $("<span class=\"popup\"><img src=\"tl_files/support/images/popup.gif\" border=\"0\"/></span>");
		var click_evnt = $(this).attr("onclick");
		var href = $(this).attr("href");
		click_evnt = (typeof click_evnt == "undefined") ? String(""): String(click_evnt);
		href = (typeof href == "undefined") ? String(""): String(href);
		if(click_evnt.indexOf("window.open(") > -1 || href.substr(0,7).toLowerCase() == "http://" || href.substr(0,4).toLowerCase() == "www." ) {
			//recent update (exclude sites own BASE url
			if (href.indexOf(baseURL) == -1) { 
				$(this).append(popupImg);
				//Assign a click event if not present
				if ((typeof $(this).attr("onclick") == "undefined") && (typeof $(this).attr("target") == "undefined"))
				{
					$(this).attr("onclick", "window.open(this.href); return false;");
				}
			}
		}
	});
});

/** Sidebar Content **/
$(document).ready(function() { 
	var $imgCallout = $("div#imgRotate img");
	if ($imgCallout.size()>0) {
		
		var sidebarDir = imgLocation+"sidebar/";
		var sidebarArray = ["sidebar01.jpg","sidebar02.jpg","sidebar03.jpg","sidebar04.jpg","sidebar05.jpg"];
		var sidebarArrayMax = sidebarArray.length - 1;
		var sidebarIndex = Math.round(Math.random() * (sidebarArray.length-1));
		

		if (document.images) {
			for (sidebarIndexImg in sidebarArray) {
				eval("sidebar"+sidebarIndex+" = new Image()");
				eval("sidebar"+sidebarIndex+".src = '"+(sidebarDir+sidebarArray[sidebarIndexImg])+"'");
			}
		}

		var changeCallout = function() {
			sidebarIndex = (sidebarIndex < sidebarArrayMax) ? sidebarIndex+1: 0;
			$imgCallout.parents("div#imgRotate").css({'height':$imgCallout.height(), 'width': $imgCallout.width()});
			$imgCallout.BlindUp("normal", function() { 
													$imgCallout.attr("src", String(sidebarDir+sidebarArray[sidebarIndex])); 
													window.setTimeout(rotateCallout, 500); 
											});
		}

		var rotateCallout = function() {
				$imgCallout.BlindDown("normal");
				window.setTimeout(changeCallout, 15*1000);

		}

		var firstRun = function() { $imgCallout.hide(); changeCallout(); }
		sidebarIndex++;
		window.setTimeout(firstRun, 5000);
	}
});


/** Javascript Image map **/
$(document).ready(function() {
	var jsImageMap = function(hash) {
		
		var hash = (hash) ? hash: false;
		var $obj = (hash.obj) ? hash.obj: false;
		var xMin = (hash.xMin) ? hash.xMin: 0;
		var xMax = (hash.xMax) ? hash.xMax: false;
		var yMin = (hash.yMin) ? hash.yMin: 0;
		var yMax = (hash.yMax) ? hash.yMax: false;
		var href = (hash.href) ? hash.href: false;

		if (!hash || !$obj || !href) return -1;

		var getYMax = function(y, $obj) {
			var _y = $obj[0].offsetTop;
			if ($.browser.msie) {
				$obj.parents().each(function() {
					_y+=parseInt($(this)[0].offsetTop);
				});
			}
			offsetY = _y;
			yMax = offsetY + parseInt(y);
			return yMax;
		}

		var getXMax = function(x, $obj) {
			var _x = $obj[0].offsetLeft;
			if ($.browser.msie) {
				$obj.parents().each(function() {
					_x+=parseInt($(this)[0].offsetLeft);
				});
			}
			offsetX = _x;
			xMax = offsetX + parseInt(x);
			return xMax;
		}

		if (yMax != false) yMax = getYMax(yMax, $obj);
		if (xMax != false) xMax = getXMax(xMax, $obj);

		href = $("BASE").attr("href") + href;

		$obj
			.click(function(event) {
				y = event.pageY;
				x = event.pageX;

				if (!yMax || ((y > yMin) && (y < yMax))) {
					if (!xMax || ((x > xMin) && (x < xMax))) {
						//jump to location
						location.href = href;
					}
				}
			})
			.mouseover(function(event) {
				y = event.pageY;
				x = event.pageX;

				if (!yMax || ((y > yMin) && (y < yMax))) {
					if (!xMax || ((x > xMin) && (x < xMax))) {
						//Show mouse over
						$(this).css("cursor","pointer");
					} else {
						$(this).css("cursor","");
					}
				}
			});
	}


	//Temp fix for strange URL & SRC bug
	$(document).ready(function() {
		$("a").each(function() {
			if ($(this).attr("mce_href") != "") {
				correctUrl = $(this).attr("mce_href");
				$(this).attr("href", correctUrl);
			}
			if ($(this).attr("mce_src") != "") {
				correctSrc = $(this).attr("mce_src");
				$(this).attr("src", correctSrc);
			}
		});
	});

	//Apply to banner
	hash = 
	{
		 obj: $("div#banner"),
		xMax: 280,
		yMax: 100,
		href: "index.php/home.html"
	}
	jsImageMap(hash);


	//Expand the target area of homepage papernotes
	if ($("span#note_left").size() > 0) {
		hash = 
		{
			 obj: $("span#note_left"),
			href: $("a","span#note_left").attr("href")
		}
		jsImageMap(hash);
	}
	if ($("span#note_middle").size() > 0) {
		hash = 
		{
			 obj: $("span#note_middle"),
			href: $("a","span#note_middle").attr("href")
		}
		jsImageMap(hash);
	}
	if ($("span#note_right").size() > 0) {
		hash = 
		{
			 obj: $("span#note_right"),
			href: $("a","span#note_right").attr("href") 
		}
		jsImageMap(hash);
	}


	//Flash fix since Typolight is removing all EMBED tags
	$("object[@fixcode=true]")
		.each(
			function()
			{
				//skip if EMBED tag found within
				var $this = $(this);
				var hasEMBED = $this.find("embed").size();
				if (hasEMBED == 0)
				{

					var flashWidth = $this.attr("width");
					var flashHeight = $this.attr("height");
					var flashSRC = $this.find("param[@name=movie]").attr("value");
					var embedHTML = "<embed " +
									"src='"   + flashSRC   + "' " +
									"width='" + flashWidth + "' " +
									"height='"+ flashHeight+ "' " +
									"type='application/x-shockwave-flash' " +
									"allowfullscreen='true' " +
									"allowscriptaccess='always' " +
									"></embed>";
					if ($.browser.msie)
					{
						$this.after(embedHTML);
						$this.remove();
					}
					else
					{
						$this.append(embedHTML);
					}
				}
			}
		);

});