﻿var windowMove = false;
var windowPositionStart = new Array();
var windowPositionOffset = new Array();
var windowSizeStart = new Array();
var activeWindow = false;
var activeWindowContent = false;	
var windowSizeArray = new Array();
var windowID = 0;
var windowZIndex = 10000;
var senders = new Array();
var receivers = new Array();
	
var windowWidth = 0;
var windowHeight = 0;
var toolbarVisible = false;

var windowPositionArray = new Array();

var windowTitle = "";
	
function NewFloatingWindow(receiver, sender, title, width, height, left, top, minimized, toolbar)
{	
	toolbarVisible = toolbar;
	windowWidth = width;
	windowHeight = height;
	windowTitle = title;

	windowID = windowID + 1;	

	windowPositionArray[windowID] = [left, top]
	
	senders[windowID] = sender;
	receivers[windowID] = receiver;
	
	createFrame(minimized, false);
	
	createTitleBarControls();
	
	createMediaControls();

	if (!minimized) createViewScreen(sender, receiver, false, windowID);
}

function NewStaticWindow(receiver, sender, title, width, height, left, top)
{	
	windowWidth = width;
	windowHeight = height;
	windowTitle = title;

	windowID = windowID + 1;	

	windowPositionArray[windowID] = [left, top]

	senders[windowID] = sender;
	receivers[windowID] = receiver;

	createFrame(false, true);
	
	createTitleBarControls();
		
	createViewScreen(sender, receiver, false, windowID);
}

function Initialize(floatingWindowObject, staticWindow)
{		
	divArray = document.getElementsByTagName('DIV');
				
	for (divIndex = 0; divIndex < divArray.length; divIndex++)
	{		
		if (divArray[divIndex].className == 'FloatingWindow' && divArray[divIndex].id == "")
		{					
			divArray[divIndex].style.zIndex = windowZIndex;
				
			windowZIndex = windowZIndex / 1 + 1;
				

			if (windowID == 1)
			{
				activeWindow = divArray[divIndex];		
			}

			divArray[divIndex].id = 'FloatingWindow' + windowID;	
			divArray[divIndex].onmousedown = switchElement;

			if (windowPositionArray[windowID])
			{
				divArray[divIndex].style.left = windowPositionArray[windowID][0] + 'px';	
				divArray[divIndex].style.top = windowPositionArray[windowID][1] + 'px';	
			}
		}	
			
		if (divArray[divIndex].className == 'FloatingWindow')
		{
			//divArray[divIndex].id = 'WindowContent' + windowID;
			divArray[divIndex].style.zIndex = 1000;
			if (!staticWindow) divArray[divIndex].onmousedown = initMove;

			if (windowSizeArray[windowID])
			{
				divArray[divIndex].style.height = windowSizeArray[windowID][1] + 'px';
				divArray[divIndex].parentNode.parentNode.style.width = windowSizeArray[windowID][0] + 'px';
					
			//	if (MSIEWIN)
			//	{
					iframe.style.width = (windowSizeArray[windowID][0]) + 'px';
					iframe.style.height = (windowSizeArray[windowID][1] + 20) + 'px';
			//	}
			}
		}	
	}	
		
	if (floatingWindowObject)
	{
		document.documentElement.onmouseup = stopMove;	
		document.documentElement.onmousemove = moveWindow;
	}

	return windowID;
}

function cancelEvent()
{
	return !windowMove;
}
		
	
function hideWindow()
{
	switchElement(false, document.getElementById('FloatingWindow' + this.id.replace(/[^\d]/g,'')));	
	
	activeWindow.style.display = 'none';
}

function moveWindow(e)
{	
	if (document.all) e = event;
		
	if (!activeWindow) return;

	if (windowMove)
	{
		activeWindow.style.left = windowPositionOffset[0] + e.clientX - windowPositionStart[0]  + 'px';
		activeWindow.style.top = windowPositionOffset[1] + e.clientY - windowPositionStart[1]  + 'px';	
	}	
		
	if (!document.all) return false;
}

function initMove(e)
{		
	if (document.all) e = event;
		
	windowMove = true;
		
	switchElement(false, this);
		
	windowPositionStart = [e.clientX, e.clientY];
	windowPositionOffset = [activeWindow.offsetLeft, activeWindow.offsetTop];
		
	return true;
}
		
function stopMove(e)
{
	if(document.all) e = event;
		
	windowMove = false;
				
	if (!activeWindow || !activeWindowContent) return;
}

