﻿var closedivBulbEq;
var closedcfltypes;

$(function () {

    //remove the headers
    $('#bulbEqContent div div H2').remove();
    $('#cflContent div div H2').remove();
    $('#events-tab div div H2').html('Upcoming Events');
    $('#bulbEqContent').dialog({
        autoOpen: false,
        async: false,
        modal: true,
        width: 750,
        height: 425,
        open: function () {
            closedcfltypes = 1;
            $(document).bind('click', function () {
                if (closedcfltypes) {
                    $('#bulbEqContent').dialog('close')
                }
                closedcfltypes = 1;
            });
        },
        focus: function () {
            closedcfltypes = 0;
        },
        close: function () { $(document).unbind('click'); },
        show: "fade",
        hide: "fade",
        resizable: false,
        title: 'Bulb Equivalents',
        zIndex: 9999
        //        buttons: { x: function() { $(this).dialog('close'); } }
    });


    $('#bulbequiv').click(function (event) {
        $('#bulbEqContent').dialog('open');
        closedcfltypes = 0;
    });


    $('#cflContent').dialog({
        autoOpen: false,
        async: false,
        modal: true,
        width: 775,
        height: 490,
        open: function () {
            closedivBulbEq = 1;
            $(document).bind('click', function () {
                if (closedivBulbEq) {
                    $('#cflContent').dialog('close')
                }
                closedivBulbEq = 1;
            });
        },
        focus: function () {
            closedivBulbEq = 0;
        },
        close: function () { $(document).unbind('click'); },
        show: "fade",
        hide: "fade",
        resizable: false,
        title: 'CFL Types & Applications',
        zIndex: 9999
        //        buttons: { x: function() { $(this).dialog('close'); } }
    });


    $('#cfltypes').click(function (event) {
        $('#cflContent').dialog('open');
        closedivBulbEq = 0;
    });


    //hook up the dialog box
    $('#retailer-dialog').dialog({
        autoOpen: false,
        async: false,
        modal: true,
        resizable: true,
        zIndex: 9999,
        width: 800,
        height: 600,
        title: 'Search Results',
        close: function () {

        }
    });

    $('.search').click(function (event) {
        SearchRetailers();
        event.preventDefault();
    });

    function SearchRetailers() {
        var url = __siteRoot + '/TEP/Retailer.aspx?'
        var zip = $('.zip').val();
        var retailer = $('.retailer option:selected').val();
        var bulbtype = $('.bulbtype option:selected').val();
        var recycler = "0";
        
        if ($('.recyclers input').is(":checked")) {
            recycler = "1"
        }

        params = { "RetailerName": retailer,
            "BulbType": bulbtype,
            "ZipCode": zip,
            "Recycle": recycler
        };

        url = url + $.param(params);

        $('#page').block({ message: '<h3> Loading Retailers. Please wait...', css: { border: '3px solid red'} });

        $.ajax({ url: url,
            type: 'GET',
            data: {},
            dataType: 'html',
            complete: function (xhr, status) {
                $('#page').unblock();
                $('#retailer-dialog').dialog('open');
            },
            success: function (result) {
                $('#retailer-dialog').html(result);

            }
        });
    }
});


