// 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 g_modalPosY = 140;

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'
    });window_factory_site
    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',{});
	    var window_header_lt = new Element('div',{});
	    var window_header_rt = new Element('div',{});
    var window_contents = new Element('div',{
        id: 'window_contents'
    });
	    var box_content_bg = new Element('div',{});
	    var window_title = new Element('div',{});
	    var window_close = new Element('div',{
	        id: 'window_close'
	    });
	    var window_content = new Element('div',{
	        id: 'window_content'
	    }); 	    
    var window_footer = new Element('div',{});
	    var window_footer_lt = new Element('div',{});
	    var window_footer_rt = new Element('div',{});

    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();
    		}
    		// begin hack to make sure site modal shows up near top of page
    		var modal = $('siteModal');
    		if ('none' == modal.getStyle('display'))
    		{
    			modal = $('siteModalMini');
    		}
    		var top = modal.getStyle('top');
    		if (top)
    		{
    			top = top.replace('px','');
    		}
    		if (top > g_modalPosY + 300 || top < g_modalPosY - 300)
    		{
    			modal.setStyle({top:g_modalPosY + 'px'});
    		}
    		g_modalPosY = 140;

			centerObjectInScrolledWindow( modal );

    },
    	afterClose: function(){  
    	    $('window_content').update("");
    	}
    },options || {}));

    w.container.insert(window_header);
    	window_header.addClassName('box_header');
    	window_header.insert(window_header_lt);
    		window_header_lt.addClassName('nw');
    	window_header.insert(window_header_rt);
    		window_header_rt.addClassName('nw');
    w.container.insert(window_contents);
    	window_contents.addClassName('box_content');
    	window_contents.insert(box_content_bg);
    		box_content_bg.addClassName('box_content_bg');
    	window_close.insert(closeStr);
    	window_close.insert("<img src=\"/assets/images/global/window_close.gif\"/>");
    	window_contents.insert(window_close);
    		window_close.addClassName('window_close');
    	// window_contents.insert(window_title);
    	window_contents.insert(window_content);
    		window_content.addClassName('window_content');
    w.container.insert(window_footer);
    	window_footer.addClassName('box_footer');
    	window_footer.insert(window_footer_lt);
    		window_footer_lt.addClassName('sw');
    	window_footer.insert(window_footer_rt);
    		window_footer_rt.addClassName('se');
    return w;	
};


var window_factory_site1 = function(container,options,closeStr){

	var window_header = new Element('div',{});
	    var window_header_lt = new Element('div',{});
	    var window_header_rt = new Element('div',{});
    var window_contents = new Element('div',{
        id: 'window_contents'
    });
	    var box_content_bg = new Element('div',{});
	    var window_title = new Element('div',{});
	    var window_close = new Element('div',{
	        id: 'window_close'
	    });
	    var window_content = new Element('div',{
	        id: 'window_content'
	    }); 	    
    var window_footer = new Element('div',{});
	    var window_footer_lt = new Element('div',{});
	    var window_footer_rt = new Element('div',{});

    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();
    		}
    		// begin hack to make sure site modal shows up near top of page
    		var modal = $('siteModal');
    		if ('none' == modal.getStyle('display'))
    		{
    			modal = $('siteModalMini');
    		}
    		var top = modal.getStyle('top');
    		if (top)
    		{
    			top = top.replace('px','');
    		}
    		if (top > g_modalPosY + 300 || top < g_modalPosY - 300)
    		{
    			modal.setStyle({top:g_modalPosY + 'px'});
    		}
    		g_modalPosY = 140;

			centerObjectInScrolledWindow( modal );
    },
    	afterClose: function(){  
    	    $('window_content').update("");
    	}
    },options || {}));

    w.container.insert(window_header);
    	window_header.addClassName('box_header1');
    	window_header.insert(window_header_lt);
    		window_header_lt.addClassName('nw');
    	window_header.insert(window_header_rt);
    		window_header_rt.addClassName('nw');
    w.container.insert(window_contents);
    	window_contents.addClassName('box_content1');
    	window_contents.insert(box_content_bg);
    		box_content_bg.addClassName('box_content1_bg');
    	window_close.insert(closeStr);
    	window_close.insert("<img src=\"/assets/images/global/window_close.gif\"/>");
    	window_contents.insert(window_close);
    		window_close.addClassName('window_close');
    	// window_contents.insert(window_title);
    	window_contents.insert(window_content);
    		window_content.addClassName('window_content');
    w.container.insert(window_footer);
    	window_footer.addClassName('box_footer1');
    	window_footer.insert(window_footer_lt);
    		window_footer_lt.addClassName('sw');
    	window_footer.insert(window_footer_rt);
    		window_footer_rt.addClassName('se');
    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' );
}
var initSiteModal1 = function( ) {
	siteModal1 = window_factory_site($('siteModal1'), {}, 'Close' );
}

