// JavaScript Document
//dom ready stuff
$(function(){
	
	if($('.home')){slideTimer();}
	
	/*$('.pagingWrap').each(function(){
		pagingSetup($(this).attr('id'));
	});*/
	
	$('#monthly-archive-option').change( function() {
		var theme = $("#monthly-archive-option option:selected").val();
		window.location = "?month="+theme;
	});
	
	$('ul.slider').bxSlider({
		auto: true,
		pager: true,
		controls: false,
		pause: 8000,
		autoHover: true
	});
	
	var topicActive = $('.topic.active');
	var topicGroup = $('.topic');
	
	$('#inner-tabs a').click( function() {
		var currTabObj = $(this).parent().parent().find('a.active');
		var currTab = $(currTabObj).attr('class').split(' ')[0];
		var newTab = $(this).attr('class').split(' ')[0];
		$('.activitiesContent.activities-nested.'+currTab).removeClass('active');
		$('.activitiesContent.activities-nested.'+newTab).addClass('active');
		
		// deselect current tab
		$(currTabObj).removeClass('active');
		
		$(this).addClass('active');
		if ($(this).text() === "Newest") {
			$('p.gradeIcon').removeClass(currTab).addClass(newTab).html("NEWEST ACTIVITIES");
		} else if ($(this).text() === "ALL") {
			$('p.gradeIcon').removeClass(currTab).addClass(newTab).html("ALL ACTIVITIES");
		} else {
			$('p.gradeIcon').removeClass(currTab).addClass(newTab).html("ACTIVITIES FOR GRADES " + $(this).text());
		}
		return false;
	});
	
	$('a.btn-find-schools, a.search-again').click(function() {
		
		var hasError = false;
		
		$("a.btn-find-schools").css("display", "none");
		$("#loading").css("display", "");
		
		
		
		if ($("#strZip").val() == "" && ( $("#strCity").val() == "" || $("#strState").val() == "") ) {
			hasError = true;
			$("#schoolSearchError").html("<strong>!</strong> &nbsp; Please provide a zip code or city and state.");
		}
		
		if ($("#strZip").val() != "" && !IsZip($("#strZip").val(), true) ) {
			hasError = true;
			$("#schoolSearchError").html("<strong>!</strong> &nbsp; Please provide a valid zip code.");
		}
		
		if (hasError) {
			$("#loading").css("display", "none");
			$("a.btn-find-schools").css("display", "");
			
			$("#schoolSearchError").show(300);
			window.setTimeout("hideSchoolSearchError()", 5000);
		}
		else {
			var formData = {};
			
			if ($("#strZip").val() != "") {
				formData.strZip = $("#strZip").val();
			}
			else {
				formData.strCity = $("#strCity").val();
				formData.strState = $("#strState").val();
			}
			
			$.post("/cfcs/MDR.cfc?method=searchSchools", formData, function(data){
				var jsonData = eval("(" + data + ")");
				
				
				if (jsonData.length <= 0){
					alert("Your search resulted in 0 schools found.  Please try a new search");
					
					$("#loading").css("display", "none");
					$("a.btn-find-schools").css("display", "");
					displayStep(1);
				}
				else {
					var html = "";
					
					for (var i = 0; i < jsonData.length; i++) {
						var gAddress = "http://maps.google.com/maps?q=" + jsonData[i].STREET + ", " + jsonData[i].CITY + ", " + jsonData[i].STATE;
						
						while (gAddress.indexOf(" ") >= 0) {
							gAddress = gAddress.replace(" ", "+");
						}
						
						
						html += "<ul><li class='map-it'><a href='" + gAddress + "' target='_gMap" + i + "'>map it</a></li><li class='location'>";
						html += "<strong>" + jsonData[i].NAME + "</strong><br />"; 
						html += jsonData[i].STREET + "<br />";
						html += jsonData[i].CITY + ", " + jsonData[i].STATE + " " + jsonData[i].ZIPCODE + "<br />";
						html += jsonData[i].PHONENUMBER + "</li></ul>";
						
						if (i % 2 == 1) {
							html += "<div class='clear'></div>";
						}
					}
					
					html += "<div class='clear'></div>";
					html += "<a href='#' onclick='displayStep(1);' class='search-again'>Search again</a>";
					html += "<a href='javascript: window.print();' class='print-it'>Print list</a>";
					
					$('#find-school-2').html(html);
					
					displayStep(2);
					$("#loading").css("display", "none");
					$("a.btn-find-schools").css("display", "");
				}
				
			});
			
		}
		
		return false;
	});	
		
	$('#refresh-resources').change(function() {
		var ppt = $(this).val();
		var pptName = $(this).find("option:selected").text();
		getPPT(ppt, pptName);
		//window.open(ppt, "_new");
		return false;
	});
	
});


