/** Initialize the jQuery modal box
 *
 */
jqmodal_no_overflow = false;
jqmodal_top_space = 110;
jqmodal_bottom_space = 20;
jqmodal_out_space = jqmodal_top_space + jqmodal_bottom_space; //how much blank space @ top and bottom total 110+20
jqmodal_in_space = 61; //space inside the modal box - including title bar and margins. 27+12+12

J(function(){
    /* prepend the box to the window if it isn't there already */
    if( !J('#JQ_window').length ) {
        J('body').prepend('\n\
<div id="JQ_window" class="jqmWindow">\n\
    <div id="jqmodal_header">\n\
        <div id="jqmodal_title">&nbsp;</div>\n\
        <a href="#" id="jqmodal_close" class="jqmodal_hide">&nbsp;</a>\n\
    </div>\n\
    <div id="JQ_content">Loading...</div>\n\
</div>'
    );
    }
    
    
    /* Modal boxes - add the JQModal box*/
    J('#JQ_window').jqm({
        ajax:'@href',
        modal:true,
        trigger:'.jqmodal',
        target:'div#JQ_content',
        onShow : function(hash){
            JQpopup_resize(0,600);
            hash.w.css('left', '50%');
            hash.w.addClass('loading');
            //grab a href from the link that opened the dialog box
            var href = (hash.t)
                ? hash.t.getAttribute('href')
                : '';
            jqmodal_popupSizeFromURL(href, hash.w.attr('id') )
            hash.w.show();
            J('#jqmodal_title').html('&nbsp;');
        },
        onLoad:function(hash){
            //Real stuff
            hash.w.removeClass('loading');
            hash.w.jqmAddClose('.jqmodal_hide');
            J('#jqmodal_title').html( hash.t.getAttribute('title') );
            //Allow the overflow unless disallowed
            if(!jqmodal_no_overflow){
                JQpopup_resize();
            }
        },
        onHide:function(hash){
            // empty title
            J('#jqmodal_title').html('&nbsp;');
            // following codes must be here in order to close the box.
            hash.o.remove(); //removes background image
            hash.w.hide(); //hides modal window
            //Remove the video player if it is in the window.
            J('#JQ_content #blviewer-window').remove();
        }
    });
   
    
    //J('.jqmodal').click(function(){
    //	return false;
    //});
});


/* RESIZE FUNCTIONS FOR NEW MODAL BOX
 * Same as before, just note new IDs
 */
function JQpopup_set_overflow(no_height){
    try {
     if (no_height == undefined ){
	     var temp_height = J(window).height();
	     J('#JQ_content').height(temp_height-50);
	     var content_height = J('#JQ_content').height();
	     J('#JQ_window').height(content_height+80);
     }
     J('#JQ_content').css('overflow','auto');
     } catch (error) {
        bl_debug(error, 'JQpopup_set_overflow')
    }
}

/**
this function is used to resizing the popup dynamically as
elements change dynamically within it
*/
function JQpopup_resize(extra, width){
    if (extra == undefined){
        extra = 0;
    }

    if (width == undefined){
        //do nothing
    }  else if (width > 0){
        J('#JQ_window').width(width);
    }

    J('#JQ_content').css('height', 'auto');
    J('#JQ_window').css('height', 'auto')

    var height_win = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;

    var height_content = J('#JQ_content').height();
    var height_window = J('#JQ_window').height();

    if (height_window > height_win-jqmodal_out_space) {
        J('#JQ_window').height(height_win-jqmodal_out_space);
        J('#JQ_content').height(J('#JQ_window').height()-jqmodal_in_space);
    }

    J('#JQ_content').css('overflow','auto');
}

function jqmodal_popupSizeFromURL(url, id) {
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    selector = '#'+id;
    
//VARIABLES FROM URL
    if(vars['height']){
        J(selector).css('height',vars['height']+'px');
    }
    if(vars['width']){
        J(selector).css('width',vars['width']+'px');
        J(selector).css('margin-left',  '-'+( vars['width']/2)+'px' )
    }        
    //Don't put scroll bars on the right.
    if(vars['no_overflow']) {
        jqmodal_no_overflow = true;
    }

    //Adjustable top space
    if(vars['top_space']) {
        jqmodal_top_space = parseInt( vars['top_space'] ); //this comes as a string
    } else {
        jqmodal_top_space = 110;
    }
    J(selector).css('top', jqmodal_top_space + 'px');

    //if we want to absolutely position the popup - so user can scroll
    if(vars['absolute']) { 
        J(selector).css('position', 'absolute');
        J(selector).css('top', J(window).scrollTop() + jqmodal_top_space + 'px' );
    } else {
        J(selector).css('position', 'fixed');
    }
}
