

//this function displays the results or the poll.  argument is a json object
function display_results(results){
	var yes_percent = Math.round(100 * (results.yes / results.num_votes));
	var no_percent = Math.round(100 * (results.no / results.num_votes));
	var maybe_percent = Math.round(100 * (results.maybe / results.num_votes));
	
	//we know the percentages, now let's get rid of the poll and show it!
	$("form#poll_form").fadeOut("slow", function(){ 
		var myhtml = '<p class="bar_graph" id="yes">yes ' + yes_percent + "%</p>"
		+ '<p class="bar_graph" id="no">no ' + no_percent + "%</p>"
		+ '<p class="bar_graph" id="maybe">maybe ' + maybe_percent + "%</p>"
		+ '<p class="home_right"><a id="back_to_poll">&lt;&lt; go back to poll</a></p>';
		$("#results").html(myhtml).fadeIn("fast", function(){
			//once we load in all the data, animate the backgrounds to be the bar graph
			//we use #results as the hook and look for children since they are loaded into the DOM later
			$("#results").children("p#yes").animate({backgroundPosition: (yes_percent - 150)  + 'px 0px'}, 400);
			$("#results").children("p#no").animate({backgroundPosition: (no_percent - 150) + 'px 0px'}, 400);
			$("#results").children("p#maybe").animate({backgroundPosition: (maybe_percent - 150) + 'px 0px'}, 400);
			
			//this will clear the results and show the form again
			$("#results a#back_to_poll").click(function(){
				check_vote();
				$("#results").fadeOut("slow", function(){ $("form#poll_form").fadeIn("slow"); });											
			});
		}); 
	});
}


//this function sends an AJAX POST request to the server either with your vote, or a request for the results
function vote(){
	check_vote();
	$("form#poll_form #vote").click(function(){
		$.post("../php/poll.php", {answer: $("input[ @name='answer']:checked").val(), poll_num: $("input#poll_num").val()}, function(data){display_results(data);}, "json");
		$.cookie('voted','1',{expires: 7});
		//$(this).attr("disabled", "true");
		//$("form#poll_form #view").attr("disabled", "true");
	});
	$("form#poll_form #view").click(function(){
		$.post("../php/poll.php", {view: "view", poll_num: $("input#poll_num").val()}, function(data){display_results(data);}, "json");
		//$(this).attr("disabled", "true");
		//$("form#poll_form #vote").attr("disabled", "true");
	});
}

//have they voted?
function check_vote(){
	if($.cookie('voted') == '1'){
		$("input#vote").attr("value", "you already voted!").attr("disabled", "disabled");
	}
	else{
		$("input#vote").attr("value", "vote").attr("disabled", "");
	}
}


//straight swap of images
function rollover_swap(){
	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("_faded") != -1) {
				var newSrc = $(this).attr("src").replace("_faded.png",".png");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_faded") == -1) {
				var oldSrc = $(this).attr("src").replace(".png","_faded.png");
				$(this).attr("src",oldSrc);
			}
		}
	);
}

//fade in and out
function rollover_fade(){
	$(".rollover").hover(
		function(){
			$(this).find("img").fadeTo(400, .01);
			$(this).find("a").animate( {color : "darkgrey"}, 400);
		},
		function(){
			$(this).find("img").fadeTo(400, 1.0);
			$(this).find("a").animate( {color : "lightgrey"}, 400);
		}
	);
}


//collapse and expand faqs
function faq_toggle(){
	$(".faq span").css("display", "none");
	$(".faq a").each(function(){
							  $(this).click(function(){
													 	$(this).parent().children("span").slideToggle("slow");
													 });
							  });	
}


//swap thumbs with large image on portfolio pages
function portfolio_swap(){
	$(".small_thumb img").click(function(){
		var little_src = $(this).attr("src");
		var big_src = $(this).parent().parent().children(".featured").attr("src");
		var new_little_src = big_src.replace("big", "small");
		var new_big_src = little_src.replace("small", "big");
		$(this).fadeOut("fast",function(){
			$(this).attr("src", new_little_src).fadeIn("fast");								
		});
		$(this).parent().parent().children(".featured").fadeOut("fast",function(){
			$(this).attr("src", new_big_src).fadeIn("fast");																		 
		});
	});
}


//label the levels of navigation as well as make the current page active
function nav_label(){
		
	$(".nav_label").children("ul").addClass("lev1").children("li").addClass("lev1").children("a").addClass("lev1")
	.parent().children("ul").addClass("lev2").children("li").addClass("lev2").children("a").addClass("lev2");
	
	
	$(".nav_label").find("li").hover(
								function(){
									$(this).addClass("selected").children("a").parent("li").parent("li").addClass("selected").end();
									//$(this).children("ul.lev2").slideToggle(400);
									
								},
								function(){
									$(this).removeClass("selected").children("a").parent("li").parent("li").removeClass("selected").end();
									//$(this).children("ul.lev2").slideToggle(400);
								}
	);
	
	var current_page = document.URL.substr(document.URL.lastIndexOf("/"));
	//the following lines set the current page for items that aren't in the name. this is dumb.
	if((current_page.search("ssc-") != -1) || (current_page.search("student_center.html") != -1)){current_page = "our_philanthropy.html";}
	
	if(current_page.search(".html") != -1){
		$("#main_nav a").each(function(i){
			if($(this).attr("href").search(current_page) != -1){
				$(this).addClass("active");
				if($(this).hasClass("lev2")){
					$(this).parent().parent().parent().children("a").addClass("active");	
				}
			}
		});
		$("#left_nav a").each(function(i){
			if($(this).attr("href").search(current_page) != -1){
				$(this).addClass("active");
			}
		});
	}
	
	//for the hell of it, we're gonna add classes to the inputs here too, because i'm lazy
	$("input").each(function(){
		$(this).addClass($(this).attr("type"));
	});
	
	/*
	var toggle = function(direction, display) {
		return function() {
		  var self = this;
		  var ul = $("ul", this);
		  if( ul.css("display") == display && !self["block" + direction] ) {
			self["block" + direction] = true;
			ul["slide" + direction]("slow", function() {
			  self["block" + direction] = false;
			});
		  }
		};
	  }
	  $("li.menu").hover(toggle("Down", "none"), toggle("Up", "block"));
	  $("li.menu ul").hide();
	*/

}


