$(function() {
var qs = new QueryString()
var request = "/www/sub/ajax_topflop_list.html";
var factsheet = "/factsheet/factsheet.html?ID_NOTATION=";
$("#market > .tabs a").click(function() {
qs.set("ID_NOTATION", this.name);
var gainers = $("#gainers > table:first > tbody");
var losers = $("#losers > table:first > tbody");
var movers = $("#movers > table:first > tbody");
var requestDataAndAppendElements = function(e, type) {
e = $(e);
qs.set("TYPE", type);
//Request page.
$.getJSON(request, qs.toUpperCaseString(), function(data) {
$.each(data, function(i, item) {
//Append table row.
e.append(
"
"
+ '| ' + item.name + " | "
+ "" + item.price + " | "
+ "" + item.performance + " | "
+ (type == "VOLUME" ? "" + item.volume + " | " : '')
+ "
"
);
//Hide appended row.
e.children("tr:last").css("display", "none")
//Apply performance class.
.children("td:eq(2)").addClass(item.performance_class);
});
//Apply classes to row and row cells.
e.find("tr > td:nth-child(2), tr > td:nth-child(3), tr > td:nth-child(4)")
.addClass("right");
e.children("tr:odd").addClass("alternate");
//Fade in row group.
e.children("tr").css("opacity", 0).fadeGroup(null, 1);
});
};
//Remove old rows and request new data.
movers.children("tr").fadeGroup(null, 0, true, true, function() {
requestDataAndAppendElements(movers, "VOLUME");
});
gainers.children("tr").fadeGroup(null, 0, true, true, function() {
requestDataAndAppendElements(gainers, "TOP");
});
losers.children("tr").fadeGroup(null, 0, true, true, function() {
requestDataAndAppendElements(losers, "FLOP");
});
//Update market chart 'more charts' link.
var chartLink = $("#market > .more > a:first");
chartLink.attr("href",
new QueryString(chartLink.attr("href")).set("ID_NOTATION", this.name)
.applyUpperCaseTo(chartLink.attr("href"))
);
});
});
$.fn.fadeGroup = function(speed, opacity, reverse, remove, fn) {
if (!speed) speed = 100;
var es = $(this);
if (reverse) es = $(es.get().reverse());
var parent = es.parent(":first");
es.each(function() {
var e = $(this);
parent.queue(function() {
e.css("display", '').fadeTo(speed, opacity, function() {
parent.dequeue();
if (remove) e.remove();
});
});
});
if (fn) {
if (parent.length)
parent.queue(function(){
fn.call(es.get());
$(this).dequeue();
});
else fn();
}
return this;
};