function viewModal1( url, options, other ) {
	if (( siteModal1 == null )||( siteModal1 == undefined )){
	    new initSiteModal1();
	}
	siteModal1.href = url;
	siteModal1.open();
}

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, y) {
	if (y)
	{
		g_modalPosY = y;
	}
	viewModal( url, options, other );	
}
var viewSiteModal1 = function(url, options, other, y) {
	if (y)
	{
		g_modalPosY = y;
	}
	viewModal1( url, options, other );	
}
var viewSiteModalMini = function(url, options, other, y) {
	if (y)
	{
		g_modalPosY = y;
	}
	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);
	
}

function centerObjectInScrolledWindow( obj2Center )
{
	try
	{
		h = self.innerHeight;
		w = self.innerWidth;
		if ( h == undefined )	// if IE
		{
			h = document.documentElement.clientHeight;
			w = document.documentElement.clientWidth;
			if ( h < 1 )
				h = document.body.clientHeight;
			if ( w < 1 )
				w = document.body.clientWidth;
		}
		offset_top = Math.floor( h / 2 ) - Math.floor( obj2Center.offsetHeight / 2 );
		offset_left = Math.floor( w / 2 ) - Math.floor( obj2Center.offsetWidth / 2 );
		scroll_top = document.body.scrollTop;
		scroll_left = document.body.scrollLeft;
		if (scroll_top == 0)
		{
		    if (window.pageYOffset)
		    	scroll_top = window.pageYOffset;
		    else
		    	scroll_top = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		if (scroll_left == 0)
		{
		    if (window.pageYOffset)
		    	scroll_left = window.pageXOffset;
		    else
		    	scroll_left = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
		}
		offset_top += scroll_top;
		if (offset_top < 0) {offset_top=25};
		offset_left += scroll_left;
		obj2Center.setStyle({ top: offset_top + 'px', left: offset_left + "px" });
		if ( $('modal_loading') == obj2Center )
		{
			obj2Center.setStyle({  
				zIndex: 9999, 
				position:'absolute', 
				backgroundImage: 'url("/assets/images/filtersizing/bg_loading.gif")',
				backgroundRepeat: 'no-repeat',
				width:'100px',
				height:'100px',
				paddingTop: '16px',
				paddingLeft: '16px'
			});	
			if ( self.innerHeight == undefined )	// if IE
			{
			    if ( isBrowserIE8() )
	        	{
					obj2Center.setStyle({  
						paddingLeft: '18px',
						paddingTop: '18px',
						overflow: 'hidden' 
					});	
	        	}
			    else
		    	{
					obj2Center.setStyle({  
						paddingLeft: '0px',
						paddingTop: '18px',
						overflow: 'hidden' 
					});	
		    	}
			}
		}

	}
	catch( ex )
	{
	}
}

function isBrowserIE()
{
	if ( getBrowserTypeAndVersion().indexOf( "IE" ) == 0 )
		return( true );
	return( false );
}

function isBrowserIE8()
{
	if ( getBrowserTypeAndVersion() == "IE:8" )
		return( true );
	return( false );
}

function isBrowserIE7()
{
	if ( getBrowserTypeAndVersion() == "IE:7" )
		return( true );
	return( false );
}

function isBrowserIE6()
{
	if ( getBrowserTypeAndVersion() == "IE:6" )
		return( true );
	return( false );
}

var	g_sCachedBrowserTypeAndVersion = null;
function getBrowserTypeAndVersion()
{
	if ( g_sCachedBrowserTypeAndVersion != null )
		return( g_sCachedBrowserTypeAndVersion );
	
	var	browser = "";
	var ver = -1; // Return value assumes failure.
	var ua = navigator.userAgent;
	var re = null;
    if (navigator.appName == 'Microsoft Internet Explorer') {
        browser = "IE";
        re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    }
    else if ( navigator.userAgent.indexOf( "Chrome" ) > -1 )
	{
        browser = "Chrome";
        re = new RegExp("Chrome/([0-9]{1,}[\.0-9]{0,})");
	}
    else if ( navigator.userAgent.indexOf( "Firefox" ) > -1 )
	{
        browser = "Firefox";
        re = new RegExp("Firefox/([0-9]{1,}[\.0-9]{0,})");
	}
    else if ( navigator.userAgent.indexOf( "Safari" ) > -1 )
	{
        browser = "Safari";
        re = new RegExp("Safari/([0-9]{1,}[\.0-9]{0,})");
	}
    
    if ( re == null )
    {
    	browser = "Unknown";
    	g_sCachedBrowserTypeAndVersion = browser;
    	return( browser );
    }

    if (re.exec(ua) != null)
    	ver = parseFloat(RegExp.$1);
    if ( ver > -1 )
	{
        ver += ".";
    	browser += ":" + ver.substring( 0, ver.indexOf( "." ) );
	}
    g_sCachedBrowserTypeAndVersion = browser;
	return( browser );
}
