/**
  TradingRoom Custom extension
      VERSION: 1.2dev
     REVISION: 2
    CHANGELOG:
      20080114 - Initial creation
      20080115 - Included TradingRoom specific mootools dependancies
      20080327 - As per Dave's instructions, deleted delete statements p.cat and p.cat1
 **/

/*
  Advertisement Metadata

  Section default is NEWS.
  This file will produce a SITE and AREA tag for the current page based
  on the parameters sent through from baseAd within initPost.

  siteLookup:
    This object houses valid site values.
    A site has to exist in this object to be valid else the default
    site specified in siteAdvertDesc.site will be used
    Each entry contains an object which can have two properties:
      site: The value here will over-ride the site passed in.
        i.e:
         - 'BRISBANETIMES': {site: 'BRISTIMES'},
            BRISBANETIMES becomes BRISTIMES
      cat:
        i.e:
         - 'RUGBYHEAVEN': {cat: "SPORT"},
            RUGBYHEAVEN always has section SPORT irrespective of the cat property
            set in baseAd.
         - If a cat value is set here it is given precedence over any value in
            catSectionLookup (below)

  catSectionLookup:
    This object houses valid cat -> section conversions.
    A cat has to exist in this table to be valid else NEWS will be used.
      i.e:
       - An ad's section with cat TECHNOLOGY or DLHOME becomes TECH
       - An ad's section with cat SPORT stays as SPORT.
       - An ad's section with cat AAA becomes NEWS.

  areaExtend:
    This object houses sections' AREAs to extend with further properties.
      i.e:
       - 'BUSINESS': ['cat2', 'cat3']
        All business sections will have cat2 and cat3 appended to their AREA tag.
*/

var siteAdvertDesc = {
  group: "ONL.MH",
  site: "TRADINGROOM",
  redir: "/apps/js/adredirect.html?ad="
};

var siteLookup = {
  'TRADINGROOM': '',
  'SMH': {cat: "BUSINESS"},
  'AGE': {cat: "BUSINESS"}
};
var catSectionLookup = {
  'BUSINESS': '',
  'EXECUTIVESTYLE': 'BUSINESS',
  'NEWS': '',
  'FINANCE': ''
};
var areaExtend = {
  'BUSINESS': ['cat2']  
};
var adSizes = {
  "acquisitionpromo": "1x1",
  "acquisitionad": "1x1",
  "domainresizable": "1x1",
  "enttop": "60x18",
  "lastop": "60x18",
  "weatherad": "65x55",
  "emailfriendsponsor": "100x29",
  "toolbox": "115x28",
  "whatsonad": "140x25",
  "mmmpodcast": "145x30",
  "mmmfooty":"145x30",
  "mmmonair": "145x30",
  "mmmpoll": "145x30",
  "homeweatherad": "180x30",
  "bizquotebox": "192x16",
  "travelfeature": "192x51",
  "techfeature": "192x51",
  "sportfeature": "192x51",
  "sectiontool": "282x60",
  "scoreboard": "290x60",
  "dlbox1": "292x65",
  "dlbox2": "292x65",
  "featad": "300x36",
  "textad": "300x28",
  "traveltextad": "300x28",
  "networkad": "300x250",
  "multilink": "300x55",
  "minibar": "300x40",
  "promobox": "300x125",
  "bizlink": "300x20",
  "promobox": "300x145",
  "mmmpollresults": "330x40",
  "mmmshowsponsor": "330x40",
  "mmmpodcastlisten": "400x100",
  "acquisitionminilink": "638x20",
  "resizable": "638x68",
  "wideresizable": "645x40",
  "domaintext": "0x1",
  "iwos": "3x1"
};

