function normalize_url(input_href) {
    var this_url    = window.location.href,
        url_parts   = this_url.match(/^(https?:\/\/[^\/]+)(\/.+)(\/([^\/]+)?)(\?.+)?$/),
        domain_part = url_parts[1],
        path_part   = url_parts[2],
        file_part   = url_parts[3];
    
    if (input_href.search(/^https?:/) !== -1) {
        return [input_href, domain_part];
    }
    if (input_href.search(/^\//) !== -1) {
        return [domain_part + input_href, domain_part];
    }
    if (input_href.search(/^javascript:/) !== -1) {
        return [this_url, domain_part];
    }
    if (input_href.search(/^(#|\?)/) !== -1) {
        return [this_url, domain_part];
    }
    if (input_href.search(/^\.\//) !== -1) {
        return [domain_part + path_part + '/' + input_href.replace(/^\.\/+/, ''), domain_part];
    }
    if (input_href.search(/^\w+(?!:)/) !== -1) {
        return [domain_part + path_part + '/' + input_href, domain_part];
    }
    return [input_href, domain_part];
}
function match_url(href, exceptions) {
    var x = normalize_url(href),
        real_href = x[0],
        url_base  = x[1],
        i,l;
    for (i=0, l=exceptions.length; i<l; i++) {
        if (real_href.match(exceptions[i])) {
            return true;
        } else if (real_href.search(url_base) === 0) {
            var stripped = real_href.replace(url_base, '');
            if (stripped.match(exceptions[i])) {
                return true;
            }
        }
    }
    return false;
}



  var displaypopup = true;
  $(document).ready(function() {
    switch(abandon_type)
    {
        case "T1":
            t1();
            break;
        case "T2":
            t2();
            break;
        case "T3":
            t3();
            break;
        case "T4":
            t4();
            break;
        case "T5":
            t5();
            break;
        case "T6":
            t6();
            break;
    
    }
    $(".abandonclose").click(function(){modal.hide()});
  });

  window.onbeforeunload = confirmExit;
  function confirmExit(e)
  {
    if(abandon_type =="T1"){
        $.cookie("tarotabandon", abandon_type, {path:'/', expires: abandon_cookie_shelflife });
        displaypopup = false;
        $.ajax({
           type: "POST",
           url: "/ajax/ajaxProxy.php?url_fwd=http%3A%2F%2Fapi.tarot.com%2Fapi%2Flog_abandonment.php",
           data: {user_id:abandon_uid,promo_type:abandon_type,url:urlencode(location.href)},
           success: function(msg){
           }
         });
    }
    // before we call the popup we need to set a cookie
    if(displaypopup) {
        _gaq.push(['_trackEvent', 'Abandonment', 'impression', abandon_type]);
        $.cookie("tarotabandon", abandon_type, {path:'/', expires: abandon_cookie_shelflife });
        displaypopup = false;
                $.ajax({
           type: "POST",
           url: "/ajax/ajaxProxy.php?url_fwd=http%3A%2F%2Fapi.tarot.com%2Fapi%2Flog_abandonment.php",
           data: "user_id="+abandon_uid+"&promo_type="+abandon_type+"&url="+urlencode(location.href),
           success: function(msg){
           }
         });
        $('#dialog').modal();
        return abandon_message;
    }
  }

  function isUrl(s, urlexp) {
    var regexp = urlexp;
    return regexp.test(s);
  }
  
  function t1()
  {
    var excludelist = [/^https\:\/\/(\w+[\-\.])*tarot\.com\/karma\/store\/kc_purchase\.php/,
                     /^https\:\/\/(\w+[\-\.])*tarot\.com\/commerce\/purchase/, /\/astrosync\//];
    //disable forms
    $("form").submit(function(){
        displaypopup = false;
    });

    //handle links
    $("a").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });
  }


  function t2()
  {
    // exclude tarot.com urls and relative urls
    var excludelist = [/^http\:\/\/(\w+[\-\.])*tarot\.com/, /^\//,/^https\:\/\/(\w+[\-\.])*tarot\.com/,/\/astrosync\//];
    $("#default_submit").click(function(){
        displaypopup = false;
    });
    $("form").submit(function(){
        displaypopup = false;
    });
    $("#tarotloginform").submit(function(){
        displaypopup = true;
    });
    // handle links
    $("a").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });
    $("area").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });
  }

  function t3()
  {
    if($("#complete_btn").length > 0){
      complete_btn_inlineclick = $("#complete_btn").attr("onclick");
      $("#complete_btn").attr("onclick","");
    }
    var excludelist = [/^javascript/, /^\/product\/input\//,/^\/tarot\/flash_jump\.php/,/\/astrosync\//];
    $("input").click(function(){
        displaypopup = false;
    });
    $("#default_submit").click(function(){
        displaypopup = false;
    });
    $("form").submit(function(){
        displaypopup = false;
    });
        $("#flash_shuffle").click(function(){
                displaypopup = false;
        });
    $("#flash_shuffle").mouseover(function(){
        displaypopup = false;
    });
    $("#flash_shuffle").mouseout(function(){
        displaypopup = true;
        });
    $("#complete_btn").click(function(){
        displaypopup = false;
        complete_btn_inlineclick();
        });
    $("a").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });
  }

  function t4()
  {
    var excludelist = [/^http\:\/\/(\w+[\-\.])*freekarmacoins\.com/,/^\/login/,/^\/signup\//,/\/astrosync\//];
    $("#paypal").click(function(){ displaypopup=false; });
    $("#buykcbutton").click(function(){displaypopup=false;});
    $("a").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });    
    $("#pbutton").click(function(){displaypopup = false;});
    $("form").submit(function(){
        displaypopup = false;
    });
  }

  function t5()
  {
    var excludelist = [
        /^\/astrology\/getreport\.php/,
        /^\/promos\/trialpay_redirect\.php/,
        /^\/astrosync\/about\//,
        /^\.\/category\.php/,
        /^\.\/select\.php/,
        /^http\:\/\/(\w+[\-\.])*tarot\.com\/tarot\/select\.php/,
        /^\/login/,
        /^\/signup\//,
        /^\/astrology-reports\/category\.php/,
        /^\/tarot\/expert\.php/,
        /^\/xfer2\.php/,
        /^\/product\/input\//,
        /\/astrosync\//
    ];
    $("a").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });
    $("form").submit(function(){
        displaypopup = false;
    });
        $("area").click(function(){
        displaypopup = false;
    });
  }

  function t6()
  {
    var excludelist = [
        /^\/(articles\/)?psychics\//,
        /^http:\/\/(www\.)?keen\.com/
    ];
    $("a").click(function(){
        if (match_url($(this).attr('href'), excludelist)) {
            displaypopup = false;
        }
    });
    $("#keen-listing select").bind('change', function(e){
        displaypopup = false;
    });
    $("area").click(function(){
        displaypopup = false;
    });
  }


  function urlencode (str) {
        str = escape(str);
        return str.replace(/[*+\/@]|%20/g,
            function (s) {
                switch (s) {
                case "*": s = "%2A"; break;
                case "+": s = "%2B"; break;
                case "/": s = "%2F"; break;
                case "@": s = "%40"; break;
                case "%20": s = "+"; break;
            }
        return s;
        }
        );
  }
