	$(document).ready(function(){
		
		if($.browser.msie){
			$("#ie_browser_notice").slideDown();
		}

		jQuery.extend(
		  jQuery.expr[ ":" ], 
		  { reallyvisible : function (a) { return !(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length); }}
		);
		
		if($("a[rel*=shadowbox]").size() > 0){
			Shadowbox.init({
				overlayOpacity:0.8	
			});
		}
		
		$("input").focus(function(){
			$(this).removeClass("empty");
			if($(this).attr("value") == defaultFormValues[$(this).attr("id")]){
				$(this).attr("value","");
			}
			$(this).addClass("active");
			$("label[for="+$(this).attr("id")+"]").addClass("active");
		});
		
		$("input").blur(function(){
			$(this).removeClass("active");
			$("label[for="+$(this).attr("id")+"]").removeClass("active");
			if($(this).attr("value").length===0){
				$(this).addClass("empty");
				$(this).attr("value",defaultFormValues[$(this).attr("id")]);
			}
		});
		
		var defaultFormValues = {
			"VirtualModelQuery":'Search: "Mazda 2", "Race", etc',
			"GalleryImageQuery":'Search: "Mazda 2", "Race", etc',
			"SearchQuery":'Search: "Mazda 2", "Race", etc',
			'vb_login_username':'Username',
			'vb_login_password':'password'
		};
		
		for(key in defaultFormValues){
			if($("#"+key).length > 0){
				if($("#"+key).attr("value").length === 0){
					$("#"+key).attr("value",defaultFormValues[key]);
				}
				$("#"+key).addClass("empty");
			}
		}
		
		
		$(".section .section_name").click(function(){
			$("#"+$(this).parent().attr("id")+"_content").toggle();
		});
		$(".section .section_name").hover(function(){
			$(this).css("text-decoration","underline");
		},function(){
			$(this).css("text-decoration","none");
		});
		
		$(".my_rating .stars, .small_rating").hover(function(){
			$("img[src*=_active]", this).length;
		},function(){
			return true;
		});
		
		$(".my_rating .stars a, .small_rating a").hover(function(){
			var hovered_star = $(this).attr("rating");
			$(this).parent().children().each(function(i){
				var current_star = i + 1;
				if(current_star <= hovered_star){
					$(this).children().attr("src",$(this).children().attr("src").replace("_inactive","_active"));
				}else{
					$(this).children().attr("src",$(this).children().attr("src").replace("_active","_inactive"));
				}
			});
		},function(){
			return true;
		});
		
		
		$("#close_notice").click(function(){
			$("#popup_notice").fadeOut(1000);
		});
		
		$("#close_notice").hover(function(){
			$(this).attr("src", $(this).attr("src").replace("_out","_over"));
		},function(){
			$(this).attr("src", $(this).attr("src").replace("_over","_out"));
		});
		
		$(".image_hover").hover(function(){
			$(this).attr("src", $(this).attr("src").replace("_out","_over"));
		},function(){
			$(this).attr("src", $(this).attr("src").replace("_over","_out"));
		});
		
		$(".search_site").submit(function(){
			window.location.href='/search/'+$(this).children(".SearchQuery:first").attr('value');
			return false;
		})
		
		$("#login_signup_button").click(function(){
			document.getElementById("login_signup_form").submit();
		});
		
		$(".poll_view_results_btn").click(function(){
			$(this).hide();
			$(this).parents(".poll").find(".poll_option_results").slideDown();
		});
		
		$("div.poll_option").click(function(){
			var clicked_option_id = $(this).attr("id");
			$(this).parent().find("form #PollVotePollOptionId").attr("value", $(this).attr("id").replace("poll_option_",""));
			$("div[poll_id="+$(this).attr("poll_id")+"]").each(function(){
				if($(this).attr("id") == clicked_option_id){
					$(this).addClass("selected");
				}else{
					$(this).removeClass("selected");
				}
			});
		});
		
		$("form.PollVoteForm").bind('submit', function(){
			$.ajax({
				url: "/polls/vote",
				data: $(this).serialize(),
				type:'POST',
				dataType:'json',
				success:function(json){
					alert(json.response);
					$(this).parents(".poll").find(".poll_option_results").slideDown();
					$(this).parents(".poll").find(".poll_view_results_btn").hide();
				}
			});
			return false;
		});
		
		$("div.poll_option").hover(function(){
			$(this).addClass("hovered");
		},function(){
			$(this).removeClass("hovered");
		});
		
		$("div.user_image_gallery div.image").hover(function(){
			$(this).children(".actions:first").show();
		}, function(){
			$(this).children(".actions:first").hide();
		});
		
		
		$('#top_login_button, .login_link').click(function(event){
			$(this).blur();
			$('.popup').fadeOut();
			if($("#header .login:visible").size()>=1){
				//$("#header .login").slideUp();
			}else{
				$("#header .login").slideDown();
			}
			event.stopPropagation();
		});
		
		
		$('#top_user_button').click(function(){
			$(this).blur();
			if($("#popup_user_menu:visible").size()>=1){
				$("#popup_user_menu").fadeOut();
			}else{
				$("#popup_user_menu").fadeIn();
			}
		});
		
		
		$('a.rating_star').click(function(event){
			var div = $(this).parents(".my_rating:first");
			div.html('Saving Rating...');
			div.load($(this).attr('href'));
			return false;
		});
		
		
		
		
		// ========================
		// Slider Stuff
		if($("#main-photo-slider").size() > 0){
			$("#main-photo-slider").codaSlider();
			
			var theInt = null;
			var $crosslink, $navthumb;
			var curclicked = 0;
			
			theInterval = function(cur){
				clearInterval(theInt);
			
				if( typeof cur != 'undefined' )
					curclicked = cur;
			
				$crosslink.removeClass("active-thumb");
				$navthumb.eq(curclicked).parent().addClass("active-thumb");
					$(".stripNav ul li a").eq(curclicked).trigger('click');
			
				theInt = setInterval(function(){
					$crosslink.removeClass("active-thumb");
					$navthumb.eq(curclicked).parent().addClass("active-thumb");
					$(".stripNav ul li a").eq(curclicked).trigger('click');
					curclicked++;
					if( 6 == curclicked )
						curclicked = 0;
			
				}, 3000);
			};
			
			
			
			$navthumb = $(".nav-thumb");
			$crosslink = $(".cross-link");
	
			$navthumb.click(function() {
				var $this = $(this);
				theInterval($this.parent().attr('href').slice(1) - 1);
				return false;
			});
			
			theInterval();
		}
		
		$(window).resize(function(){
			if($(window).width() <= 1250){
				$('body').addClass('narrow');
				$('body').removeClass('wide');
			}else{
				$('body').addClass('wide');
				$('body').removeClass('narrow');
			}
		});
		
		$(window).resize();
	
		$(document).click(function(){
			$('#popup_user_menu:visible').fadeOut();
			$('#header .login:visible').slideUp();
		});

		$('#header .right, #header .login').click(function(event){
			event.stopPropagation();
		});
		
		$("a[rel^=popup]").click(function(){
			var rel = $(this).attr("rel").toString();
			var options = rel.substring(rel.indexOf('[')+1, rel.length-1).split(',');
			window.open($(this).attr("href"), options[0], 'toolbar=0,scrollbars=0,location=1,statusbar=0,menubar=0,resizable=1,width='+options[1]+',height='+options[2]);
			return false;
		});
		
		$("body").bind('load', function(){
			$(".forum_bg #posts img").each(function(){
				var width = $(this).css("width").replace("px","");
				if(width > 670){
					if(width > 870){
						$(this).addClass("forum_post_image_large");
					}else{
						$(this).addClass("forum_post_image_medium");
					}
				}
			});
		});

	});

		
	function facebook_login(){
		FB.getLoginStatus(function(response){
			if (response.session) {
				document.location = '/forum/fblogin.php';
			}
		});
	}