function displayStep(stepNum) {
	if (stepNum == 1) {
		$("#strZip").val("");
		$("#strCity").val("");
		$("#strState").val("");
		
		enableDisableSchoolSearchFields();
		
		$('#find-school-2').hide();
		$('#find-school-1').show();
	} 
	else if (stepNum == 2) {
		$('#find-school-1').hide();
		$('#find-school-2').show();
	}
};


function enableDisableSchoolSearchFields()
{
	if ($("#strZip").val() == "") {
		$("#strCity").attr("disabled", false).css("background-color", "");
		$("#strState").attr("disabled", false).css("background-color", "");
	}
	else {

		$("#strCity").attr("disabled", true).css("background-color", "#cccccc");
		$("#strState").attr("disabled", true).css("background-color", "#cccccc");
	}
	
	if($("#strState").val() == "" && $("#strCity").val() == "") {
		$("#strZip").attr("disabled", false).css("background-color", "");
	}
	else {
		$("#strZip").attr("disabled", true).css("background-color", "#cccccc");
	}

}

function hideSchoolSearchError()
{
	$("#loading").css("display", "none");
	$("a.btn-find-schools").css("display", "");
	
	$("#schoolSearchError").hide(300);
}



oldTab = 'earth'	
function swqpHomeTabs(tab){
	if(tab==""||tab=="undefined"){tab="earth";}
	if($('#tab-'+tab).hasClass('current')=="current"){ return; }

	$('#'+oldTab+'_flip').hide();
	$('#tab-'+oldTab).removeClass('current')
	$('#'+tab+'_flip').show();
	$('#tab-'+tab).addClass('current')
	oldTab = tab;
}


slideNum=1;
$(function(){
	
	$('.slides li').each(function(){
		
		//hide slides
		if(slideNum!=1){$(this).hide();}
		
		//insert slide numbers
		$(this).parent().next('.slideCounter').append('<a href="#'+slideNum+'" class="pager" onclick="return false;">'+slideNum+'</a>');				  
		
		slideNum=slideNum+1;
	});
	
	//set the paging action
	$('.slideCounter .pager').click(function(){sliderPager($(this).attr('href').substring(1)-1); clearInterval(slideInt); });		   
	$('.slideCounter .pager').eq(0).addClass('current');


});

currSlide=0;
function sliderPager(el){
	//already selected...kill it
	if(currSlide==el){return;}
	//else, find this slides index and show it/hide the old
		
	$('.slides li').eq(currSlide).fadeOut(400, function () {
            $('.slides li').eq(el).fadeIn(600);
			$('.slideCounter .pager').eq(el).addClass('current');
    });

	$('.slideCounter .pager').eq(currSlide).removeClass('current');
	
	currSlide=el;
}

function slideTimer(){
	
	slideInt = setInterval(slideAction, 8000);
	
}
	
function slideAction(){
	
	if(currSlide==slideNum-2){
		sliderPager(0);
		return;
	}
	
	sliderPager(currSlide+1);
	
}



/*do not release past dev*/
function magic(){
		
		var lessonTitle = $('#actContent h1').html();
		var lessonThumb = $('img.act_img').attr('src');
		var lessonPDF = $('a.btn_dL').attr('href');
		var lessonSummary = $('div.summary').html();
		var lessonOverview = $('div.overview').html();
		var lessonObjective = $('div.objective').html();
		var lessonDifficulty = $('#difficulty b.pu').html();
		var url = window.location.pathname;
		var filename = url.substring(url.lastIndexOf('/')+1);
		var filename=filename.substring(0, filename.indexOf('.cfm'));
		var vidInt=flashvars.vidSolo;
		
		if(!($('#one-content').is(':hidden'))){
			var sciType="Earth Science";
		}else if(!($('#two-content').is(':hidden'))){
			var sciType="Life Science";
		}else if(!($('#three-content').is(':hidden'))){
			var sciType="Physical Science";
		}
		
		var data = '{"lessonTitle":"'+lessonTitle+'","lessonThumb":"'+lessonThumb+'","lessonPDF":"'+lessonPDF+'","lessonSummary":"'+lessonSummary+'","lessonOverview":"'+lessonOverview+'","lessonObjective":"'+lessonObjective+'","lessonDifficulty":"'+lessonDifficulty+'"}';
		
		$.post('/cfcs/createJson.cfc?method=createJSONFILE&filename='+filename,
		  { strLessonTitle:lessonTitle, strLessonThumb:lessonThumb, strLessonPDF:lessonPDF, strLessonSummary:lessonSummary, strLessonOverview:lessonOverview, strLessonObjective:lessonObjective, strLessonDifficulty:lessonDifficulty, strVidInt:vidInt, strSciType:sciType}
		);

}




