/* Copyright (c) 2011 Lexmark International, Inc. */
$(document).ready(function() {
    // Closure scope the currently active lightbox.
    var cb = null;
    
    function adjustElements() {
	var wh = $(window).height();
	var ww = $(window).width();
	$('#boxer-mask').css({'width': ww, 'height': wh});
	if (cb) {
	    var ox = (ww - cb.width()) / 2;
	    var oy = (wh - cb.height()) / 2;
	    oy = Math.max(oy, 17);
	    oy += $(window).scrollTop();
	    cb.css({'top': oy,'left': ox});
	}
    }
    
    function close() {
	$('#boxer-mask').hide();
	$('.boxer-modal').fadeOut(500).find('iframe').attr('src', '');
    }
    
    $('.box-iframe, .box-img').each(function(index) {
        var dlg_id = 'boxer-'+index;
        var scrolling = $(this).attr("box:scrolling");
        if (typeof(scrolling)!='undefined' && scrolling.length > 0) {
            scrolling = 'scrolling="' + scrolling + '" ';
        }
        var height = $(this).attr('box:height');
        if (typeof(height)== 'undefined' || isNaN(height+0)) {
            height = 670;
        }
        var width = $(this).attr('box:width');
        if (typeof(width)== 'undefined' || isNaN(width+0)) {
            width = 800;
        }
	var modal_box = '<div id="' + dlg_id + '" class="boxer-modal"' +
	    '" style="display:none;position:absolute;background:white;z-index:1000;'
	    + 'min-width:'+width+ 'px; min-height:'+height +'px;" >';
	    
        if ($(this).hasClass('box-img')) {
	    modal_box += '<img alt="" src="' + $(this).attr('href') + '"/>';
        } else {
            modal_box += '<iframe frameborder="0" style="width:'+width+'px; height:'+height+'px" '+ scrolling +'></iframe>';
        }
	modal_box += '</div>';
	var box = $(modal_box).appendTo('body');
	$('<div id="closeIt" style="position:absolute; width:37px; height:37px; right:-14px; margin-top:-18px; z-index:1001; background:transparent url(/common/images/misc/close-button.png) center center no-repeat;"></div>')
	    .prependTo(box).click(close);
        $(this).click(function(event){
            event.preventDefault();
	    $('#boxer-mask').show();
            cb = $('#'+dlg_id);	    
            cb.find('iframe').attr('src', $(this).attr('href'));
            cb.fadeIn(150);
	    adjustElements();
        })
    });
    $('<div id="boxer-mask" style="display:none; position:fixed; top:0; left:0; z-index:999; background:transparent url(/common/images/misc/transparency.png) 0 0 repeat;"></div>')
	.appendTo('body')
	.click(close);
    $(window).resize(adjustElements);
    adjustElements();
});
