window.addEvent('domready', function() {

	var right_arrow = new Element('img',{'src':'/common/images/news/right_arrow.png','id':'right_arrow'});
	var left_arrow = new Element('img',{'src':'/common/images/news/left_arrow.png','id':'left_arrow'});
	right_arrow.setStyles({
		'position':'absolute',
		'top':'223px',
		'right':'0',
		'cursor':'pointer'
	});
	left_arrow.setStyles({
		'position':'absolute',
		'top':'223px',
		'left':'0',
		'cursor':'pointer'
	});

	right_arrow.inject($('content'));
	left_arrow.inject($('content'));

	initialize();

	document.addEvent('keydown', catchKey);
	if(!$$('#news_carrousel .news_img')[0].hasClass('pos2face'))
		left_arrow.addEvent('click',goLeft);
	if(!$$('#news_carrousel .news_img').getLast().hasClass('pos2face'))
		right_arrow.addEvent('click',goRight);

		
		
//getCarrouselItems().addEvent('mouseover',function(){$('test').innerHTML=this.className;});		
		
});

function catchKey(event){
		if(event.key == 'left' && !$$('#news_carrousel .news_img')[0].hasClass('pos2face')) goLeft();
		if(event.key == 'right' && !$$('#news_carrousel .news_img').getLast().hasClass('pos2face')) goRight();
}

function toTilt(item) {
	var image = item.getElement("img.news_img");
	item.className="li_pos2";
	image.src = image.getParent().getElement(".news_img_tilt").src;
	image.removeProperty('style');
	image.className="news_img pos2";
}

function toFace(item) {
	var image = item.getElement("img.news_img");
	item.className="li_pos2face";
//alert('boom');
	image.src = image.getParent().getElement(".news_img_face").src;
	image.className="news_img pos2face";
	image.setStyle('opacity','1');
}

function goRight() {
	var step = 1;
	if(!this.document)
		step = (image_position = this.getProperty('class').substring(12).toInt())?(image_position-2):1;
	document.removeEvent('keydown', catchKey);
	$('left_arrow').removeEvent('click',goLeft);
	$('right_arrow').removeEvent('click',goRight);
	$$('#news_carrousel .news_img').removeEvent('click',grabImage);
	$$('#news_carrousel .news_img').removeEvent('click',goLeft);
	$$('#news_carrousel .news_img').removeEvent('click',goRight);
	var carrousel_items = getCarrouselItems();
	carrousel_items.each(function(item,index){
		var img = item.getElement("img.news_img");
		
		if(img.hasClass('pos2face')){
			item.getElement(".title").setStyle('opacity',0);
			item.getElement(".abstract").setStyle('opacity',0);
			toTilt(item);
		}
		var position = img.getProperty('class').substring(12).toInt()
		if( position > 0 && position <= 4+step){
			img.setStyle('display','');
			var myMorph = new Fx.Morph(img, {
				duration: 1000,
				transition:Fx.Transitions.Back.easeInOut,
				onComplete: function(){
					img.className="news_img pos"+(position-step);
					item.className="li_pos"+(position-step);
					if(img.hasClass('pos2')){
					img.removeProperty('style');
						toFace(item);
						item.getElement(".title").set('morph',{onComplete:function(){
							document.addEvent('keydown', catchKey);
							$$('#news_carrousel .news_img').addEvent('click',grabImage);
							if(!$$('#news_carrousel .news_img')[0].hasClass('pos2face'))
								$('left_arrow').addEvent('click',goLeft);
							if(!$$('#news_carrousel .news_img').getLast().hasClass('pos2face'))
								$('right_arrow').addEvent('click',goRight);
						}});
						item.getElement(".title").morph({'opacity':1});
						item.getElement(".abstract").morph({'opacity':1});
					}
				}
			});
			myMorph.start('.pos'+(position-step));
			myMorphLi = new Fx.Morph(item, {
				duration: 1000,
				transition:Fx.Transitions.Back.easeInOut,
				onComplete: function(){
//					if(!img.hasClass('pos2') && !img.hasClass('pos2face'))
//						item.className="li_pos"+(position-step);
					item.removeProperty('style');
				}
			});
			myMorphLi.start('.li_pos'+(position-step));

		}
		else{
			if(position-step <= 6 && position-step > -2){
				img.className="news_img pos"+(position-step);
				img.setStyle('display','');
			}
			else{
				img.className="news_img hid"+(position-step);
				img.setStyle('display','none');
			}
			item.className="li_pos"+(position-step);
		}
	});
}

