﻿
var childrens = new Array();
var messengerPopup = new Array();

//------------- OPEN CHATS -----------------------

function OpenChild(name, params, settings)
{
     var url = "/instant_messenger/" + name + ".aspx" + params;
     messengerPopup[messengerPopup.length] = window.open(url, name, settings);
}

function OpenChat(chatId)
{
    GetChildsRefs();
    for (i = 0; i < childrens.length; i++)
    {
	if (childrens[i].name == ("text" + chatId))
	{
	    childrens[i].focus();
	    return 0;
	}
    }
     var url = "/instant_messenger/chat.aspx?chatId=" + chatId;
     oChatWindow = window.open(url, "text" + chatId, "top=100, left=100, height=600, width=800, location=no, resizable=yes, scrollbars=no, toolbar=no, status=no");
     //childrens[childrens.length] = oChatWindow;
}

function OpenVideoChat(chatId)
{
    GetChildsRefs();
    for (i = 0; i < childrens.length; i++)
    {
	if (childrens[i].name == ("textv" + chatId))
	{
	    childrens[i].focus();
	    return 0;
	}
    }
     var url = "/instant_messenger/videochat.aspx?chatId=" + chatId;
     oChatWindow = window.open(url, "textv" + chatId, "top=100, left=100, height=600, width=760, location=no, resizable=yes, scrollbars=no, toolbar=no, status=no");
     //childrens[childrens.length] = oChatWindow;
}

function OpenContactList(memberId)
{
    GetChildsRefs();
    for (i = 0; i < childrens.length; i++)
    {
	if (childrens[i].name == "textm")
	{
	    childrens[i].focus();
	    return 0;
	}
    }
    var url = "/instant_messenger/contactlist.aspx";
    oContactListWindow = window.open(url, "textm", "top=100, left=300, height=550, width=240, location=no, resizable=yes, scrollbars=no, toolbar=no, status=no");
    //childrens[childrens.length] = oContactListWindow;
}

function RefreshContactList()
{
    if (document.cookie.search("textm") != -1)
    {
        var url = "/instant_messenger/contactlist.aspx";
        oContactListWindow = window.open(url, "textm", "top=100, left=300, height=550, width=240, location=no, resizable=yes, scrollbars=no, toolbar=no, status=no");
        oContactListWindow.location = oContactListWindow.location;
    }
}

function OpenMessageWindow(palId, isProvider)
{
    if (isProvider == true)
	var url = "/instant_messenger/messagewindow.aspx?palId=" + palId + "&IsProvider=" + isProvider;
    else
	var url = "/instant_messenger/messagewindow.aspx?palId=" + palId;
    GetChildsRefs();
    var windowName = "textmw" + palId;
    for (i = 0; i < childrens.length; i++)
    {
	if (childrens[i].name == windowName)
	{
	    childrens[i].focus();
	    return 0;
	}
    }
    oWindow = window.open(url, windowName, "width=650,height=450,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no");
    try
	{
	childrens[childrens.length].focus();
    	FocusWindow();
	}
    catch(err)
	{
	}
    //childrens[childrens.length] = oWindow;
}

//---------------------------------------------------------

function SetParent(name)
{
    if (BrowserDetect.browser == "Opera") return;
    document.cookie = "p" + name + "=true; path=/";
    GetMessengerPopupRef(name);
}

function RemoveParent(name)
{
    if (BrowserDetect.browser == "Opera") return;
    document.cookie = "p" + name + "=true; expires=Friday,31-Dec-99 23:59:59 GMT; path=/";
    if (messengerPopup[0] != null)
    {
	    messengerPopup[0].setTimeout("CheckToCloseMessengerPopup('" + name + "')", 1000);
    }
}

function SetChild(name)
{
    if (BrowserDetect.browser == "Opera") return;
    document.cookie = "c" + name + "=true; path=/";
}

function RemoveChild(name)
{
    if (BrowserDetect.browser == "Opera") return;
    document.cookie = "c" + name + "=true; expires=Friday,31-Dec-99 23:59:59 GMT; path=/";
}

function GetChildsRefs()
{
    cooks = document.cookie.split('; ');
    childrens.length = 0;
    for (i = 0; i < cooks.length; i++)
    {
        if (cooks[i].search("text") != -1)
        {
            wName = cooks[i].split('=')[0];
            oChatWindow = window.open("", wName, "top=100, left=100, height=600, width=800, location=no, resizable=yes, scrollbars=no, toolbar=no, status=no", false);
            childrens[childrens.length] = oChatWindow;
            this.focus();
        }
    }
}

function GetMessengerPopupRef(name)
{
    if (document.cookie.search("c" + name) != -1)
    {
	messengerPopup[messengerPopup.length] = window.open("", name);
    }
}

function CheckToCloseMessengerPopup(name)
{
    if (document.cookie.search("p" + name) == -1)
    {
	    close();
    }
}

//------------------ SET/REMOVE POPUP REFERENCES--------------

function SetTextPopupRef()
{
   if (BrowserDetect.browser == "Opera") return;
    params = document.location.href.split('?')[1];
    param = params.split('=')[1];
    document.cookie = "text" + param + "=true; path=/";
}

function RemoveTextPopupRef()
{
    params = document.location.href.split('?')[1];
    param = params.split('=')[1].split('#')[0];
    document.cookie = "text" + param + "=true; expires=Friday,31-Dec-99 23:59:59 GMT; path=/";
}

function SetVideoPopupRef()
{
    if (BrowserDetect.browser == "Opera") return;
    params = document.location.href.split('?')[1];
    param = params.split('=')[1];
    document.cookie = "textv" + param + "=true; path=/";
}

function RemoveVideoPopupRef()
{
    params = document.location.href.split('?')[1];
    param = params.split('=')[1].split('#')[0];
    document.cookie = "textv" + param + "=true; expires=Friday,31-Dec-99 23:59:59 GMT; path=/";
}

function SetMessengerPopupRef()
{
   if (BrowserDetect.browser == "Opera") return;
    document.cookie = "textm=true; path=/";
}

function RemoveMessengerPopupRef()
{
    document.cookie = "textm=true; expires=Friday,31-Dec-99 23:59:59 GMT; path=/";
}

function SetMessageWindowPopupRef()
{
    if (BrowserDetect.browser == "Opera") return;
    params = document.location.href.split('?')[1];
    param = params.split('=')[1];
    document.cookie = "textmw" + param + "=true; path=/";
}

function RemoveMessageWindowPopupRef()
{
    params = document.location.href.split('?')[1];
    param = params.split('=')[1].split('#')[0];
    document.cookie = "textmw" + param + "=true; expires=Friday,31-Dec-99 23:59:59 GMT; path=/";
}

//--------------------------------------------------------------
		
function thisMovie(movieName)
{
	return document.getElementById(movieName);
}
		
function FocusWindow()
{
	try
	{
		if (BrowserDetect.browser == "Explorer"
			|| BrowserDetect.browser == "Firefox"
			|| BrowserDetect.browser == "Netscape")
		{
			window.focus();
			if (thisMovie(swfID) != null) 
			{
				thisMovie(swfID).focus();
				thisMovie(swfID).blur(); // HACK
				thisMovie(swfID).SetFocusInField();					
				thisMovie(swfID).focus(); // HACK
			}
		}
	}
	catch(exception)
	{
	}
}

function CloseWindow()
{
	close();
}

//-----------------------------------------------------------------
//-----------------------------------------------------------------
//--------------------------BROWSER DETECT-------------------------
//-----------------------------------------------------------------
//-----------------------------------------------------------------

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

