var Menu = {
	elements : null,
	all : false,
	menuWidth : 140,
	menuHeight : 237,
	
	init : function(elements,all) {
		window.addEvent('load', function(){
			Menu._init(elements,all);
		});
		
		return;
	},
	_init : function (elements,all) {
		this.all = all;
		this.elements = document.getElements(elements);
		
		if (!this.elements) return false;
		
		this.elements.forEach(function(item,index) {
			if (item.hasClass("menuDropdown") && (this.all)) {
				item.setStyles({'position' : 'relative','z-index' : '10000'});
			}
			
			if (!item.hasClass("corner") && !item.hasClass("clear") && !item.hasClass("menuDropdown") &&  !item.hasClass("spr-menu")) {		
				if  ((!item.hasClass("active")) || (item.hasClass("active") && this.all)) {
					item.addEvent("mouseenter",Menu.onMouseEnter.bindWithEvent(item));
					item.addEvent("mouseleave",Menu.onMouseLeave.bindWithEvent(item));
					//item.addEvent("completeleave",Menu.onCompleteLeave.bindWithEvent(item));
					
					item.expandEffect = new Fx.Morph(item.getLast(), {duration: 1000, transition: Fx.Transitions.linear});
					item.expandEffect.addEvent("start",Menu.onExpandStart.bindWithEvent(item));
					item.expandEffect.addEvent("complete",Menu.onExpandCompleted.bindWithEvent(item))
					
					item.collapseEffect = new Fx.Morph(item.getLast(), {duration: 1000, transition: Fx.Transitions.linear});
					item.collapseEffect.addEvent("start",Menu.onCollapseStart.bindWithEvent(item));
					item.collapseEffect.addEvent("complete",Menu.onCollapseCompleted.bindWithEvent(item));
					
					item.getElements('ul li').forEach(function(list,listindex) {
						//list.setStyles({'display' : 'none'})
					});
				}
			}
		}.bind(this));
		
		return true;
	},
	onMouseEnter : function() {
		this.collapseEffect.cancel();
		
		var currentWidth = this.getStyle('width').toInt();
		var currentHeight = this.getStyle('height').toInt();
		
		this.expandEffect.start({
			//'width' : [currentWidth,Menu.menuWidth],
			'height' : [currentHeight,Menu.menuHeight]
		});
		
		return;
	},
	onMouseLeave : function() {
		this.expandEffect.cancel();
		
		var currentWidth = this.getStyle('width').toInt();
		var currentHeight = this.getStyle('height').toInt();
		
		if (currentHeight > Menu.menuHeight) {
			currentHeight = Menu.menuHeight;
		}
		
		this.collapseEffect.start({
			//'width' : [currentWidth,100],
			'height' : [currentHeight,0]
		});
		
		return;
	},
	onExpandStart : function () {
		if (!this.hasClass('hover'))
			this.getFirst().addClass("active");
			
		this.getLast().addClass("shadow");
		
		this.getLast().getElements('ul li').forEach(function(item,index) {
			//item.setStyles({'display' : 'none'})
		});
	},
	onExpandCompleted : function() {		
		//console.log(this);
		this.getLast().getElements('ul li').forEach(function(item,index) {
			//item.setStyles({'display' : 'block'})
		});
	},
	onCollapseStart : function() {
		this.getLast().getElements('ul li').forEach(function(item,index) {
			//item.setStyles({'display' : 'none'})
		});
	},
	onCollapseCompleted : function() {
		this.getLast().getElements('ul li').forEach(function(item,index) {
			//item.setStyles({'display' : 'none'})
		});
		
		if (!this.hasClass('hover'))
			this.getFirst().removeClass("active");
			
		this.getLast().removeClass("shadow")
	}
};

