$(document).ready(function(){
  $('#deals article:not(.disabled)').click(function(e){
    if ($(this).hasClass('selected')){
      // If the bubble clicked is already marked as 'selected', then unmark it and close its bubble
      $(this).toggleClass('selected');
      $(this).children('div').toggle();
    } else {
      // Find any deals that are slected and bubbles that are already open
      // then unmark them and close the bubbles
      $('#deals article.selected').children('div').fadeToggle(200);
      $('#deals article.selected').toggleClass('selected');
      $(this).children('div').toggle();
      $(this).toggleClass('selected');
    }
    if($('#deals article.selected').length == 0) {
      $('#deals').removeClass('selected');
      $('#limited-time-offer').fadeIn();
    } else {
      $('#deals').addClass('selected');
      $('#limited-time-offer').fadeOut();
    }
    
    e.preventDefault();
  });

  
  $('#older-deals-link').click(function(e){
    $('#twenty-euro-deals').toggleClass('expanded');
    if($('#deals article.selected').length == 0) {
      $('#limited-time-offer').fadeToggle();
    } else {
      $('#deals article.selected').click();
    }
    $('#older-deals').toggle();
    $('#thirty-euro-deals').hide().show();
    e.preventDefault();
  });
  
  $('#top-up-options-link').click(function(e){
    $('#top-up-options').slideToggle(250);
    $(this).toggleClass('expanded');
    e.preventDefault();
  });
  
  

  var view_plan_clicked = false;
  $("a.view_plan").click("click", function() {
    
    $tabs.tabs('select', 0);
    if (view_plan_clicked === false) {
      view_plan_clicked = true;
      $("tr.limited_offer").animate({ backgroundColor: "#F2A500" }, 1000).animate({ backgroundColor: "#333333"}, 1000);
    }
    
    view_plan_clicked = false;
  });

  var $tabs = $("#meteor_plans").tabs();
  $("#meteor_plans").bind('tabsshow', function(event, ui) {
    var tagWithHashId = $(ui.tab.hash);
    var hash = ui.tab.hash;
    hash = hash.replace("#", "");
    $(tagWithHashId).attr('id', hash + '-tmp');
    window.location.hash = ui.tab.hash;
    $(tagWithHashId).attr('id', hash);
  });
  
  $('.iphone_plan_selector').click(function(){ 
    $('.iphone_plan_rows').hide(); 
    $("#" + $(this).attr('id') + "_rows").show()
  });
  
  $('.iphone_plan_selector').first().click()

  $(".purchase_type_select").click(function(){
    planId = $(this).attr('data-plan-id')
    links = $("#billpay-phone-purchase-choice .submit").each(function(index) {
      href = $(this).attr('href')
      $(this).attr('href', href.replace(/plan_id=\d*/, 'plan_id=' + planId))
    })
  })
  
  $(".purchase_type_select").colorbox({
     inline: true, 
     href: "#lightbox-content-holder #billpay-phone-purchase-choice",
     transition:	"elastic",
     speed:		700,
     width:		400,
     height:		285,
     fixed:		true,
     opacity:	0.6
   });
  
});
