

//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").children("ul").removeClass("hideNav");
	$("div.nav_label").removeClass("hideNav");
	
	$(".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);
								}
	);
	
	//create arrays for each section, just the ones that aren't in the nav
	var press_array = new Array("press-016.html", "press-015.html", "press-014.html", "press-013.html", "press-012.html", "press-011.html", "press-010.html", "press-009.html", "press-008.html", "press-007.html", "press-004.html", "press-003.html");
	var clients_array = new Array("company_client_list.html");
	
	var path = location.pathname;
	if ( path.search('index.html') == -1){
		for(i in press_array){
			if(path.search(press_array[i]) != -1){
				path = "press.html";	
			}
		}
		for(i in clients_array){
			if(path.search(clients_array[i]) != -1){
				path = "our_clients.html";						  
			}
		}
		if ( path ){
			$('#main_nav a[href$="' + path + '"]').addClass("active");
			$('#left_nav a[href$="' + path + '"]').addClass("active");
			if($('ul li a.active').length > 0){
				if($('ul li a.active').attr('class').search('lev2') != -1){
					$('ul li a.active').parent().parent().parent().children('a.lev1').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], from: params[2]});
	});
}

//delete from db either client or rfp
function delete_me(id_del, name_del, from_del){
	if(confirm("Do you really want to delete " + name_del + " ?")){
		$.post("../php/rfp_status.php", {id: id_del, status: "delete", from: from_del});
		//then we just make that row disappear for now
		$("tr#rowid_" + id_del).hide();
	}
}

//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_action.php", 
					{email: $("#email").attr("value")},
					function(data){
						if(data == "error"){
							alert('You have already signed up.');						
						}
						else{
							$("#email").attr("value", "thanks for signing up!");
						}
						$("#email_submit").attr("disabled","disabled");
					});
			}
			else{
				alert("please enter an email address");	
			}
			return false;
		});
}

//this function is used by homepage_swap to change the image map href
function set_map(map){
	var new_href = "";
	map = parseInt(map);
	switch(map){
		case 1:
			new_href = "/web_design_flash.html";	
			break;
		case 2:
			new_href = "/graphic_design_print.html";
			break;
		case 3:
			new_href = "/web_design_portfolio.html";
			break;
		case 4:
			new_href = "/graphic_design_print.html";
			break;
		case 5:
			new_href = "/web_design_flash.html";	
			break;
		case 6:
			new_href = "/web_design_flash.html";	
			break;
		case 7:
			new_href = "/graphic_design_print.html";
			break;
		default:
			new_href = "/portfolio.html";
	}
	$("map#homepage_imageMap area").attr("href", new_href);
}


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"));
	set_map(random_num);
	
	$(".homepage_links a").click(function(){
		$(".homepage_links a.current").removeClass("current");
		$(this).addClass("current");
		var new_num = $(this).attr("id");
		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  + ".gif")).fadeIn("slow");});
		set_map(new_num);
	});
}
	  