var News = {
	element : null,
	imageWidth : 960,
	imageHeight : 238,
	textWidth : 500,
	thumbnails : null,
	images : null,
	texts : null,
	imageWrap : null,
	textWrap : null,
	scroller : null,
	textScroller : null,
	cancelLeave : false,
	
	init : function(element,thumbnails,images,texts) {
		window.addEvent('load', function(){
			News._init(element,thumbnails,images,texts);
		});
		
		return;
	},
	_init : function (element,thumbnails,images,texts) {
		this.element = $(element);
		this.thumbnails = document.getElements(thumbnails);
		this.images = document.getElements(images);
		this.texts = document.getElements(texts);
		
		if (!this.element || !this.thumbnails || !this.images || !this.texts) return false;
		
		this.element.addEvent('mouseleave',this.onCompleteLeave.bindWithEvent(this));
		
		var totalWidth = News.imageWidth*this.images.length;
		
		this.imageWrap = new Element('div');
		this.imageWrap.setStyles({'width' : totalWidth + 'px','position' : 'relative','margin-left' : -1*totalWidth + 'px'});
		
		this.imageWrap.inject(this.images[0].getParent());
		
		this.images.forEach(function(item,index) {
			item.inject(News.imageWrap);
		});
		
		var totalTextWidth = News.textWidth*this.texts.length;
		this.textWrap = new Element('div');
		this.textWrap.setStyles({'width' : totalWidth + 'px','position' : 'relative','margin-left' : -1*totalTextWidth + 'px'});

		this.textWrap.inject(this.texts[0].getParent());
		
		this.texts.forEach(function(item,index) {
			item.inject(News.textWrap);
		});

		this.scroller = new Fx.Morph(this.imageWrap, {duration: 1500, transition: Fx.Transitions.Expo.easeInOut});
		this.textScroller = new Fx.Morph(this.textWrap, {duration: 1500, transition: Fx.Transitions.Expo.easeInOut});
		
		this.thumbnails.each(function(item,index) {
			if (index < News.images.length) {
				item.index = index;
				item.displayImage = News.images[index];
				item.displayText = News.texts[index];
				
				item.displayImage.setStyles({'display' : 'block'});
				
				item.addEvent("mouseenter",News.onMouseEnter.bindWithEvent(item));
				item.addEvent("mouseleave",News.onMouseLeave.bindWithEvent(item));
			}
		});
		
		//this.element.getParent().addEvent('mouseleave',this.onCompleteLeave.bindWithEvent(this));
		
		return true;
	},
	
	onCompleteLeave : function() {
		var totalWidth = News.imageWidth*this.images.length;
		var totalTextWidth = News.textWidth*this.texts.length;
		
		News.scroller.cancel();
		News.textScroller.cancel();
		
		var group = new Group(News.scroller, News.textScroller);
		
		group.addEvent('complete',function() {
			//this.imageWrap.getParent().setStyles({'display' : 'none'});
		}.bind(this));

		News.scroller.start({
			'margin-left' : -1*totalWidth
		});
		News.textScroller.start({
			'margin-left' : -1*totalTextWidth
		});
	},
	onMouseEnter : function() {
		//News.imageWrap.getParent().setStyles({'display' : 'block'});
		
		News.scroller.cancel();
		News.textScroller.cancel();
		
		News.element.getFirst().setStyles({'display' : 'block'});
		
 		var currentLeft = News.imageWrap.getStyle('margin-left').toInt();
		var targetLeft = -1*News.imageWidth*this.index;
		
 		var currentTextLeft = News.textWrap.getStyle('margin-left').toInt();
		var targetTextLeft = -1*News.textWidth*this.index;
		//var textOpacity = this.displayText.getStyle('opacity');
		
		News.scroller.start({
			'margin-left' : [currentLeft, targetLeft]
		});
		News.textScroller.start({
			'margin-left' : [currentTextLeft, targetTextLeft]
		});
		
		this.getFirst().setStyles({'display' : 'none'});
		this.getLast().setStyles({'display' : 'block'});
		
		return;
	},
	onMouseLeave : function() {
		//this.getFirst().setStyles({'display' : 'block'});
		//this.getLast().setStyles({'display' : 'none'});
		
		return;
	}
};

