/*
	name : com.aol.bv.MostCommented
	author : Girish Prasad & Sajit Mouvery
	$Revision 2$
	Updated by Girish Prasad
	(C) Copyright 2008 AOL LLC
	///////////////////////////
	///////////////////////////		
	dependencies : mootools 1.2
	///////////////////////////
	///////////////////////////
*/

if( typeof com === "undefined" ) { var com = {}; }
if( typeof com.aol === "undefined" ) { com.aol = {}; }
if( typeof com.aol.bv === "undefined" ) { com.aol.bv = {}; }


com.aol.bv.MostCommented = new Class({
    //Declaring all the variable and moving to initialize function.
    Implements: [Options, Events],
  
        options : {
	MainId : '',
        TabList : [],
        DataList : [],
        DataURL : '/most-commented-ajax-module/', //This URL is only for reference
        errorMsg : 'Sorry, we are having trouble getting that - please try again',
        css : {  noDataClss : 'McNoData', dataClss : 'McWithData', postClss: 'post'  }
                               
	},
       
     //setting the options with values by calling function of the markup.      
     initialize : function (options)
	{
	  //for setting options value
          this.setOptions(options);
          //Calling all the classes on initialization of the page.
          
          this.setInitialClasses();
          //Ajax call for initial display 7days data
          this.InitialData();
                          		
	},
        
        
     setInitialClasses : function()
        {
         var topUl = $(this.options.MainId).getElementsByTagName('ul');
         //Return false if there is no UL under Main Id
         if(!$defined( topUl ) ) { return false; }
         //To Get all the Data UL under the Day Tabs
         var subUl = topUl[0].getElementsByTagName('ul');	
	 this.storeButtons( subUl );
                
        },
        storeButtons : function( list )
        {
            $$( list ).each( function( el ) {
                //For Storing the data ULs under the tabs
                this.options.DataList.include( $( el ) );
                //To Get the Day Tab
		var daytab = el.getParent( 'li' );
                //Storing the tabs to an array
                this.options.TabList.include( $( daytab ) );
                //Adding the function on click on tab
		daytab.addEvent('click', this.displayData.bind(this, daytab));
	    }, this );
        
        },
        displayData : function(tabs, e1)
        {
          //To remove all the hightlighted Tab
          this.options.TabList.each( function( item ){item.getElement('a').removeClass( 'current' );}, this);
          //To remove all the commentes under Tab
          this.options.DataList.each( function( item ){item.removeClass( 'show' );}, this);
          //To Highlight clicked Tab
	  tabs.getElement('a').addClass( 'current' );
          //To show the comments under clicked Tab
          tabs.getElement('ul').addClass( 'show' );
          //Making Ajax Call to retrive the Comments
          this.doAjaxCall(tabs.getElement('ul'),this);
          //Adjusting the height of comments field.
          this.checkForData(tabs, e1);
          //To Deactivate the Hyper link
          //return false;
         
         
         
        },
        InitialData : function()
        {
            //To Get 7 days Tab
            var d3= (this.options.DataList[this.options.DataList.length-2]);
            //alert(this.options.DataList.length);
           
            
            //Making Ajax Call to retrive the Comments
            this.doAjaxCall(d3,this);    
         
        },
        //Using Ajax call to get comments onSuccess of request calling attachData function, on failure of request calling attachDataOnFail function
        doAjaxCall : function(datanum)
        {
           
            var lis = datanum.getElementsByTagName('li');
            
            if(lis.length <= 0)
            {
            
            datanum.set( 'send', { onSuccess : function( data ) {this.attachData(data, datanum);}.bind( this ), 
				   method : 'get',
                                   onFailure : function(){this.attachDataOnFail(datanum);}.bind( this )
				
				} );
            //To Assign the URL for Ajax Requests
            datanum.send(this.buildURL( datanum ));
            }
        },
        //Function for buidling URL for Ajax Call
        buildURL : function(datatab)
        {
            //Only Reference Call No Actual URL
            
            return this.options.DataURL + datatab.getParent('li').getElement('a').get('duration')+'?'+$random(1, 99999);
        },
        //To add the comments to the tab on success of request and adjusting the height of comments field.
        attachData : function(data, tab)
        {
            tab.set('html', data);
            
            this.checkForData(tab);
              
          
        },
        //On failure of ajax request adding text to the comments field and adjusting the height of comments field.
        attachDataOnFail : function(tab)
        {
            tab.set('html', this.options.errorMsg);
            this.checkForData(tab);
                       
        },
        //adjusting the height of comments field.
        checkForData : function(tab, e1)
        {
        
         var lis = tab.getElementsByTagName('li');
         tab.getParent('ul').className='';
         if(lis.length <=0)
          {
           tab.getParent('ul').addClass(this.options.css.noDataClss);
            
          }else {
           tab.getParent('ul').addClass(this.options.css.dataClss+lis.length);
           
               //alert($type(lis));
               
                for(var j=0;j < lis.length; j++ )
                   {
                    //as addClass won't work for IE
                    lis[j].className=this.options.css.postClss+j;
                   }
                    
                      
          }
             
        }
          
    
   });
