// JavaScript Document
$(document).ready(function() {
//canvi fondo
var imagenbg="library/interface/stadium2.jpg";
var aleatorio = Math.floor(Math.random()*6)  
switch (aleatorio) {
    case 0:
       imagenbg="library/interface/tennis.jpg";
       break
    case 1:
       imagenbg="library/interface/atlete.jpg";
       break
    case 2:
		imagenbg="library/interface/snow.jpg";
        break
	case 3:
		imagenbg="library/interface/fondostadium.jpg";
        break
	case 4:
		imagenbg="library/interface/basket.jpg";
		break
	case 5:
		imagenbg="library/interface/stadium2.jpg";
		break
    default:
		imagenbg="library/interface/fondostadium.jpg";
} 

var ratio=1.6;
var bg=$("#bg");
$("#bg").attr('src',imagenbg)

function updateBackground() {
  screenWidth = $(window).width(); screenHeight = $(window).height();
  if (screenWidth/screenHeight > 1.6) {$(bg).height("auto");$(bg).width("100%");} else {$(bg).width("auto");$(bg).height("100%");}
}

updateBackground();
$(window).bind("resize", function() {updateBackground();});
/*
$("body").ezBgResize({
	img     : imagenbg, //"library/interface/fondostadium.jpg", // Relative path example.  You could also use an absolute url (http://...).
	opacity : 1, // Opacity. 1 = 100%.  This is optional.
	center  : true // Boolean (true or false). This is optional. Default is true.
});*/

//menu separadors
$('.boton').mouseenter(function(){
  $(this).prev().css("backgroundImage","none");
  $(this).next().css("backgroundImage","none");
}).mouseleave(function(){
  $(this).prev().css("backgroundImage","url(library/interface/separador.jpg)");
  $(this).next().css("backgroundImage","url(library/interface/separador.jpg)");
});

//login
$('#register, #registro').click(function(){location.href='registro.php';});
$('#login').click(function(){
	$('.entrar').show();
	$('#login').addClass('rojo')
});
$('#micuenta').click(function(){$('#tucuenta').show()});
$('#verca').click(function(){location.href='carrito.php';});
$('#cuenta').click(function(){location.href='editar.php';});
$('#estado').click(function(){location.href='estado.php';});
$('#logout').click(function(){location.href='logout.php';});
$('#close').click(function(){
	$('.entrar').hide();
	$('#login').removeClass('rojo')
});
$('#check').click(function(){
	$('.entrar').hide();
	$('#login').removeClass('rojo')
});

//links menu
$('#s1').click(function(){location.href='index.php';});
$('#s2').click(function(){location.href='empresa.php';});
$('#s3').click(function(){location.href='productos.php';});
$('#s4').click(function(){location.href='estudios.php';});
$('#s5').click(function(){location.href='deportistas.php';});
$('#s6').click(function(){location.href='galeria.php';});
$('#s7').click(function(){location.href='contacto.php';});

//shadowbox		
$(".yoxview").yoxview({ autoHideMenu: false });

//productos
$('.categoria').click(function() {
//	$('.remarcar').removeClass('remarcar');
//	$('#categoria').empty();
//	$('#contingut').empty();
//	$('.deporte').css('color','#9E9696');
//	$('#'+$(this).attr("id")+' div:first-child').addClass('remarcar');
//	$('#'+$(this).attr("id")+' .deporte').css('color','#E31E24');
//	$('#productes').show(800);
	categoria($(this).attr("num"),$(this).attr("id"));
});

$('.categorias').css('height',97*$('.categorias').attr("fil"));

//alert($('.categorias').attr("fil"))
//ajax
function categoria(idf,titol){
	location.href='productos.php?cat='+idf+'&pos=195';
/*	$.ajax({
		type: "GET",
		url: "producto.php",
		data: "cat="+idf,
		success: function(msg){
			$('#categoria').append(titol);
			$('#contingut').append(msg);
		}
	});*/
}

//slideshow noticias
  var currentPosition = 0;
  var slideWidth = 380;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
	.wrapAll('<div id="slideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
	  'float' : 'left',
	  'width' : slideWidth
	});

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
	.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
	.append('<span class="control" id="rightControl">Clicking moves right</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  
  function clica(){
	// Determine new position
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
	
	// Hide / show controls
	manageControls(currentPosition);
	// Move slideInner using margin-left
	$('#slideInner').animate({
	  'marginLeft' : slideWidth*(-currentPosition)
	});
  }
  
  //$('.control').bind('click', clica);
  function goPage(page){
  	currentPosition = page-1;
	manageControls(currentPosition);
	$('#slideInner').animate({'marginLeft' : slideWidth*(-currentPosition)});
  }
  
  function startPage(page){
  	currentPosition = page-1;
	manageControls(currentPosition);
	$('#slideInner').css({'marginLeft' : slideWidth*(-currentPosition)});
  }

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
	// Hide left arrow if position is first slide
	//if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	if(position==0){ $('#leftControl').css("backgroundPosition","bottom").unbind('click'); } else{ $('#leftControl').css("backgroundPosition","top").unbind('click').bind('click', clica)}
	// Hide right arrow if position is last slide
	//if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
	if(position==numberOfSlides-1){ $('#rightControl').css("backgroundPosition","bottom").unbind('click'); } else{ $('#rightControl').css("backgroundPosition","top").unbind('click').bind('click', clica) }
  }	

  startPage($('html').attr('pag'))
//bordes redondejats
/*$(".rounded-img, .rounded-img2").load(function() {
    $(this).wrap(function(){
      return '<span class="' + $(this).attr('class') + '" style="background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
    });
    $(this).css("opacity","0");
  });
*/
});
