$(function() { /* * Chart groups. * ------------- * chart: An element that contains a collection of chart elements including * the chart image element and any autorotator groups. * links: Collection of anchor elements that cause the chart image to update. * attribute: The querystring attribute to update on the chart image when * one of the links is clicked. * highlight: Boolean flag that determines whether links are highlighted by * applying a class to the clicked link. Class defaults to "selected". * autorotate: Boolean flag that determines whether clicking the links * causes any autorotator groups to update. */ var chartGroups = { market: { chart: $("#market > div.chart > div:first"), links: $("#market > .tabs a"), attribute: "ID_NOTATION", highlight: true, autorotate: false }, stock_chart: { chart: $("#stock_chart > div.chart:first > a:first"), links: $("#stock_chart > .tabs a"), attribute: 'TIME_SPAN', highlight: true }, fund_chart: { chart: $("#fund_chart > div.chart:first > a:first"), links: $("#fund_chart > .tabs a"), attribute: 'TIME_SPAN', highlight: true }, index_chart: { chart: $("#index_chart > div.chart:first > a:first"), links: $("#index_chart > .tabs a"), attribute: 'TIME_SPAN', highlight: true }, commodity_chart: { chart: $("#commodity_chart > div.chart:first > a:first"), links: $("#commodity_chart > .tabs a"), attribute: 'TIME_SPAN', highlight: true }, currency_chart: { chart: $("#currency_chart > div.chart:first > a:first"), links: $("#currency_chart > .tabs a"), attribute: 'TIME_SPAN', highlight: true } }; //Enumerate chart groups. for (cg in chartGroups) (function() { //Store options group. var group = chartGroups[cg]; //Alias chart group. var chart = group.chart; //Get and store chart image. var image = chart.children("img:first"); //Flag that determines whether the function has run once. var ranonce = false; //Enumerate chart links. for (i = 0; i < group.links.length; i++) { //Attach event handler to each link. $(group.links[i]).click(function(event) { var link = $(this); var index = group.links.index(this); //Do not perform fades on first run. if (ranonce) chart.animate({opacity: 0}); chart.queue(function(){ //Get chart image source. var src = image.attr("src"); //Ensure source contains querystring denotation character. if (src.indexOf('?') < 0) src += '?'; //Update chart image source. image.attr("src", new QueryString(src) .set(group.attribute, link.attr("name")) .applyUpperCaseTo(src) ); //Update any automatic rotators within the chart. if (group.autorotate) $(chart).children(".autorotate").each(function() { $(this).children().hide().eq(index).show(); }); //Apply highlighting to links if desired. if (group.highlight) group.links.click(function() { //Highlight link clicked. group.links.highlight(this); }).mousedown(function(event) { //Prevent focus rectangle before click for Mozilla. event.preventDefault(); }); //Run this effect and proceed to next. $(this).dequeue(); }); //Defer chart fade-in to image load event. if (!ranonce) image.load(function() { chart.animate({opacity: 1}); }); //Prevent link navigation. event.preventDefault(); }); } //Run event handler for first link of group. $(group.links[0]).trigger("click"); //Function has run once. ranonce = true; })(); }); $.fn.highlight = function(e, c) { //Default class. if (!c) c = "selected"; //Remove class from each element. $(this).removeClass(c); //Add class to the passed element. $(e).addClass(c); return this; };