Element.addMethods('iframe', {
    document: function(element) {
        element = $(element);
        if (element.contentWindow)
            return element.contentWindow.document;
        else if (element.contentDocument)
            return element.contentDocument;
        else
            return null;
    },
    $: function(element, frameElement) { 
        element = $(element);
        var frameDocument = element.document();
        if (arguments.length > 2) {
            for (var i = 1, frameElements = [], length = arguments.length; i < length; i++)
            frameElements.push(element.$(arguments[i]));
            return frameElements;
        }
        if (Object.isString(frameElement))
            frameElement = frameDocument.getElementById(frameElement);
        return frameElement || element;
    }
});

var SMLightWindow = {
	_ActiveOptions: null,
	_ActiveResponseValue: null,
	
	Init: function() {
		$$('.lightwindow').each(function(link){
			$(link).stopObserving('click'); //bug fix: if re-init, clear prior binding
			link.observe('click',function(event){
				event.stop();
				SMLightWindow.ShowURL(link.readAttribute('href'), link.readAttribute('params'));
			});
		});
	},
	Expand: function() {
	    if ( Prototype.Browser.MobileSafari || isIE ) return;
	    
	    var height = $('SMLightWindowFrame').document().body.getHeight();
	    var min_height = $('SMLightWindowContainer').options.lightwindow_height;
	    var max_height = document.viewport.getHeight() - 100;
	    
	    if ( height < min_height ) height = min_height;
	    if ( height > max_height ) height = max_height;
	    
	    $('SMLightWindowFrame').setStyle({ height:height + 'px' });
	    var el = $('SMLightWindowContainer');
	    el.setStyle({
		    top:( (document.viewport.getHeight()*0.5) - (el.getHeight()*0.5) ) + 'px',
		    left:( (document.viewport.getWidth()*0.5) - (el.getWidth()*0.5) ) + 'px',
		    visibility:'visible'
	    });
	    $('SMLightWindowFrame').document().body.scrollTo(0,0);
	},
	Validate: function(){
	    if ( $('SMLightWindowContainer') ) {
		if ( SMLightWindow.Zoomed ) {
		    
		    var el = $('SMLightWindowFrame');
		    el.setStyle({
			width:document.viewport.getWidth() + 'px',
			height:document.viewport.getWidth() + 'px'
		    });
		    
		} else {
		    
		    var height = $('SMLightWindowContainer').getHeight();
		    var width = $('SMLightWindowContainer').getWidth();
		    var content_height = $('SMLightWindowFrame').document().body.getHeight();
		    var max_width = document.viewport.getWidth() - 100;
		    var max_height = document.viewport.getHeight() - 100;
		    
		    if ( content_height > height ) height = content_height;
		    if ( width > max_width ) width = max_width;
		    if ( height > max_height ) height = max_height;
		    
		    $('SMLightWindowFrame').setStyle({ width:width + 'px', height:height + 'px' });
		    var el = $('SMLightWindowContainer');
		    el.setStyle({
			top:( (document.viewport.getHeight()*0.5) - (el.getHeight()*0.5) ) + 'px',
			left:( (document.viewport.getWidth()*0.5) - (el.getWidth()*0.5) ) + 'px',
			visibility:'visible'
		    });
		    
		}
		$('SMLightWindowFrame').document().body.scrollTo(0,0);
	    }
	},
	ShowURL: function(url, params)
	{
		var options = { href: url };
		var parts = params.split(',');
		for( var i=0; i<parts.length; ++i ) {
			var param = parts[i].split('=');
			options[ param[0] ] = param[1];
		}
		
		SMLightWindow.Show(options);
	},
	Show: function(options)
	{
	    SMLightWindow.Hide();
	    SMLightWindow.Zoomed = false;
	    SMLightWindow._ActiveOptions = options;
	    
	    if ( options.href.indexOf('LightWindowMaster') < 0 ) {
		    if ( options.href.indexOf('?') > 0 ) {
			    options.href += '&LightWindowMaster=true';
		    } else {
			    options.href += '?LightWindowMaster=true';
		    }
	    }
	    if ( options.width ) options.lightwindow_width = options.width;
	    if ( options.height ) options.lightwindow_height = options.height;
	    
	    var max_width = document.viewport.getWidth() - 100;
	    var max_height = document.viewport.getHeight() - 100;
	    
	    if ( !options.lightwindow_width ) options.lightwindow_width = max_width;
	    if ( !options.lightwindow_height ) options.lightwindow_height = max_height;
	    if ( options.lightwindow_width > max_width ) options.lightwindow_width = max_width;
	    if ( options.lightwindow_height > max_height ) options.lightwindow_height = max_height;
	    
	    if ( !options.title ) options.title = "";
	    var content = new Template(
		( Prototype.Browser.MobileSafari ? '' : '<div id="SMLightWindowCapture">&nbsp;</div>' ) + 
		'<div id="SMLightWindowContainer"><div>' +
		'<h1>#{title}<img id="SMLightWindowZoom" src="/media/ImageElements/Icons/zoom.png"/><img id="SMLightWindowClose" src="/media/ImageElements/Icons/close.png"/></h1>' +
		'<iframe id="SMLightWindowFrame" src="#{href}" width="#{lightwindow_width}" height="#{lightwindow_height}" frameborder="0" onload="SMLightWindow.Expand();"></iframe>' +
		( Prototype.Browser.MobileSafari ? '</div>' : '</div></div>' )
	    );
	    $(document.body).insert( content.evaluate( options ) );
	    $('SMLightWindowClose').observe('click',function(){
		    SMLightWindow.Hide();
	    });
	    $('SMLightWindowZoom').observe('click',function(){
		    SMLightWindow.Zoom();
	    });
	    if ( !Prototype.Browser.MobileSafari ) $('SMLightWindowCapture').observe('click',function(){ SMLightWindow.Hide(); });
	    var el = $('SMLightWindowContainer');
	    el.setStyle({
		    top:( (document.viewport.getHeight()*0.5) - (el.getHeight()*0.5) ) + 'px',
		    left:( (document.viewport.getWidth()*0.5) - (el.getWidth()*0.5) ) + 'px',
		    visibility:'visible'
	    });
            el.options = options;
	},
	Resize: function( w, h ) {
	    if ( Prototype.Browser.MobileSafari ) return;
	    var el = $('SMLightWindowFrame');
	    el.setStyle({
		    width:w + 'px',
		    height:h + 'px'
	    });
	    el = $('SMLightWindowContainer');
	    el.setStyle({
		    left:( (document.viewport.getWidth()*0.5) - (el.getWidth()*0.5) ) + 'px',
		    visibility:'visible'
	    });
	},
	Hide: function() {

	    $$('body')[0].setStyle({
		height:'',
		overflow:''
	    });

	    if ( $('SMLightWindowCapture') ) $('SMLightWindowCapture').remove();
	    if ( $('SMLightWindowContainer') ) $('SMLightWindowContainer').remove();
	    
	    if (SMLightWindow._ActiveOptions != null &&
		    SMLightWindow._ActiveOptions.lightwindow_callback == "true")
	    {
		    SMLayoutManager.FireAjaxEventType(
			    SMLightWindow._ActiveOptions.lightwindow_callback_controlid,
			    SMLightWindow._ActiveOptions.lightwindow_callback_controltype,
			    'OnLightWindowClose',
			    { ResponseValue: SMLightWindow._ActiveResponseValue } );
			    
		    SMLightWindow._ActiveResponseValue = null;
	    }
	    
	    SMLightWindow._ActiveOptions = null;
	},
	Zoom: function() {
            if ( Prototype.Browser.MobileSafari ) return;
            var el = $('SMLightWindowFrame');
            
            if ( SMLightWindow.Zoomed ) {
                SMLightWindow.Zoomed = false;
                
                $$('body')[0].setStyle({
                    height:null,
                    overflow:null
                });
                
                el.setStyle({
                    width:SMLightWindow.ZoomRestoreWidth + 'px',
                    height:SMLightWindow.ZoomRestoreHeight + 'px'
                });
                
                el = $('SMLightWindowContainer');
		el.setStyle({
                    top:( (document.viewport.getHeight()*0.5) - (el.getHeight()*0.5) ) + 'px',
                    left:( (document.viewport.getWidth()*0.5) - (el.getWidth()*0.5) ) + 'px',
                    visibility:'visible'
		});
                
            } else {
                SMLightWindow.Zoomed = true;
                SMLightWindow.ZoomRestoreWidth = el.getWidth();
                SMLightWindow.ZoomRestoreHeight = el.getHeight();
                
                $$('body')[0].setStyle({
                    height:'100%',
                    overflow:'hidden'
                });
		
                el.setStyle({
                    width:document.viewport.getWidth() + 'px',
                    height:(document.viewport.getHeight()-$$('#SMLightWindowContainer h1')[0].getHeight()) + 'px'
                });
                
                el = $('SMLightWindowContainer');
                el.setStyle({
                    top:'-10px',
                    left:'-10px',
                    visibility:'visible'
                });
                
            }
	},
	AjaxCallBack: function(evt)
	{
		SMLightWindow.ResetHack();
	},
	ResetHack: function()
	{
		SMLightWindow.Init();
	}
}

document.observe( 'dom:loaded', SMLightWindow.Init );
if ( !Prototype.Browser.MobileSafari ) Event.observe( window, 'resize', SMLightWindow.Validate );
document.observe( 'SMAjax:CallBack', SMLightWindow.Init );
