﻿var tripWire = new function() {

    this.Active = false;
    this.Enabled = false;
    this.OnTrap = '';

    this.activate = function() { this.Active = true; return true; }

    this.deactivate = function() { this.Active = false; return true; }

    this.trip = function() {

        if (!this.Enabled || !this.Active)
            return;

        scroll(0, 0);

        this.deactivate();

        eval(this.OnTrap);

        return true;
    }

    this.setTrap = function(trapHandler) {

        var secondChanceEnabled = $.cookie("secondChance.Enabled");
        if (secondChanceEnabled != null && secondChanceEnabled == "false") {
            this.Enabled = false;
            return;
        }

        this.setWires();
        this.OnTrap = trapHandler;
        this.Enabled = true;
        return;
    }

    this.setWires = function() {
        $(document.body).append(
            '<div id="tripWireZone">' +
            this.getTripWireElement('trip') +
            this.getTripWireElement('activate') +
            this.getTripWireElement('deactivate') +
            '</div>'
            );

        $(window).scroll(function() {
            $('#tripWireZone').css('top', $(this).scrollTop() + "px");
            $('#secondChancePage').css('top', $(this).scrollTop() + "px");
        });
    }

    this.getTripWireElement = function(methodName) {
        return '<div class="tripWire" onmouseover="tripWire.' + methodName + '();"><img src="/UserControls/SecondChance/spacer.gif" alt="" /></div>';
    }

}

var secondChance = new function() {

    this.searchUrlBase = '';

    this.show = function() {
        window.onscroll = function() { $('#secondChancePage').css('top', document.body.scrollTop); };

        $('#secondChancePage')
        .css('display', 'block')
        .css('top', document.body.scrollTop);
    }

    this.hide = function() {
        $("#secondChancePage").css("display", "none");
        return false;
    }

    this.search = function() {
        var searchTerm = $('#secondChanceSearchInput').val();
        if (searchTerm.length == 0) {
            $('#secondChanceSearchInput').css('border-color', 'red').trigger('focus');
            window.setTimeout("$('#secondChanceSearchInput').css('border-color', 'black');", 1000);

            return false;
        }

        document.location = this.searchUrlBase + escape(searchTerm);
        return false;
    }

    this.searchOnEnter = function(e) {
        var characterCode = (e && e.which) ? e.which : event.keyCode;

        if (characterCode == 13) {
            this.search();
            return false;
        }

        return true;
    }

    this.relax = function() {
        if (this.href.toLowerCase().indexOf('result.php') > 0) {
            tripWire.Enabled = false;
            $.cookie("secondChance.Enabled", "false");
        }

        return true;
    }
}


