window.addEvent('domready', function(){
	new HomeRandomImage($('intro'));	 
	$$('#menu-ppal li li:last-child').addClass('last-child');	
	
	// LOGO PATROCINADOR CABECERA
	new LogoPatrocinador({
		// habilitado -> por defecto false (no se ejecuta) -> Comentar o dejar a false para desactivar
		//habilitado: true, 
		alt: 'DWARF 8', 
		rutaImgLogo: '/coolz/patrocinios/logo-dwarf8.png',
		link: 'http://www.dwarf8.com/'
	});
});

// movemos like button de sitio en los perfiles de usuario (imposible posicionarlo con CSS con la maquetacion actual)
(function(){
	// solo en perfiles de usuario
	if($$('body')[0].id != 'global_page_user-profile-index') return;
	
	var likeButton = document.getElement('div.layout_sitelike_profile_like_button');		
	var profileStatusWrapper = $('profile_status');
	var profileTitle = $('profile_status').getElement('h2');				
	var likeButtonClone = likeButton.clone().injectAfter(profileStatusWrapper.getElement('h2'));	
	likeButtonClone.setStyle('display','block');
	likeButton.dispose();						
})();


// Permalinks publicaciones muro
(function(){
	var LinkToPost = new Class({
		Implements: Options,
		options:{
			date: 'span.timestamp',
			itemPrefix: 'activity-item-',
			profileLink: 'a.feed_item_username',
			linkAction: '/action_id/'
		},
		initialize: function(element, options){
			if(!element) return;
			this.setOptions(options);		
			this.element = element;
			this.getPostId();
			this.linkDate();		
		},
		getPostId: function(){
			postId = this.element.id.split(this.options.itemPrefix)[1];
		},
		linkDate: function(){
			var date = this.element.getElement(this.options.date),
				profileLink = this.element.getElement(this.options.profileLink).get('href'),
				link = new Element('a',{
					'href' : profileLink + this.options.linkAction + postId,
					'class': 'feed_item_date',
					'style': 'font-size: 1em'
				}),
				clonDate = date.clone();								
			link.grab(clonDate).replaces(date);
		}
	});
	
	$$('ul#activity-feed li[id^="activity-item-"]').each(function(item){
		new LinkToPost(item);		
	});		
})();


var Wisuki = Wisuki || {};
// CUIDADO CON LA ULTIMA COMA!!
Wisuki.imgs = [
	// Agosto 2011
	{
		ruta: 'http://wisuki.com/coolz/img/home/5d30_24fe.JPG',
		autor: 'Francisco Mazzarella',
		autorLink: 'http://wisuki.com/profile/franmdq'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/5ca9_1ba7.jpg',
		autor: 'Carlos Horstmann',
		autorLink: 'http://wisuki.com/profile/elconde'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/5f8e_70ea.png',
		autor: 'Florentino Lafuente',
		autorLink: 'http://wisuki.com/profile/ToN3Ty'
	},
	// Julio 2011
	{
		ruta: 'http://wisuki.com/coolz/img/home/5699_8057.jpg',
		autor: 'Alberto Vich',
		autorLink: 'http://wisuki.com/profile/Berto1'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/5699_8057.jpg',
		autor: 'Alberto Vich',
		autorLink: 'http://wisuki.com/profile/Berto1'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/562c_2d21.jpg',
		autor: 'Gustavo Garnica',
		autorLink: 'http://wisuki.com/profile/titusgarnica'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/5600_2cb4.jpg',
		autor: 'Javier',
		autorLink: 'http://wisuki.com/profile/iJavi'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/55cb_664b.jpg',
		autor: 'Javier Quesada',
		autorLink: 'http://wisuki.com/profile/javichu'
	},
	// Junio 2011
	{
		ruta: 'http://wisuki.com/coolz/img/home/4cdb_a9c9.jpg',
		autor: 'Sara Serrano Santiago',
		autorLink: 'http://wisuki.com/profile/sserrano'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/4c59_b71e.JPG',
		autor: 'Carlos Morales',
		autorLink: 'http://wisuki.com/profile/carlos2371'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/4c0e_79a1.png',
		autor: 'Quique Rico',
		autorLink: 'http://wisuki.com/profile/kike'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/4bde_8130.jpg',
		autor: 'David',
		autorLink: 'http://wisuki.com/profile/zuma'
	},
	// Mayo 2011
	{
		ruta: 'http://wisuki.com/coolz/img/home/4728_fded.jpg',
		autor: 'Fura',
		autorLink: 'http://wisuki.com/profile/Fura'
	},
	//{
	//	ruta: 'http://wisuki.com/coolz/img/home/46b7_b4bd.jpg',
	//	autor: 'Viajeskitesurf.com',
	//	autorLink: 'http://wisuki.com/profile/viajeskitesurf'
	//},
	{
		ruta: 'http://wisuki.com/coolz/img/home/46b1_71fe.jpg',
		autor: 'David',
		autorLink: 'http://wisuki.com/profile/zuma'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/4674_d659.jpg',
		autor: 'Rafa',
		autorLink: 'http://wisuki.com/profile/Racris'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/4600_b693.JPG',
		autor: 'Carlos Morales',
		autorLink: 'http://wisuki.com/profile/carlos2371'
	},
	{
		ruta: 'http://wisuki.com/coolz/img/home/44aa_2837.jpg',
		autor: 'Fran Parras',
		autorLink: 'http://wisuki.com/profile/franbomber'
	}
];