//handle rollover states for navs
function background_pulse(){
	
	var orig_bgcolor_lev2 = $("#main_nav a.lev2:not(.active)").css("background-color");
	var new_bgcolor_lev2 = "rgb(214,214,214)";
	var orig_color_lev2 = $("#main_nav a.lev2:not(.active)").css("color");
	var new_color_lev2 = "rgb(68,68,68)";
	
	$("#main_nav a.lev2:not(.active)").hover(
				  function(){
					  $(this).stop().animate({backgroundColor: new_bgcolor_lev2, color: new_color_lev2 }, 400);
				  },
				  function(){
					  $(this).stop().animate({backgroundColor: orig_bgcolor_lev2, color: orig_color_lev2}, 400);
				  }
	);
	
	
	var orig_color_lev1 = $("#main_nav a.lev1:not(.active)").css("background-color");
	var new_color_lev1 = "rgb(180,180,2)";
	
	$("#main_nav li.lev1").hover(
				  function(){
					  $(this).find("a.lev1:not(.active)").stop().animate({backgroundColor: new_color_lev1}, 400);
					  /*$(this).animate({color: "rgb(255,255,255)"}, 200);*/
				  },
				  function(){
					  $(this).find("a.lev1:not(.active)").stop().animate({backgroundColor: orig_color_lev1}, 400);
					  /*$(this).animate({color: "rgb(157,179,196)"}, 100);*/
				  }
	);
	
	
	var orig_bgcolor = $("#left_nav a:not(.active)").css("background-color");
	var new_bgcolor = "rgb(235,235,206)";
	var orig_textcolor = $("#left_nav a:not(.active)").css("color");
	var new_textcolor = "rgb(102,102,0)";
	
	$("#left_nav a:not(.active)").hover(
					function(){
						$(this).stop().animate({backgroundColor: new_bgcolor, color: new_textcolor, backgroundPosition: "150px 5px"}, 400);
					},
					function(){
						$(this).stop().animate({backgroundColor: orig_bgcolor, color: orig_textcolor, backgroundPosition: "170px 5px"}, 400);
					}
	);
}

//this is gonna be afunction to update the status via AJAX
function update_status(){
	$(".status").change(function(){
		var params = $(this).find("option:selected").attr("value").split(",");
		$.post("../php/rfp_status.php", {id: params[0], status: params[1]});
	});
}

//why join the ss newsletter
function why_join(){
	$("div.why").hide();
	
	$("a.why").click(function(){			  
		$("div.why").css("left", "745px").css("top", "560px");
		$("#sub div.why").css("left", "760px").css("top", "460px");
		$("#sub #newsletter_portfolio div.why").css("left","0px").css("top","70px");
		$("div.why").toggle("fast");						  
	});
	$("img.close").click(function(){
		$("div.why").toggle("fast");						  
	});
}



function textbox_clear(){
	//this is to fill or remove the text in the newsletter box
	var input_value = $("input#email").attr("value");
	$("input#email").focus(function(){
									if($(this).attr("value") == input_value){
										$(this).attr("value", "");   
								   }
	});
	$("input#email").blur(function(){
								   if($(this).attr("value") == ""){
										$(this).attr("value", input_value);   
								   }
	});
							
}

function signup(){
		$("#newsletter_signup form").submit(function(){
			if($("#email").attr("value").search("@") != -1){
				$.post("../php/signup.php", {email: $("#email").attr("value")}, function(){
					$("#email").attr("value", "thanks for signing up!");
					$("#email_submit").attr("disabled","disabled");
				});
			}
			else{
				alert("please enter an email address");	
			}
		});
}


function homepage_swap(){
	//preload the images first!
	if (document.images)
    {
      preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_1.gif"; 
	  preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_2.gif"; 
	  preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_3.gif"; 
	  preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_4.gif"; 
	  preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_5.gif"; 
	  preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_6.gif"; 
	  preload_image = new Image(816,336); 
      preload_image.src="../images/content/homepage_7.gif"; 
    }
	
	//now choose a random image for it to start on
	var random_num = Math.ceil(7*Math.random());
	$(".homepage_links a").each(function(){
		if(parseInt($(this).attr("id")) == random_num){$(this).addClass("current");}							 
	});
	var old_num_random = $("#homepage_image").attr("src").substr($("#homepage_image").attr("src").lastIndexOf("_") + 1);
	$("#homepage_image").attr("src", $("#homepage_image").attr("src").replace(old_num_random, random_num + ".gif"));
	
	$(".homepage_links a").click(function(){
		$(".homepage_links a.current").removeClass("current");
		$(this).addClass("current");
		var new_num = $(this).attr("id") + ".gif";
		var old_num = $("#homepage_image").attr("src").substr($("#homepage_image").attr("src").lastIndexOf("_") + 1);
		$("#homepage_image").fadeOut("fast", function(){ $(this).attr("src", $("#homepage_image").attr("src").replace(old_num, new_num)).fadeIn("slow");});
	});
}
	  
