var GE_AdCatDropDown = Class.create( GE_DropDown, {
	initialize:function($super, el) {
		$super(el);
		if (document.location.href.indexOf('.html') < 0) { this.titleEl.update('Latest'); }
		else {
			this.items.each(
				function(el) {
					var a = el.down('a');
					if (document.location.href.indexOf(a.href) >= 0) this.titleEl.update(a.innerHTML);
				}.bind(this)
			);
		}
	}
});

GE_Main.mapCSSToFn('compaing_list', GE_AdCatDropDown);

var GE_AdDropDown = Class.create( GE_DropDown, {
	ad_list:null,
	ads:null,
	initialize:function($super, el) {
		$super(el);
		this.titleEl.update('All Formats');
		this.ad_list = $$('.ad_list')[0];
		this.ads = this.ad_list.childElements();
		this.items.each(
			function(el) {
				var a = el.down('a');
				a.observe('click',function(e){
					e.stop();
					this.o.titleEl.update(this.a.innerHTML);
					this.o.filter(this.a.innerHTML);
					this.o.toggleSelect();
				}.bind({o:this,a:a}));
			}.bind(this)
		);
	},
	filter:function(c) {
		var ads_on = [];
		this.ads.each(function(el){
			el.removeClassName('clear');
			el.removeClassName('last');
			if (el.hasClassName(c) || c=='All Formats') {
				ads_on.push(el);
			}
			if (/MSIE/i.test(navigator.userAgent)) el.remove();
		});
		if (!/MSIE/i.test(navigator.userAgent)) this.ad_list.update('');
		ads_on.each(function(el){
			var i = this.ads_on.indexOf(el);
			if (i%2==0) el.addClassName('clear');
			if (i==this.ads_on.length-1) el.addClassName('last');
			this.ad_list.insert(el);
		}.bind({ads_on:ads_on,ad_list:this.ad_list}));
	}
});

GE_Main.addInitFn(
	function() {
		var list = new Element('ul').addClassName('filter_list dropdown_list');
		list.insert( new Element('li').addClassName('dropdown_item').insert( new Element('a',{href:'#'}).insert('All Formats') ) );
		if ($$('.Television').length>0) list.insert( new Element('li').addClassName('dropdown_item').insert( new Element('a',{href:'#'}).insert('Television') ) );
		if ($$('.Radio').length>0) list.insert( new Element('li').addClassName('dropdown_item').insert( new Element('a',{href:'#'}).insert('Radio') ) );
		if ($$('.Print').length>0) list.insert( new Element('li').addClassName('dropdown_item').insert( new Element('a',{href:'#'}).insert('Print') ) );
		if ($$('.Online').length>0) list.insert( new Element('li').addClassName('dropdown_item').insert( new Element('a',{href:'#'}).insert('Online') ) );
		if ($$('.Entertainment').length>0) list.insert( new Element('li').addClassName('dropdown_item').insert( new Element('a',{href:'#'}).insert('Entertainment') ) );
		
		var el = new Element('div').addClassName('single_column ad_filter')
			.insert( new Element('strong').insert('Select a format:') )
			.insert( list );
			
		$$('.ad_list')[0].insert({before:el});
	}
);

GE_Main.mapCSSToFn('filter_list', GE_AdDropDown);