var fromMinimize = false;

function minimizeWindow(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('FloatingWindow' + numericId).style.height =  '26px';
	document.getElementById('FloatingWindow' + numericId).style.background = "url(Images/FrameMinimize.png)";		
	document.getElementById('FloatingWindow' + numericId).style.left = windowPositionArray[numericId][0] + 'px';
	document.getElementById('FloatingWindow' + numericId).style.top = windowPositionArray[numericId][1] + 'px';	
	document.getElementById('FloatingWindow' + numericId).onclick = maximizeWindow;
	document.getElementById('FloatingWindow' + numericId).title = "Click to view channel";
	document.getElementById('FloatingWindow' + numericId).style.cursor = "pointer";

	document.getElementById('Frame' + numericId).style.visibility = "hidden";
	document.getElementById('minimizeImage' + numericId).style.visibility = "hidden";
	document.getElementById('pushtodesktop' + numericId).style.visibility = "hidden";

	fromMinimize = true;
}

function maximizeWindow(e, inputObj)
{
	if (fromMinimize) 
	{
		fromMinimize = false;
		return;
	}
	
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');

	createViewScreen(senders[numericID], receivers[numericID], false, numericID);
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('FloatingWindow' + numericId).style.height =  windowHeight + "px";
	document.getElementById('FloatingWindow' + numericId).style.background = "url(Images/Frame.png)";		
	document.getElementById('FloatingWindow' + numericId).style.left =  '300px';
	document.getElementById('FloatingWindow' + numericId).style.top = '300px';	
	document.getElementById('FloatingWindow' + numericId).onclick = "";
	document.getElementById('FloatingWindow' + numericId).title = "";
	document.getElementById('FloatingWindow' + numericId).style.cursor = "";

	document.getElementById('Frame' + numericId).style.visibility = "visible";
	document.getElementById('minimizeImage' + numericId).style.visibility = "visible";
	document.getElementById('pushtodesktop' + numericId).style.visibility = "visible";
}

function playChannel(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('stopImage' + numericId).src = 'Images/stop_playing_active.png';
	document.getElementById('Frame' + numericId).contentWindow.StartSlideShow();
}

function stopChannel(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('stopImage' + numericId).src = 'Images/stop_playing.png';
	document.getElementById('Frame' + numericId).contentWindow.StopSlideShow();
}

function nextImage(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('Frame' + numericId).contentWindow.NextImage();
}

function previousImage(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('Frame' + numericId).contentWindow.PreviousImage();
}

function firstImage(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('Frame' + numericId).contentWindow.FirstImage();
}

function lastImage(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));
		
	document.getElementById('Frame' + numericId).contentWindow.LastImage();
}

function closeWindow(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));

	document.getElementById('FloatingWindow' + numericId).style.visibility = "hidden";
	document.getElementById('Frame' + numericId).style.visibility = "hidden";
}

function pushToDesktop(e, inputObj)
{
	if (!inputObj) inputObj = this;
		
	numericID = inputObj.id.replace(/[^0-9]/g, '');
		
	switchElement(false, document.getElementById('FloatingWindow' + numericID));

	document.getElementById('Frame' + numericId).contentWindow.Liberate();
}

function switchElement(e, inputElement)
{
	if (!inputElement) inputElement = this;
		
	numericId = inputElement.id.replace(/[^0-9]/g, '');
							
	windowZIndex = windowZIndex / 1 + 1;
		
	activeWindow = document.getElementById('FloatingWindow' + numericId);	
	activeWindow.style.zIndex = windowZIndex;
}

function setDate(date, ID)
{
	document.getElementById('dateSent' + ID).innerHTML = date;
}

function createFrame(minimized, staticWindow)
{
	floatingWindow = document.createElement('DIV');
	floatingWindow.className = 'FloatingWindow';
	floatingWindow.style.width =  windowWidth + 'px';
	floatingWindow.style.height = (minimized) ? '26px' : windowHeight + 'px';
	floatingWindow.style.left =  windowPositionArray[windowID][0] + 'px';
	floatingWindow.style.top = windowPositionArray[windowID][1]  + 'px';	
	floatingWindow.style.position = "absolute";
	floatingWindow.style.overflow = "hidden";
	floatingWindow.style.zIndex = "10000";
	floatingWindow.style.background = (minimized) ? "url(Images/FrameMinimize.png)" : "url(Images/Frame.png)";		

	if (minimized && !toolbarVisible)	
	{
		floatingWindow.onclick = maximizeWindow;
		floatingWindow.title = "Click to view channel";
		floatingWindow.style.cursor = "pointer";
	}

	document.body.appendChild(floatingWindow);	
	
	divId = Initialize(floatingWindow, staticWindow); 	
}

