// Plain version.
// container: reference to the div being used as the popup window container.  e.g. ${'my_window'}
// options: options for the Control.Window class being used for popup window (livepipe)
// closeStr: the string to display next to the close icon
// onOpen: function to execute after the window has opened

var debug = true;
var debugAlerts = function( msg ) {
	if(debug){
		alert(msg);
	}
}

var window_factory = function(container,options,closeStr,onOpen){

    var window_header = new Element('div',{
        className: 'window_header'
    });
    var window_title = new Element('div',{
        className: 'window_title'
    });
    var window_close = new Element('div',{
        className: 'window_close'
    });
    var window_rule = new Element('div',{
        className: 'hr_rule_green'
    });      
    var window_contents = new Element('div',{
        className: 'window_contents'
    });
    if (!closeStr)
    {
    	closeStr = "Close";
    }
    var w = new Control.Window(container,Object.extend({
        className: 'popup_window',
        closeOnClick: window_close,
        draggable: window_header,
        baseZIndex: 999999,
        insertRemoteContentAt: window_contents,
        afterOpen: onOpen
    },options || {}));
    w.container.insert(window_header);
    window_header.insert(window_title);
    window_close.insert(closeStr);
    window_close.insert("<img src=\"/assets/images/pathways/window_close.gif\"/>");
    window_header.insert(window_rule);
    window_header.insert(window_close);
    w.container.insert(window_contents);
    return w;
};


var window_factory_simple = function(container,options, onOpen){
    var window_contents = new Element('div',{
        className: 'window_contents'
    });
    var w = new Control.Window(container,Object.extend({
        className: 'modal_window',
        draggable: false,
        closeBtn: false,
        baseZIndex: 999999,
        insertRemoteContentAt: window_contents,
        afterOpen: onOpen
    },options || {}));
    w.container.insert(window_contents);
    return w;
};


var window_factory_box = function(container,options,closeStr,onOpen){

    var window_header = new Element('div',{
        className: 'box_header'
    });
	    var window_header_lt = new Element('div',{
	        className: 'nw'
	    });
	    var window_header_rt = new Element('div',{
	        className: 'ne'
	    });
    var window_contents = new Element('div',{
        id: 'window_contents',
        className: 'box_content'
    });
	    var box_content_bg = new Element('div',{
	    	className: 'box_content_bg'
	    });
	    var window_title = new Element('div',{
	        className: 'window_title'
	    });
	    var window_close = new Element('div',{
	        id: 'window_close',
	        className: 'window_close'
	    });         
    var window_footer = new Element('div',{
        className: 'box_footer'
    });
	    var window_footer_lt = new Element('div',{
	        className: 'sw'
	    });
	    var window_footer_rt = new Element('div',{
	        className: 'se'
	    });
    if (!closeStr) {
    	closeStr = "Close";
    }
    var w = new Control.Window(container,Object.extend({
        className: 'popup_window boxx type_1',
        closeOnClick: window_close,
        draggable: window_title,
        baseZIndex: 999999,
        insertRemoteContentAt: window_contents,
        afterOpen: onOpen
    },options || {}));
    w.container.insert(window_header);
    	window_header.insert(window_header_lt);
    	window_header.insert(window_header_rt);
    w.container.insert(window_contents);
    	// window_contents.insert(box_content_bg);
    	window_close.insert(closeStr);
    	window_close.insert("<img src=\"/assets/images/pathways/window_close.gif\"/>");
    	window_contents.insert(window_close);
    	window_contents.insert(window_title);
    w.container.insert(window_footer);
    	window_footer.insert(window_footer_lt);
    	window_footer.insert(window_footer_rt);
    return w;
};


