Cufon.replace('#menu a, h1, .myr', {fontFamily: 'Myriad', hover:true});

var banact=0;
var bantim;
var smin=0;

$(function(){
	var option = {
		resizeLgImages: true,
		displayNav: true,
		handleUnsupported : 'remove',
		keysClose: ['c', 27], 
		players:  ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv']
	};
	Shadowbox.init(option);
	
	$('.categoria').children('a').click(function() { desplegar($(this)); return false; });
	//$('.catsel').each(function() { desplegar($(this)); });
	
	if($('.catsel').length) {
		auto($('.catsel'));
	}
	
	$('.tbg').fadeTo(0,0.7);
	smin=parseInt($('#descarga').children('div').css('top'));
	$('#descarga .u').click(function() {
		var e=$('#descarga').children('div');
		var s=parseInt($(e).children('a:eq(0)').outerHeight(1));
		
		var p=parseInt($(e).position().top);
		p+=s;
		
		if(p>smin) p=smin;
		
		$(e).stop().animate({top:p},300,0,'swing');
		
		return false;
	});
	$('#descarga .d').click(function() {
		var e=$('#descarga').children('div');
		var s=parseInt($(e).children('a:eq(0)').outerHeight(1));
		var he=s+parseInt($(e).children('a:eq(0)').outerHeight(0));
		var ht=parseInt($(e).outerHeight());
		var hc=parseInt($('#descarga').innerHeight());
		
		var p=parseInt($(e).position().top);
		p-=s;
		
		if(p<hc-ht) p=hc-ht;
		
		$(e).stop().animate({top:p},300,0,'swing');
		
		return false;
		
	});
	
	if($('#banner').length) {
		$('#banner .i').click(function() {
			banact--;
			var tot=$('#banner li').length;
			tot--;
			if(banact<0) banact=tot;
			banner();
			return false;
		});

		$('#banner .d').click(function() {
			banner_r();
			return false;
		});
	
		banner();
	}
	
	var s=$('#slider').children('div').children('div');
	var itm=$(s).children('a');
	var w=0;
	for(var i=0;i<itm.length;i++) w+=parseInt($(itm[i]).outerWidth(1));
	$(s).css('width',w+10);
	
	$('#slider .l').click(function() {
		var pos=parseInt($('#slider').children('div').children('div').css('left'));
		pos+=$('#slider').children('div').children('div').children('a:eq(0)').outerWidth(1);
		
		var slw=parseInt($('#slider').children('div').children('div').innerWidth());
		var slcw=parseInt($('#slider').children('div').innerWidth());
		var mx=-1*(slw-slcw);
		
		if(pos>0) pos=mx;
		
		$('#slider').children('div').children('div').stop().animate({left:pos+'px'},500,'swing');
		return false;
	});
	
	$('#slider .r').click(function() {
		var pos=parseInt($('#slider').children('div').children('div').css('left'));
		pos-=$('#slider').children('div').children('div').children('a:eq(0)').outerWidth(1);
		
		var slw=parseInt($('#slider').children('div').children('div').innerWidth());
		var slcw=parseInt($('#slider').children('div').innerWidth());
		var mx=-1*(slw-slcw);
		
		if(pos<mx) pos=0;

		$('#slider').children('div').children('div').stop().animate({left:pos+'px'},500,'swing');
		return false;
	});
});

function banner_r() {
	banact++;
	var tot=$('#banner li').length;
	tot--;
	if(banact>tot) banact=0;
	banner();
}

function banner() {
	var src=$('#banner li:eq('+banact+') a').attr('rel');
	var href=$('#banner li:eq('+banact+') a').attr('href');
	var target=$('#banner li:eq('+banact+') a').attr('target');
	var tit=$('#banner li:eq('+banact+') a').attr('title');
	var txt=$('#banner li:eq('+banact+') a').text();
	
	$('#banner .imagen').attr('href',href);
	$('#banner .imagen').attr('target',target);
	
	$('#tbg').fadeTo(0,0.7);
	$('#tit').fadeTo(0,0).html('<strong>'+tit+'</strong><br />'+txt).fadeTo(800,1);
	Cufon.refresh();
	
	var img=new Image();
	
	clearTimeout(bantim);
	
	//$('.prelo').stop().fadeTo(300,0.5);
	
	$(img).load(function() {
		var src=$('#banner li:eq('+banact+') a').attr('rel');
		//var href=$('#banner li:eq('+banact+')').text();
		
		if(src!=$(this).attr('src')) return;
		
		//$('.prelo').stop().fadeTo(300,0);
		
		$('#banner .imagen img').stop().addClass('a').css('z-index',1);
		
		//if(href!='') $(this).css('cursor','pointer').attr('title',href).click(function() { window.open($(this).attr('title')); });
		
		$(this).appendTo($('#banner .imagen')).addClass('b').fadeTo(0,0).css('z-index',2);
		
		$('#banner .b').fadeTo(600,1,function() {
			$('#banner .a').remove();
			$(this).removeClass('b');
			clearTimeout(bantim);
			bantim=setTimeout('banner_r();',8000);
		});
	}).attr('src',src);
}



function desplegar(q) {
	var e=$(q).parent().children('div');
	if($(e).children('div').length||$(e).children('a').length) {
		$(e).slideToggle(300);
	} else {
		window.location.href=$(q).attr('href');
	}
}

function auto(q) {
	$(q).parent().children('div').slideDown(0);
	if($(q).parent().parent().parent().attr('id')!='der') {
		if($(q).parent().hasClass('categoria')) {
			auto($(q).parent().parent().parent().children('a'));
		} else {
			auto($(q).parent().parent().children('a'));
		}
	}
}
