$(document).ajaxSend(function(event, request, settings) {
	if (typeof(AUTH_TOKEN) == "undefined") return;
	// settings.data is a serialized string like "foo=bar&baz=boink" (or null)
	settings.data = settings.data || "";
	settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

//helper for focus and blur
function formFocusBlur(element, inputVal) {
	$(element).focus(function() {
		if(this.value == inputVal) {
			this.value = "";
		}	
	});
	$(element).blur(function() {
		if(this.value == "") {
			this.value = inputVal;
		}
		if(inputVal == "Search Here") {
			setTimeout(function() {$("#store_search form ul").hide()}, 150);
		}
	});
}

function loadStarRating() {
	var starImages;
	if ($("#comments").length) {
		starImages = $("#comments dd a img");
		starRating(starImages);
	} else if($("#forum").length) {
		starImages = $("#forum dd a img");
		starRating(starImages);
	}
}

function starRating(starImages) {
	// DOM Structure: dl(dt, dd(a(img)), dd(a(img)), dd(a(img)), dd(a(img)), dd(a(img)),)
	starImages.hover(function() {
		$(this).attr("src", "/images/star_red.gif");
		var currentdd = $(this).parent().parent();
		currentdd.prevAll("dd").children().children().attr("src", "/images/star_red.gif");
		currentdd.nextAll("dd").children().children().attr("src", "/images/star_white.gif");
	}, function() {
		var rating = $(this).parent().parent().parent().attr("name");
		var dl = $(this).parent().parent().parent();
		dl.children("dd:lt(" + rating  + ")").children().children().attr("src", "/images/star_red.gif")
		dl.children("dd:gt(" + rating + ")").children().children().attr("src", "/images/star_white.gif")
		dl.children("dd:eq(" + rating + ")").children().children().attr("src", "/images/star_white.gif")
	});
	starImages.click(function() {
		$.ajax({
			type: "POST",
			async: true,
			url: $(this).parent().attr("href"),
			success: function(msg){
				eval(msg);
			}
		});
		//$(this).parent().parent().parent().append('<dd><img src="/images/icon-check.gif" alt="voted" /></dd>');
		return false;
	});
}

//Screener filter
function moveSlider() {
	$("#handle").slider({
		range: "min",
		value: 50,
		min: 0,
		max: 100,
		slide: function(event, ui) {
			$("#min_price").html("$" + ui.value);
		},
		stop: function(event, ui) {
			$.ajax({
				type: "GET",
				async: true,
				url: '/stocks/filter',
				data: "&minprice=" + ui.value,
				beforeSend: function(){
					screenerTableFade("in");
				},
				success: function(msg){
					eval(msg);
					screenerTableFade("out");
				}
			});
		}
	});
}
function screenerTableFade(fadeinorout) {
	//Set overlay height to the same as the height of the table
	var height = $("#screenerTable table").height();
	$("#loader").height(height);
	
	//fade in / fade out effects
	if (fadeinorout == "in") {
		$("#loader").fadeIn();
	}
	if (fadeinorout == "out") {
		$("#loader").fadeOut();
	}
}

function bindTableSorter() {
	$("#screenerTable table").tablesorter({sortList:[[2,0]], widgets: ['zebra']});
	$("#screenerTable table").bind("sortStart", function() {
		var height = $("#screenerTable table").height();
		$("#loader").height(height);
	});
	$("#screenerTable table").bind("sortEnd", function() {
		$("#screenerTable table tbody tr:lt(26)").css("display", "table-row");
		$("#screenerTable table tbody tr:gt(25)").css("display", "none");
		$("#screenerTable table tbody tr:lt(51):odd").attr("class", "odd");
		$("#screenerTable table tbody tr:lt(51):even").attr("class", "");
		$("#screenerTable a.btn_red").html("More");
		$("#screenerTable a.btn_red").css("background", "#b61d1d url(/images/bg_btn_red.jpg) top right no-repeat");
	});
}

function showMore() {
	$("#screenerTable a.btn_red").toggle(function() {
		$(this).html("Less");
		$(this).css("background", "#b61d1d url(/images/btn_red_down.gif) top right no-repeat");
		$("#screenerTable table tbody tr:lt(51)").show();
	}, function() {
		$(this).html("More");
		$(this).css("background", "#b61d1d url(/images/bg_btn_red.jpg) top right no-repeat");
		$("#screenerTable table tbody tr:gt(26)").hide();
	});
}

/*For Message board log in / sign up forms*/ 
function switchForm() {
	$("#forms a.switch").click(function() {
		var showDiv = $(this).attr("href");
		if(showDiv == "#forum_signup") {
			$("#notmember").hide();
			$("#amember").show();
			$("#forms h1").html("Sign Up");
		} else if(showDiv == "#forum_signin") {
			$("#amember").hide();
			$("#notmember").show();
			$("#forms h1").html("Sign In");
		}
		$("#forms form").hide();
		$(showDiv).show();
		return false;
	});
}

function submitForm() {
	$("#new_post").submit(function() {
		$("#post_submit").attr("disabled", "true");
		$("#post_submit").attr("value", "Submitting...");
	});
	$("#new_comment").submit(function() {
		$("#comment_submit").attr("disabled", "true");
		$("#comment_submit").attr("value", "Submitting...");
	});
}

$(document).ready(function() {
	loadStarRating();
	formFocusBlur($("#ticker"), "Enter Ticker Here");
	if($("#slider").length) {
		moveSlider();
		bindTableSorter();
		showMore();
	}
	if($("#forum").length) {
		formFocusBlur($("#ticker_filter"), "Enter Ticker Here");
	}
	if ($("#forum_signin").length) {
		switchForm();
	}
	if ($("#forms").length) {
		formFocusBlur($("#forms input.exp").eq(0), "month");
		formFocusBlur($("#forms input.exp").eq(1), "year");
		submitForm();
	}
});