var MF = new function(){
 return {
   ratingsDomain:'http://ratings.thestreet.com',
   quotesDone:new YAHOO.util.CustomEvent("quotesDone"), // subscribe to this
   solrDone:new YAHOO.util.CustomEvent("solrDone"),
   mwDone:new YAHOO.util.CustomEvent("mwDone"),
   results:[], // array of all quotes returned
   qsetlen:0, // number of quote sets to grab
   nqueries:0, // number of query sets returned
   docs:[],
   displayCount:10,
   rated:{},
   /* return a list of all mf tickers from solr */
   SolrMFTickers:null,
   handleQuoteResults:function(o){

   	 this.results=o.results;
     this.checkResults();

   },
   pricesortasc:function(a,b){
      var aint = a.percentChange.replace(/,/g,"");
      var bint = b.percentChange.replace(/,/g,"");
        if (aint != "n.a." && bint != "n.a." ){
        return (aint - bint);
       }
       else
       return (0);
   },
   pricesortdesc:function(a,b){
      var aint = a.percentChange.replace(/,/g,"");
      var bint = b.percentChange.replace(/,/g,"");
       if (aint != "n.a." && bint != "n.a." ){
        return (bint - aint);
       }
       else
       return (0);
   },
   monthreturnsortasc:function(a,b){
      var aint = a.monthReturn.replace(/,/g,"");
      var bint = b.monthReturn.replace(/,/g,"");
      return (aint - bint);
   },
   monthreturnsortdesc:function(a,b){
      var aint = a.monthReturn.replace(/,/g,"");
      var bint = b.monthReturn.replace(/,/g,"");
      return (bint - aint);
   },
   checkResults:function(){

         var sortedresults = new Array();

         var results = MF.results;
		 sortedresults=results;
         this.sortedresults = sortedresults;


		 var monthreturnsortedquotes=sortedresults.sort(MF.monthreturnsortdesc);

         this.monthreturnsortedquotes=[];
		 for(var i=0;i<20;i++){
		    //if (gainerquotes[i].percentChange.indexOf("-") == -1) {
			//if (gainerquotes[i].percentChange >0) {
				//alert(gainerquotes[i].percentChange);
				this.monthreturnsortedquotes.push(monthreturnsortedquotes[i]);
			//}
           // else{
            //   break;
            // }
         }

		 var gainerquotes = sortedresults.sort(MF.pricesortdesc);

         this.gainerquotes = [];
         for(var i=0;i<20;i++){
		    //if (gainerquotes[i].percentChange.indexOf("-") == -1) {
			//if (gainerquotes[i].percentChange >0) {
				//alert(gainerquotes[i].percentChange);
				this.gainerquotes.push(gainerquotes[i]);
			//}
            //else{
            //   break;
             //}
         }

         var loserquotes = sortedresults.sort(MF.pricesortasc);

         this.loserquotes = [];
		 for(var i=0;i<20;i++){
         //  if(loserquotes[i].percentChange.indexOf("-")!=-1){
		  // if (loserquotes[i].percentChange<0){
		   	//alert(loserquotes[i].percentChange);
               this.loserquotes.push(loserquotes[i]);
           // }
			//else{
            //  break;
            //}
         }

        // MF.quotesDone.fire();
		 var ds1 = {"name":"datasource1",results:this.monthreturnsortedquotes};

	 	MF.formatResults(ds1,"active_data");


      // gainer
	  	var ds2 = {"name":"datasource2",results:this.gainerquotes};
		MF.formatResults(ds2,"gainers_data");


      // loser
	 	var ds3 = {"name":"datasource3",results:this.loserquotes};
	    MF.formatResults(ds3,"losers_data");

      //}
   },
   formatResults:function(ds,targetdiv){

   	var max_items = MF.displayCount;
	var target = YAHOO.util.Dom.get(targetdiv);
	var stuff="<ul class='stockList'>";
	try {

		for (var i=0; i<ds.results.length && i<max_items; i++) {
			var item = ds.results[i];
			var liColor="x3LtBlueBgrd";
			if (i % 2 == 0) {liColor = "whiteBgrd";}
			stuff+="<li class="+ liColor  + ">\n";
			stuff+="<div class='stock-first'>\n<a href='" + TSCM.cfg.contextRoot+ "/quote/" + item.ticker + ".html'>" + item.ticker + "</a>\n</div>\n";
			stuff+="<div>\n";
			stuff+="<span>" + item.rating + "</span>\n";
			stuff+="<span>" + item.currentPrice + "</span>\n";
			stuff+="</div>\n";
			stuff+="<div class='middle'>\n";
			var changeImg= (item.percentChange <0)? "../../css/images/quoteDownArrow.gif" :"../../css/images/quoteUpArrow.gif";
			stuff+="<img src='" + changeImg + "' width='8' height='7'/>\n" + item.percentChange + "%";
			stuff+="\n</div>\n";
			stuff+="<div class='report'>\n<a href='" + TSCM.cfg.contextRoot+ "/quote/" + item.ticker + "/details/fund.html'>Fund Details</a>\n</div>\n</li>\n";
		}
		target.innerHTML=stuff + "</ul>\n";
	 } catch(ex) {
		//target.innerHTML="<a href='/video/'>Click here for latest videos</a>";
		log(ex);
	}

   }

} ;

};




registerNS("TSCM.mf");

/* this is the main app */

TSCM.mf.Center = new function(){

   var foo = 1;
   var DOM = YAHOO.util.Dom;
   var E = YAHOO.util.Event;



   // init the app
   this.init = function(){
      //MF.topten();
	  MF.handleQuoteResults(jsonMFList);
   }

};


//YAHOO.util.Event.onContentReady('the-street-dot-com',TSCM.mf.Center.init,TSCM.mf.Center,true);

YAHOO.util.Event.on(window, "load", TSCM.mf.Center.init,TSCM.mf.Center,true);


