var CompanyPopup = Class.create();

CompanyPopup.instances = [];
CompanyPopup.cancelAll = function(){
	CompanyPopup.instances.each(function(cp){
		cp.clear();
	});	
}
CompanyPopup.prototype = {
	initialize : function(trigger, options){
		this.trigger = $(trigger);
		this.options = Object.extend({
			delay : 1, 
			overlap :10, 
			popSelector : '.popContent',
			activeClass : null,
			autoHide : false
			}, options || {});
		this.timeout = null;
		this.appear = null; this.fade = null;
		this.popContent = this.trigger.select(this.options.popSelector)[0];
		if (options.autoHide) this.popContent.hide();
		this.setup();
		CompanyPopup.instances.push(this);
	},
	setup : function(){
		this.trigger.observe('mouseenter', this.showContent.bindAsEventListener(this));
		this.trigger.observe('mouseleave', this.close.bindAsEventListener(this));
	},
	showContent : function(event){
		if (! CompanyPopup.hoversEnabled) return;
		this.timeout = (function(){
			this.trigger.style.position="relative";
			if (this.options.activeClass)
			{
			this.trigger.addClassName(this.options.activeClass);
			}
			else{
				if (this.fade) this.fade.cancel();
				this.appear = new Effect.Appear(this.popContent, {duration: 0})
			}

		}).bind(this).delay(this.options.delay);
		event.stop();
	},
	close : function(event){
		if (! CompanyPopup.hoversEnabled) return;
		window.clearTimeout(this.timeout);
			if (this.options.activeClass)
			{
			this.trigger.removeClassName(this.options.activeClass);
			}
			else{
				if (this.appear) this.appear.cancel();
				this.fade = new Effect.Fade(this.popContent, {duration: 0, afterFinish:function(){this.trigger.style.position="static"}.bind(this)})
			}

		event.stop();
	},
	clear : function(){
		if (this.appear) this.appear.cancel();
		if (this.fade) this.fade.cancel();
		this.popContent.setOpacity(1);
	}
};

/************ dom ready function calls *************/
document.observe('dom:loaded', function() {
	// Grid/List views
if($('overviewz')){
	CompanyPopup.hoversEnabled = true;
} else{
	CompanyPopup.hoversEnabled = false;
}

	if($('viewList')) {
		$('viewList').observe('click', function(){
			CompanyPopup.cancelAll();
			$$('div.listing')[0].addClassName('listView').removeClassName('gridView');
			$('viewGrid').removeClassName('active'); this.addClassName('active');
			var hideGrid = $$('div.listing')[0].select('.listInfo');
			hideGrid.each(function(el) {
			el.style.display ='block';
			});
			CompanyPopup.hoversEnabled = false;
		});
	}
	if($('viewGrid')) {
		$('viewGrid').observe('click', function(){
			$$('div.listing')[0].addClassName('gridView').removeClassName('listView');
			//$$('div.listing')[0].className = 'gridView';
			$('viewList').removeClassName('active'); this.addClassName('active');
			var hideGrid = $$('div.listing')[0].select('.listInfo');
			hideGrid.each(function(el) {
			el.style.display ='none';
			});
			CompanyPopup.hoversEnabled = true;
		});
	}
	
	
	//activate filter buttons
	$$('.filter_buttons').each(function(el){
		var inp = el.select('input')[0];
		el.select('a').each(function(a){
			var param_val = a.readAttribute('id').split("_");
			a.observe('click', function(){
				el.select('li.active')[0].removeClassName("active");
				a.up('li').addClassName("active");
				inp.setAttribute('value', param_val[1]);
				inp.form.onsubmit();
			});
		});
	});
	
	$$('a.slidenav').each(function(el){
		
		var close = function(el){
			new Effect.BlindUp(el, {duration: .3, afterFinish:function(){el.removeClassName('opened');}});
		};
		
		el.observe('click', function(){
			var panel = el.next('ul');
			if (panel.hasClassName('opened')){
				close(panel);
			}else{
				panel.up('#sidebar').select('.opened').each(close);
				new Effect.BlindDown(panel, {duration: .3, afterFinish: function(){panel.addClassName("opened");}});
			}
		});
	});
	
	if ($$('a.morebtn').length)
		if ($('colB').getHeight()+30 < $('colA').getHeight()){
			var content = $('colA');
			var height = $('colB').getHeight()-19;
			var hide = function(){
				content.writeAttribute("style", "zoom: 1; overflow: hidden; height: "+height+"px");
			};
			var show = function(){
				content.writeAttribute('style','');
			};
			hide();
			var btn =$$('a.morebtn')[0];
			btn.show().observe('click', function(){
				if (content.readAttribute('style') == '') {
					hide();
					btn.innerHTML='More &raquo;';
				}
				else {
					show();
					btn.innerHTML='Less &raquo;';
				}
			});
		}
	
});

function initializePopups() {
		var hide = $$('.gridView').length > 0;
		$$('li.companyHover').each(function(el){
			new CompanyPopup(el, {delay : .1, overlap : 40, popSelector : '.listInfo' , autoHide : hide});
		});
	}
	
/************ dom ready function calls *************/
document.observe('dom:loaded', function() {
	if ($('flashcontent')) {
		var so = new SWFObject("/swfs/"+ gaUserLanguage +"/cases.swf", "general_atlantic", "545", "292", "8", "#ffffff");
		so.addVariable("content_url", "/" + gaUserLanguage + "/companies/promos.xml");
		so.addParam("wmode", "opaque");
		so.write("flashcontent");
	}
	
	
	initializePopups();
	document.observe('ga:filter', initializePopups);
});