$(document).ready(function() {
  ReplaceLightboxLink();
  $('a[rel*=facebox]').click(function() { // thickbox
    var href = $(this).attr('href');
    href = AddParamsToLink('KeepThis=true&TB_iframe=true&width=450', href);

    tb_show('', href, '');
    return false;
  });


  // To close thickbox
  $('.thickbox-close').click(function() {
    self.parent.tb_remove();
    return false;
  });


  // To clear out form
  $('input[name="btnClear"]').css('display', 'inline');
  $('input[name="btnClear"]').click(function() {
    $('input[type="text"]').val('');
  });


  var source = ReadCookie('utm_source');
  if( source !== null && source == 'google' ) {
    $('a.SubscribeButton').each(function() {
      var href = $(this).attr('href');
      href = AddParamsToLink('cds_response_key=FIGOOGLE', href);
      $(this).attr('href', href);
    });
  }

});


function AddParamsToLink(Params, Href) {
  if( Href.indexOf('?') == -1 ) {
    Href += '?' + Params;
  } else {
    Href += '&' + Params;
  }
  return Href;
}


function ReplaceLightboxLink() {
  $('a[rel*=facebox]').each(function() {
    var href = $(this).attr('href');
    href += '&v=l';
    $(this).attr('href', href)
  });
}

function ReadCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