function goLeft() {
	var step = 1;
	document.removeEvent('keydown', catchKey);
	$('left_arrow').removeEvent('click',goLeft);
	$('right_arrow').removeEvent('click',goRight);
	$$('#news_carrousel .news_img').removeEvent('click',grabImage);
	$$('#news_carrousel .news_img').removeEvent('click',goLeft);
	$$('#news_carrousel .news_img').removeEvent('click',goRight);
	var carrousel_items = getCarrouselItems();
	carrousel_items.each(function(item,index){
		var img = item.getElement("img.news_img");
		if(img.hasClass('pos2face')){
			item.getElement(".title").setStyle('opacity',0);
			item.getElement(".abstract").setStyle('opacity',0);
			toTilt(item);
		}
		var position = img.getProperty('class').substring(12).toInt()
		if( position < 5 && position >=0){
			var myMorph = new Fx.Morph(img, {
				duration: 1000,
				transition:Fx.Transitions.Back.easeInOut,
				onComplete: function(){
					img.className="news_img pos"+(position+1);
					item.className="li_pos"+(position+1);
					if(img.hasClass('pos2')){
						img.removeProperty('style');
						toFace(item);
						item.getElement(".title").set('morph',{onComplete:function(){
							document.addEvent('keydown', catchKey);
							$$('#news_carrousel .news_img').addEvent('click',grabImage);
							if(!$$('#news_carrousel .news_img')[0].hasClass('pos2face'))
								$('left_arrow').addEvent('click',goLeft);
							if(!$$('#news_carrousel .news_img').getLast().hasClass('pos2face'))
								$('right_arrow').addEvent('click',goRight);
						}});
						item.getElement(".title").morph({'opacity':1});
						item.getElement(".abstract").morph({'opacity':1});
					}
				}
			});
			myMorph.start('.pos'+(position+1));
			myMorphLi = new Fx.Morph(item,{
				duration: 1000,
				transition:Fx.Transitions.Back.easeInOut,
				onComplete: function(){
//					if(!img.hasClass('pos2') && !img.hasClass('pos2face'))
//						item.className="li_pos"+(position-step);
//						item.className="li_pos"+(position+1);
					item.removeProperty('style');
				}
			});
			myMorphLi.start('.li_pos'+(position+1));
		}
		else{
			if(position == -1){
				img.className="news_img pos"+(position+1);
				img.setStyle('display','');
			}
			else{
				img.className="news_img hid"+(position+1);
				img.setStyle('display','none');
			}
			item.className="li_pos"+(position+1);
		}
	});
}

function grabImage() {
	this.removeEvent('click',grabImage);
	var image_position = this.getProperty('class').substring(12).toInt();
	if(image_position>2) {
		this.addEvent('click',goRight);
		this.fireEvent('click',goRight);
		return false;
	}
	if(image_position<2) {
		this.addEvent('click',goLeft);
		this.fireEvent('click',goLeft);
		return false;
	}
	this.addEvent('click',grabImage);
}

function initialize() {
	$$('#news_carrousel .title','#news_carrousel .abstract').setStyle('opacity','0');
	var first_position = ($$('#news_carrousel .news_img').length > 2 ) ? 1 : 0;
	var carrousel_items = getCarrouselItems();
       carrousel_items.each(function(item,index){
		var img = item.getElement("img.news_img");
		img.removeProperty('style');
		if(index!=first_position){
			toTilt(item);
			item.className="li_pos"+(index+Math.abs(first_position-1)+1);
			if(index < 5){
				img.className="news_img pos"+(index+Math.abs(first_position-1)+1);
			}
			else{
				img.className="news_img hid"+(index+Math.abs(first_position-1)+1);
				img.setStyle('display','none');
			}
		}
		else{
			toFace(item);
			img.className="news_img pos2face";
			img.setStyle('opacity','1');
			item.getElement(".title").setStyle('opacity',1);
			item.getElement(".abstract").setStyle('opacity',1);

		}
		img.addEvent('click',grabImage);
		item.removeProperty('style');
	});
}

// If we're in Fatwire InSite mode (and have permission to modify
// the page), Fatwire has integrated our <li>s inside its <span>s,
// which need to be manipulated instead of the <li>s. This function
// encapsulates the test and returns a list of items (<li>s or <span>s)
function getCarrouselItems()
{
	var carrousel_items = $$('#news_carrousel span span');
	return carrousel_items.length > 0 ? carrousel_items : $$('#news_carrousel li');
}

function fatwireInsiteAssetChangedCallback(assetid, assettype, assetname, attributename,startdate,enddate)
{
	initialize();
}

