$(document).ready(function(){
  // Tabs


  if ($('.tab-container').length > 0) {
  	if ($('#homepage-tabs').length > 0) {
  	  	$(function() {$( ".tab-container" ).tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 7500);});
  	} else{
  		$('.tab-container').tabs({ fx: { opacity: 'toggle' } });
  	}
  }
  
  $(".tabs li a , .lateral-tabs li a").click(function () { $(this).blur(); });

  // Tables extra styling
  if ($('.tableData').length > 0) {
    $(".tableData tr:even td").css("background-color","#e5e5e5");
    $(".tableData th:last").css("border-right","none");
  }
  
  if ($('#PersonalPortfolio').length > 0) {
    $("#PersonalPortfolio tr:odd td , #PersonalPortfolio tr:odd th").css("background-color","#e5e5e5");
    $("#PersonalPortfolio tr:even td").css("background-color","transparent");
  }
  
  $(".msgError a , .msgAnnounce a , .msgConfirm a").click(function(){$(this).parent("div").hide();})
  
  // Adds target _blank to links with class "targetBlank"
  if ($('.targetBlank').length > 0) {
    $("a.targetBlank").attr('target','_blank');
  }
  // Navigation expand-collapse
  $("#LeftNav ul li.on a.header , #LeftNav ul li.off a.header").click(function(){
         var $this = $(this).parents("li");
       
       if( $this.is('.on') ) {
               $this.removeClass('on');
               $this.addClass('off');
       }
       if( $this.is('.off') ) {
               $("#LeftNav ul li").removeClass('on');
               $("#LeftNav ul li").addClass('off');
               $this.removeClass('off');
               $this.addClass('on');
               $this.blur();
       }
     });
  
  // Portfolio tables
  if ($('#PortfolioSummary').length > 0) {
      $("td:nth-child(even)").css("background-color","#e5e5e5");
  }
  if ($('.portfolioTable').length > 0) {
      $(".portfolioTable tr:nth-child(even) td , .portfolioTable .portfolioTable tr:nth-child(even) td").css("background-color","#f4f4f4");
	  $(".portfolioTable tr:nth-child(odd) td ,.portfolioTable .portfolioTable tr:nth-child(odd) td").css("background-color","#ffffff");
  }
  
  // Join page Free service confirmation
  $("#optin_3rd_email").click(function(){
    if ($("#optin_3rd_email:checked").val() == null) {
	  $(this).attr('checked', true);  
	  var answer = confirm('Do you really want to miss out on this special information including share tips, free research, newsletters and other special offers and information? If you do wish to receive it please click OK, if not please click CANCEL.');
	  if(answer){
	  $(this).attr('checked', true);
      } else {$(this).attr('checked', false);}
    }
  });

  if($("#TradeStep1").length>0){
    if ($('#BuyRadio')[0].checked) {                          // If BUY is selected 
	  $("#ConfirmBtn").attr("value","Confirm (BUY)");
	} else {$("#ConfirmBtn").attr("value","Confirm (SELL)");}
	$("#BuyRadio").click(function(){$("#ConfirmBtn").attr("value","Confirm (BUY)");});
	$("#SellRadio").click(function(){$("#ConfirmBtn").attr("value","Confirm (SELL)");});
  }
  
  // Join page (second stage) - show/hide options for students 
  $('#role option').click(function(){
      if ($(this).attr('value')=='8' || $(this).attr('value')=='12' || $(this).attr('value')=='13') { 
              $('#isStudent').fadeIn()
      } else {$('#isStudent').fadeOut()}
  });
  
  $(".inputRow #country").change(function(){
     selected_country = $("#country").val();
     if (selected_country != "GB") {
     	     //alert('no GB');
       $("#postcode-finder-wrapper").slideUp('fast');
     }  else {$("#postcode-finder-wrapper").slideDown('fast');}
  });
  if ($('#guide-modal-welcome').length > 0) {
    $("#guide-modal-welcome").jqm().jqmShow();  
  }
  
});

function chooseGame(epic,company){
  $("a.stock").attr("href","stock.trade.php?epic=" + epic);
  $("a.spread").attr("href","spread.trade.php?epic=" + epic);
  $("a.cfd").attr("href","cfd.trade.php?epic=" + epic);
  $(".epicContainer").html(epic + " - " + company);
  $("#ChooseGame").jqm().jqmShow();
}

function showLateralTab(tab){
	switch(tab) {
		case 1:
		  activeThis = 'tab-stock-trading'
          break;
		case 2:
		  activeThis = 'tab-cfd-trading'
          break;
		case 3:
		  activeThis = 'tab-spread-betting'
          break;
		case 4:
		  activeThis = 'tab-fx-trading'
          break;
		case 5:
		  activeThis = 'tab-fixed-odds'
          break;
    }
	activeThis = '#' + activeThis;
	$('.lateral-tabs li').removeClass('on');
	$('.lateral-tabs li').eq(tab-1).addClass('on');
	$('.tab-content').hide();
	$(activeThis).show();
}
