﻿var rev_popupHasNotBeenVisited = true;
var rev_popupRevationServer="";
var rev_popupPresenceId = "";
var rev_popupGroup = "";
var rev_popupIntervalId = 0;

function changeObjectVisibility(objectId, newVisibility) {
	var obj = document.getElementById(objectId);
	if (obj) {
		obj.style.visibility = newVisibility;
		return true;
	}
	return false;
}

function show(objectId) {
	changeObjectVisibility(objectId,"visible");
}

function hide(objectId) {
	changeObjectVisibility(objectId,"hidden");
}

function rev_closePopupLayer() {
	rev_popupHasNotBeenVisited = false;
	hide("rev_popup_layer");
	clearInterval(rev_popupIntervalId);
}

function rev_positionPopupLayer() {
	//define universal dsoc left point
	var dsocleft=document.all? document.body.scrollLeft : pageXOffset
	//define universal dsoc top point
	var dsoctop=document.all? document.body.scrollTop : pageYOffset
	//define universal browser window width
	var window_width=document.all? document.body.clientWidth : window.innerWidth
	//define universal browser window height
	var window_height=document.all? document.body.clientHeight : window.innerHeight

	var popupobj = document.getElementById("rev_popup_layer");
	if (popupobj) {
		var popup_width = popupobj.clientWidth;
		var popup_height = popupobj.clientHeight;
		if (parseInt(window_width) <= parseInt(popup_width)) {
			popupobj.style.left = parseInt(dsocleft);
		}
		else {
			popupobj.style.left = parseInt(dsocleft) + ((parseInt(window_width) - parseInt(popup_width)) / 2);
		}
		if (parseInt(window_height) <= parseInt(popup_height)) {
			popupobj.style.top = parseInt(dsoctop);
		}
		else {
			popupobj.style.top = parseInt(dsoctop) + ((parseInt(window_height) - parseInt(popup_height)) / 2);
		}
	}
}

function rev_updateStatus() {
	var script=document.createElement('script');
	script.type='text/javascript';
	script.id='myscript';
	script.defer=true;
	script.src = 'http://' + rev_popupRevationServer + '/status/js/?im=' + rev_popupPresenceId + '&group=' + rev_popupGroup + '&fn=rwc_popupStatus';
	var head = document.getElementsByTagName('head').item(0);
	head.appendChild(script);
}

function rev_chatPopupWindow() {
	rev_closePopupLayer();
	var chatUrl = "https://" + rev_popupRevationServer + "/?im=" + rev_popupPresenceId + "&group=" + rev_popupGroup + "&pres=0";
	window.open(chatUrl,"rev_chatPopupWindow","scrollbars=no,menubar=no,status=no,resizable=yes,location=no,width=450,height=452,left=200,top=100");
}

function rwc_popupStatus(pid, status, custom, display, group, id) {
	if (rev_popupHasNotBeenVisited && status == "online") {
		clearInterval(rev_popupIntervalId);
		rev_positionPopupLayer();
		show("rev_popup_layer");
	}
}

function rev_initializePopupLayer(server, im, group, popTimeout) {
	clearInterval(rev_popupIntervalId);
	if (popTimeout > 0) {
		rev_popupRevationServer = server;
		rev_popupPresenceId = im;
		rev_popupGroup = group;
		rev_popupIntervalId = setInterval("rev_updateStatus()", popTimeout * 1000);
	}
}

