var popIndex = 2000;
var PopUp = new Class({
	initialize: function(element) {
		this.element  = $(element);
		this.triggers = $$('.show_' + this.element.id);
		this.closers  = $$('.hide_' + this.element.id);
		this.active   = false;
		this.bindings = {
			show: this.show.bind(this),
			hide: this.hide.bind(this)
		};
		this.setup();
	},
	setup: function() {
		//this.fx = new Fx.Style(this.element, 'opacity', {duration: 300});
		this.fx = new Fx.Style(this.element, 'opacity', {duration: 0});
		this.handle = $ES('.common', this.element);
		this.triggers.addEvent('click', this.bindings.show);
		this.closers.addEvent('click', this.bindings.hide);
	},
	show: function(event) {
		if (this.active) { return; }
		this.active = true;
		this.element.setStyles({
			display: 'block',
			zIndex:  popIndex++,
			opacity: 0,
			margin: 0
		});
		
		var dims = this.element.getSize();
		var windowheight = window.getHeight();
		var windowwidth = window.getWidth();
		if(windowheight == 0) windowheight = 600;
		if(windowwidth == 0) windowwidth = 800;
		this.element.setStyles({
			top:  window.getScrollTop()  + (windowheight / 2) - (dims.size.y / 2),
			left: window.getScrollLeft() + (windowwidth / 2)  - (dims.size.x / 2)
		});
		
		this.animate(0,1);
		
		this.element.addEvent('click', function(event) {
			/*this.setStyle('zIndex', popIndex++);*/
			event = new Event(event);
			event.stop();
		});
		/*document.addEvent('click', this.bindings.hide);*/		
		var opts = {};
		opts.onStart = function(){this.element.setStyle('zIndex', popIndex++)}.bind(this);
		opts.handle  = this.handle[0] ? this.handle[0] : false;
		//this.element.makeDraggable(opts);//in FF.. all elements on pop are non responsive to clicks when draggable.
		event = new Event(event);
		event.stop();
	},
	hide: function(event) {
		this.active = false;
		//this.element.setStyle('opacity', 100);
		this.element.setStyle('opacity', 0);
		this.animate(1,0);
		$$('.choosePet').setStyle('visibility','visible');
		/*document.removeEvent('click', this.bindings.hide);*/
		event = new Event(event);
		event.stop();
	},
	animate: function(start, end) {
		this.fx.start(start, end);
	}
});

var PopHover = new Class({
	initialize: function(container) {
		this.container = $(container);
		this.triggers  = $$('.hover_' + this.container.id);
		this.setup();
	},
	setup: function() {
		this.fx = new Fx.Style(this.container, 'opacity', {
			//duration: 100
			duration: 0
		});
		this.triggers.each(function(e) {
			e.setStyle('cursor', 'pointer');
			e.addEvent('mouseenter', this.show.bind(this));
			this.container.addEvent('mouseleave', this.hide.bind(this));
		}.bind(this));
	},
	show: function(event) {
		this.animate(0,1);
		this.container.setStyle('display', 'block');
		event = new Event(event);
		event.stop();
	},
	hide: function(event) {
		this.animate(1,0);
		this.container.setStyle('display', 'none')
		event = new Event(event);
		event.stop();
	},
	animate: function(start, end) {
		/*
		if (window.ie) {
			this.fx.set(end)
		} else {
			this.fx.start(start, end);
		}
		*/
		this.fx.start(start, end);
	}
});

function initTT() {
	var Tips2 = new Tips($$('.Tips44'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
}

window.addEvent('domready', function(){
	//nav submenus
	var navTriggers = $$('.navTrigger');
	navTriggers.each(function(element) {
		var menuID = element.id.split('show_')[1];
		var menuButton = 'btn_' + menuID;
		//var fx = new Fx.Style(menuID, 'opacity', {duration: 300} );
		
		element.addEvent('mouseenter', function() {
			$(menuButton).setStyles({
				backgroundPosition: '0 -63px'
			});
			try{$('pc_optionsDD').setStyle('visibility','hidden');}
			catch(r){}
			$(menuID).setStyles({
				display: 'block'
				//opacity: 0
			});
			//fx.start(0,1);
		});
		element.addEvent('mouseleave', function() {
			$(menuButton).setStyles({
				backgroundPosition: '0 0'
			});
			try{$('pc_optionsDD').setStyle('visibility','visible');}
			catch(r){}
			$(menuID).setStyles({
				display: 'none'
				//opacity: 100
			});
			//fx.start(1,0);
		});
	});

	var popups = $$('.popDefault');
	popups.each(function(e) {
		new PopUp(e);
	});

	var hovers = $$('.popHover');
	hovers.each(function(e){
		x = new PopHover(e);
	});
	
	document.addEvent('mouseup',function(){
		try {
			$('Searches').style.display='none';
			$('Searches').style.opacity='0';
			$('Searches').style.visibility='hidden';
			}
		catch(error){} 
	});
	
	/* Tips 2 */
	var Tips2 = new Tips($$('.Tips44'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 300, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
	
});



