﻿var enumDialogSize = { small: 0, medium:1, large:2, templatechooser:3 };

var modalBeforeLoginOptions;
var modalDialog;
var refreshOnClose;
var dialogOpen;
var onFileUploadComplete;
    
function getDialogBox(){ return document.getElementById('dialogBox'); }
function getDialogBoxContentOuter(){ return document.getElementById('dialogBoxContentOuter'); }
function getDialogBoxContent(){ return document.getElementById('dialogBoxContent'); }
function getDialogBoxTitleBar(){ return document.getElementById('dialogBoxTitleBar'); }
function getDialogBoxTitle(){ return document.getElementById('dialogBoxTitle'); }
function getDialogBoxClose(){ return document.getElementById('dialogBoxClose'); }
function getLoader(){ return document.getElementById('loader'); }
function getToolTip(){ return document.getElementById('toolTip'); }
function getToolTipContent(){ return document.getElementById('toolTipContent'); }
function getChannelNav(){ return document.getElementById('channelsNav'); }

function getBGOverlay(){ return document.getElementById('bgoverlay'); }
function getBGOverlayLight(){ return document.getElementById('bgoverlaylight'); }
function getBGOverlayTransparent(){ return document.getElementById('bgoverlaytransparent'); }

function setDialogTitle(title)
{
    var dialogTitle = getDialogBoxTitle();
    dialogTitle.innerHTML = title; 
}

function setDialogSize(width, height)
{
    var dialogBox = getDialogBox();
 
    dialogBox.style.width = width + 'px';
    dialogBox.style.height = height + 'px';   
    
    var dialogBoxContentOuter = getDialogBoxContentOuter();
    dialogBoxContentOuter.style.width = width + 'px';
    dialogBoxContentOuter.style.height = height + 'px';      
   
    var dialogBoxContent = getDialogBoxContent();
    dialogBoxContent.style.width = width - 40 + 'px';
    dialogBoxContent.style.height = height - 180 + 'px';      
}

function hideDialog()
{
    if(refreshOnClose)
    {
        reloadPage();
        return;
    }
    
    var dialog = getDialogBox();
    dialog.style.display = 'none';
    showFlashContent();
    enableScreen();
    dialogOpen = false;
    onLoginInPlace = null;
    
    if(typeof onHideDialog == 'function')
    {
        onHideDialog();
    }
}

function setDialogBackground(bgImage)
{
    // Set the appropriate background image
    var dialogBox = getDialogBox();

    dialogBox.style.backgroundImage = "url('" + siteRoot + '/content/core/images/' + bgImage + "')";
}

function clearDialogBackground()
{
   var dialogBox = getDialogBox();
   dialogBox.style.backgroundImage = "";
}

function showDialogWithForm(url, form, fileUploadFunction)
{
    clearDialogBackground(); // Since we might already have a background image that's the wrong size
    showDialog(url, null, null, null, form, fileUploadFunction); 
}

function showDialogStandard(url, title, size, form, fileUploadFunction)
{
    switch(size)
    {
        case enumDialogSize.small:
            setDialogBackground('ve-dialog-bg-small.png');
            showDialog(url, title, 513, 385, form, fileUploadFunction);
            
        break;
        case enumDialogSize.medium:
            setDialogBackground('ve-dialog-bg-me.png');
            showDialog(url, title, 580, 280, form, fileUploadFunction);
            
        break;
        case enumDialogSize.large:
            setDialogBackground('ve-dialog-bg-large.png');
            showDialog(url, title, 712, 662, form, fileUploadFunction);
            
        break;
        case enumDialogSize.templatechooser:
            setDialogBackground('ve-dialog-centre.png');
            showDialog(url, title, 593, 543, form, fileUploadFunction);

        break;
        default:
            alert('Developer Warning:\n\n Unknown standard dialog size....please review enumDialogSize');
        break;
    }
}

function showDialog(url, title, width, height, form, fileUploadFunction)
{
    if(fileUploadFunction) 
    {
        onFileUploadComplete = fileUploadFunction;
    }

    dialogOpen = true;
    hideFlashContent();

    var dialogContent = getDialogBoxContent();
    dialogContent.innerHTML = '';

    if(!modalDialog)
        reloadDialog(url, title, width, height, form); 
    else
    {
        if(title)
            setDialogTitle(title);
        if(width && height)
        {
            setDialogSize(width, height);
            repositionDialog();
        }
        setDialogAjaxContent(url, form);
    }
}

