var MostPopular = Class.create();
MostPopular.prototype = {	
	initialize : function() {
		var self = this;
		var most_popular_tabs = $A($('most-popular-categories').getElementsByTagName('LI'));
		var divs = $A($('most-popular').getElementsByTagName('DIV'));
		if ($('most-popular-hub')) {
			this.most_popular_hub_init();
		}
		most_popular_tabs.each(function(tab) {
			var ankor = $(tab).firstDescendant();
			addEvent(ankor, "click", function() {
				if ($(this.parentNode).hasClassName('selected')) { return false; }
				var cln = this.className;
								
				most_popular_tabs.each(function(t) {
					if (Element.hasClassName(t,'selected')) { 
						Element.removeClassName(t,'selected');
					}
					if ((cln!="most-viewed-all") && $('most-popular-hub')) {						
						$('most-popular-hub').hide();						

					} else if ((cln=="most-viewed-all") && $('most-popular-hub')) {						
						$('most-popular-hub').show();
					}
				});
				divs.each(function(d) {
					if ($(d).className == cln) {
						if (cln == "most-viewed-all" && $('most-popular-hub')) {
							//auto-selects the "Report on Business" link when MOST POPULAR link is chosen.
							//de-selects the "All News" link. makes a default to choose "Report on Business" when 
							//the MOST POPULAR tab has been chosen.
							$('most-popular-hub').down().addClassName('selected');
							$('most-popular-hub').down(2).removeClassName('selected');							
							
							$('hub-view').show();
						} else {
							
							$(d).show();
						}
					} else {	
					 $(d).hide();
					}
				});
				Element.addClassName(this.parentNode,'selected');
				self.adjust_ad_spots();
	       		return false;	
					event.returnValue = false;	
			});
		});
	},
	
	most_popular_hub_init : function() {
		var self = this;
		addEvent($('most-popular-hub').down().firstDescendant(), "click", function() {
		
			self.most_popular_hub_toggle(this, 'next', 'hub');	
					
			return false;	 
		});
		addEvent($('most-popular-hub').down(2).firstDescendant(), "click", function() {
		
			self.most_popular_hub_toggle(this, 'previous', 'all');
					
			return false;	 
		});	
	},
	
	most_popular_hub_toggle : function(elm, direction, hub) {
		var klassname;
		var divs = $A($('most-popular').getElementsByTagName('DIV'));
		hub == "all" ? klassname = "most-viewed-all" : klassname = "most-viewed-hub";
		Element.addClassName($('most-popular-categories').getElementsByTagName('LI')[0], 'selected');
		
		if ($(elm.parentNode).hasClassName('selected')) {
			return false;
		} 
		if (direction == "next") {
			if ($(elm.parentNode).next().hasClassName('selected')) {
				$(elm.parentNode).next().removeClassName('selected');
			}
		} else {
			if ($(elm.parentNode).previous().hasClassName('selected')) {
				$(elm.parentNode).previous().removeClassName('selected');
			}
		}	
		$(elm.parentNode).addClassName('selected');		
		divs.each(function(d) {
			if ($(d).className == klassname) {
				d.show();
			} else {
				d.hide();
			}
		});
	},

	adjust_ad_spots : function() {
	        var adSpots = ['halfpageR','lug2'];
	        for (var i=0; i<adSpots.length; i++) {
	            if ($(adSpots[i])) {
	                placeAC(adSpots[i]);
	            }
	        }
	    }
	
};