var VideoNoticias = {
	initialize: function(){
		this.ref = document.id('video_noticias');
		this.items = this.ref.getChildren();

		this.handles = {};
		this.handles.container = new Element('div',{'id':'videons_handles'}).injectTop(this.ref);
		this.handles.items = this.items.getElements('p.image');

		this.handles.items.each(function(el){
			el.inject(this.handles.container);
		},this);

		this.viewer = new Swiff('img/videos/viewer.swf',{
			width: 320,
			height: 100,
			container: this.handles.container
		});

		this.current = false;

		this.place = document.id('video_noticia').getChildren().associate(['info','object','title','description']);

		this.show(0,true);
	},

	show: function(i,first){
		this.items.each(function(el,j){
			if(i===j){
				el.setStyle('display','block');
				if(!first){
					var temp = el.getChildren().associate(['info','title','description']);
					this.place.info.innerHTML = temp.info.innerHTML;
					this.place.title.innerHTML = temp.title.innerHTML;
					this.place.description.innerHTML = temp.description.innerHTML;
					this.place.object.innerHTML = VideoNoticias_embeds[j];
				}
			}else{
				el.setStyle('display','none');
			}
		},this);
	}
};

var NoticiasPortada = {
	initialize: function(){
		var ref = document.id('noticias_portada');

		this.items = ref.getChildren();

		this.controls = {};
		this.controls.container = new Element('p',{'id':'noticiap_handles'}).inject(body);

		var links = new Element('span').inject(this.controls.container);

		this.controls.prev = new Element('a',{'class':'prev','href':'#previous','html':'<span>Anterior</span>'}).addEvent('click',this.previous.bindWithEvent(this)).inject(links);
		this.controls.next = new Element('a',{'class':'next','href':'#next','html':'<span>Siguiente</span>'}).addEvent('click',this.next.bindWithEvent(this)).inject(links);

		this.info = new Element('strong').inject(this.controls.container);
		this.counter = new Element('em').inject(links);

		this.current = false;

		this.show(0);
	},
	
	previous: function(e){
		new Event(e).stop();
		this.show(this.current==0 ? this.items.length-1 : this.current-1);
	},

	next: function(e){
		new Event(e).stop();
		this.show(this.current==this.items.length-1 ? 0 : this.current+1);
	},

	show: function(i){
		this.items.each(function(el,j){
			if(i===j){
				el.setStyle('display','block');
				this.info.set('html',el.getElement('.img_text').get('value'));
				//this.info.set('html',el.getElement('.noticiap_titulo').get('text'));
			}else{
				el.setStyle('display','none');
			}
		},this);

		this.counter.set('html','<b>'+(i+1)+'</b> de '+this.items.length);

		this.current = i;
	}
};

NoticiasPortada.initialize();
VideoNoticias.initialize();