function createMediaControls()
{
	center = windowWidth / 2;
	
	skipBackwardImage = document.createElement('IMG');
	skipBackwardImage.src = 'Images/skip_backward.png';
	skipBackwardImage.width = 24;
	skipBackwardImage.height = 24;
	skipBackwardImage.style.position = "absolute";
	skipBackwardImage.style.left = center - 92 + "px";
	skipBackwardImage.style.top = "265px";
	skipBackwardImage.id = 'skipBackwardImage' + windowID;
	skipBackwardImage.onclick = firstImage;
	skipBackwardImage.title = "Earliest";
	skipBackwardImage.style.cursor = "pointer";

	document.getElementById('FloatingWindow' + divId).appendChild(skipBackwardImage);	

	rewindImage = document.createElement('IMG');
	rewindImage.src = 'Images/rewind.png';
	rewindImage.width = 24;
	rewindImage.height = 24;
	rewindImage.style.position = "absolute";
	rewindImage.style.left = center - 64 + "px";
	rewindImage.style.top = "265px";
	rewindImage.id = 'rewindImage' + windowID;
	rewindImage.onclick = previousImage;
	rewindImage.title = "Previous";
	rewindImage.style.cursor = "pointer";

	document.getElementById('FloatingWindow' + divId).appendChild(rewindImage);	

	stopImage = document.createElement('IMG');
	stopImage.src = 'Images/stop_playing.png';
	stopImage.width = 24;
	stopImage.height = 24;
	stopImage.style.position = "absolute";
	stopImage.style.left = center - 28 + "px";
	stopImage.style.top = "265px";
	stopImage.id = 'stopImage' + windowID;
	stopImage.onclick = stopChannel;
	stopImage.title = "Stop Slide Show";
	stopImage.style.cursor = "pointer";

	document.getElementById('FloatingWindow' + divId).appendChild(stopImage);	

	playImage = document.createElement('IMG');
	playImage.src = 'Images/play.png';
	playImage.width = 24;
	playImage.height = 24;
	playImage.style.position = "absolute";
	playImage.style.left = center + 4 + "px";
	playImage.style.top = "265px";
	playImage.id = 'playImage' + windowID;
	playImage.onclick = playChannel;
	playImage.title = "Start Slide Show";
	playImage.style.cursor = "pointer";

	document.getElementById('FloatingWindow' + divId).appendChild(playImage);	
		
	fastForwardImage = document.createElement('IMG');
	fastForwardImage.src = 'Images/fast_Forward.png';
	fastForwardImage.width = 24;
	fastForwardImage.height = 24;
	fastForwardImage.style.position = "absolute";
	fastForwardImage.style.left = center + 40 + "px";
	fastForwardImage.style.top = "265px";
	fastForwardImage.id = 'fastForwardImage' + windowID;
	fastForwardImage.onclick = nextImage;
	fastForwardImage.title = "Next";
	fastForwardImage.style.cursor = "pointer";

	document.getElementById('FloatingWindow' + divId).appendChild(fastForwardImage);	

	skipForwardImage = document.createElement('IMG');
	skipForwardImage.src = 'Images/skip_Forward.png';
	skipForwardImage.width = 24;
	skipForwardImage.height = 24;
	skipForwardImage.style.position = "absolute";
	skipForwardImage.style.left = center + 68 + "px";
	skipForwardImage.style.top = "265px";
	skipForwardImage.id = 'skipForwardImage' + windowID;
	skipForwardImage.onclick = lastImage;
	skipForwardImage.title = "Latest";
	skipForwardImage.style.cursor = "pointer";

	document.getElementById('FloatingWindow' + divId).appendChild(skipForwardImage);	
}

