/*--------------------------------------------------------------------*\
| This file is part of Videocore 3.1                                  *|
|---------------------------------------------------------------------*|
| Copyright (C) 2008 Silversoft Solutions Ltd. All Rights Reserved    *|
| This file may not be redistributed in whole or significant part     *|
| Videocore is not Free Software | http://www.videocore.com           *|
|-------------------------------------------------------------------- *|
| For more information see http://www.videocore.com/license.html      *|
\*--------------------------------------------------------------------*/

var Dialog = 
{
	Error: function (message)
	{
		// ##### DISPLAY DIALOG MESSAGE #####
		Dialog._show (message);
		show (get_element('dialog_dismiss'));
	},

	Message: function (message)
	{
		// ##### DISPLAY DIALOG MESSAGE #####
		Dialog._show (message);
		show (get_element('dialog_dismiss'));
	},

	Dismiss: function (id)
	{
		// ##### REMOVE OVERLAY COMPLETELY #####
		if (get_element('overlay') != null)
		{
			var overlay = document.body.removeChild (get_element('overlay'));
		}

		// ##### REMOVE DIALOG MESSAGE #####
		Animation.fadeOut(get_element(id), 100);
		
		// ##### SPECIAL CASE FOR THE ADMIN UPLOADS PAGE #####
		if (typeof reloadOnDismiss != "undefined" && reloadOnDismiss==true)
		{
			window.location.href = window.location;
		}
		
		if(document.getElementById("mediaplayer"))
		{
			var mediaPlayer = document.getElementById("mediaplayer");
			var mediaPlayer2 = document.getElementById("auxmediaplayer");
			mediaPlayer2.style.display="";
			var mediaPlayerParent = mediaPlayer.parentNode;
			mediaPlayerParent.appendChild(mediaPlayer2);
			mediaPlayerParent.removeChild(mediaPlayer);
			mediaPlayer2.id = "mediaplayer";	
		}	
	},

	_show: function (message)
	{
		if(document.getElementById("mediaplayer"))
		{
			var mediaPlayer = document.getElementById("mediaplayer");
			var mediaPlayer2 = document.getElementById("mediaplayer").cloneNode(true);
			var auxdiv = document.getElementById("auxdiv");
			auxdiv.appendChild(mediaPlayer2);
			mediaPlayer2.id = "auxmediaplayer";
			mediaPlayer2.style.display="none";
			
			mediaPlayer.setAttribute("wmode","transparent");
			var mediaPlayerParent = document.getElementById("mediaplayer").parentNode;
			var code = mediaPlayerParent.innerHTML;
			mediaPlayerParent.innerHTML = "";
			mediaPlayerParent.innerHTML = code;
		}
		
		
		// ##### GET DIALOG ELEMENT #####
		var dialog = get_element ('dialog');

		// ###### SET OPACTIY #####
		Animation.setOpacity (dialog, 0);
		
		// ##### GET PAGE SIZE #####
		var window_size = Dialog._getWindowSize();

		// ##### GET CURRENT WINDOW LOCATION #####
		var window_location = Dialog._getWindowPosition ();

		// ###### GET PAGE SIZE #####
		var page_size = Dialog._getPageSize(window_size[0], window_size[1]);

		// ##### CREATE OVERLAY #####
		Dialog._createOverlay (page_size);

		// ##### SET THE MESSAGE #####
		get_element('dialog_message').innerHTML = message;

		// ##### SHOW THE DIALOG BOX #####
		show (dialog);
		
		// ##### SET LEFT POSITION #####
		dialog.style.cssText += 'left: ' + ((window_size[0] - dialog.offsetWidth) / 2) + 'px;';

		// ##### FADE IN #####
		Animation.fadeIn(dialog, 0);
		
		

	},
	
	_createOverlay: function (page_size)
	{
		// ##### CREATE OVERLAY #####
		var overlay = document.createElement ('div');
		overlay.style.cssText = 'height: ' + page_size[1] + 'px;';
		overlay.setAttribute ('id', 'overlay');
		document.body.appendChild (overlay);
		Animation.setOpacity (get_element('overlay'), 55);
	},

	_random: function (num)
	{
		return (Math.floor(Math.random() * num + 1));
	},
	
	_getPageSize: function (windowWidth, windowHeight)
	{
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY)
		{	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight)
		{
			// ##### ALL BUT MAC EXPLORER #####
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else
		{ 
			// ##### MAC EXPLORER #####
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		// 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 = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		return new Array(pageWidth, pageHeight);
	},

	_getWindowSize: function ()
	{
		var windowWidth, windowHeight;

		if (self.innerHeight)
		{
			// ##### EVERYTHING BUT EXPLORER #####
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{ 
			// ##### EXPLORER 6 #####
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			// ##### IE IS LAME #####
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		return new Array(windowWidth, windowHeight);
	},

	_getWindowPosition: function ()
	{		
		var width = self.pageYOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : null;
		var height = self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : null;
		
		return new Array(width, height);
	}
}