var window_factory_site = function(container,options,closeStr){

	var window_header = new Element('div',{
        className: 'box_header'
    });
	    var window_header_lt = new Element('div',{
	        className: 'nw'
	    });
	    var window_header_rt = new Element('div',{
	        className: 'ne'
	    });
    var window_contents = new Element('div',{
        id: 'window_contents',
        className: 'box_content'
    });
	    var box_content_bg = new Element('div',{
	    	className: 'box_content_bg'
	    });
	    var window_title = new Element('div',{
	        className: 'window_title'
	    });
	    var window_close = new Element('div',{
	        id: 'window_close',
	        className: 'window_close'
	    });
	    var window_content = new Element('div',{
	        id: 'window_content',
	        className: 'window_content'
	    }); 	    
    var window_footer = new Element('div',{
        className: 'box_footer'
    });
	    var window_footer_lt = new Element('div',{
	        className: 'sw'
	    });
	    var window_footer_rt = new Element('div',{
	        className: 'se'
	    });

    if (!closeStr) { closeStr = "Close"; }
    
    var w = new Control.Modal(container,Object.extend({
    	indicator: 'modal_loading',
    	position: 'center',
        className: 'boxx w_type_1',
        closeBtn: true,
        closeOnClick: window_close,
        draggable: window_header,
        baseZIndex: 999999,
        fade: true,
        iframeshim: false,
        ieHideSelectBox: true,
        insertRemoteContentAt: window_content,
        afterOpen: function(){
    		siteModal.remoteContentLoaded=false;
    		if (window.initModalSub) {
    			initModalSub();
    		}
       	},
    	afterClose: function(){  
    	    $('window_content').update("");
    	}
    },options || {}));

    w.container.insert(window_header);
    	window_header.insert(window_header_lt);
    	window_header.insert(window_header_rt);
    w.container.insert(window_contents);
    	window_contents.insert(box_content_bg);
    	window_close.insert(closeStr);
    	window_close.insert("<img src=\"/assets/images/global/window_close.gif\"/>");
    	window_contents.insert(window_close);
    	// window_contents.insert(window_title);
    	window_contents.insert(window_content);
    w.container.insert(window_footer);
    	window_footer.insert(window_footer_lt);
    	window_footer.insert(window_footer_rt);
    return w;	
};


var initSiteModal = function( ) {
	siteModal = window_factory_site($('siteModal'), {}, 'Close' );
}
var initSiteModalMini = function( ) {
	siteModalMini = window_factory_site($('siteModalMini'), {'className':'boxx w_type_1c'}, 'Close' );
}


function viewModal( url, options, other ) {
	if (( siteModal == null )||( siteModal == undefined )){
	    new initSiteModal();
	}
	siteModal.href = url;
	siteModal.open();
}
function viewModalMini( url, options, other ) {
	if (( siteModalMini == null )||( siteModalMini == undefined )){
	    new initSiteModalMini();
	}
	siteModalMini.href = url;
	siteModalMini.open();
}


var viewSiteModal = function(url, options, other) {
	viewModal( url, options, other );	
}
var viewSiteModalMini = function(url, options, other) {
	viewModalMini( url, options, other );	
}


var closeSiteModal = function() {
	siteModal.close();
}
var closeSiteModalMini = function() {
	siteModalMini.close();
}


var viewSiteModalVideo = function(movie) {
	windowTitle = '';
	viewModal( windowTitle, url, 'video', '' ) ;
	centerModal('siteModal','680');
}

function reloadModalForm(theForm) {
    new Ajax.Updater("window_content", theForm.action, {
            method : 'post',
            parameters: Form.serialize( theForm.id ),
            evalScripts : true
    });
}

function reloadModalUrl(url) {
    new Ajax.Updater("window_content", url, {
            method : 'get',
            evalScripts : true
    });
}

var positionModal = function(){
	debugAlerts("TEST : centerModal"); 
	divId = 'siteModal';
	nModalWidth = '700'
		
	try {
		var	divModal = $(divId);
		var	leftOffset = Math.floor( document.viewport.getWidth() / 2 ) - Math.floor( nModalWidth / 2 );
		var scrollOffset = Math.floor( document.body.clientHeight / 2 ) - Math.floor( divModal.offsetHeight / 2 );
		var topOffset = scrollOffset + divModal.style.top;
		
		divModal.style.position="absolute";
		divModal.style.width= nModalWidth;
		divModal.style.left = leftOffset;
		divModal.style.top = topOffset;
		
	}
		catch ( ex ) {}
		
	/**/
	debugAlerts(" divModal : " + divModal.id + 
	"\n divModal.style.width : " + divModal.style.width + 
	"\n divModal.style.position : " + divModal.style.position + 
	"\n divModal.style.left : " + divModal.style.left + 
	"\n divModal.style.top : " + divModal.style.top);
	
}
