timeoutID="";
analyticsTimer="";
$(document).ready(function() {
 //initialize
 $('.hpT').each(function() {
	thisType=$(this).hide();
 });

 $('.tabFeaturedStory').each(function() {
	$(this).hide();
 });
 $('.tabFeaturedStory:first-child').each(function() {
	$(this).show();
	$(this).addClass("selected");
 });

 // remove h3 from homepage tab
 $("#hpT-homepage h3").html("");

if ((($.browser.msie)&&($.browser.version.substr(0,1)<7))) {
// DEPRECATED BROWSERS
	$('#hpT-homepage').css('display','block');
	$('#hpT-homepage').addClass('selected');
	$("#hpT-homepage .hpT-carousel").each(function(d) {
		$(this).children().css("display","none");
		$(this).children(":first-child").css("display","block");
	});
	$('.hpLrD').remove();
	$('#hpTNav').css('display','block');
} else {
 // MODERN BROWSERS
 $('#hpTNav').css('display','block');
 // HOVER STATE FOR TAB NAVIGATION
 $("#hpTNav a").bind('mouseenter click', function(e){
	e.preventDefault();
	resetMddHideTimer('about',0);
	thisType=$(this).attr("id");
	if (!$(this).hasClass("selected")) hpSwitchTabs(thisType);
 });

 // DISPLAY DEFAULT TABS IN EVENTS, EMERG
 $(".hpT-even-menu .hpT-e-selected").each(function(e){
		eventName=$(this).attr('name');
		$(".ev-"+eventName).css('display','block');	
		$(".ev-"+eventName).prev("h3").css('display','block');
 });
 $("#hpT-emergency .hpT-emerg-default").css('display','block');
 $("#hpT-emergency").data("emergLoaded",0);
 $("#hpT-map").data('mapLoaded',0);

	// SET UP CAROUSELS
	 $(".hpT-carousel").each(function(d) {
			maxElements=0;
			$(this).children().css("display","none");
			$(this).children(":first-child").css("display","block");
			maxElements=$(this).children().length;

			$(this).prev().data("current","1");			
			$(this).prev().data("max",maxElements);	
			$(this).prev().data("elementType","");	
	 });		


 // SET UP AJAX NAVIGATION FOR EACH TAB ELEMENT
 $(".hpTE").each(function(e){
	thElement=$(this).attr("id");
	thElement=thElement.split("-");
	useAjax="";
	if ($(this).hasClass("hpT-carousel")) useAjax=" noAjax";
	if ((useAjax) || ((thElement[2]!==undefined)&&(thElement[2]>1))) {
		thID=thElement[1];
		if (!useAjax) {
			thCount=thElement[2];
			if (thCount>4) thCount=4;
		} else {
			thCount=$("#hpTE-t-"+thID).data("max");
		}
		
		div = $("<div class='hpTE-n"+useAjax+"'>");
		div.append('<a class="prev" title=\"View Previous Page\">&lt;</a>');
		for (i=1;i<=thCount;i++) {
			selected = (i==1) ? " selected" : "";
			div.append('<a class="hpTE-n-n'+selected+'" name="'+i+'" title="Page '+i+'"></a>');
			if (i>3) break;
		}
		div.append('<a class="next" title=\"View Next Page\">&gt;</a>');

		if ($(this).hasClass("hpT-carousel")) div.prepend('<a class="pause" title=\"Pause\"></a>');
		// MOVE THE NEW DIV INTO THIS ELEMENT'S H3
		if ($("#hpTE-t-"+thID).children(".hpT-link").size()>0) {
			$(div).insertBefore("#hpTE-t-"+thID+" .hpT-link");
		} else if ($("#hpTE-t-"+thID).children(".hpT-twitter").size()>0) {
			$(div).insertBefore("#hpTE-t-"+thID+" .hpT-twitter");
		} else {
			$("#hpTE-t-"+thID).append(div);
		}

		$("#hpTE-t-"+thID).data("eid",thID);
		$("#hpTE-t-"+thID).data("current","1");
		$("#hpTE-t-"+thID).data("max",thCount);
	}
 });

	// FUNCTIONALITY FOR EVENT TAB INTERACTIVITY
	$(".hpT-even-menu a").click(function(e) {
		e.preventDefault();

		$("#hpT-events .hpT-tabW ul").each(function(d) {
			$(this).css('display','none');
		});
		$("#hpT-events .hpT-tabW h3").each(function(d) {
			$(this).css('display','none');
		});
		eventName=$(this).attr('name');

		$(".ev-"+eventName).prev("h3").fadeIn();
		$(".ev-"+eventName).fadeIn();


		$(".hpT-even-menu a").each(function(d) {
			$(this).removeClass("hpT-e-selected");
		});

		$(this).addClass("hpT-e-selected");
	});


	// FUNCTIONALITY FOR EMERGENCY TAB INTERACTIVITY
	$(".hpT-emerg-menu a").click(function(e) {
		e.preventDefault();
		$("#hpT-emergency .hpT-emerg-proc").each(function(d) {
			$(this).css('display','none');
		});
		$("#hpT-emergency .hpT-tabW h3").each(function(d) {
			$(this).css('display','none');
		});
		eventName=$(this).attr('name');
		$("#hpT-emerg-"+eventName+" h3").css('display','block');
		$("#hpT-emerg-"+eventName).fadeIn();

		$(".hpT-emerg-menu a").each(function(d) {
			$(this).removeClass("hpT-e-selected");
		});

		$(this).addClass("hpT-e-selected");
	});

	// FUNCTIONALITY FOR YOUTUBE VIDEO PLAYING
	$(".hpT-playvid").click(function(e) {
		e.preventDefault();
		vidID=$(this).attr("name");
		if (vidID.length>0) {
 			youtubeCode='<object width="640" height="385" id="hpT-youtube"><param name="movie" value="http://www.youtube.com/v/'+vidID+'&hl=en_US&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+vidID+'&hl=en_US&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
			clz='<a id="hpT-closevid" title="close video">X</a>';
			$("#hpT-vidWrap").html(clz+youtubeCode);
		}
		$("#hpTabs").css("display","none");
		$("#hpTNav").css("display","none");
		$("#hpT-vidWrap").fadeIn("slow");
		_gaq.push(['_trackEvent', 'CampusHomepage', 'PlayVideo', 'http://www.youtube.com/watch?v='+vidID]);	
	});

	$("#hpT-closevid").live("click",function(e) {
		e.preventDefault();
		$("#hpT-vidWrap").css("display","none");
		$("#hpTNav").fadeIn("slow");
		$("#hpTabs").fadeIn("slow");
		$("#hpT-vidWrap").html("");
	});

	// analytics tracking for homepage carousel
	$("#hpT-homepage .hpT-carousel a").click(function(e) {
		e.preventDefault();
		href=$(this).attr("href");
		_gaq.push(['_trackEvent', 'CampusHomepage', 'RotatorClick', href]);
		if (href.length>0) window.location.href=href;
	});	

	// analytics tracking for homepage carousel
	$("#hpT-magazine a").click(function(e) {
		e.preventDefault();
		title=$(this).attr("title");
		href=$(this).attr("href");
		_gaq.push(['_trackEvent', 'CampusHomepage', 'Magazine2010CTs', title]);
		if (href.length>0) window.location.href=href;
	});	

	// analytics tracking for homepage carousel
	$(".hpT-hotspot a").click(function(e) {
		e.preventDefault();
		href=$(this).attr("href");
		_gaq.push(['_trackEvent', 'CampusHomepage', 'HotspotClick', href]);
		if (href.length>0) window.location.href=href;
	});	

	//SET UP H3 STYLE IN FF 3.0
	if ($.browser.mozilla) {
		vers=$.browser.version.replace(/\./g,"");
		if (vers.len==2) vers=vers*10;
		if (vers.substr(0,3)<191) $("head").append("<style type=\"text/css\">.hpT h3 div { margin-top:-15px; } a.hpT-twitter,a.hpT-link { display:none;} #hpT-homepage h3 div {margin-top:0;}");
	}
	// set up play icon div for all video icons
	$(".hpT-playvid").each(function(e) {
		newDiv=$('<div class="hpT-vidLink"></div>');
		$(this).append(newDiv);
	});

	$(".hpT-playvid").hover(function(e) {
		$(this).children(".hpT-vidLink").addClass('hpT-vidLinkON'); 
	},
	function(e) {
		$(this).children(".hpT-vidLinkON").removeClass('hpT-vidLinkON');
	});
	
//	$(".hpT-ts").hover(function(e) { stopCarousel(); }, function(e) { restartCarousel(); });

	// FUNCTIONALITY FOR ELEMENT NAVIGATION
	// Most elements will use AJAX and import the data from a script.
	// Some, though, use a carousel. This function handles both types.
	$(".hpTE-n a").click(function(e) {
		e.preventDefault();
		id=$(this).parent().parent().data("eid");
		currentPage=$(this).parent().parent().data("current");
		maxPage=$(this).parent().parent().data("max");

		useAjax=1;
		if ($(this).parent().hasClass("noAjax")) useAjax=0;
		pg=0;
		if (!$(this).hasClass("pause")) {
		stopCarousel();
			if ($(this).hasClass("prev")) {
				pg=(currentPage>1) ? currentPage-1 : maxPage;
			} else if ($(this).hasClass("next")) {
				pg=(currentPage<maxPage) ? Number(currentPage)+1 : 1;
			} else {
				pg=$(this).attr("name");
			}	
			if ((pg>0)&&(useAjax)) {
				// MAKE AJAX REQUEST, SENDING GET VARIABLES EID & P
				$.get("http://www.iupui.edu/homepage2010/", { eid: id, p: pg}, function(data){
					// update the data
					$("[id^='hpTE-"+id+"-']").fadeOut(100,function() {
						$("[id^='hpTE-"+id+"-']").html($.trim(data));
						$("[id^='hpTE-"+id+"-']").fadeIn('slow');
					});

					// toggle the appropriate selector
					$("#hpTE-t-"+id+" .hpTE-n a").each(function(d) {
						$(this).removeClass("selected");
						if ($(this).attr("name")==pg) $(this).addClass("selected");
					});
				});
			} else if (!useAjax) {
				// toggle the appropriate selector
				$("#hpTE-t-"+id+" .hpTE-n a").each(function(d) {
					$(this).removeClass("selected");
					if ($(this).attr("name")==pg) $(this).addClass("selected");
				});
				elem=$("[id^='hpTE-"+id+"-']").children().first().tagName;
		 		$("[id^='hpTE-"+id+"-']").children().css("display","none");
			 $("[id^='hpTE-"+id+"-']").children(":nth-child("+pg+")").fadeIn();
				startCarousel();
//				timeoutID = window.setInterval("rotateCarousel()", 4000);
			}
			$("#hpTE-t-"+id).data("current",pg);
		} else {
			//logic for pause/play
			if ($(this).hasClass("paused")) {
				restartCarousel();
			} else {
				stopCarousel();
			}

		}

	});

// SET UP MAP LINKS
$("#hpT-map-controls a").each(function(e){
	if (!$(this).hasClass("fullMap")) {
		$(this).append('<span class="hpT-map-controls-arrow">&darr;</span>');
	}
});

// SET UP MAP MENUS
function clearMapMenus() {
	$("#hpT-map-menus").css('display','none');
	$("#hpT-map-menus div").each(function(){
		$(this).css('display','none');
	});
}
clearMapMenus();

	$("#hpT-map-menus div").each(function(){
		$(this).html('<span class="hpT-loading"><img src="http://www.iupui.edu/map/ajax-loader.gif" alt="Loading"/></span>');
	});
	$("#map_canvas").each(function(){
		$(this).html('<span class="hpT-loading"><img src="http://www.iupui.edu/map/ajax-loader.gif" alt="Loading"/></span>');
	});

/****  A-Z Index ***/
	$(".siNav").data("page","a");
	$('.siXref a').live('click',function(e){
		e.preventDefault();
		thisPage=$(this).attr("href").split("=");
		thePage=thisPage[1].substr(0,1);
		unit=thisPage[2].substr(0);
		if ($(".siNav").data("page")!==thePage) {
			$.get("http://www.iupui.edu/index/?a="+thePage, function(data){
				$("#hpT-siteindex .hpT-tabF ul").fadeOut(100,function() {
					$(".siNav").data("page",thePage);
					$("#hpT-siteindex .hpT-tabF ul").remove();
					$("#hpT-siteindex .hpT-tabF").append($.trim(data));
					$("#hpT-siteindex .hpT-tabF ul").fadeIn('slow');
						siteIndexUnitHighlight(unit);
                                        });
                                });
		} else {
			siteIndexUnitHighlight(unit);
		}
	});

	function siteIndexUnitHighlight(unit) {
		$(".siInfo").hide();
                $("#siItem"+unit+" .siInfo").load('/index/index.php?c='+unit, function() {
			$("#siItem"+unit+" .siInfo").fadeIn('fast');
                });
	}

        $("a.siMore").live('click',function(e){
                e.preventDefault();
         	t=$(this).parent().parent().attr("name");
		siteIndexUnitHighlight(t);
        });

	$('.siNav a').click(function(e){
		e.preventDefault();
		thisPage=$(this).attr("id");
		thisPage=thisPage.split("-")[1];
		if ($(".siNav").data("page")!==thisPage) {
                               $.get("http://www.iupui.edu/index/?a="+thisPage, function(data){
	                                $("#hpT-siteindex .hpT-tabF ul").fadeOut(100,function() {
						$(".siNav").data("page",thisPage);
						$("#hpT-siteindex .hpT-tabF ul").remove();
						$("#hpT-siteindex .hpT-tabF").append($.trim(data));
						$("#hpT-siteindex .hpT-tabF ul").fadeIn('slow');
                                        });
                                });
		}
	});

	$('#hpT-map-controls a').click(function(e){
		e.preventDefault();
		if ($(this).attr('href')) window.location.href=$(this).attr('href');
		thisName=$(this).attr('name');

		if ($("#hpT-map-menus-"+thisName).css('display')=='none') {
			clearMapMenus();
			//display & position the box
			$("#hpT-map-menus-"+thisName).css('top',$("#map_canvas").offset().top-8);
			$("#hpT-map-menus-"+thisName).css('left',$(".hpT-map-"+thisName).offset().left);
			$("#hpT-map-menus").css("display","block");

			// position the preloader image
			positionPreloaders();

//			$("#hpT-map-menus-"+thisName).fadeIn('1000');
			$("#hpT-map-menus-"+thisName).slideDown('1000');

			if ($('#hpT-map-menus-'+thisName).data('loaded')!=='1') {
				// update the data 
				switch(thisName) {
					case 'bldg':
						$.get('/map/buildings.html', function(a){
							$('#hpT-map-menus-bldg').html('<a class="sortBldgCode" id="buildingSort">Sort list by building code</a><ul>'+$.trim(a)+'</ul>');
							$('#hpT-map-menus-bldg').data('loaded','1');
						});
					break;
					case 'park':
						$.get('/map/parking.html', function(a){
							$('#hpT-map-menus-park').html('<ul>'+$.trim(a)+'</ul>');
							$('#hpT-map-menus-park').data('loaded','1');
						});
					break;
					case 'dept':
						$.get('/map/departments.html', function(a){
							$('#hpT-map-menus-dept').html('<ul>'+$.trim(a)+'</ul>');
							$('#hpT-map-menus-dept').data('loaded','1');
						});
					break;
				}
			}
		} else {
			clearMapMenus();
		}
	});

 $('#hpT-map-menus-bldg ul a').live ('click',function(e) {
 e.preventDefault();
		clearMapMenus();
 exTrig($(this).attr('name'));
 });
 $('#hpT-map-menus-dept ul a').live ('click',function(e) {
 e.preventDefault();
		clearMapMenus();
 exTrig($(this).attr('name'));
 });
 $('#hpT-map-menus-park ul a').live ('click',function(e) {
 e.preventDefault();
		clearMapMenus();
 });
 

 // FUNCTION FOR PHOTO/STORY ROTATION SLIDERS
 $(".tabFeaturedStoryNext").click(function(e){
	e.preventDefault();
	thisType=$(this).parent().parent().attr("id");

	var currentStory;
	var lastStory;
	var i=0;
	// DETERMINE NUMBER OF ARTICLES, AND CURRENT ARTICLE
	$("#"+thisType+" .tabFeaturedStory").each(function() {
		i++;
		if ($(this).hasClass('selected')) currentStory=i;
		lastStory=i;
	});
	(lastStory-currentStory>0) ? newStory=currentStory+1 : newStory=1;

var options={};
	$('#'+thisType+" .tabFeaturedStory.article"+currentStory).css('display','none');
//$('#'+thisType+" .tabFeaturedStory.article"+currentStory).hide("slide",{direction:"right"});
	$('#'+thisType+" .tabFeaturedStory.article"+newStory).fadeIn('slow');
//$('#'+thisType+" .tabFeaturedStory.article"+newStory).show("slide",{direction:"left"});
	$('#'+thisType+" .tabFeaturedStory.article"+currentStory).removeClass('selected');
	$('#'+thisType+" .tabFeaturedStory.article"+newStory).addClass('selected');
 });

// FUNCTION TO CALL TO SWITCH THE SELECTED HOMEPAGE TAB
function hpSwitchTabs(thisTab,switchSelected) {
	switchSelected = typeof(switchSelected) != 'undefined' ? switchSelected : 1;
	var tabName=thisTab.substr(6);
	window.clearInterval(analyticsTimer);
	$('.hpT').css('display','none');
	// STOP ANY CAROUSELS IF THEY EXIST
	$('.hpT-carousel').each(function(d) {
		stopCarousel(this);
	});
	$(".paused").removeClass("paused");
	$('.hpT.selected').removeClass('selected');
	if (switchSelected) $('#hpTNav .selected').removeClass('selected');
	$('#hpT-'+tabName).fadeIn('fast',function() {
		if (tabName=="map") {
			// position the preloader image
			positionPreloaders();
			if ($("#hpT-map").data('mapLoaded')!==1) {
				$("#hpT-map").data('mapLoaded',1);
				$.getScript('http://maps.google.com/maps/api/js?sensor=false&async=2&callback=loadMapScripts');
			} else {
				google.maps.event.trigger(map, 'resize'); 
			}
		}
	});
	$('#hpT-'+tabName).addClass('selected');
	if (switchSelected) $('#'+thisTab).addClass('selected');
	
	// add play icon to all image graphics
	$('#hpT-'+tabName+" .hpT-playvid").each(function(e) {
		if ($(this).children("img").size()>0) {
			thisW=$(this).children("img").innerWidth();
			thisH=$(this).children("img").innerHeight();			
			imgOffset=$(this).innerHeight();
		} else {
			thisW=$(this).innerWidth();
			thisH=$(this).innerHeight();
			imgOffset=thisH;
		}
		$(this).children(".hpT-vidLink").css("width",thisW+"px");
		$(this).children(".hpT-vidLink").css("height",thisH+"px");
		$(this).children(".hpT-vidLink").css("margin-top","-"+imgOffset+"px");
	});
	// SET ANALYTICS TIMER
	if (tabName!=="homepage") analyticsTimer = window.setTimeout("analyticsTab('"+tabName+"')", 2000);

	// BEGIN ANY CAROUSELS IF THEY EXIST
	$("#hpT-"+tabName+" .hpT-carousel").each(function(d) {
		startCarousel(this);
	});

	// TAB-SPECIFIC CODE
	if (tabName=="emergency") {
		// position the preloader image
		positionPreloaders();
		if ($("#hpT-emergency").data("emergLoaded")==0) {
			$.get("http://www.iupui.edu/homepage2010/data/emerg.html", function(data){
				$($.trim(data)).insertAfter("#hpTE-35");
				$("#hpT-emergency").data("emergLoaded",1);
				$(".hpT-emerg-proc ul li").each(function(e) {
					$(this).html("&bull;&nbsp;&nbsp;"+$(this).html());
				});

			});
		}
	}
//sjh
//alert($('#hpT-homepage h3').position().top);
}

	function startCarousel(carouselElement) {
		$(".hpT-rotating").removeClass("hpT-rotating");
		$(carouselElement).addClass("hpT-rotating");
		$(".paused").removeClass("paused");
		// SET WRAPPER HEIGHT THE SAME HEIGHT AS THE FIRST CHILD
		// We do this *NOW* instead of when the carousels are initialized
		// because, when they are initialized, they are generally hidden, thus have no height!
		//
//		if (!$(carouselElement).data("height")) {
//			$(carouselElement).css("height",$(carouselElement).children(":first-child").height());
//			$(carouselElement).data("height","set");
//		}
	
		 cur=$(carouselElement).prev().data("current");
		 max=$(carouselElement).prev().data("max");
		timeoutID = window.setInterval("rotateCarousel()", 4000);
	}

	function restartCarousel() {
		$(".paused").removeClass("paused");
		timeoutID = window.setInterval("rotateCarousel()", 4000);
	}
	function stopCarousel(carouselElement) {
		$(".pause").addClass("paused");
		window.clearInterval(timeoutID);
	}

function positionPreloaders() {
	$(".hpT-loading").each(function() {
		mTop=($(this).parent().height()/2)+($(this).children("img:first-child").height()/2);
		mLeft=($(this).parent().width()/2)+($(this).children("img:first-child").width()/2);
		$(this).children("img:first-child").css('margin-top', mTop);
		$(this).children("img:first-child").css('margin-left', mLeft);
	});	
}
hpSwitchTabs("hpNav-homepage");

} //END IE6 CHECK


});

