window.getScrollTop = function() {
    if(typeof pageYOffset != 'undefined'){
        //most browsers
        return pageYOffset;
    }
    else{
        var B = document.body; //IE 'quirks'
        var D = document.documentElement; //IE with doctype
        D = (D.clientHeight) ? D : B;
        return D.scrollTop;
    }
};

window.getViewportHeight = function() {
     var viewportheight;

     // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

     if (typeof window.innerWidth != 'undefined')
     {
          viewportheight = window.innerHeight
     }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

     else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientHeight !=
         'undefined' && document.documentElement.clientHeight != 0)
     {
           viewportheight = document.documentElement.clientHeight
     }

     // older versions of IE

     else
     {
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
     }
     
     return viewportheight;
};

window.getDocHeight = function() {
    var D = document,
        el = D.body || D.documentElement;
    
    return Math.max(
        el.scrollHeight,
        el.offsetHeight,
        el.clientHeight
    );
};

(function($, undefined) {
    jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", (getViewportHeight()/2 + getScrollTop() - $.jwbox.lightbox.height()/2) + "px");
        this.css("left", ( $(document).width() - this.outerWidth() ) / 2 + "px");
        return this;
    }
    
    jQuery.jwbox = {
        playerOptions: {
            flashplayer: '/international/wp-content/themes/hdeng/js/jwbox/jwplayer/player.swf',
            height: 398,
            width: 530,
            autostart: true,
            netstreambasepath: 'http://www.heidelbergengineering.com/',
            controlbar: {
                position: 'over'
            },
            events: {
                onReady: function() {
                    $.jwbox.center();
                }
            }
        },
        lightbox: null,
        player	: null,
        toggle	: function(context) {

            if (!$.jwbox.lightbox) {
            
                $.jwbox.lightbox = $('#jwbox_hidden');
                $.jwbox.center();
                
                $('#jwbox_background').height(getDocHeight()).fadeIn('fast');
                $.jwbox.lightbox.css('display','block');
                $.jwbox.center();
                $('#jwbox_background').fadeTo(0,0.8);
                
                if (!$.jwbox.player) {
                    $('#jwbox_hidden .jwbox_content').prepend('<div id="jwplayer"></div>');
                    $('#jwplayer').height($.jwbox.playerOptions.height).width($.jwbox.playerOptions.width);
                    $.jwbox.center();
                    
                    $.jwbox.playerOptions.file = $(context).attr('href');
                    $.jwbox.player = jwplayer('jwplayer').setup($.jwbox.playerOptions);
                }
            
            }
            else if (context.className == 'jwbox_content' || context.id == 'jwplayer') {}
            else {
                try {
                    $.jwbox.player.stop();
                    $.jwbox.player = null;
                } catch(error) {}
                $('#jwbox_hidden .jwbox_content').empty();
                $.jwbox.lightbox.css("display","none");
                $.jwbox.lightbox = null;
                $("#jwbox_background").fadeOut("fast");
            
            }
            
        },
        center	: function() {
            if ($.jwbox.lightbox) {
                $.jwbox.lightbox.center();
            }
        }
    }
    
    $(document).keyup(function(event){
        if (event.keyCode == 27 && $.jwbox.lightbox) {
            $.jwbox.toggle($("#jwbox_background"));
        }
    });
    
    $(document).ready(function () {
    
        $('embed, object').each(function(){
            var wrapper = $('<div></div>');
            wrapper .height($(this).height())
                    .width($(this).width())
                    .css('position','relative');
                    
            $(this) .css('position','absolute')
                    .css('z-index','1')
                    .attr('wmode','opaque')
                    .wrap(wrapper);
        });
    
        $("body").append('<div id="jwbox_background">&nbsp;</div><div id="jwbox_hidden"><div class="jwbox_content"></div><a href="#" class="close">Close</a></div>');
        
        $(".openJwBox").click(function () {$.jwbox.toggle(this); return false;});
        
        $("#jwbox_background").click(function () {$.jwbox.toggle(this); return false;});
        
        $("#jwbox_hidden .close").click(function () {$.jwbox.toggle(this); return false;});
        
        $(window).resize(function() {$.jwbox.center();});
        
    });
})(jQuery);
