$(document).ready(function () {
    $("#subForm").submit(function () {
        firstName = $("#Fornavn").val();
        lastName = $("#Efternavn").val();
        zipCode = $("#Postnummer").val();
        email = $("#mltily-mltily").val();
        formValid = true;
        checkCount = 0;

        if (firstName.length < 1) { formValid = false; }
        if (lastName.length < 1) { formValid = false; }
        if (zipCode.length < 1) { formValid = false; }
        if (email.length < 1) { formValid = false; }

        $(':checkbox').each(function () {
            if (this.checked) { checkCount++ }
        });

        if (checkCount < 1) { formValid = false; }


        if (!(formValid)) {
            alert("Du skal udfylde alle felter samt minimum 1 nyhedskategori");
            return false;
        }
    });

});

jQuery.fn.simpleLightbox = function (_options) {
    // defaults options
    var _options = jQuery.extend({
        lightboxContentBlock: '.lightbox',
        faderOpacity: 0.3,
        faderBackground: '#000000',
        closeLink: 'a.close-btn, a.cancela',
        href: true,
        onClick: null
    }, _options);

    return this.each(function (i, _this) {
        var _this = jQuery(_this);
        if (!_options.href)
            _this.lightboxContentBlock = _options.lightboxContentBlock;
        else _this.lightboxContentBlock = _this.attr('href');
        if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
            _this.faderOpacity = _options.faderOpacity;
            _this.faderBackground = _options.faderBackground;
            _this.closeLink = _options.closeLink;
            var _fader;
            var _lightbox = $(_this.lightboxContentBlock);
            if (!jQuery('div.lightbox-fader').length)
                _fader = $('body').append('<div class="lightbox-fader"></div>');

            _fader = jQuery('div.lightbox-fader');
            _lightbox.css('zIndex', 999);
            _fader.css({
                opacity: _this.faderOpacity,
                backgroundColor: _this.faderBackground,
                display: 'none',
                position: 'absolute',
                top: 0,
                left: 0,
                zIndex: 998,
                textIndent: -9999
            }).text('$nbsp');

            _this.click(function () {
                if (jQuery.isFunction(_options.onClick)) {
                    _options.onClick.apply(_this);
                }
                /*
                _fader.fadeIn(300, function(){
                _lightbox.fadeIn(400);
                jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
                });*/

                _fader.show();
                _lightbox.show();

                jQuery.fn.simpleLightbox.positionLightbox(_lightbox);

                return false;
            });

            jQuery(_this.closeLink).click(function () {

                _lightbox.hide();
                _fader.hide();
                StopPlayers();
                /*_lightbox.fadeOut(400, function(){ 
                _fader.fadeOut(300);
                });*/

                return false;
            });

            _fader.click(function () {
                _lightbox.hide();
                _fader.hide();
                StopPlayers();
                /*_lightbox.fadeOut(400, function(){
                _fader.fadeOut(300);
                });*/
                return false;
            });

            jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
                $(".text-area", _lbox).css("width", "600px");
                var _height = $(window).height();
                var _width = $(window).width();
                var _minWidth = $('body > div:eq(0)').outerWidth();
                var _thisHeight = _lbox.outerHeight();
                var _page = $('body > div:eq(0)');
                if (_lbox.length) {
                    var imgWidth = $("img", _lbox).outerWidth() + "px";
                    $(".text-area", _lbox).css("width", imgWidth);

                    _fader.css("height", $(document).height());
                    if (_width < _minWidth) { _fader.css('width', _minWidth); } else { _fader.css('width', '100%'); }
                    if (_height > _thisHeight) {
                        if (!window.innerHeight) {
                            _lbox.css({
                                position: 'absolute',
                                top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2) + "px"
                            });
                        } else {
                            _lbox.css({
                                position: 'fixed',
                                top: ((_height - _lbox.outerHeight()) / 2) + "px"
                            });
                        }
                    }
                    else {
                        _lbox.css({
                            position: 'absolute',
                            top: 0
                        });
                    }
                    if (_width > _lbox.outerWidth()) _lbox.css({ left: (_width - _lbox.outerWidth()) / 2 + "px" });
                    else _lbox.css({ position: 'absolute', left: 0 });
                }
            }

            jQuery(window).resize(function () {
                jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
            });

            jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
            $(document).keydown(function (e) {
                if (!e) evt = window.event;
                if (e.keyCode == 27) {

                    _fader.hide();
                    _lightbox.hide();
                    StopPlayers();
                    /*
                    _lightbox.fadeOut(400, function(){
                    _fader.fadeOut(300);
                    });*/
                }
            });
        }
    });
}

$(document).ready(function() {
    $('a.view').simpleLightbox({
        closeLink: 'a.lnk-close'
    });

    $('a.flash').simpleLightbox({
        closeLink: 'a.lnk-close'
    });    
});

function StopPlayers() {
    $("a.player").flowplayer().each(function () {
        if ($.browser.msie)
            this.stop();
    });
    $("a.player_bryg").flowplayer().each(function () {
        if ($.browser.msie)
            this.stop();
    });
}
