﻿(function($) {

    var sessionTimer;
    function PopupBlocked() {
        var PUtest = window.open(null, "", "width=100,height=100");
        try { PUtest.close(); return false; }
        catch (e) { return true; }
    }

    $(document).ready(function() {

        var endDate = new Date(2010, 04, 01);
        var curDate = new Date();
        if (curDate < endDate) {
            $('body').append('<div id="confirmSurvey" class="hidden"><img src="/Images/logo2.gif" alt="Honda Power Equipment" /><br /><p>Would you like to participate in a brief, 3 minute survey on this site?</p></div>');
            $('body').append('<a href="http://www.acurasurveys.com/se.ashx?s=3FB418BF31C239FF" class="survey"></a>');

            $("#confirmSurvey").dialog({
                bgiframe: true,
                width: 300,
                modal: false,
                autoOpen: false,
                title: 'Take a survey',
                buttons: {
                    Yes: function() {
                        $(this).dialog('close');
                        var url = "http://www.acurasurveys.com/se.ashx?s=3FB418BF31C239FF&KeepThis=true&TB_iframe=true&width=650&height=700";
 
                        tb_show("Survey", url)      
                    },
                    No: function() {
                        $(this).dialog('close');
                    }
                }
            });


            $("#confirmSurvey").removeClass('hidden');

            $(document).bind('keydown', 'ctrl+s', function(e) {
                $(document).trigger('takesurvey');
                e.preventDefault();
                return false;
            });

            $('a').click(function(e) {
                var href = $(this).attr('href');
                if (href.indexOf('http') == -1)
                    $(document).data('DisableSurvey', true);
            });
            $('form').submit(function(e) {
                $(document).data('DisableSurvey', true);
            });


            $(document).bind("takesurvey", function(e) {
                $.cookie('PESurveyCookie', 'offered', { path: '/', expires: 14 })
                $("#confirmSurvey").dialog('open');
            });

            var surveyCookie = $.cookie('PESurveyCookie');
            var timeout = $.cookie('PESurveyTimer');
            if (!timeout)
                timeout = 350000;
            if (!surveyCookie) {
                $.cookie('PESurveyTimer', timeout, { path: '/', expires: 14 });
                sessionTimer = window.setTimeout("showSurvey()", 1000);
            }
        } // curdate < endDate
    });

})(jQuery);

function showSurvey() {
    var timeout = jQuery.cookie('PESurveyTimer');
    if (timeout > 0) {
        var newTime = timeout - 1000;
        jQuery.cookie('PESurveyTimer', newTime, { path: '/', expires: 14 });
        sessionTimer = window.setTimeout("showSurvey()", 1000);
        jQuery('#countdown').text(newTime);
    }
    else {
        jQuery(document).trigger('takesurvey');
    }
}
