if (!window.Enterprise) {
	window.Enterprise = {};
}
Enterprise.templatesPattern =  /(^|.|\r|\n)(\{\{(.*?)\}\})/;

Enterprise.TopCart= {
	initialize: function (container) {
		this.container = $(container);
		this.element = this.container.up(0);
		this.elementHeader = this.container.previous(0);
		this.intervalDuration = 10;
		this.interval = null;
		this.onElementMouseOut = this.handleMouseOut.bindAsEventListener(this);
		this.onElementMouseOver = this.handleMouseOver.bindAsEventListener(this);
		//this.onElementMouseClick = this.handleMouseClick.bindAsEventListener(this);
		
		if(document.all) {
			this.element.onmouseout = this.onElementMouseOut;
			this.element.onmouseover = this.onElementMouseOver;			
		} else {
			this.element.observe('mouseout', this.onElementMouseOut);
			this.element.observe('mouseover', this.onElementMouseOver);
		}

		this.upElement = this.container.down('#up');
		if(this.upElement) {
			this.onMouseOverScroll = this.handleScrollStart.bindAsEventListener(this);
			this.onMouseOutScroll = this.handleScrollEnd.bindAsEventListener(this);
			
			this.downElement = this.container.down('#down');
			this.upElement.observe('mouseover', this.onMouseOverScroll);
			this.upElement.observe('mouseout', this.onMouseOutScroll);
			this.downElement.observe('mouseover', this.onMouseOverScroll);
			this.downElement.observe('mouseout', this.onMouseOutScroll);
			this.cartElement = this.container.down('#mini-cart-wrapper');
			this.scrolltop = 0;
		}

		//this.elementHeader.observe('mouseover', this.onElementMouseClick);
	},
	
	handleScrollStart: function(evt) {
		this.direction = (evt.element() == this.upElement) ? 'down' : 'up';
		this.interval = setInterval(this.onScrolling.bind(this), this.intervalDuration);
	},
	
	handleScrollEnd: function(evt) {
		if(this.interval !== null) {
			clearInterval(this.interval);
			this.interval = null;
		}
	},
	
	onScrolling: function(evt) {
		if(this.direction == 'up' && this.cartElement.scrollTop >-1 && this.cartElement.scrollTop < this.maxScrollTop) {
			this.cartElement.scrollTop += 1;
		} else if(this.cartElement.scrollTop > 0) {
			this.cartElement.scrollTop -= 1;
		}
		this.scrolltop = this.cartElement.scrollTop;
	},

	handleMouseOut: function (evt) {
		this.hideCart();
	},

	handleMouseOver: function (evt) {
		this.showCart();
		if(this.upElement) {
			this.cartElement.scrollTop = this.scrolltop;
			this.maxScrollTop = this.container.down('#mini-cart').getHeight();
		}
	},

	handleMouseClick: function (evt) {
		if (!$(this.elementHeader).hasClassName('expanded') && !$(this.container.id).hasClassName('process') )  {
			this.showCart();
		}
		else {
			this.hideCart();
		}
	},

	showCart: function (timePeriod) {
		this.container.parentNode.style.zIndex=992;
		$(this.elementHeader).addClassName('expanded');
		this.container.show();
		if(timePeriod) {
			this.timePeriod = timePeriod*1000;
			this.interval = setTimeout(this.hideCart.bind(this), this.timePeriod);
		}
	},

	hideCart: function () {
		if (!$(this.container.id).hasClassName('process') && $(this.elementHeader).hasClassName('expanded')) {
			this.container.hide();
		}

		$(this.elementHeader).removeClassName('expanded');
	}
};

Event.observe(window, "load", function(event) {
	Event.observe("top-currency", "mouseover", function(e) {
		element = $('currency');
		element.show();
	});
	Event.observe("top-currency", "mouseout", function(e) {
		element = $('currency');
		element.hide();
	});
});