if(window['FD']) {
  FD.getSiteAdvertDesc = function(adObj) {
    // Custom getter for siteAdvertDesc
    var section = "FINANCE";
    var siteName = siteAdvertDesc.site;
    var area;
    var p = adObj.params;
    if(p && p.site) {
      p.site = p.site.toUpperCase();
      if(p.site in siteLookup) {
        siteName = (siteLookup[p.site].site) ? siteLookup[p.site].site : p.site;
      }

      // Strip non-word chars from adSection
      if(p.cat) {
        p.cat = p.cat.replace(/\W/g, "").toUpperCase();
        if(siteLookup[p.site] && siteLookup[p.site].cat) {
          section = siteLookup[p.site].cat;
        } else if(p.cat && p.cat in catSectionLookup) {
          section = (catSectionLookup[p.cat] != '') ? catSectionLookup[p.cat] : p.cat;
        }
        area = p.cat;
      }

      if(p.cat1) {
        p.cat1 = p.cat1.toUpperCase();
        area += "."+p.cat1;
      }

      if(p.adspace) {
        var adspace = p.adspace.toLowerCase();
        if(adspace in adSizes) {
          p.adspace = adSizes[adspace];
        }
      }

      // Determine whether this section has additionally appended properties on its AREA prop
      if(section in areaExtend) {
        var x = 0, pName;
        while(pName = areaExtend[section][x]) {
          if(p[pName]) {
            area += "."+p[pName].toUpperCase();
          }
          x++;
        }
      }
    }

    adObj.siteName = siteName;
    adObj.site = (siteAdvertDesc.group+"."+siteName+"."+section).toUpperCase();
    adObj.area = (section+"."+siteName+((area) ? "."+area : "")).toUpperCase();
    adObj.redir = siteAdvertDesc.redir;

    return adObj
  };
  
  /* Tabs */
     function initTabs() {
	  // find all <ul> which have class 'cN-tabBox':
      var tabs = $$('ul.cN-tabBox');
      this.tabIDs = [];
      if(tabs) {
		// For all <ul> found, add 'click' event (which extends to its child elements too):
        for(var i = 0, c = tabs.length; i < c; i++) {
          tabs[i].addEvent("click", this.swapTab.create({
            bind: this,
            event: true,
			// If the event occurrs, use the <ul> id as parameter:
            arguments: tabs[i].id
          }));
          this.tabIDs.push(tabs[i].id);
		  // Remove class which makes tab box accessible if JS is disabled:
		  tabs[i].removeClass("accessibleTab");
        }
      }
    };
    
    function swapTab(e, id) {
      if(e && e.target && $(id)) {
        var a = $(e.target);
		if ( a.href && a.href.test("\#$") ) { // String ends with '#' => abort event
	        new Event(e).stop();
		}
		// If user clicked on <li>, but not on <a>, get first child of <li> (= <a>):
        if(!a.href) {
          a = a.getFirst(); // Gets first child
        }
		if (!a || a.getParent().tagName != 'H4' ) return;
		// Now we're sure user clicked on a tab => stop following the link:
        //new Event(e).stop();
		// Get all direct children of the element which was clicked on (i.e. the <li>)
		// and remove any 'selected' class:
        var buttons = $(id).getChildren().removeClass("selected");
		// Add the class to the <li> clicked on (which is the 2nd level parent element).
		// CSS takes care of the rest:
        a.getParent().getParent().addClass("selected");
      }
    };
	
	window.addEvent('domready', function(){
	  this.initTabs();
	});


}

if(window["FD"]){FD.TradingRoom=new Class({initialize:function(){this._initCC()},_initCC:function(){var B=$("currencyConverter");if(B){B.ajax.value="true";var A=new Element("input",{"type":"hidden","name":"ajax","value":"true"}).inject(B);B.addEvent("submit",this._getCC.create({bind:this,event:true}))}},_getCC:function(D){if(D&&D.target){var B=D.target;if(!isNaN(B.currNumber.value.toInt())){var C=$("currencyConverterResp");
C.addClass("loading");var A={onComplete:this._setCC};B.send(A)}else{alert("Please enter a number to convert");B.currNumber.focus()}}return false},_setCC:function(G){if(G){var E=$("currencyConverterResp");if(E){var F=$("currConverterMsg");var D=$("currConverterResult");try{G=Json.decode(G);var A=(G.exchangeRate*G.input).round(4);var C=(G.revExchangeRate*G.input).round(4);if(F){F.remove();D.setStyle("display","block")
}E.removeClass("loading");$("curr1Resp").empty().innerHTML=G.input+" "+G.curr1;$("curr2Resp").empty().innerHTML=A+" "+G.curr2;$("revCurr1").empty().innerHTML=C+" "+G.curr1;$("revCurr2").empty().innerHTML=G.input+" "+G.curr2;$("curr1Name").empty().innerHTML=G.curr1Long;$("curr2Name").empty().innerHTML=G.curr2Long}catch(B){FD.error(B)}}}return false}});var tradingRoom;function initTradingRoom(){tradingRoom=new FD.TradingRoom()
}FD.register("TradingRoom",1)}
