
// ============================================================================
// PrototypeJS Extension: 
// Adds mouse:enter and mouse:leave support to Event
if (typeof Event.observe_without_mouseenter_and_mouseleave == 'undefined') {
  Event.observe_without_mouseenter_and_mouseleave = Event.observe
  Event.observe = function (element, event_name) {
    if (['mouse:enter', 'mouse:leave'].include(event_name)) {
      var mouse_is_over = false
      element.observe('mouseover', function (event) {
        if (mouse_is_over) return
        mouse_is_over = true
        if (event_name == 'mouse:enter') element.fire('mouse:enter')
      })
      element.observe('mouseout', function (event) {
        if (!mouse_is_over) return
        var to = (event.relatedTarget) ? event.relatedTarget : event.toElement
        if (!to) return false
        if (to == element) return false
        // the mouse has left if +to+ is _not_ within +element+
				try {
					while (to && to.nodeName != 'BODY' && to != element) to = to.parentNode
					if (to != element) {
						mouse_is_over = false
						if (event_name == 'mouse:leave') element.fire('mouse:leave')
					}
				} catch (e) {
					// Ugly hack, if mouse:leave is triggerd by replaced elements
					// firefox throws a permission denied exception on access to nodeName 
					// property.
					// Our assumption is that mouse has leaved to an input element,
					// but that must not be true in any case.
					mouse_is_over = false
					if (event_name == 'mouse:leave') element.fire('mouse:leave')
				}
      })
    }
    return Event.observe_without_mouseenter_and_mouseleave.apply(element, arguments)
  }

  Element.addMethods({
    observe: Event.observe
  })
}
// ----------------------------------------------------------------------------

var FcSlidingMiniBasket = Class.create({
	containerId: 'fc_top_basket',
	slidedElementId: 'basket_layer',
	duration: 0.4,
	
	initialize: function () {
		this.container = $(this.containerId);
		this.slidedElement = $(this.slidedElementId);
		if (!this.container || !this.slidedElement) return;
		
		this.effect = null;
		this.actual = 'closed';
		this.should = 'closed';
		
		this.container.observe('mouse:enter', this.onMouseEnter.bind(this));
		this.container.observe('mouse:leave', this.onMouseLeave.bind(this));
	},
	
	onMouseEnter: function () {
		this.should = 'open';
		if (this.effect) return;
		
		this.actual = 'open';
		this.effect = new Effect.SlideDown(this.slidedElement, {
			duration: this.duration, 
			afterFinish: this.onSlideFinished.bind(this)});
	},
	
	onMouseLeave: function () {
		this.should = 'closed';
		if (this.effect) return;

		this.actual = 'closed';
		this.effect = new Effect.SlideUp(this.slidedElement, {
			duration: this.duration, 
			afterFinish: this.onSlideFinished.bind(this)});
	},
	
	onSlideFinished: function () {
		this.effect = null;
		if (this.actual != this.should) {
			if (this.should == 'open') {
				this.onMouseEnter();
			} else { // this.should == 'closed'
				this.onMouseLeave();
			}
		}
	}
});

// Init Basket
document.observe("dom:loaded", function() {
	new FcSlidingMiniBasket();
});
// ----------------------------------------------------------------------------

function toolTip(e, content){
    if($('tooltip')){
        $('tooltip').remove();
    }
    if(e){
        tooltip = new Element('div', {
                        id: 'tooltip'
        });
        tooltip.className = 'tooltip';
        tooltip.style.left = (Event.pointerX(e)-365)+'px';
        tooltip.style.top = (Event.pointerY(e)+0)+'px';
        tooltip.innerHTML = content;
        document.body.appendChild(tooltip);
    }
}