function showLoader()
{
    // Center the loader
    var loader = getLoader();
    if(!loader) return;
    
    loader.style.visibility = 'hidden';
    loader.style.display = 'block';
    
    centerObj(loader, loader.offsetWidth, loader.offsetHeight);
    
    hideFlashContent();
    hideToolTip();
    
    //disableScreen();
    loader.style.visibility = '';
}

function showToolTip(anchor, url, title, form)
{
    var toolTip = getToolTip();
    
    toolTip.style.left = '0px';
    toolTip.style.top = '0px';
    toolTip.style.visibility = 'hidden';
    toolTip.style.display = 'block';
    
    hideFlashContent();
    disableScreenLight();   

    var toolTipContent = getToolTipContent();

    ajaxPanel(url, toolTipContent, form);
}

function positionToolTip(anchor, width, height) 
{
    // Set the dimensions
    var toolTip = getToolTip();
      
    toolTip.style.display = 'block';
   
    var width = toolTip.offsetWidth;
    if(toolTip && toolTip.style.display == 'block')
    {
        var obj = document.getElementById(anchor);
        if(!obj) return;
   
        // Get the position of the object to anchor to
        var pos = findPos(obj);
        
        // Now get the width and height of the object
        var anchorWidth = obj.offsetWidth;
        var anchorHeight = obj.offsetHeight;
        
        toolTip.style.left = ((pos[0]+(anchorWidth/2))-(width/2)) + 'px';
        toolTip.style.top = (pos[1]+anchorHeight+22) + 'px';

        toolTip.style.visibility = '';
        //disableScreen();
    }
}

function hideToolTip()
{
    var toolTip = getToolTip();
    if(!toolTip) return;
    toolTip.style.display = 'none';
    
    var overlayLight = getBGOverlayLight();
    overlayLight.style.display = 'none';
    
    if(!dialogOpen) showFlashContent();
}

function hideLoader()
{
    var loader = getLoader();
    if(!loader) return;
    loader.style.display = 'none';
    // re-show the video if we have no dialog on screen
    if (dialogOpen == false)
        showFlashContent();
    // enableScreen();
}

function reloadDialogFromOptions(url, options)
{
    clearDialogBackground(); // Since we might already have a background image that's the wrong size
    showDialog(url, options.title, options.width, options.height, null);
}

function reloadDialog(url, title, width, height, form)
{
    clearDialogBackground(); // Since we might already have a background image that's the wrong size
    
    if(!width) width = 750;
    if(!height) height = 500;
         
    // Set the dimensions
    var dialog = getDialogBox();
    var dialogContent = getDialogBoxContent();

    setDialogSize(width, height);

    // Set the title
    setDialogTitle(title);
    
    var dialogClose = getDialogBoxClose();
    
    // Display - which gives the object dimensions
    dialog.style.display = 'block';
    
    centerObj(dialog, dialog.offsetWidth, dialog.offsetHeight);
    
    disableScreen();
    
    // Make visible
    dialog.style.visibility = '';
    
    // Now load the content
    ajaxPanel(url, dialogContent, form);
}

function setDialogAjaxContent(url, form)
{
    var dialogContent = getDialogBoxContent();
    
    ajaxPanel(url, dialogContent, form);
}

function hideFlashContent()
{
    // Hide all video clips
    var videos = $$('.flashContent');
    for(var i = 0; i < videos.length; i++)
        videos[i].style.visibility = 'hidden';
}

