/** 
 *	Global Javascript functions.
 *	by Tsuen "Chris" Siu
 */
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	when the document is ready..
////////////////////////////////////////////////////////////////////////////////////////////////////////////

var gStudentsSpeakOut = "";
var gHowToGetInvolvedImg = "";

$(document).ready(function(){

	updateHowToGetInvolvedImg();
//	window.setInterval("updateHowToGetInvolvedImg()", 15000);
	
	initStudentsSpeakOut();
//	window.setInterval("updateStudentsSpeakOut()", 25000);	
	
	//click the logo
	$("#involved_evolving_logo").click(function(event) {
		window.location = "../index.aspx";
	});

	//default setup for the banner slide image
	$(".section_banner_slide").fadeTo("slow", 0.7);
	
	// no action
	$('.no_action').click(function(event) {
	//	event.preventDefault();
	});
	
	//user click on the comment link...
	$('.post_box .links .comment').click(function(event) { 	
		event.preventDefault();
		var sId = $(this).parents(".post_box").attr("id");
		var sTitle = escape( removeHTMLTags($(this).parents(".post_box").children(".header").html() ) ) ;
//		alert("stitle: " + sTitle );
		var sUrl = "../contact.aspx?article_id=" + sId + "&article_title=" + sTitle + "&article_type=1" ;
		window.location = sUrl;				
	});
	
	//on the move comment link..
	/*
	$('#page_on_the_move .comment').click(function(event) { 	
		event.preventDefault();
		var sId = "a000000"; //hard code for now
		var sTitle = $("#on_the_move_article .title").html();
		//alert( "title: " + sTitle );
		var sUrl = "../contact.aspx?article_id=" + sId + "&article_title=" + sTitle + "&article_type=1" ;
		window.location = sUrl;
	});
	*/
	
	// comment link in the page menu box
	$('.page_menu_box .comment').click(function(event) { 	
		event.preventDefault();
		var sId = "a000000"; //hard code for now
		//remove the html tag, encode and trim the text...
		var sTitle = escape( jQuery.trim( $("#on_the_move_article .title").text() ) );
		var sUrl = "../contact.aspx?article_id=" + sId + "&article_title=" + sTitle + "&article_type=1" ;
		window.location = sUrl;
	});
	
	//handle the sumbit contact event..
	$("form.ContactForm").submit(function(event) {
		var objValidate = validateForm(this);
		if ( (objValidate!=null) && (!objValidate.isValid) ) {
			var oErrorMsg = $(this).children(".error_msg");
			oErrorMsg.html("Error: <br />" + objValidate.error_msg);
			oErrorMsg.fadeIn("slow");
			return false;
		} else {
			//removeCarriageReturn();	
			return true;
		}
	});

	//handle the invite your friend form	
	$("form.InviteYourFriendsForm").submit(function(event) {
		//event.preventDefault();
		var objValidate = validateInivteYourFriendsFrom(this);
		if ( (objValidate) && (objValidate.isValid) ) {
			//removeCarriageReturn();	
			return true;
		} else {
			var oErrorMsg = $(this).children(".error_msg");
			oErrorMsg.html("Error: <br />" + objValidate.error_msg);
			//remove the orginal paragraph ...
			$("form.InviteYourFriendsForm .dialog_header p").hide();		
			oErrorMsg.fadeIn("slow");
			return false;
		}
		return false;
	});
	
	//handle the feedback form
	$("form.FeedBackForm").submit(function(event) {
		//event.preventDefault();
		var sShareLink = getShareLink(this);
		this.optionalTextarea3.value = sShareLink;
		var objValidate = validateFeedBackFrom(this);
		if ( (objValidate) && (objValidate.isValid) ) {
			//removeCarriageReturn();	
			return true;
		} else {
			var oErrorMsg = $(this).children(".error_msg");
			oErrorMsg.html("Error: <br />" + objValidate.error_msg);
			oErrorMsg.fadeIn("slow");
			return false;
		}
		return false;
	});
		
	/*		
	// our services 
	$("#sub_menu_item_2").mouseover(function(){
		showSectionBannerSlide("#section_banner_slide_1");
    }).mouseout(function(){
		hideSectionBannerSlide("#section_banner_slide_1");
    });
	
	// our greenness 
	$("#sub_menu_item_3").mouseover(function(){
		showSectionBannerSlide("#section_banner_slide_2");
    }).mouseout(function(){
		hideSectionBannerSlide("#section_banner_slide_2");
    });

	// our communities 
	$("#sub_menu_item_4").mouseover(function(){
		showSectionBannerSlide("#section_banner_slide_3");
    }).mouseout(function(){
		hideSectionBannerSlide("#section_banner_slide_3");
    });

	// our people 
	$("#sub_menu_item_5").mouseover(function(){
		showSectionBannerSlide("#section_banner_slide_4");
    }).mouseout(function(){
		hideSectionBannerSlide("#section_banner_slide_4");
    });
	*/
	
/* ***************************************** */
/* the share link...  bookmark it! */
	var hide = false;
	
	$('.bookmarkIt').hover(
	function(){
		//alert("mouse over");
		var offset = $(this).offset(); 
		$('.popupBookmark').css('left', offset.left);
		//$('.popupBookmark').css('left', 100);
		var oBookmark = $(this).parents().children(".popupBookmark");
		//alert("bookmark: "+ oBookmark.())
		 //var oBookmark = $(this).next("popupBookmark");
		oBookmark.fadeIn("slow");
	}, function() {
		var oPopupBookmark = $(this).parents().children(".popupBookmark"); 
		hide = setTimeout( function(){ oPopupBookmark.fadeOut("slow"); }, 250);
	});
	//mouse over the share popup dialog
	$(".popupBookmark").hover(function(){
	    if (hide) clearTimeout(hide);
	}, function() {
		var oPopupBookmark = $(this).parents().children(".popupBookmark"); 
		hide = setTimeout(function() { oPopupBookmark.fadeOut("slow"); }, 10);		
	});


	$('.popupBookmark').bookmark({compact: true, addEmail: true, addFavorite: true});
	
	/* ***************************************** */
	/* the fact box... get the section id. Then get a random section fact and put it in the fact box */
	var section_id = -1;
	if ( $("body.section_1").length > 0 ) { 
		//our services
		section_id = 0;
	} else if ( $("body.section_2").length > 0 ) { 
		//our greenness
		section_id = 1;		
	} else if ( $("body.section_3").length > 0 ) { 
		//our communities
		section_id = 2;		
	} else if ( $("body.section_4").length > 0 ) { 
		//our people
		section_id = 3;				
	} else {
		//do nothing...
	}
	
	if (section_id>=0) {
		var factStr = "";
		factStr = getFactStr(section_id);
		$(".fact_box").html("<p>" + factStr + "</p>");
	}
	
	/* xxx.aspx?dialog_id=thankyou */
	/* to popu up the invit your frineds from ... */	
	var dialog_id = gup( 'dialog_id' );
	if (dialog_id=="thankyou") {
		showInvitYourFriendsLightBox();
	} 		
	
	//scroll to the bottom of the page after the from is sumbited
	$(".btn_send").click(function(event) {
		window.scrollBy(0,30000); // horizontal and vertical scroll increments

	});
	
	//scrolling..
	var iScroll = gup( 'scroll' );
	if ( checkNotEmpty(iScroll) ) {	
			window.scrollBy(0,iScroll);
	}
	
	//on the move arachive box	
	$("#on_the_move_archives_box ul li:odd").addClass("even_row");		
	
});
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	showSectionBannerSlide
//	show the slide in the section banner
////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
function showSectionBannerSlide(slide) {
	$(slide).fadeIn("slow");
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	hideSectionBannerSlide
//	hide the slide in the section banner
////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
function hideSectionBannerSlide(slide) {
	$(slide).fadeOut("slow");
}
*/

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	addPoll
//	write the poll to the page
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function addPoll() {
	if (SWFObject) {
		var so = new SWFObject("../flash/poll_resizable.swf",  
			"poll_resizable", 
			"300",
			"160", 
			"8.0.0.0");
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent"); 
		so.write("poll_box"); 
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	gup
//	Get the query parameter.
//	EX:
//	var frank_param = gup( 'frank' );
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	showInvitYourFriendsLightBox
//	Show the tell Invite Your Friends lightbox
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function showInvitYourFriendsLightBox() {
	$('#formModalContent').modal({ 
		onShow: function (dialog) {
			dialog.overlay.fadeIn('slow', function () {
				dialog.container.fadeIn('slow', function () {
					dialog.data.hide().fadeIn('slow');	 
				});
			});
		}, onClose: function (dialog) {
			dialog.data.fadeOut('slow', function () {
				dialog.container.fadeOut('slow', function () {
					dialog.overlay.fadeOut('slow', function () {
						$.modal.close();
					});
				});
			});
		}
	});
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 	getFactStr(section_id)
//	Get the fact string using the section id (number).
//	Ex. getFactStr(1); // to get a random greenness string
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getFactStr(section_id) {
	var returnStr = "";
	var data = new Array();

	/* service */
	data[0] = new Array();	
	data[0][0] = "ARAMARK is one of America's largest employers of dietitians, and the only professional services company to partner with the American Dietetic Association (ADA).";
	data[0][1] = "Not only will you find ARAMARK at over 1,800 K-12 schools, colleges and universities across the country, we've also served at 14 Olympic Games since 1968.";
	data[0][2] = "ARAMARK was named one of \"America's Most Admired Companies\" by <i>FORTUNE</i> Magazine, which marks our 10th straight year among the top three in our industry.";
	data[0][3] = "ARAMARK serves over 80% of the Princeton Review's listing of America's Best Colleges and Universities.";
	
	/* greenness */
	data[1] = new Array();	
	data[1][0] = "ARAMARK serves 15 national and state parks and forests, with over 11 million visitors a year.";
	data[1][1] = "ARAMARK is using the EPA's SmartWay&reg; program to choose fleet vehicles that are more efficient and better for the environment.";
	data[1][2] = "Whenever possible, we use soy-based ink in our ARAMARK marketing materials because it's easier to recycle the paper we print on and it's more environmentally friendly.";
	data[1][3] = "Green French fries? You bet! ARAMARK is working to implement a program that recycles spent frying oil so it can be used as biodiesel fuel.";
	
	/* communities */
	data[2] = new Array();	
	data[2][0] = "ARAMARK has announced a national, multiyear commitment to City Year in the United States, involving employee volunteerism, financial resources, and leadership support.";
	data[2][1] = "As part of our Building Community National Volunteer Tour, every month over the next two years, ARAMARK will be launching relationships with community center networks in nearly 30 cities including Chicago, Los Angeles, New York City and more.";
	data[2][2] = "A recent fundraising program by ARAMARK UK brought water with a twist to a small South African school. As children play on their new PlayPump&reg; merry-go-round, the rotations drive a pump that brings fresh, clean underground water into a community storage tank.";
	data[2][3] = "In the midst of hurricane devastation along the Gulf Coast, the ARAMARK Charitable Fund donated a gift of $500,000 to the American Red Cross for disaster relief. Also, personal donations made by ARAMARK Employees were matched dollar for dollar by the company.";
	
	/* people */	
	data[3] = new Array();		
	data[3][0] = "ARAMARK has been recognized as one of the \"40 Best Companies for Diversity\" by <i>Black Enterprise</i> magazine.";
	data[3][1] = "ARAMARK was ranked among Top 100 \"Best Places to Launch a Career\" by <i>BusinessWeek</i> magazine.";
	data[3][2] = "ARAMARK understands that a mosaic of backgrounds, styles, perspectives, values and beliefs is indispensible to our business. Since 1999, Our Kaleidoscope initiative has helped us recruit, retain and develop a diverse, high-performance workforce.";
	data[3][3] = "AIM Services (our joint venture in Japan) has been honored repeatedly for its efforts to employ the disabled, most recently in the cities of Tokyo, Nagoya, and Takarazuka.";
	
	if( (section_id >= 0) && (section_id <=4) ) {
		returnStr = data[section_id][rand(data.length-1)];
	}
	else {
		alert ("unknow section_id in getFactStr() ");
	}
	
	return returnStr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Get the section number by the class in the body.
//	
//	
//	
////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	rand( n )
//	Get a random number between 1 to n
//	Ex: alert( rand( 100 ) );
//	
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function rand( n ) {
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Randomly display the image for the "How to Get Involved"
//	
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function updateHowToGetInvolvedImg() {
//	gHowToGetInvolvedImg
	var img_name = new Array("1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "12.jpg", "13.jpg", "14.jpg", "15.jpg");
	var l = img_name.length;
	var rnd_no = Math.floor(l*Math.random());
	
//	if (gHowToGetInvolvedImg != rnd_no ) {
		$('#how_to_get_involved_img').fadeOut(500, function() {
		    var sSrc = $(this).attr("src");
		    var sPath = "";
		    var srcArray = sSrc.split("/");
		    for (i = 0; i < srcArray.length; i++) {
		        if (i != srcArray.length - 1) {
		            sPath = sPath + srcArray[i] + "/";
		        }
		    };
		    sPath = sPath + img_name[rnd_no];
		    $(this).attr('src', sPath);
		    $(this).fadeIn(2000);
		});
//	}
//	gHowToGetInvolvedImg = rnd_no
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Randomly display student speak out
//	
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function updateStudentsSpeakOut() {
//	var sso_name = new Array("#student_speak_out_1", "#student_speak_out_2", "#student_speak_out_3", "#student_speak_out_4", "#student_speak_out_5", "#student_speak_out_6", "#student_speak_out_7", "#student_speak_out_8" );
//	var sso_name = new Array("#student_speak_out_1", "#student_speak_out_2", "#student_speak_out_7", "#student_speak_out_8" );
	var sso_name = new Array("#student_speak_out_1", "#student_speak_out_2", "#student_speak_out_7", "#student_speak_out_8", "#student_speak_out_9", "#student_speak_out_10", "#student_speak_out_11", "#student_speak_out_12" );

	var l = sso_name.length;
	var rnd_name = sso_name[Math.floor(l*Math.random())];
	
	if (gStudentsSpeakOut!=rnd_name ) {
		/*
		$(gStudentsSpeakOut).slideUp("slow");
		$(rnd_name).slideDown("slow");
		*/
		$(gStudentsSpeakOut).fadeOut(5000, function(event){
			$(rnd_name).fadeIn(5000);
		});
	} 
	gStudentsSpeakOut = rnd_name;
}

function initStudentsSpeakOut() {
//	var sso_name = new Array("#student_speak_out_1", "#student_speak_out_2", "#student_speak_out_3", "#student_speak_out_4", "#student_speak_out_5", "#student_speak_out_6", "#student_speak_out_7", "#student_speak_out_8" );
	var sso_name = new Array("#student_speak_out_1", "#student_speak_out_2", "#student_speak_out_7", "#student_speak_out_8", "#student_speak_out_9", "#student_speak_out_10", "#student_speak_out_11", "#student_speak_out_12", "#student_speak_out_13" );
	var l = sso_name.length;
	var rnd_name = sso_name[Math.floor(l*Math.random())];
	$(rnd_name).fadeIn("slow");
	gStudentsSpeakOut = rnd_name;
}


/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
/*
function removeHTMLTags(){
 	if(document.getElementById && document.getElementById("input-code")){
 		var strInputCode = document.getElementById("input-code").innerHTML;

  			//This line is optional, it replaces escaped brackets with real ones, 
  			//i.e. < is replaced with < and > is replaced with >

 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
 		alert("Output text:\n" + strTagStrippedText);	
   // Use the alert below if you want to show the input and the output text
   //		alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
 	}	
}
*/

function removeHTMLTags( str ){
	var strTagStrippedText = str;
	if (str) {
		strTagStrippedText = str.replace(/<\/?[^>]+(>|$)/g, "");
//		alert("Output text:\n" + strTagStrippedText);	
	}
	return strTagStrippedText;
}
