/*
 *
 * this module defines JustLoggedView class, which represents dynamic window showing users just being logged
 * into mpmads application (those having filled at least nickname in their user profile)
 * it uses justlogged.aspx service
 *
 */

function JustLoggedView(servicePath, jlViewCssID, jlViewContentCssID, jlViewCaptionCssID, jlViewWindowStateCssID, refreshInterval) {
	/*
	 *	constructor takes six arguments, three CssIDs of window parts (each window consists of Content, Caption and
	 *	encapsulating element), jlViewWindowStateCssID CSS ID of hidden input element, indicating maximalization state of window
	 *  (when changing state, request is sent to the service with action parameter via XMLHTTP, maximalization state is then saved in session),
	 *  servicePath argument, giving path to the service returning HTML text which will be shown
	 *	in Content element and the last, refreshInterval giving the number of miliseconds between refreshes
	 */


	/* CONSTRUCTOR AND PRIVATE MEMBER DEFINITIONS */

	var _service_uri = servicePath;	
	var _jlView_id = jlViewCssID;
	var _previousResponse;
	var _suppressExceptions = true;
	var _intervalTimer;

	var _jlViewElement = window.document.getElementById(jlViewCssID);
	if (_jlViewElement == null) {
		throw new Error ("Element with specified ID (" + jlViewCssID + ") does not exists in the document.");
	}

	var _jlViewContentElement = window.document.getElementById(jlViewContentCssID);
	if (_jlViewContentElement == null) {
		throw new Error ("Element with specified ID (" + jlViewContentCssID + ") does not exists in the document.");
	}

	var _jlViewCaptionElement = window.document.getElementById(jlViewCaptionCssID);
	if (_jlViewContentElement == null) {
		throw new Error ("Element with specified ID (" + jlViewCaptionCssID + ") does not exists in the document.");
	}

	var _jlViewWindowState = window.document.getElementById(jlViewWindowStateCssID);
	if (_jlViewWindowState == null) {
		throw new Error ("Element with specified ID (" + jlViewWindowStateCssID + ") does not exists in the document.");
	}

	var _jlViewMinMaxButton = window.document.getElementById("minMaxButton");
	if (_jlViewMinMaxButton == null) {
		throw new Error ("Element with specified ID (minMaxButton) does not exists in the document.");
	}

//	window.document.onscroll = onScrollEventHandler;
	window.onscroll = onScrollEventHandler;

	if (_jlViewWindowState.value == "minimized") {
		minimize();
	} else {
		maximize();
	}

	_jlViewElement.style.display = "block";

	/* PRIVATE METHOD DEFINITIONS */

	function update() {
		doRequest(_service_uri + "?" + hex_md5(new Date().toString()));
	}

	function setAutorefresh() {
		_intervalTimer = setInterval(update, refreshInterval);
	}

	function unsetAutorefresh() {
		if (_intervalTimer != null) {
			clearInterval(_intervalTimer);
		}
	}

	function doRequest(url) {
		var xmlhttp = false;
		if(window.XMLHttpRequest) {
			try {
				xmlhttp = new XMLHttpRequest();
			} catch(e) {
				xmlhttp = false;
				_previewElement.innerHTML =
					"<div align='center'><strong>Error</strong><br /><br>cannot create or handle request</div>";
			}
		} else {
			if(window.ActiveXObject) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						xmlhttp = false;
						_previewElement.innerHTML =
							"<div align='center'><strong>Error</strong><br /><br>cannot create or handle request</div>";
					}
				}
			}
		}

		try {
			xmlhttp.open("GET", url, true);
		} catch(e) {
			_previewElement.innerHTML =
				"<div align='center'><strong>Security block</strong><br /><br>data cannot be loaded from other domain</div>";
			return false;
		}
		xmlhttp.onreadystatechange=function() {
			requestReadyStateChangeHandler(xmlhttp);
		}
		xmlhttp.send(null);
		return false;
	}
/***************/

	function doBlindRequest(url) {
		var xmlhttp = false;
		if(window.XMLHttpRequest) {
			try {
				xmlhttp = new XMLHttpRequest();
			} catch(e) {
				xmlhttp = false;
			}
		} else {
			if(window.ActiveXObject) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						xmlhttp = false;
					}
				}
			}
		}

		try {
			xmlhttp.open("GET", url, true);
		} catch(e) {
			return false;
		}
//		xmlhttp.onreadystatechange = null;
		xmlhttp.send(null);
		return false;
	}
/***************/

	function requestReadyStateChangeHandler(xmlhttp) {
		try {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var currentResponse = xmlhttp.responseText.toString();
					if (currentResponse != _previousResponse) {
						_jlViewContentElement.innerHTML = currentResponse;
						_previousResponse = currentResponse;
					}
					return true;
				}
			}
		} catch(e) {
			if (_suppressExceptions == false) {
				alert ("No images found or other error: " + e);
			}
		}
	}


	function onScrollEventHandler()
	{
		var offsetx = 5;
//		var offsety = 100;
		var initialOffsetY = 120;
//		var constantOffsetY = 10;
		var x = document.documentElement.scrollLeft + offsetx;
		var y; // = document.documentElement.scrollTop + offsety;

		if (document.documentElement.scrollTop < initialOffsetY) {
			y = initialOffsetY;
		} else {
			y = document.documentElement.scrollTop;// + constantOffsetY;
		}
		if (ns6) {
			_jlViewElement.style.left = x.toString() + 'px';
			_jlViewElement.style.top = y.toString() + 'px';
		} else {
			_jlViewElement.style.posLeft = x;
			_jlViewElement.style.posTop = y;
		}
	}

	function isMinimized() {
		if (_jlViewContentElement.style.display == "none") {
			return true;
		}
	}

	function minimize()
	{
		_jlViewContentElement.style.display = "none";
		_jlViewCaptionElement.style.display = "none";
		_jlViewElement.style.width = "19px";
		_jlViewMinMaxButton.className = "maximizeButton";
		unsetAutorefresh();
	}

	function maximize()
	{
		_jlViewContentElement.style.display = "block";
		_jlViewCaptionElement.style.display = "inline";
		_jlViewElement.style.width = "175px";
		_jlViewMinMaxButton.className = "minimizeButton";
		update();
		setAutorefresh();
	}


	/* PUBLIC METHOD DEFINITIONS */

	function minimizeOrMaximize()
	{
		if (isMinimized()) {
			maximize();
			doBlindRequest(_service_uri + "?action=maximize");
		} else {
			minimize();
			doBlindRequest(_service_uri + "?action=minimize");
		}	
	}
	this.minimizeOrMaximize = minimizeOrMaximize;

	function getWidth() {
//		return _jlViewElement.style.width;
		return _jlViewElement.offsetWidth;
	}
	this.getWidth = getWidth;
}


