$(document).ready(function(){
	$('#destacados-home #controles a').click(cambiarDestacado);
	var tiempo = 5000;
	setInterval( "slideDestacado(clickeado)", tiempo );
	
	function cambiarDestacado(){
		$('#destacados-home .activo').removeClass('activo');
		$(this).parent().addClass('activo');
		mostrar = $(this).attr('href');
		$(mostrar).addClass('activo');
		clickeado = 1;
		return false;
	};
	
});

var clickeado = 0;
function slideDestacado(clickeado){
	if(clickeado != 1) {
		if($('#destacados-home #controles li:last-child').hasClass('activo')) {
			activo = $('#destacados-home #controles li:first-child');
		} else {
			activo = $('#destacados-home #controles .activo').next();
		}
		$('#destacados-home .activo').removeClass('activo');
		activo.addClass('activo');
		mostrar = activo.find('a').attr('href');
		$(mostrar).addClass('activo');
	}
	return false;
};

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// Embed YouTube video
(function($){
    
    $.fn.youtube = function(options) {
        
        var defaults = {
            iframe: true,
            video: "",
            width: "835",
            height: "425",
            play: "1",
            loop: "0",
            link: "external",
            controls: "1"
        };
        
        var o = $.extend(defaults, options);
                
        var height = o.height - 26;
        
        var videoDIV = "yt-" + o.video;
                                
        return this.each(function() {
            
            if (o.iframe === true) {
                $(this).html('<iframe id="'+videoDIV+'" width="'+o.width+'" height="'+height+'" src="http://www.youtube.com/embed/' + o.video + '?enablejsapi=1&amp;version=3&amp;wmode=transparent&amp;rel=0&amp;autohide=1&amp;showinfo=0&amp;controls='+o.controls+'&amp;autoplay='+o.play+'" frameborder="0" allowfullscreen></iframe>');
            }
                
            if (o.iframe === false) {
                $(this).html('<div id="' + videoDIV + '">')

                var flashvars = {
                    hd: "1",
                    fs: "1",
                    fmt: "18",
                    egm: "0",
                    loop: o.loop,
                    autoplay: o.play,
                    showinfo: "0",
                    autohide: "1",
                    showsearch: "0",
                    enablejsapi: "1",
                    playerapiid: this.id,
                    wmode: "transparent"
                };
                var params = {
                    wmode: "transparent",
                    allowscriptaccess: "always",
                    allowfullscreen: "true",
                    allownetworking: o.link
                };
                var attributes = {
                    id: o.video,
                    name: o.video
                };
                // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
                swfobject.embedSWF("http://www.youtube.com/v/" + o.video +"&rel=0&version=1", videoDIV, o.width, height, "8", false, flashvars, params, attributes);            
            }
        })       
    };
}) (jQuery);