function loadMapScripts() {
	$.getScript('/map/djs.js', function() {
		zoom=15;
		$.getScript('/map/js.js.jgz',function() {
			mapLeftNav=0;
			hideRouteMarkers('points-of-interest');
		});
	});
}

function rotateCarousel() {
	carouselElement=$(".hpT-rotating");
	cur=Number($(carouselElement).prev().data("current"));
	max=$(carouselElement).prev().data("max");
	next=1;
	if (cur<max) {
		next=Number(cur)+1;
	}

	elem=$(carouselElement).prev().data("elementType");
	$(carouselElement).children().css("display","none");
	$(carouselElement).prev().data("current",next);
	$(carouselElement).prev().children(".hpTE-n").children("a").each(function(d) {
		$(this).removeClass("selected");
		if ($(this).attr("name")==next) $(this).addClass("selected");
	});
	// SAFARI 4 DOESN'T LIKE THIS FADEIN, SO JUST DISPLAY IT WITH CSS
	if (($.browser.safari)&&($.browser.version.substr(0,3)<=531)) {
		$(carouselElement).children(elem+":nth-child("+next+")").css('display','block');
	} else {
		$(carouselElement).children(elem+":nth-child("+next+")").fadeIn();
	}

}


function matchColumns() {
}


preloadImages('/homepage2010/play_icon_mo.png','/homepage2010/play_icon.png','/homepage2010/vidplayer_bg.jpg','http://magazine.iupui.edu/10Fall/_Assets/trans_90.png','http://magazine.iupui.edu/10Fall/_Assets/trans_80.png');

 // image preloader
 // Arguments are image paths relative to the current page.
function preloadImages () {
 var args_len = arguments.length; 
 imageCache=[];
 for (var i = args_len; i--;) {
 var cacheImage = document.createElement('img');
 cacheImage.src = arguments[i];
 imageCache.push(cacheImage); 
 }
}

function analyticsTab(thisTab) {
_gaq.push(['_trackEvent', 'CampusHomepage', 'Viewtab', thisTab]);	
}

