class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
after_filter :store_location
before_filter :capture_refferal


def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
  return unless request.get?
  if (request.path != "/users/sign_in" &&
      request.path != "/users/sign_up" &&
      request.path != "/users/password/new" &&
      request.path != "/users/password/edit" &&
      request.path != "/users/confirmation" &&
      request.path != "/users/sign_out" &&
      !request.xhr?) # don't store ajax calls
    session[:previous_url] = request.fullpath
  end
end

def after_sign_in_path_for(resource)
  session[:previous_url] || root_path
end

# def after_sign_in_path_for(resource)
#   session["#{resource}_return_to"] || root_url
# end

def after_sign_up_path_for(resource)
  session[:previous_url] || root_path
end

def after_update_path_for(resource)
  root_path
end

def after_cancel_user_registration_path_for(resource)
  root_path
end

private

def capture_refferal
  cookies[:tracking_link] = request.url
  cookies[:ref] = params[:ref] if params[:ref]
end


end
