/*
 * JavaScript Components Beta
 * http://dewcms.com/
 *
 * Copyright 2010, dewenir
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.dewenir.es/license
 *
 * Date: Sat Feb 13 22:33:48 2010 -0500
 */

var cargarComponenteHomeYoutube = {
		id: '#section',
		css: '._videosyoutube',
		cssIgnoreAjax: 'ignoreAjax',
		cssContenedorRedesSociales: '.listado-social-media',
		cssContenedorYoutube: '.listadoVideosCanalYoutube',
		init: function(capaEnLaQueSeIncluye){
			if($(this.css).size() == 0) return;
			this.capaDondeSeIncluye = $(capaEnLaQueSeIncluye);
			this.config();
			this.engancharAutomatico();
			this.traerGaleria();
		},
		config: function(){
			this.section = $(this.id); 
			this.contenedor = this.section.find(this.cssContenedorRedesSociales);
			this.youtube = this.contenedor.find(this.css);
			this.capaDondeSeIncluye.prepend(this.contenedorGaleria());
			this.contenedorYoutube = this.section.find(this.cssContenedorYoutube);
			return;
		},
		engancharAutomatico: function(){
			this.contenedorYoutube.html(this.loadingTemplate());
			return;		
		},
		loadingTemplate: function(){
			var html = '';
			html += "<p class=\"loadingGaleria\">waiting youtube channel, please wait...</p>\n";
			return html;
			
		},
		contenedorGaleria: function(){
			var html = '';
			html += '<div class=\"listadoVideosCanalYoutube"\><p>componente youtube...</p><\/div>';
			return html;
		},
		traerGaleria: function(){		
			var that = this;
			var action = this.youtube.attr('href');
			$.ajax({
				type: "POST",
			   	url: action,
			   	dataType: "html",
			   	success: function(respuesta){
					var servidor = $(respuesta);
					var filtro = servidor.find('#section .listadoVideosCanalYoutube').children();
					var html = '<h3 class=\"youtube\">Youtube<\/h3>';
					filtro.each(function(indice){
						if(indice < 1) html += '<div class=\"itemListado\">' + $(this).html() + '</div>';
					});					
					that.contenedorYoutube.html(html);
					return;
			   	},
			   	error: function (XMLHttpRequest, textStatus, errorThrown) {
			   		//$("#error").html(XMLHttpRequest.responseText);
				   alert('lo sentimos. Ha habido un error en el servidor ' + textStatus);
			   	}
			});
			
		}	
				
	};	
