

function email(name, domain) {
	var address = name + "\u0040" + domain;
	var url = "mailto:" + address;
	document.write("<a href=\"" + url + "\">Send Email</a>");
}


$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};


function quickFinderGetProgram(id, searchtype) {
	if (id != '') {
		$("#quick-finder-ajax-program-url").html('<option value="">Loading...please wait</option>');
		$.getJSON("http://www.amerispan.com/ajax/quick_finder.asp",{search_id: id, search_type: searchtype}, function(j){
			var options = '<option value="">Show All Programs</option>';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			$("#quick-finder-ajax-program-url").html(options);   // fill in program select
		})
	}
}


function quickFinderGo(selval) {
	if (selval == '') {
		window.location = $("#quick-finder-ajax-default-url").val();
	} else {
		window.location = selval;
	}
}


function quickFinderGoCategory() {
	if ($("#quick-finder-ajax-category-id").val() == '') {
		window.location = $("#quick-finder-ajax-default-url").val();
	} else {
		window.location = $("#quick-finder-ajax-default-url").val() + "?Category_ID=" + $("#quick-finder-ajax-category-id").val();
	}
}


function quickFinderGoLanguage() {
	if ($("#quick-finder-ajax-language-id").val() == '') {
		window.location = $("#quick-finder-ajax-default-url").val();
	} else {
		window.location = $("#quick-finder-ajax-default-url").val() + "?Language_ID=" + $("#quick-finder-ajax-language-id").val();
	}
}


function resetQuickFinder() {
	$("#quick-finder-ajax-program-url").html('<option value="">Show All Programs</option>');
}

function fixGoogleTranslate(googLoop) {
	if (googLoop < 5) {
		setTimeout(function(){
			$(".goog-te-gadget").css('fontSize','8px');
			$(".goog-te-gadget").css('whiteSpace','normal');
			$(".goog-te-combo").css('width','120px');
			googLoop++;
			fixGoogleTranslate(googLoop);
		},1000);	
	}
}

function toggleStory(trigger) {
  $(".fr-story-cell td").hide();
  trigger.parent().next().children().show();
}

/* POPUP FUNCTIONS */
//SETTING UP OUR POPUP
var popupStatus = 0;
var activePopup = '';
var activeSection = '';

function loadPopup(thisPopup){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(thisPopup).fadeIn("slow");
		popupStatus = 1;
		activePopup = thisPopup;
	}
}

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(activePopup).fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(".app-popup").height();
	var popupWidth = $(".app-popup").width();
	//centering
	$(".app-popup").css({
		"position": "absolute"
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

/* END POPUP FUNCTIONS */

/*scrolling slider*/
var homesliderTimer;
var homesliderPlayState = 'play';
var homesliderCurrent;

function totalScroll(li) {
	
	var currentLi = li;
	if (currentLi == 0) {
		// make currentLi the first li
		currentLi = $('#homeslider').children('li:first');
	}
	var nextLi = $(currentLi).next('li');
	
	// if there is no next li, meaning this is the last li, make nextLi the first
	if ($(currentLi)[0] == $('#homeslider').children('li:last')[0]) {
		nextLi = $('#homeslider').children('li:first');
	}
	
	// scroll to the nextLi
	$('#homeslider-inner').scrollTo($(nextLi), 500);
	$('#homeslider-tabs li').removeClass('active');
	$('#homeslider-tabs li.' + $(nextLi).attr('id')).addClass('active');
	
	//set play state
	homesliderPlayState = 'play';
	homesliderCurrent = nextLi;
	
	// call totalScroll again in 5 seconds
	homesliderTimer = setTimeout(function(){totalScroll($(nextLi))}, 5000);
}

function homesliderToggle() {
    if(homesliderPlayState == 'play') {
        clearTimeout(homesliderTimer);
        homesliderPlayState = 'temp-paused';
    } else if(homesliderPlayState == 'temp-paused') {
        homesliderPlayState = 'play';
        $('#homeslider-play').text('Pause');
        homesliderTimer = setTimeout(function(){totalScroll($(homesliderCurrent))}, 500);
    } else if(homesliderPlayState == 'hard-paused') {
        clearTimeout(homesliderTimer);
        $('#homeslider-play').text('Play');
    }
}


/* end scrolling slider*/


$(document).ready(function(){
	//fix font size on google translate text
	var googLoop = 0;
	fixGoogleTranslate(googLoop);
	
	// reset quick finder
	resetQuickFinder();
	
	//show quick finder box on sections' default pages
	//$("#quick-finder-ajax").wait().slideDown("slow");
	
	//if type is chosen in quick finder, get programs thru ajax
	$("select#quick-finder-ajax-search-id").change(function(){
		quickFinderGetProgram($(this).val(), $("#quick-finder-ajax-type").val());
	});
	
	//if a program is chosen in quick finder, go to the ajax supplied url
	$("select#quick-finder-ajax-program-url").change(function(){
		quickFinderGo();
	});
	
	//if go button is clicked in quick finder, go to the url
	$("#quick-finder-ajax-go").click(function(){
		quickFinderGo();
	});
	
	//open new window if vol options box is clicked
	$(".voltypehelp").click(function(){
		window.open("http://www.amerispan.com/volunteer_intern/placement_options.asp","VolunteerTypes");
	});
	
	//if a category is chosen in quick finder, go to the ajax supplied url
	$("#quick-finder-ajax-category-id").change(function(){
		quickFinderGoCategory();
	});
	
	//if a language is chosen in quick finder, go to the ajax supplied url
	$("#quick-finder-ajax-language-id").change(function(){
		quickFinderGoLanguage();
	});
	
	//
	$(".quick-finder-ajax-select").change(function() {
		quickFinderGo($(this).val());
	});
	
	$(".fr-story-trigger").click(function(){
	  toggleStory($(this));
	});
	
	//LOADING POPUP
	$("#already_enrolled").change(function(){
	  if ($("#already_enrolled").val() == 'no'){
	    //centering with css
  		centerPopup();
  		//load popup
  		loadPopup('#fr-enrolled-popup');
	  }
	});
	
	//CLOSING POPUP
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	//start homeslider scrolling
	homesliderTimer = setTimeout(function(){totalScroll(0)}, 6000);
	$('#homeslider li').hover(function(){
	    homesliderToggle();
	});
	$('#homeslider li').click(function(){
	    homesliderPlayState = 'hard-paused';
	    homesliderToggle();
	});
	$('#homeslider li *').click(function(){
	    homesliderPlayState = 'hard-paused';
	    homesliderToggle();
	});
	$('#homeslider-tabs li').click(function(){
	    // scroll to the specified li
    	$('#homeslider-inner').scrollTo($('#homeslider li#' + $(this).attr('name')), 500);
    	$('#homeslider-tabs li').removeClass('active');
    	$(this).addClass('active');

    	//set play state
    	homesliderPlayState = 'hard-paused';
    	homesliderCurrent = $('#homeslider li#' + $(this).attr('name')); 
    	homesliderToggle();
	});
	$('#homeslider-play').click(function(){
	    if($('#homeslider-play').text() == 'Play') {
	        //it was paused. start slide again
	        homesliderPlayState = 'temp-paused';
	    } else {
	        homesliderPlayState = 'hard-paused';
	    }
	    homesliderToggle();
	});
    
}); // end ready()