var HomeRandomImage = new Class({
	Implements: Options,
	options: {
		colorFondoInicial: '#0B65A3',
		colorFondoFinal: '#001D3B',
		literales:{
			ES: {
				autor: 'Autor: '
			},
			EN: {
				autor: 'Author: '
			}
		}
	},
	initialize: function(element, options){
		if(!element) return;
		this.element = element;
		this.setOptions(options);	
		this.getDocumentLang();		
		this.lang.lastIndexOf('es') != -1 ? this.literales = this.options.literales.ES : this.literales = this.options.literales.EN;
		this.generarBackground();		
	},
	getDocumentLang: function(){
		// obtengo el valor de xml:lang o de lang si xml:lang no se ha usado en el HTML
	    this.lang = $$('html')[0].get('xml:lang') || $$('html')[0].get('lang');
	    if(!this.lang) this.lang = 'es'; // idioma por defecto si no se ha declarado el idioma principal del documento
	    return this.lang;
	},
	obtenerImgAleatoria: function(){				
		var i = Math.floor(Wisuki.imgs.length * Math.random());		
		return Wisuki.imgs[i];		
	},
	generarBackground: function(){		
		var datos = this.obtenerImgAleatoria();
		var autor = new Element('a',{
			'class': 'autor',
			'href': datos.autorLink,
			'text': this.literales.autor + datos.autor			
		}).setStyles({
			'z-index': 99999
		});		
		// transicion de color contenedor
		var bgColorTransition = new Fx.Tween(this.element);
		bgColorTransition.start('background-color', this.options.colorFondoInicial, this.options.colorFondoFinal);
		
		//bgColorTransition.set('background-color', this.options.colorFondoFinal);
		
		var loader = this.element.getElement('div.img-loader');
		loader.fade('hide');		
		loader.setStyles({
			'background-image': 'url('+datos.ruta+')',
			'background-position': 'top right',
			'background-repeat': 'no-repeat'
		});
		this.element.grab(autor);	
		// hacemos visible la capa cuando la img este cargada
		var imgFondo = new Element('img',{
			src: datos.ruta,
			events: {
				'load': function(){
					loader.fade('in');					
				}
			}
		});	
	}
});


// GENERAMOS EL LOGO DEL PATROCINADOR
var LogoPatrocinador = new Class({
	Implements: Options,
	options: {
		habilitado: false,
		alt: '',
		rutaImgLogo: '',
		link: '',
		wrapperLogo: 'div#global_header div.layout_core_menu_logo'
	},
	initialize: function(options){
		this.setOptions(options);
		if(!this.options.habilitado) return;
		this.anadirLogo(this.generarHTML());		
	},
	anadirLogo: function(html){
		if(!document.getElement(this.options.wrapperLogo)) return;
		document.getElement(this.options.wrapperLogo).grab(html, 'bottom');
	},
	generarHTML: function(){
		var wrapper = new Element('div',{
			id: 'logo-patrocinador'
		}).grab(new Element('a',{
			href: this.options.link
		}).grab(new Element('img',{
			alt: this.options.alt,
			src: this.options.rutaImgLogo
		})));
		return wrapper;		
	}
});

