$(document).ready(function () {
	
	// set up the tabs for the kop widget
	$("#tabsKop").tabs(
		{ selected : 0 }
	);
	// set up the tabs for the match widget
  $("#tabsMatch").tabs();
	// set up the tabs for the news widget
	$("#tabsNewsWidget").tabs();
	// set up the tabs for the teams widget
	$("#tabsTeam").tabs();
	
	// set up the LFC TV accordion
	
	// builds the tabs first to ensure the accordion is the correct size
	$(".Marg2").tabs();
  $('#tv_day_options li a').bind("click", function(e){
    $('#tv_day_options li a').parent().removeClass('DaysOn');
    $(this).parent().addClass('DaysOn')
    var target = $(this).attr('rel');
    $('#'+target).triggerHandler("click"); // invoke a fake click on the relevent hidden tab
  });

	$("div#accordion").accordion({
    header: "dl",
		icons: {}
  });
	
	// set up the squad member summary widget
	// squad variable comes from an external json file
	$("#tabsTeam-1 .SquadPics img").bind("click", function(e){
    id = $(this).attr('class').replace(/squadimg_/, '');
    $('#tabsTeam-1 #large-player-pic').attr('src',squad[id]['image']);
    $('#tabsTeam-1 .Shirt').html(squad[id]['squad_number']);
    $('#tabsTeam-1 .Squad a').attr('href', squad[id]['url']).html(squad[id]['firstname']+'<br />'+squad[id]['surname']);
    $('#tabsTeam-1 .position').html(squad[id]['position']);
    $('#tabsTeam-1 .nationality').html(squad[id]['nationality']);
    $('#tabsTeam-1 .dob').html(squad[id]['dob']);
		return false;
  });
	// triggers the first item to load on page load
  $("#tabsTeam-1 .SquadPics img:first").triggerHandler("click");
	
	// set up the legends summary widget
	// legends variable comes from an external json file
	$("#tabsTeam-2 .SquadPics img").bind("click", function(e){
    id = $(this).attr('class').replace(/squadimg_/, '');
    $('#tabsTeam-2 #large-legend-pic').attr('src',legends[id]['image']);
    $('#tabsTeam-2 .Squad a').attr('href', legends[id]['url']).html(legends[id]['firstname']+'<br />'+legends[id]['surname']);
    $('#tabsTeam-2 .position').html(legends[id]['position']);
    $('#tabsTeam-2 .nationality').html(legends[id]['nationality']);
    $('#tabsTeam-2 .games').html(legends[id]['games']);
		return false;
  });
	// triggers the first item to load on page load
  $("#tabsTeam-2 .SquadPics img:first").triggerHandler("click");

	// shop carousel
	$('.TabR a').bind("click", function(e){
    moveShop('right');
    return false;
  });

  $('.TabL a').bind("click", function(e){
    moveShop('left');
    return false;
  });
	
});

// move action for the homepage shop carousel
function moveShop(direction,callback) {
  $('#Shop').queue(function() {
    var left = parseInt($('#Shop').css('left'));
    var width = parseInt($('#Shop').css('width'));
    var max = (width - 316) * -1;
    if(direction == 'right' && left > max) {
      var move = parseInt(left) - 316;
      $(this).animate(
        { left : move+'px'},500
      );
    }
    if(direction == 'left' && left < 0) {
      var move = parseInt(left) + 316;
      $(this).animate(
        { left : move+'px'},500
      );
    }
    $(this).dequeue();
  });
  $('#Shop').queue(function() {
    updateButtons();
    $(this).dequeue();
  });
}

// action to update the interface buttons for the homepage shop carousel
function updateButtons() {
  var left = parseInt($('#Shop').css('left'));
  var width = parseInt($('#Shop').css('width'));
  var max = (width - 316) * -1;
  if(left == 0) {
    $('.TabL').removeClass('UpL');
    $('.TabR').addClass('UpR');
  } else if(left <= max) {
    $('.TabL').addClass('UpL');
    $('.TabR').removeClass('UpR');
  } else {
    $('.TabL').addClass('UpL');
    $('.TabR').addClass('UpR');
  }
}
