$(document).ready(function() {
    // Acá va de menos importante a más importante. Ojo! Si el código carga cosas, dejarlas al final!
    
    // Cargamos el texto por defecto del buscador
    search_default = $('#buscador input#s').attr('value');
    
    // Links con rel="external" se abren en tab-ventana nueva
    $('a[rel="external"]').attr("target","_blank");
    
    // Blanquea caja de búsqueda
    $('#buscador input#s').focus(function(){
	if($(this).attr('value') === search_default){
	    $(this).attr('value', '')
	}
    });
    
    // En el home, cambia el video del player central
    $('li a', '#lista-videos').click(function(evento){
	evento.preventDefault();
	cambiaVideo($(this).attr('href'));
    });
    
    // Cycle
    $('div#destacados').append('<div id="destacados-control" class="prefix_7 grid_3"></div>');
    elCantidad = $('ol#desta li').length;
    $('ol#desta').cycle({
	fx: 'fade', delay: 15000, speed: 1000, pause: true, pager: '#destacados-control',
	pagerAnchorBuilder: function(idx, slide) {
	    gimmeLastClass = elCantidad-1 == idx?' class="last"':'';
	    return '<a href="#" title="Ir al destacado '+ (idx*1+1) +'"'+ gimmeLastClass +'>' + (idx*1+1) +'</a>';
        }
    });
    
    // ???
    $(window).konami(function(){
	var konami = true;
	swfobject.embedSWF("http://www.youtube.com/v/98ew0VtHmik&amp;rel=0&amp;fs=1&amp;showsearch=0&amp;showinfo=0&amp;enablejsapi=1&amp;playerapiid=videoyutuv&amp;autoplay=1", "youtube-video-container", "350", "350", "9", vvqexpressinstall, vvqflashvars, vvqparams, vvqattributes);
	$("#youtube-video-container").expose({color: '#000', opacity: '0.8', onBeforeClose: function() {
	    ytplayer.stopVideo();
	    $('#youtube-video-container').css({width:'280px', height:'250px'});
	}});
    });

});

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("youtube-video-container");
    console.log('listo. ytplayer ya está!');
    ytplayer.addEventListener("onStateChange", "nytplayerStateChange");
}

function onytplayerStateChange(newState) {
   alert("Player's new state: " + newState);
}
function cambiaVideo(videoURL){
    if (ytplayer) {
	// Piece of gold from http://bit.ly/bpIe6d
	videoId = videoURL.replace(/^[^v]+v.(.{11}).*/,"$1");
	ytplayer.loadVideoById(videoId);
	console.log('Cambiando video a '+ videoId);
    }
}

// esto es fome!
function konamiChange(newState){
    console.log('%s - %s -- %s', konami, $.mask.isLoaded(), newState);
    if(konami == true && $.mask.isLoaded() == true && newState == 0){
	$.mask.close();
    }
}