
jQuery(document).ready(function(){
	
	rollover_fade();
	nav_label();
    background_pulse();
    
    
    /*
	vote();
	why_join();
	textbox_clear();
	signup();
	homepage_swap();
	*/
});

//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!
	jQuery("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>';
		jQuery("#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
			jQuery("#results").children("p#yes").animate({backgroundPosition: (yes_percent - 150)  + 'px 0px'}, 400);
			jQuery("#results").children("p#no").animate({backgroundPosition: (no_percent - 150) + 'px 0px'}, 400);
			jQuery("#results").children("p#maybe").animate({backgroundPosition: (maybe_percent - 150) + 'px 0px'}, 400);
			
			//this will clear the results and show the form again
			jQuery("#results a#back_to_poll").click(function(){
				check_vote();
				jQuery("#results").fadeOut("slow", function(){ jQuery("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();
	jQuery("form#poll_form #vote").click(function(){
		$.post("../php/poll.php", {answer: jQuery("input[ @name='answer']:checked").val(), poll_num: jQuery("input#poll_num").val()}, function(data){display_results(data);}, "json");
		$.cookie('voted','1',{expires: 7});
		//jQuery(this).attr("disabled", "true");
		//jQuery("form#poll_form #view").attr("disabled", "true");
	});
	jQuery("form#poll_form #view").click(function(){
		$.post("../php/poll.php", {view: "view", poll_num: jQuery("input#poll_num").val()}, function(data){display_results(data);}, "json");
		//jQuery(this).attr("disabled", "true");
		//jQuery("form#poll_form #vote").attr("disabled", "true");
	});
}

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


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

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


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


//swap thumbs with large image on portfolio pages
function portfolio_swap(){
	jQuery(".small_thumb img").click(function(){
		var little_src = jQuery(this).attr("src");
		var big_src = jQuery(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");
		jQuery(this).fadeOut("fast",function(){
			jQuery(this).attr("src", new_little_src).fadeIn("fast");								
		});
		jQuery(this).parent().parent().children(".featured").fadeOut("fast",function(){
			jQuery(this).attr("src", new_big_src).fadeIn("fast");																		 
		});
	});
}


//label the levels of navigation as well as make the current page active
function nav_label(){
		
	jQuery(".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");
	
	//jQuery(".nav_label").children("ul").removeClass("hideNav");
	jQuery("div.nav_label").removeClass("hideNav");
	
	jQuery(".nav_label").find("li").hover(
								function(){
									jQuery(this).addClass("selected").children("a").parent("li").parent("li").addClass("selected").end();
									//jQuery(this).children("ul.lev2").slideToggle(400);
									
								},
								function(){
									jQuery(this).removeClass("selected").children("a").parent("li").parent("li").removeClass("selected").end();
									//jQuery(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 == "/") {path = "/index.html"}
	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 ){
			jQuery('#main_nav a[href$="' + path + '"]').addClass("active");
			jQuery('#left_nav a[href$="' + path + '"]').addClass("active");
			if(jQuery('ul li a.active').length > 0){
				if(jQuery('ul li a.active').attr('class').search('lev2') != -1){
					jQuery('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
	jQuery("input").each(function(){
		jQuery(this).addClass(jQuery(this).attr("type"));
	});
	
	/*
	var toggle = function(direction, display) {
		return function() {
		  var self = this;
		  var ul = jQuery("ul", this);
		  if( ul.css("display") == display && !self["block" + direction] ) {
			self["block" + direction] = true;
			ul["slide" + direction]("slow", function() {
			  self["block" + direction] = false;
			});
		  }
		};
	  }
	  jQuery("li.menu").hover(toggle("Down", "none"), toggle("Up", "block"));
	  jQuery("li.menu ul").hide();
	*/

}


//handle rollover states for navs
function background_pulse(){
	
	var orig_bgcolor_lev2 = jQuery("#main_nav a.lev2:not(.active)").css("background-color");
	var new_bgcolor_lev2 = "rgb(214,214,214)";
	var orig_color_lev2 = jQuery("#main_nav a.lev2:not(.active)").css("color");
	var new_color_lev2 = "rgb(68,68,68)";
	
	jQuery("#main_nav a.lev2:not(.active)").hover(
				  function(){
					  jQuery(this).stop().animate({backgroundColor: new_bgcolor_lev2, color: new_color_lev2 }, 400);
				  },
				  function(){
					  jQuery(this).stop().animate({backgroundColor: orig_bgcolor_lev2, color: orig_color_lev2}, 400);
				  }
	);
	
	
	var orig_color_lev1 = jQuery("#main_nav a.lev1:not(.active)").css("background-color");
	var new_color_lev1 = "rgb(180,180,2)";
	
	jQuery("#main_nav li.lev1").hover(
				  function(){
					  jQuery(this).find("a.lev1:not(.active)").stop().animate({backgroundColor: new_color_lev1}, 400);
					  /*jQuery(this).animate({color: "rgb(255,255,255)"}, 200);*/
				  },
				  function(){
					  jQuery(this).find("a.lev1:not(.active)").stop().animate({backgroundColor: orig_color_lev1}, 400);
					  /*jQuery(this).animate({color: "rgb(157,179,196)"}, 100);*/
				  }
	);
	
	
	var orig_bgcolor = jQuery("#left_nav a:not(.active)").css("background-color");
	var new_bgcolor = "rgb(235,235,206)";
	var orig_textcolor = jQuery("#left_nav a:not(.active)").css("color");
	var new_textcolor = "rgb(102,102,0)";
	
	jQuery("#left_nav a:not(.active)").hover(
					function(){
						jQuery(this).stop().animate({backgroundColor: new_bgcolor, color: new_textcolor, backgroundPosition: "150px 5px"}, 400);
					},
					function(){
						jQuery(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(){
	jQuery(".status").change(function(){
		var params = jQuery(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
		jQuery("tr#rowid_" + id_del).hide();
	}
}

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



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

function signup(){
		jQuery("#newsletter_signup form").submit(function(){
			if(jQuery("#email").attr("value").search("@") != -1){
				$.post("../php/signup_action.php", 
					{email: jQuery("#email").attr("value")},
					function(data){
						if(data == "error"){
							alert('You have already signed up.');						
						}
						else{
							jQuery("#email").attr("value", "thanks for signing up!");
						}
						jQuery("#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_portfolio.html";	
			break;
		case 2:
			new_href = "/graphic_design_print.html";
			break;
		case 3:
			new_href = "/web_design_portfolio.html";
			break;
		case 4:
			new_href = "/web_design_portfolio.html";
			break;
		case 5:
			new_href = "/web_design_flash.html";	
			break;
		case 6:
			new_href = "/web_design_portfolio.html";	
			break;
		case 7:
			new_href = "/web_design_flash.html";
			break;
		default:
			new_href = "/portfolio.html";
	}
	jQuery("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());
	jQuery(".homepage_links a").each(function(){
		if(parseInt(jQuery(this).attr("id")) == random_num){jQuery(this).addClass("current");}							 
	});
	var old_num_random = jQuery("#homepage_image").attr("src").substr(jQuery("#homepage_image").attr("src").lastIndexOf("_") + 1);
	jQuery("#homepage_image").attr("src", jQuery("#homepage_image").attr("src").replace(old_num_random, random_num + ".gif"));
	set_map(random_num);
	
	jQuery(".homepage_links a").click(function(){
		jQuery(".homepage_links a.current").removeClass("current");
		jQuery(this).addClass("current");
		var new_num = jQuery(this).attr("id");
		var old_num = jQuery("#homepage_image").attr("src").substr(jQuery("#homepage_image").attr("src").lastIndexOf("_") + 1);
		jQuery("#homepage_image").fadeOut("fast", function(){ jQuery(this).attr("src", jQuery("#homepage_image").attr("src").replace(old_num, new_num  + ".gif")).fadeIn("slow");});
		set_map(new_num);
	});
}
	  