//set up linking directly to tab
	$('#hometabs li a').address(function() {
		return $(this).attr('href').replace(/^#/, '');  
	});  
	
	$.address.change(function(event) {
		if (!event.value.indexOf("/")) {
			var goTab1 = event.value.replace("/", "").toLowerCase();
			var goTab = goTab1.replace("-science","");
			swqpHomeTabs(goTab);
		}
	}); 


//$(function(){magic()});

function pagingSetup(wrapper) {

	perPage=$('.activitiesContent ul').attr('class');
		if(perPage.indexOf('max-') != -1){
			perPage=perPage.replace("max-","");	
		}else{
			perPage=10;
		}	
	
	rowCount=1;
	thisPage=1;
	$('#'+wrapper+' .activitiesContent li').each(function() {
			
		if(rowCount > perPage){
			currentPage= new Array();
			
			if(thisPage==1){$('#'+wrapper+' .activitiesPaging').append('<a id="page-prev" href="#" onclick="return false;" class="pagePrev inactive">Previous</a><a id="page-next" href="#" onclick="return false;" class="pageNext">Next</a><a id="page-'+thisPage+'" href="#" onclick="return false;" class="current page-'+thisPage+'">'+thisPage+'</a>'); currentPage=thisPage;}
			
			thisPage = thisPage+1;	
			
			$('#'+wrapper+' .activitiesPaging').append('<a id="page-'+thisPage+'" class="page-'+thisPage+'" href="#" onclick="return false;">'+thisPage+'</a>');
			
		}
		
		if(thisPage!=1){$(this).hide();}
		
		$(this).addClass('page-'+thisPage);
		
		
		if(rowCount > perPage){
			rowCount=2;	
		}else{
			rowCount=rowCount+1;	
		}
		
	});
	
	$('#'+wrapper+' .activitiesPaging a').click(function(){
		//alert(wrapper)
	var currentPage = parseInt($('#'+wrapper+' .activitiesPaging a.current').attr('id').replace('page-',''));

	//alert(currentPage)

	if( $(this).hasClass('current') || $(this).hasClass('inactive')){return;}
		
		
		if($(this).hasClass('pageNext')){
			var nextPage=currentPage+1;
			var toShow="page-"+nextPage;
		}else if($(this).hasClass('pagePrev')){
			var nextPage=currentPage-1;
			var toShow="page-"+nextPage;
		}else{
			var toShow=$(this).attr('id');
		}

		$('#'+wrapper+' .activitiesContent li.page-'+currentPage).hide();
		$('#'+wrapper+' .activitiesPaging a').removeClass('current');

		
		$('#'+wrapper+' .activitiesContent li.'+toShow).show();
	 	$('#'+wrapper+' a.'+toShow).addClass('current');
		
		currentPage=parseInt(toShow.replace('page-',''));

		if(currentPage==thisPage){
			$('#'+wrapper+' .pageNext').addClass('inactive')
			$('#'+wrapper+' .pagePrev').removeClass('inactive');
		}else if(currentPage==1){
			$('#'+wrapper+' .pagePrev').addClass('inactive');	
			$('#'+wrapper+' .pageNext').removeClass('inactive')
		}else{
			$('#'+wrapper+' .pagePrev').removeClass('inactive');
			$('#'+wrapper+' .pageNext').removeClass('inactive');	
		
		}
		
	});
	
}

function getPPT(i, n) {
	window.scrollTo(0,0);
	if (i != '') {
		displayFloatingDiv('DL',450,250,'hidden');
		$('#divHiddenForm').html(
			"<div style=\'padding: 20px; text-align:center;\'><h1>5-Minute Refresher:</h1><h2><a href='"+i+"' target=\"_new\"'>Download: "+n+"</a></h2></div>"
		);
		$('#dimClose').css('color','#333');
	}
}	

function getPlayer(vidNum) {
		window.scrollTo(0,0);
		displayFloatingDiv('Player',356,350,'hidden');
		
		$('#divHiddenForm').load('/common/player.cfm?vidNum='+vidNum, function(){
			//$('#windowcontent').css('background','none')													
			//$('#divHiddenForm').css('background','none')		
			//$('#dimClose').css('top','-3px')
			//$('#dimClose').css('right','22px')
			$('#dimClose').css('color','#333')
		});		
	}
	