function showFlashContent()
{
    // Show all videos
    var videos = $$('.flashContent');
    for(var i = 0; i < videos.length; i++)
        videos[i].style.visibility = '';
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') 
	{
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) 
		{
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) 
			{
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth() 
{
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') 
	{
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function getScrollXY() 
{
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) 
    {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } 
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
    {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } 
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
    {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function centerObj(obj, width, height) 
{
	if (document.getElementById) 
	{
		var windowHeight = getWindowHeight();
		var windowWidth = getWindowWidth();
		
		if (windowHeight > 0) 
		{
			var contentHeight = height;
			if (windowHeight - contentHeight > 0) 
			{
				obj.style.top = ((windowHeight / 2) - (contentHeight / 2)) + getScrollXY()[1] + 'px';
			}
		}
		
		if (windowWidth > 0) 
		{
			var contentWidth = width;
			if (windowWidth - contentWidth > 0) 
			{
				obj.style.left = ((windowWidth / 2) - (contentWidth / 2)) +  getScrollXY()[0] + 'px';
			}
		}
	}
}

function align(e, position) {
	e = $(e);
	var container = Try.these(
		function () { return e.parentNode },
		function () { return e.parentElement },
		function () { return false });
	if (container != false) {
		var dimensions = Element.getDimensions(e);
		e.removeAttribute('style');
		Element.setStyle(container, { position: 'relative' });
		for (var name in position) {
			if (name == 'hAlign') {
				switch (position[name]) {
					case 'left':	Element.setStyle(e, { 
										position: 'absolute', 
										left: 0 
									});
									break;
					case 'center':	Element.setStyle(e, { 
										position: 'absolute', 
										left: '50%', 
										marginLeft: ((dimensions.width / 2) * -1) + 'px' 
									});
									break;
					case 'middle':	Element.setStyle(e, { 
										position: 'absolute', 
										left: '50%', 
										marginLeft: ((dimensions.width / 2) * -1) + 'px' 
									});
									break;
					case 'right':	Element.setStyle(e, { 
										position: 'absolute', 
										right: 0 
									});
									break;
				}
			} else if (name == 'vAlign') {
				switch (position[name]) {
					case 'top':		Element.setStyle(e, { 
										position: 'absolute', 
										top: 0 
									});
									break;
					case 'center':	Element.setStyle(e, { 
										position: 'absolute', 
										top: '50%', 
										marginTop: ((dimensions.height / 2) * -1) + 'px' 
									});
									break;
					case 'middle':	Element.setStyle(e, { 
										position: 'absolute', 
										top: '50%', 
										marginTop: ((dimensions.height / 2) * -1) + 'px' 
									});
									break;
					case 'bottom':	Element.setStyle(e, { 
										position: 'absolute', 
										bottom: 0 
									});
									break;
				}
			}
		}
	}
	return e;
}

function repositionDialog() 
{
    // Set the dimensions
    var dialog = getDialogBox();
    if(dialog && dialog.style.display == 'block')
    {
        centerObj(dialog, dialog.offsetWidth, dialog.offsetHeight);
        disableScreen();
    }

    var loader = getLoader();   
    if(loader && loader.style.display == 'block')
    {
        centerObj(loader, loader.offsetWidth, loader.offsetHeight);
    }
    
    enableScreenLight();
}

// Disable and Enable the screen
function disableScreen() {
    var overlay = getBGOverlay();
        
    //return an array [pageWidth,pageHeight]
    var arrayPageSize = getPageSize();
    var pageWidth  = arrayPageSize[0];
    var pageHeight = arrayPageSize[1];
    
    //alert(Lightbox.getPageSize());
    
    overlay.style.width = pageWidth + 'px';
    overlay.style.height = pageHeight + 'px';
    overlay.style.display = 'block';    
}

function enableScreen() {
    var overlay = getBGOverlay();
    overlay.style.display = 'none';
    enableScreenLight();
}

function enableScreenLight() {
    hideToolTip();
}

function enableScreenTransparent() {
    var overlay = getBGOverlayTransparent();
    overlay.style.display = 'none';
}

function disableScreenLight() {
    var frame = getMainFrame();
    var overlay = getBGOverlayLight();
    overlay.style.width = document.body.clientWidth + 'px';
    overlay.style.height = document.body.clientHeight  + 'px';
    overlay.style.display = 'block';    
}

function disableScreenTransparent() {
    var frame = getMainFrame();
    var overlay = getBGOverlayTransparent();
    overlay.style.width = document.body.clientWidth + 'px';
    overlay.style.height = document.body.clientHeight  + 'px';
    overlay.style.display = 'block';    
}


function getPageSize()
{	        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
		
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
	pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}
