$(document).bind('click', function(e){
  if(!($(e.target).isChildOf('.filterContainer')))
  {
    if($("#guidesSelectBox").css('display')=='block')
      saveGuidesSelectBox();
    if($("#dealsSelectBox").css('display')=='block')
      saveDealsSelectBox();
  }
});

function saveGuidesSelectBox()
{
  // Store these categories
  storeCategories();
  // Check whether the main checkbox is required.
  tickCorrectBoxes('guides');

  hideGuidesSelectBox();
}

function saveDealsSelectBox()
{
  // Store these categories
  storeCategories();
  // Check whether the main checkbox is required.
  tickCorrectBoxes('deals');

  hideDealsSelectBox();
}

function showGuidesSelectBox()
{
  if(typeof(currentDeals)!='undefined' && ($("#guidesSelectBox").css('display'))=='block')
    cancelDealsSelectBox();  // In case it is shown.

  // Store these categories
  storeCategories();

  $("#guidesSelectBox").css('display', 'block');
  $(".dealSelectButton").css('display', 'none');

  $("#dealsSelectBox").css('display', 'none');
}

function hideGuidesSelectBox()
{
  $("#guidesSelectBox").css('display', 'none');
  $(".dealSelectButton").css('display', 'block');

  regenerateButton();
}

function showDealsSelectBox()
{
  // Store these categories
  storeCategories();

  $("#dealsSelectBox").css('display', 'block');
  $("#guidesSelectBox").css('display', 'none');
}

function hideDealsSelectBox()
{
  $("#dealsSelectBox").css('display', 'none');
  regenerateButton();
}

function storeCategories()
{
  currentGuides=new Array();
  if($("#tagSelectG").attr('checked'))
    currentGuides[0] = 1;
  else
    currentGuides[0] = 0;
  $(".guideCategory").each( function() {
      if(this.checked)
        currentGuides[this.id.replace("cat","")]=1;
      else
        currentGuides[this.id.replace("cat","")]=0;
    } )
  currentDeals=new Array();
  if($("#tagSelectD").attr('checked'))
    currentDeals[0] = 1;
  else
    currentDeals[0] = 0;
  $(".tipCategory").each( function() {
      if(this.checked)
        currentDeals[this.id.replace("cat","")]=1;
      else
        currentDeals[this.id.replace("cat","")]=0;
    } )
}

function tickCorrectBoxes(boxtype)
{
  if(boxtype=='guides')
  {
    var currentGuidesLength=currentGuides.length;
    $("#guidesBox").attr('checked',false);
    for ( var i=0, len=currentGuidesLength; i<len; i++ ) {
      if(currentGuides[i]==1)
        $("#guidesBox").attr('checked',true);
    }
  }
  else
  {
    var currentDealsLength=currentDeals.length;
    $("#dealsBox").attr('checked',false);
    for ( var i=0, len=currentDealsLength; i<len; i++ ) {
      if(currentDeals[i]==1)
        $("#dealsBox").attr('checked',true);
    }
  }
}

function cancelGuidesSelectBox()
{
  // Reset checkboxes within this box
  if(currentGuides[0]==1)
    $("#tagSelectG").attr('checked',true);
  else
    $("#tagSelectG").attr('checked',false);
  toggleTagOnlyG();

  $(".guideCategory").each( function() {
        if(currentGuides[this.id.replace("cat","")]==1)
          this.checked=true;
        else
          this.checked=false;
      } )
  hideGuidesSelectBox();
}

function cancelDealsSelectBox()
{
  // Reset checkboxes within this box
  if(currentDeals[0]==1)
    $("#tagSelectD").attr('checked',true);
  else
    $("#tagSelectD").attr('checked',false);
  toggleTagOnlyD();

  $(".tipCategory").each( function() {
        if(currentDeals[this.id.replace("cat","")]==1)
          this.checked=true;
        else
          this.checked=false;
      } )

  hideDealsSelectBox();
}

function toggleTagOnlyG()
{
  if($("#tagSelectG").attr('checked'))
    disableAllGuideCategories();
  else
    enableAllGuideCategories();
}

function guideson()
{
  $("#tagSelectG").attr('checked',false);
  toggleTagOnlyG();
  $(".guideCategory").attr('checked',true);
}

function guidesoff()
{
  $(".guideCategory").attr('checked',false);
}

function dealson()
{
  $("#tagSelectD").attr('checked',false);
  toggleTagOnlyD();
  $(".tipCategory").attr('checked',true);
}

function dealsoff()
{
  $(".tipCategory").attr('checked',false);
}


function toggleTagOnlyD()
{
  if($("#tagSelectD").attr('checked'))
    disableAllDealCategories();
  else
    enableAllDealCategories();
}

function disableAllDealCategories()
{
  $(".dealCategoryLabel").attr('disabled',true);
  $(".dealCategoryLabel").css('color','#999');
  $(".dealCategoryCheckbox").attr('disabled',true);
}

function enableAllDealCategories()
{
  $(".dealCategoryLabel").attr('disabled',false);
  $(".dealCategoryLabel").css('color','');
  $(".dealCategoryCheckbox").attr('disabled',false);
}

function disableAllGuideCategories()
{
  $(".guideCategoryLabel").attr('disabled',true);
  $(".guideCategoryLabel").css('color','#999');
  $(".guideCategoryCheckbox").attr('disabled',true);
}

function enableAllGuideCategories()
{
  $(".guideCategoryLabel").attr('disabled',false);
  $(".guideCategoryLabel").css('color','');
  $(".guideCategoryCheckbox").attr('disabled',false);
}

function regenerateButton()
{
  var destination = '/article/search.htm?';
  var gcategorylist = '';
  var dcategorylist = '';
  if ($("#guidesBox").attr('checked'))
  {
    destination = destination + 'guidesBox=on&';
    if($("#tagSelectG").attr('checked'))
      destination = destination + 'tagOnlyG='+$("#tagValue").attr("value")+"&";
    else
    {
      $(".guideCategory").each( function() {
        if(this.checked)
          gcategorylist = gcategorylist + this.id.replace("cat","")+'|';
      } )
    }
  }
  if ($("#dealsBox").attr('checked'))
  {
    destination = destination + 'dealsBox=on&';
    if($("#tagSelectD").attr('checked'))
      destination = destination + 'tagOnlyD='+$("#tagValue").attr("value")+"&";
    else
    {
      $(".tipCategory").each( function() {
        if(this.checked)
          dcategorylist = dcategorylist + this.id.replace("cat","")+'|';
      } )
    }
    if($("#includeExpiredDeals").attr('checked'))
      destination = destination + 'includeExpired=on&';
  }

  if (gcategorylist.length > 1)
  {
    destination = destination + 'gcategories='+gcategorylist.substr(0,gcategorylist.length - 1)+'&';
  }

  if (dcategorylist.length > 1)
  {
    destination = destination + 'dcategories='+dcategorylist.substr(0,dcategorylist.length - 1)+'&';
  }

  if ($("#recentRadio").attr('checked'))
    destination = destination + 'orderChoice=recent';
  if ($("#popularRadio").attr('checked'))
    destination = destination + 'orderChoice=popular';
  if ($("#categoryRadio").attr('checked'))
    destination = destination + 'orderChoice=category';

  $("#filterFormLink").attr('href',destination);
}