var QuickMenu = {
    parent : null,
	element : null,
	btnClose : null,
	overlay : null,
	elementEffect : null,
	overlayEffect : null,
	menuHeight : 0,
	
	init : function(parent,element,btnClose) {
		window.addEvent('load', function(){
			QuickMenu._init(parent,element,btnClose);
		});
	},
	_init : function (parent,element,btnClose) {
		this.parent = $(parent);
		this.element = $(element);
		this.btnClose = $(btnClose);

		if (!this.parent || !this.element || !btnClose) return false;
		
		this.parent.addEvent('click',QuickMenu.display.bindWithEvent(QuickMenu));
		this.btnClose.addEvent('click',QuickMenu.hide.bindWithEvent(QuickMenu));
		
		//console.log(this.parent.offsetTop + ' ' + this.parent.offsetHeight);
		this.parent.setStyles({'position' : 'relative'});
		
		this.menuHeight = this.element.scrollHeight;
		
		var top = this.parent.offsetTop + this.parent.offsetHeight;
		var left = (this.parent.offsetLeft + this.parent.offsetWidth) - this.element.offsetWidth;
		
		this.overlay = new Element('div').inject(document.body);
		this.overlay.addClass('shadow');
		this.overlay.setStyles({
			'position': 'absolute',
			'width': this.element.offsetWidth + 'px',
			'height' : this.element.offsetHeight + 'px',
			'overflow' : 'hidden'
		});		
		
		this.elementEffect = new Fx.Morph(this.element, {duration: 2000, transition: Fx.Transitions.Expo.easeInOut});
		this.overlayEffect = new Fx.Morph(this.overlay, {duration: 2000, transition: Fx.Transitions.Expo.easeInOut});
		
		//this.overlay.addEvent('click',QuickMenu.hide.bindWithEvent(QuickMenu));
		
		this.position();

		this.eventPosition = this.position.bind(this);
		
		return;
	},
	display : function (event) {
		this.elementEffect.cancel();
		this.overlayEffect.cancel();
		
		window.addEvent('resize', this.eventPosition);
		
	    if (!window.ie) {
			event = new Event(event);
			event.preventDefault();
		}else {
		    event.cancelBubble = true;
		    event.returnValue = false;
		}
		
		this.position();
		
		var currentElementHeight = this.element.getStyle('height').toInt();
		var currentOverlayHeight = this.element.getStyle('height').toInt();
		
		this.elementEffect.start({'height' : [currentElementHeight,this.menuHeight]});
		this.overlayEffect.start({'height' : [currentOverlayHeight,this.menuHeight]});
	},
	hide : function(event) {
		this.elementEffect.cancel();
		this.overlayEffect.cancel();
		
		window.removeEvent('resize', this.eventPosition);
		
	    if (!window.ie) {
			event = new Event(event);
			event.preventDefault();
		}else {
		    event.cancelBubble = true;
		    event.returnValue = false;
		}
		
		var currentElementHeight = this.element.getStyle('height').toInt();
		var currentOverlayHeight = this.element.getStyle('height').toInt();
		
		this.elementEffect.start({'height' : [currentElementHeight,0]});
		this.overlayEffect.start({'height' : [currentOverlayHeight,0]});
	},
	position : function() {
	    //console.log(this.parent.getSize());		
		var parentPosition = this.parent.getPosition();
		var parentSize = this.parent.getSize();
		var elementSize = this.element.getSize();
		
		var top = parentPosition.y + parentSize.y + 5;
		var left = (parentPosition.x + parentSize.x) - elementSize.x + 10;
		
		this.element.setStyles({'top' : top + 'px','left' : left + 'px'});
		this.overlay.setStyles({'top' : top + 'px','left' : left + 'px'});
	}
}