var industriesOn = false;
var FROBSC = (function(window, document, undefined) {

    function _bindSizeCheck() {
        var form = $('#sizeCheck form'),
            modal = $('#sizeCheckWindow');

        modal.dialog({
            modal: true,
            closeText: 'close x',
            closeOnEscape: false,
            width: 565
        });

        form.submit(function() {
            if (form.val('mediumEnterprise') == "0") {
                modal.close();
                return false;
            }
        });
    }

    function _bindAssessment() {
        var form = $('#formCol form'),
            assessment = $('#assessment');

        assessment.dialog({
            autoOpen: false,
            modal: true,
            width: 565,
            closeText: 'close x'
        });

        $('.button', assessment).click(function(e) {
            form.submit();
            e.preventDefault();
        });

        form.submit(function() {
            if ($('#industry', form).val() == '' && $('input[type=checkbox]:checked', form).length == 0) {
                $('#formCol .error').show();
                return false;
            }
            else {
                if (!assessment.dialog('isOpen')) {
                    assessment.dialog('open');
                    return false;
                }
            }
        });
    }

    function _bindIndustrySolutions() {
        var photoElem = $('#photos'),
            currentIndex = -1,
            index = -1;

        $('#industries ul a').mouseover(function() {
            index = $(this).parent().index();
            if (index != currentIndex) {
                currentIndex = index;
                photoElem.stop(true, true).fadeOut(100, function() {
                    photoElem.css({ 'background-position': ['0 ', -210 * (index + 1), 'px'].join('') }).fadeIn(100);
                });
            }
        })
        $('#industries ul').mouseleave(function() {
            photoElem.stop(true, true).css({ 'background-position': '0 0' }).fadeIn(100);
            currentIndex = -1;
        });
    }
    function _bindProductCarousel() {
        $('#products ul').jcarousel({ scroll: 4, visible: 4 });
    }

    function _bindDrawerCarousel() {
        $('#drawers ul').jcarousel({ scroll: 4, visible: 4 });
        $("#drawers").accordion();
        $("#drawers").accordion("option", "icons", { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' });
        $("#drawers").css("visibility", "visible");
    }

    function _init() {
        _bindSizeCheck();
        _bindAssessment();
        _bindIndustrySolutions();
        //_bindIndustries();
        _bindProductCarousel();
        _bindDrawerCarousel();
    }

    return {
        init: _init
    };

})(this, this.document);

function toggleIndustries() {
    if (industriesOn) {
        industriesOn = false;
        $(".industriesNav a").css("color", "");
        $("menu").css("display", "none");
    }
    else {
        industriesOn = true;
        $(".industriesNav a").css("color", "#A9102A");
        $("menu").css("display", "block");
    }
}

$(document).ready(function() {
    $('html').removeClass('no-js');
    FROBSC.init();
});
function _bindIndustries() {
    var photoElem = $('#browseByIndustry'),
            currentIndex = -1,
            index = -1;

    $('#browseByIndustry-overlay ul a').mouseover(function() {
        index = $(this).parent().index();
        if (index != currentIndex) {
            currentIndex = index;
            photoElem.stop(true, true).fadeOut(100, function() {
                photoElem.css({ 'background-position': ['0 ', -384 * (index + 1), 'px'].join('') }).fadeIn(100);
            });
        }
    })
    $('#browseByIndustry-overlay ul').mouseleave(function() {
        photoElem.stop(true, true).css({ 'background-position': '0 0' }).fadeIn(100);
        currentIndex = -1;
    });
}
function _bindEducation() {
    var photoElem = $('#browseByIndustry'),
            currentIndex = -1,
            index = -1;

    $('#browseByIndustry-overlay ul a').mouseover(function() {
        index = $(this).parent().index();
        if (index != currentIndex) {
            currentIndex = index;
            photoElem.stop(true, true).fadeOut(100, function() {
                photoElem.css({ 'background-position': ['0 ', -384 * (index + 1), 'px'].join('') }).fadeIn(100);
            });
        }
    })
    $('#browseByIndustry-overlay ul').mouseleave(function() {
        //alert("mouseleave");
        photoElem.stop(true, true).css({ 'background-position': '0 -768px' }).fadeIn(100);
        currentIndex = 1;
    });
}
function _bindGovernment() {
    var photoElem = $('#browseByIndustry'),
            currentIndex = -1,
            index = -1;

    $('#browseByIndustry-overlay ul a').mouseover(function() {
        index = $(this).parent().index();
        if (index != currentIndex) {
            currentIndex = index;
            photoElem.stop(true, true).fadeOut(100, function() {
                photoElem.css({ 'background-position': ['0 ', -384 * (index + 1), 'px'].join('') }).fadeIn(100);
            });
        }
    })
    $('#browseByIndustry-overlay ul').mouseleave(function() {
        //alert("mouseleave");
        photoElem.stop(true, true).css({ 'background-position': '0 -1152px' }).fadeIn(100);
        currentIndex = 2;
    });
}
