$(document).ready(function(){
	$("table.styled tr:nth-child(odd)").addClass("alt");  // Add alt class to alternating rows on styled tables
	$(".table-container td a").parent().addClass("event"); // Add event class to month view 
	$(".table-container td h4").parent().parent().parent().addClass("event"); // Add event class to month view
	if($("#gallery-form #select-gallery").children().length <= 2) { $("#gallery-form").addClass("none"); $("#gallery-form select").addClass("none"); }
	if($(".gallery-attachment dl.image-list dt").length > 1){$(".gallery-attachment .gallery-directional").removeClass("none");}
	$('.calendar-list-more p a').addClass("button");  // Add button class to linked calendar paging
	$(".gallery-attachment dl.image-list dt:first").addClass("active");
	$(".gallery-attachment dl.image-list dd:first").addClass("active");
	$(".gallery-attachment dl.image-list dt:not(.active)").addClass("none");
	$(".gallery-attachment dl.image-list dd:not(.active)").addClass("none");
	
	
	$('#program h2.section').prependTo('#nav-sub');
	
	$('.tabbedcontent div.tab-row').siblings().addClass('tabcontent');
	
	var changeButtons = function(){
		// Change display state of buttons
		// If not first child or last child: display next and prev
		if( !$(".gallery-attachment dl.image-list dt:first").hasClass("active") && !$(".gallery-attachment dl.image-list dt:last").hasClass("active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").removeClass("disabled");
		} 
		// If first child: display next, disable prev
		if( $(".gallery-attachment dl.image-list dt:first").is(".active") ){
			$(".gallery-next a").removeClass("disabled");
			$(".gallery-previous a").addClass("disabled");
		} 
		// If last child: display prev, disable next
		if( $(".gallery-attachment dl.image-list dt:last").is(".active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").addClass("disabled");
		}
		
		SI_clearFooter();
	}
	$("#nav-sub ul:first > li").each(function(){	
		if($(this).index() > 9 && ($(this).hasClass("open") || $(this).hasClass("active"))){
			$(this).addClass("thirdTier");
		}else if($(this).index() > 4 && ($(this).hasClass("open") || $(this).hasClass("active"))){
			$(this).addClass("secondTier");
		}
	});
	var updateImages = function(){
		$(".gallery-attachment dl.image-list dt.active").addClass("none");
		$(".gallery-attachment dl.image-list dd.active").addClass("none");
		$(".gallery-attachment dl.image-list dt.active").removeClass("active");
		$(".gallery-attachment dl.image-list dd.active").removeClass("active");
	}
	
	$(".gallery-next a").click(function(){	
		if(!$(this).hasClass("disabled")){
			var nextImg = $(".gallery-attachment dl.image-list dd.active");
			updateImages();
			nextImg.next().removeClass("none");
			nextImg.next().next().removeClass("none");
			nextImg.next().addClass("active");
			nextImg.next().next().addClass("active");
			changeButtons();
		}
	});
	$(".gallery-previous a").click(function(){		
		if(!$(this).hasClass("disabled")){
			var prevImg = $(".gallery-attachment dl dt.active");
			updateImages();
			prevImg.prev().addClass("active");
			prevImg.prev().prev().addClass("active");
			prevImg.prev().removeClass("none");
			prevImg.prev().prev().removeClass("none");
			changeButtons();
		}
	});
	
	//set up megaNav and clean up
	navInit = function(){
		$('#nav-main ul ul')
			.each(function () {
				$(this)
					.find('li')
					.wrapAll('<span class="link-group"></span>');
				$(this)
					.find(('.spotlight'))
					.wrapAll('<span class="spotlight-group"></span>');
		});
		$('.spotlight-group')
			.each(function () {
				$(this).prependTo($(this).parent().parent());				
		});
		cleanUpLinkGroups();
		$("#nav-main ul li#nav-li-about_sandia_prep ul").append('<div class="thumb"><img src="/images/nav/meganav/about.jpg"/></div>');
		$("#nav-main ul li#nav-li-admission ul").append('<div class="thumb"><img src="/images/nav/meganav/admission.jpg"/></div>');
		$("#nav-main ul li#nav-li-academics ul").append('<div class="thumb"><img src="/images/nav/meganav/academics.jpg"/></div>');
		$("#nav-main ul li#nav-li-athletics ul").append('<div class="thumb"><img src="/images/nav/meganav/athletics.jpg"/></div>');
		$("#nav-main ul li#nav-li-giving ul").append('<div class="thumb"><img src="/images/nav/meganav/giving.jpg"/></div>');
		$("#nav-main ul li#nav-li-alumni ul").append('<div class="thumb"><img src="/images/nav/meganav/alumni.jpg"/></div>');
	}
	cleanUpLinkGroups = function(){
		$('.link-group').each(function () {
				$(this).find('li').last().addClass('last-child');
		});
		$('.spotlight-group').each(function () {
				$(this).find('li').last().addClass('last-child');
		});
	}
	navInit(cleanUpLinkGroups);
	// end megaNav
			
	
	$(".gallery-thumbnails ul li:odd").addClass("alt");
	
	
	/* unused CSS load/unload functionality
	$(".togglespan a").toggle(function(){
					$(this).addClass("switchoff");
					$(this).removeClass("switchon");
					loadjscssfile("/css/toggle.css", "css"); 
					return false;
	}, function() {
					$(this).addClass("switchon");
					$(this).removeClass("switchoff");
					removejscssfile("/css/toggle.css", "css"); 
					return false;
	});
	*/
 

	$(".runonce .search").mouseenter(function(){
			$(".search .input").show();
			$(".search .input").animate({width: 160}, 'fast',function(){
				//$("input.input").val("enter text here");
				$('.runonce').removeClass('runonce');
			});
	});
	$("input.input").focus(function() {
		if( this.value == "enter text here" ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = "enter text here";
		}
	});
	
	$('.revealButton').prependTo("#masthead");
	$("#container").parent().prepend('<div class="reveal"></div>');
	
	// On resize window
	$(window).resize(function() {
	loadControls();
		if ($(window).width() > 1200) {
 			$('.revealcycle #cycle-control').show();
		} else { 
			$('.revealcycle #cycle-control').hide();
		}
		});
	function loadControls() {
		if ($(window).width() > 1200) {
			$('.revealcycle #cycle-control').show();
		} else { 
			$('.revealcycle #cycle-control').hide();
		}
	}

	$('.revealButton a').click(function() {
		$('.reveal').slideToggle('slow', function() {
			// Animation complete.
	  });
	  return false;
	});
	
	revealCycle = function(){ //this calls the two cycles, the slides and the button as well
	
		//cookie stuff
		//var startIndex = getNext();
		//if (startIndex == null){ 
		//	startIndex = 0; 
		//}

		$('.reveal #revealContent') 
			.before('<span id="cycle-nav">\
						<span id="cycle-control">\
							<a href="#" id="cycle-prev">&lt;</a>\
							<a href="#" id="cycle-next">&gt;</a>\
						</span>\
					</span>\
					<div id="cycle-pager-reveal">\
						<div />\
					</div>') 
			.cycle({ 
				fx: 'fade', 
				cleartype: 1,
				cleartypeNoBg: true,
				slideResize: 0,
				manualTrump: false,
				fastOnEvent: 200,
				prev: '#cycle-prev',
				next: '#cycle-next', 
   				pager: '#cycle-pager-reveal div',
				pagerAnchorBuilder: function(i) {
					// for first slideshow, return a new anchor
					return '<a href="#">'+(i+1)+'</a>';
				}
			}).cycle('pause');
			
		$('.revealButton .reveal-Cycle').cycle({ 
				fx: 'fade',  
				cleartype: 1,
				cleartypeNoBg: true,
				slideResize: 0,
				manualTrump: false,
				fastOnEvent: 200,
				prev: '#cycle-prev',
				next: '#cycle-next', 
        		after: function (curr, next, opts) {
					$(window).resize();
				},
   				pager: '#cycle-pager-reveal div' ,
				pagerAnchorBuilder: function(i) {
					// for 2nd slideshow, select the anchor created previously
					return '#cycle-pager-reveal div a:eq('+i+')';
				}
			}).cycle('pause');
		$(window).resize();
					
	}
	$('#cycle-prev, #cycle-next').click(function () {
		return false;
	});	
	
	
	//take the reveal HTML from the div in teh footer and put it in .reveal, then process 
	$("#tempReveals").appendTo(".reveal");
	$("#tempReveals").removeClass("none");
				$(".reveal #PrimaryContentZone_Body").attr('id','revealContent');
				revealCycle();
				$("#revealContent > div").each(function(index) {
					$(this).attr('id','slide-' + (index + 1));
				});
	/* commented out because not working in https
	function loadReveals() {
		if (!document.getElementsByTagName || !(window.location.href.indexOf('https')!=-1)) { 
			$(".reveal").load("http://www.sandiaprep.org/reveals/cycle/index.aspx #PrimaryContentZone_Body", function(){
				$(".reveal #PrimaryContentZone_Body").attr('id','revealContent');
				
				revealCycle();
				
				$("#revealContent > div").each(function(index) {
					$(this).attr('id','slide-' + (index + 1));
				});
			});
		
		}else{
			$(".reveal").load("https://www.sandiaprep.org/reveals/cycle/index.aspx #PrimaryContentZone_Body", function(){
				$(".reveal #PrimaryContentZone_Body").attr('id','revealContent');
				
				revealCycle();
				
				$("#revealContent > div").each(function(index) {
					$(this).attr('id','slide-' + (index + 1));
				});
			});
		}
	}
	loadReveals();
	*/
		
	// On resize window
	$(window).resize(function() {
		if ($(window).width() > 1100) {
			$('.reveal #cycle-nav').show();
			$('#cycle-pager-reveal').hide();
		} else { 
			$('.reveal #cycle-nav').hide();
			$('#cycle-pager-reveal').show();
		}
	});
	
		$(window).resize();

});

