var dialogTemplate = new Template('<div class="dialogClose">#{closeI18n} x</div><span class="dialogContent">#{content}</span>');

function dialogImage(src, alt)
{	
	//preload
	var image = new Image();
	image.src = src;
	image.alt = alt;

    /*img = new Element('img', {src: src});*/
    toggleDialog('<img src="'+src+'" alt="'+alt+'" />');
}

function dialogSWF(src, width, height)
{
	
	var swfString = '<object id="dialogFlashContainer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+width+'" height="'+height+'">';
		swfString += '<param name="movie" value="'+src+'" />';

		swfString += '<param name="wmode" value="transparent" />';
		swfString += '<param value="noScale" name="scale"/>';
		swfString += '<param value="TL" name="salign"/>';

		swfString += '<param name="flashvars" value="" />'
		swfString += '<!--[if !IE]>-->';
		swfString += '<object type="application/x-shockwave-flash" data="'+src+'" width="'+width+'" height="'+height+'">'
		swfString += '<param name="flashvars" value="" />'
		swfString += '<param name="wmode" value="transparent" />';
		swfString += '<param value="noScale" name="scale"/>';
		swfString += '<param value="TL" name="salign"/>';
		swfString += '<!--<![endif]-->';
    
		swfString += '<!--[if !IE]>-->';
		swfString += '</object>';
		swfString += '<!--<![endif]-->';
		swfString += '</object>';
	
   	
   toggleDialog(swfString);
  // swfobject.switchOffAutoHideShow();
  // swfobject.registerObject("dialogFlashContainer", "11", "/js/swfobject/expressInstall.swf");
	
}

function toggleDialog(content, keepAlive, namespace)
{
    namespace = namespace ? namespace : 'default';
    contentContainerId = 'dialog'+namespace+'container';
    backgroundContainerId = 'dialog'+namespace+'background';
    if(!$(backgroundContainerId) || keepAlive){
        if(!$(backgroundContainerId)){
            bg = new Element('div');
            bg.id = backgroundContainerId;
            bg.className = 'dialogBackground';
            
            if(isIE6()) {
                bg.style.height = $(document.body).getHeight()+'px';
                bg.style.position = 'absolute';
                bg.style.width = '100%';
            }
            
            bg.observe('click', function(){toggleDialog('', false, namespace)});
            $(document.body).appendChild(bg);
        }
        if(!$(contentContainerId)){
            contentContainer = new Element('div');
            contentContainer.id=contentContainerId;
            contentContainer.className='dialogContainer';
            
            if(true || isIE6()) {
                contentContainer.style.position='absolute';
            } else {
                //disabled
                contentContainer.style.position='fixed';
            }
            $(document.body).appendChild(contentContainer);
        }
        if(content){
            contentContainer.innerHTML = dialogTemplate.evaluate({content: content, closeI18n: closeI18n});
            $$('#'+contentContainerId+' .dialogClose')[0].observe('click', function(){toggleDialog('', false, namespace)});
        }
        
        //Solange Bild noch nicht geladen ist...
		
		if ($$('.dialogContent').first().childElements().first().tagName == "IMG") {
		
			$$('.dialogContent').first().childElements().first().observe('load', function(){
				centerElement(contentContainerId);
			});
		}else
		{
		//für Flash Elemente nimm Höhe und Breite	
				centerElement(	contentContainerId,
								$$('.dialogContent').first().childElements().first().width,
								$$('.dialogContent').first().childElements().first().height
							 );
		
			
			if(!swfobject.hasFlashPlayerVersion("8.0.0"))
			{
				var att = { data:"/js/swfobject/expressInstall.swf",
						    width: $$('.dialogContent').first().childElements().first().width,
							height: $$('.dialogContent').first().childElements().first().height
						   };
                var par = {};
                var id = "dialogFlashContainer";
                swfobject.showExpressInstall(att, par, id);
			
			}
		
		
		
		}
		
		
	
        
    } else {
        $(contentContainerId).remove();
        $(backgroundContainerId).remove();
    }
}

function centerElement(elementId,width,height)
{
	
	if (width) {
		$(elementId).style.width = width;
		$(elementId).style.height = height;
		dimensions = $(elementId).getDimensions();
		dimensions.height = height;
	}
	else {
		contDimensions = $$('#' + elementId + ' .dialogContent')[0].getDimensions();
		$(elementId).style.width = contDimensions.width + 'px';
		dimensions = $(elementId).getDimensions();
	}
    
    
	//console.dir( dimensions);
	
    if($(elementId).style.position == 'absolute')
    {
        $(elementId).style.top = parseInt(document.viewport.getScrollOffsets().top + ((getWindowHeight() - dimensions.height) / 2))+'px';
    } 
    else if($(elementId).style.position == 'fixed')
    {
        $(elementId).style.top = parseInt((getWindowHeight() - dimensions.height) / 2)+'px';
    }
    
    $(elementId).style.left = parseInt((getWindowWidth() - dimensions.width) / 2)+'px';
    
}

function getWindowHeight()
{
    if(window.innerHeight){
        windowHeight = window.innerHeight;
    } else {
        windowHeight = document.documentElement.clientHeight;
    }
    return windowHeight;
}

function getWindowWidth()
{
    if(window.innerWidth){
        windowWidth = window.innerWidth;
    } else {
        windowWidth = document.documentElement.clientWidth;
    }
    return windowWidth;
}

function isIE6()
{
    return parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
}
