$(document).ready(function(){
	initTabs();
	initCarrousel();
	initRandom();
});

function initTabs(){
	$('.tabs').find('.hide-js').removeClass('hide-js'); 
	$('.tabs').find('.bolas-cupon-sin-js').removeClass('bolas-cupon-sin-js');
	$('.tabs').find('.bolas-super-once-sin-js').removeClass('bolas-super-once-sin-js');
	$('.tabs').find('.bolas-739-sin-js').removeClass('bolas-739-sin-js');
	$('.tabs').find('.reintegro-sin-js').removeClass('reintegro-sin-js');
	$('.tabs').find('.hide-bg').removeClass('hide-bg');
	$('.tabs').find('.show-js').removeClass('show-js');
	$('.tabs').find('.que-sin-js').removeClass('que-sin-js');
	$('.tabs .contentWhite').addClass('borde');
	$('.tabs').tabs({onShow:function(elm){$(elm).focus();}});
	var f = new Date();
	if(f.getDay() >= 2 && f.getDay() <= 5){
		$('.uno').triggerTab(1);
	}
	else if(f.getDay() == 6){
		$('.uno').triggerTab(2);
	}
	else{
		$('.uno').triggerTab(3);
	}	
}

function initCarrousel(){
	
	var sep = 8.8;
	var total = $("#carrusel ul li").length;
	
	if(total % 2 != 0){
		var elm = $('#carrusel ul li:nth('+ Math.round(total / 2) +') a').parent('li');
		elm.before($('#carrusel ul li:nth(0) a').parent('li').clone());
		total = $("#carrusel ul li").length;
	}
	
	$('.left-arrow, .right-arrow').removeClass('hide-js');
	$('.hide-items').removeClass('hide-items');
	$('#carrusel li a').children('strong').detach();
	
	$('#carrusel li a').each(function(index){
		$(this).data('img', $(this).children('img').attr('id'));
		$(this).data('href', $(this).attr('href'));
		$(this).data('alt', $(this).children('img').attr('alt'));
		$(this).data('idx', index + 1);
		$(this).css('position', 'absolute');
		$(this).css('left', (((index - 0) * sep) + 0) + 'em');
	});
	
	var cont = 0;
	var mov = false;
	var paso = 2;
	 
	$('#flecha-izquierda').click(function(){
		if(!mov){
			mov = true;
			if(cont >= total - 2){
				$('#carrusel li a').each(function(index){
						if($(this).data('idx') <= total - 2){
							$(this).css('left', ($(this).data('idx') * sep + sep) + 'em');
							$(this).data('idx', parseInt($(this).data('idx')) + 2);
						}
						else{
							if($(this).data('idx') == total){
								$(this).data('idx', '2');
							}
							else{
								$(this).data('idx', '1');
							}
						}
				});
				cont = 0;
			}
			cont += 2;
			$('#carrusel li a').each(function(index){
				var dis = '-=' + (sep * paso) + 'em';
				$(this).animate(
					{'left': dis}, 
					'normal', 
					function(){
						if($(this).data('idx') == total){
							mov = false
						}
					}
				);
			});
		}
	});
	
	$('#flecha-derecha').click(function(){
		if(!mov){
			mov = true;
			if(cont <= 0){
				$('#carrusel li a').each(function(index){
						if($(this).data('idx') > 2){
							$(this).css('left', (($(this).data('idx') - total) * sep - sep) + 'em');
							$(this).data('idx', parseInt($(this).data('idx')) - 2);
						}
						else{
							if($(this).data('idx') == 1){
								$(this).data('idx', total - 1);
							}
							else{
								$(this).data('idx', total);
							}
						}
				});
				cont = total - 2;
			}
			cont -= 2;
			$('#carrusel li a').each(function(index){
				var dis = '+=' + (sep * paso) + 'em';
				$(this).animate(
					{'left': dis}, 
					'normal',
					function(){
						if($(this).data('idx') == total -1){
							mov = false;
						}
					}
				);
			});
		}
	});
	
	$('#carrusel li a').mouseenter(function(e){
		e.preventDefault();
		e.stopPropagation();
		$('#a-item-central').css('display', 'none');
		$('#a-item-central a').attr('href', $(this).data('href'));
		$('#a-item-central a img').attr('src', '/dicadiw/img/' + $(this).data('img'));
		$('#a-item-central a img').attr('alt', $(this).data('alt'));
		$('#a-item-central a span.texto').html($(this).data('alt').replace(/([0-9]+\.[0-9]+ .)/, '<span class="premio">$1</span>').replace(/(con.+)/, '<span class="ocultar">$1</span>'));
		$('#a-item-central a span.precio').text($(this).children('img').attr('class'));
		$('#a-item-central').fadeIn('slow');
		$('#a-item-central a').focus();
	});
	$('#carrusel li a').focus(function(e){
		e.preventDefault();
		e.stopPropagation();
		$('#a-item-central').css('display', 'none');
		$('#a-item-central a').attr('href', $(this).data('href'));
		$('#a-item-central a img').attr('src', '/dicadiw/img/' + $(this).data('img'));
		$('#a-item-central a img').attr('alt', $(this).data('alt'));
		$('#a-item-central a span.texto').html($(this).data('alt').replace(/([0-9]+\.[0-9]+ .)/, '<span class="premio">$1</span>').replace(/(con.+)/, '<span class="ocultar">$1</span>'));
		$('#a-item-central a span.precio').text($(this).children('img').attr('class'));
		$('#a-item-central').fadeIn('slow');
	});

}

function initRandom(){
		var aleatorio = (Math.round(Math.random() * ($('#carrusel li a').length - 1)) + 1);
		var elm = $('#carrusel ul li:nth('+ (aleatorio - 1) +') a');
		$('#a-item-central').css('display', 'none');
		$('#a-item-central a').attr('href', elm.data('href'));
		$('#a-item-central a img').attr('src', '/dicadiw/img/' + elm.data('img'));
		$('#a-item-central a img').attr('alt', elm.data('alt'));
		$('#a-item-central a span.texto').html(elm.data('alt').replace(/([0-9]+\.[0-9]+ .)/, '<span class="premio">$1</span>').replace(/(con.+)/, '<span class="ocultar">$1</span>'));
		$('#a-item-central a span.precio').text(elm.children('img').attr('class'));
		$('#a-item-central').fadeIn('slow');
}