function createTitleBarControls()
{
	channelName = document.createElement('DIV');
	channelName.width = 200;
	channelName.height = 16;
	channelName.style.position = "absolute";
	channelName.style.left = "24px";
	channelName.style.top = "4px";
	channelName.style.backgroundColor = "Transparent";
	channelName.id = 'channelName' + windowID;
	channelName.style.color = "white";
	channelName.style.fontFamily = "Tahoma";
	channelName.style.fontSize = "8pt";

	channelName.innerHTML = windowTitle;

	document.getElementById('FloatingWindow' + divId).appendChild(channelName);	

	dateSent = document.createElement('DIV');
	dateSent.width = 200;
	dateSent.height = 16;
	dateSent.style.position = "absolute";
	dateSent.style.left = windowWidth / 2 - 66 + "px";
	dateSent.style.top = "235px";
	dateSent.style.backgroundColor = "transparent";
	dateSent.id = 'dateSent' + windowID;
	dateSent.style.color = "white";
	dateSent.style.fontFamily = "Tahoma";
	dateSent.style.fontSize = "10pt";
	dateSent.style.textAlign = "center";
	
	document.getElementById('FloatingWindow' + divId).appendChild(dateSent);	

	minimizeImage = document.createElement('IMG');
	minimizeImage.src = 'Images/minimize.png';
	minimizeImage.width = 17;
	minimizeImage.height = 17;
	minimizeImage.className='minimizeButton';
	minimizeImage.style.position = "absolute";
	minimizeImage.style.left = "188px";
	minimizeImage.style.top = "2px";
	minimizeImage.id = 'minimizeImage' + windowID;
	minimizeImage.onclick = minimizeWindow;
	minimizeImage.title = "Minimize";
	minimizeImage.style.cursor = "pointer";

	minimizeImage.style.visibility = (toolbarVisible) ? "visible" : "hidden";

	document.getElementById('FloatingWindow' + divId).appendChild(minimizeImage);	
		
	maximizeImage = document.createElement('IMG');
	maximizeImage.src = 'Images/maximize.png';
	maximizeImage.width = 17;
	maximizeImage.height = 17;
	maximizeImage.className='maximizeButton';
	maximizeImage.style.position = "absolute";
	maximizeImage.style.left = "188px";
	maximizeImage.style.top = "2px";
	maximizeImage.id = 'maximizeImage' + windowID;
	maximizeImage.onclick = maximizeWindow;
	maximizeImage.title = "Restore";
	maximizeImage.style.cursor = "pointer";

//	document.getElementById('FloatingWindow' + divId).appendChild(maximizeImage);	

	closeImage = document.createElement('IMG');
	closeImage.src = 'Images/close.png';
	closeImage.width = 16;
	closeImage.height = 16;
	closeImage.className='minimizeButton';
	closeImage.style.position = "absolute";
	closeImage.style.left = "188px";
	closeImage.style.top = "3px";
	closeImage.id = 'closeImage' + windowID;
	closeImage.onclick = closeWindow;
	closeImage.title = "Close";
	closeImage.style.cursor = "pointer";

//	document.getElementById('FloatingWindow' + divId).appendChild(closeImage);	

	pushtodesktopImage = document.createElement('IMG');
	pushtodesktopImage.src = 'Images/pushtodesktop.png';
	pushtodesktopImage.width = 17;
	pushtodesktopImage.height = 17;
	pushtodesktopImage.className='minimizeButton';
	pushtodesktopImage.style.position = "absolute";
	pushtodesktopImage.style.left = "206px";
	pushtodesktopImage.style.top = "2px";
	pushtodesktopImage.id = 'pushtodesktop' + windowID;
	pushtodesktopImage.onclick = pushToDesktop;
	pushtodesktopImage.title = "Clone To Desktop";
	pushtodesktopImage.style.cursor = "pointer";

	pushtodesktopImage.style.visibility = (toolbarVisible) ? "visible" : "hidden";

	document.getElementById('FloatingWindow' + divId).appendChild(pushtodesktopImage);	
}

function createViewScreen(sender, receiver, minimized, id)
{
	x = document.getElementById('FloatingWindow' + id).getElementsByTagName("UserHistory" + id);
	
	if (x.length > 0) return;
	
	div = document.createElement('DIV');
	div.id = "UserHistory" + id;
	div.style.paddingTop = "0px";
	div.style.height = "250px";
	div.style.width = "200px";
	div.style.position = "absolute";
	div.style.left = "24px";
	div.style.top = "25px";

	frame = document.createElement('IFRAME');
	frame.id = "Frame" + id;
	frame.name = "Frame" + id;
	frame.src = "ContentViewer.aspx?ID=" + id + "&URL=" + sender + receiver;
	frame.frameBorder = '0';
	frame.width = 180;
	frame.height = 200;
	if (minimized) frame.style.visibility = "hidden";

	div.appendChild(frame);
		
	document.getElementById('FloatingWindow' + id).appendChild(div);	
}