﻿// JScript File
var ChatWindow = function(id, target, sGroup, config) {
    if (id) {
        this.init(id, sGroup, config);
        this.target = target;
        this.isOver = false;
        
        /* Start Building Window Contents */
        
        // Header div
        var headerDiv = document.createElement("div");
        headerDiv.style.height="20px";
        headerDiv.style.width = "100%";
        headerDiv.style.backgroundColor="Blue";
		headerDiv.style.color="#FFFFFF";
        headerDiv.style.cursor = "move";
        
        // Create title element
        var titleDiv = document.createElement("div");
        titleDiv.style.cssFloat = "left";
        titleDiv.style.styleFloat = "left";
        titleDiv.style.fontWeight = "bold";
        titleDiv.style.paddingLeft = "3px";
        titleDiv.style.paddingTop = "1px";
		titleDiv.style.fontSize = "14px";
        titleDiv.appendChild(document.createTextNode("Private Chat With " + userList[target].UserName));
        
        // Create close button
        var closeButton = document.createElement("div");
        closeButton.onclick = function()
        {
            document.getElementById("panel_" + target).parentNode.removeChild(document.getElementById("panel_" + target));
        }
        closeButton.appendChild(document.createTextNode("X"));
        closeButton.style.cssFloat = "right";
        closeButton.style.fontWeight = "bold";
        closeButton.style.paddingRight = "3px";
        closeButton.style.paddingTop = "1px";
        closeButton.style.styleFloat = "right";
        closeButton.style.cursor = "pointer";
        
        // Append Elements to header div
        headerDiv.appendChild(titleDiv);
        headerDiv.appendChild(closeButton);
        
        // Add header div
        this.getEl().appendChild(headerDiv);
        
        // Create chat window
        var chatDiv = document.createElement("div");
        chatDiv.className = "PrivScrollDiv WindowStyle";
        chatDiv.id = "contents_" + target;
        
        // Append chat window
        this.getEl().appendChild(chatDiv);
        
        // Add input box
        var inputBox = document.createElement("input");
        inputBox.type = "text";
        inputBox.id = "input_" + target;
        inputBox.className = "pmessage WindowStyle";
        inputBox.onkeydown = function (e)
        {
			var charCode;
			if(e && e.which)
				charCode = e.which
			else
			{
				e = window.event;
				charCode = e.keyCode;
			}
	        if (charCode == 13)
                SendPrivateMessage(target);
        }
        
        // Append input box
        this.getEl().appendChild(inputBox);
        
        // Add send button
        var sendBtn = document.createElement("button");
        sendBtn.id = "sendbtn_" + target;
        sendBtn.className = "sendBtn";
        sendBtn.appendChild(document.createTextNode("Send"));
        sendBtn.onclick = function()
        {
            SendPrivateMessage(target);
        }
        
        this.getEl().appendChild(sendBtn);
        
        var scrollX = (window.ActiveXObject ? document.documentElement.scrollLeft : window.pageXOffset);
        var scrollY = (window.ActiveXObject ? document.documentElement.scrollTop : window.pageYOffset);
        var winX = (window.ActiveXObject ? document.documentElement.clientWidth : window.innerWidth) + scrollX;
        var winY = (window.ActiveXObject ? document.documentElement.clientHeight : window.innerHeight) + scrollY;
        this.getEl().style.top = (scrollY + Math.floor(Math.random()*(winY-300-scrollY))) + "px";
        this.getEl().style.left = (scrollX + Math.floor(Math.random()*(winX-340-scrollX))) + "px";
        //this.getEl().style.left = ((window.ActiveXObject ? document.documentElement.scrollLeft : window.pageXOffset) + Math.floor(Math.random()*((window.ActiveXObject ? window.innerHeight : document.documentElement.clientHeight)-330-(window.ActiveXObject ? document.documentElement.scrollLeft : window.pageXOffset)))) + "px";
        var style = this.getEl().style;

        // The z-index needs to be set very high so the element will indeed be on top
        z = z+1;
        style.zIndex = z;
    }
};

var VideoWindow = function(sGroup, config) {
    this.init("VideoWindow", sGroup, config);
    this.target = target;
    this.isOver = false;
    
    /* Start Building Window Contents */
    
    // Header div
    var headerDiv = document.createElement("div");
    headerDiv.style.height="17px";
    headerDiv.style.width = "100%";
    headerDiv.style.backgroundColor="Blue";
	headerDiv.style.color="#FFFFFF";
    headerDiv.style.cursor = "move";
    headerDiv.id = "VideoWindowHandle";
    
    // Create title element
    var titleDiv = document.createElement("div");
    titleDiv.style.cssFloat = "left";
    titleDiv.style.styleFloat = "left";
    titleDiv.style.fontWeight = "bold";
    titleDiv.style.paddingLeft = "3px";
    titleDiv.style.paddingTop = "1px";
	titleDiv.style.fontSize = "12px";
    titleDiv.appendChild(document.createTextNode("Your Video" ));
    
    // Create close button
    var closeButton = document.createElement("div");
    closeButton.onclick = function()
    {
        //document.getElementById("VideoWindow").parentNode.removeChild(document.getElementById("VideoWindow"));
        DisableWebcam();
    }
    closeButton.appendChild(document.createTextNode("X"));
    closeButton.style.cssFloat = "right";
    closeButton.style.fontWeight = "bold";
    closeButton.style.paddingRight = "3px";
    closeButton.style.paddingTop = "1px";
    closeButton.style.styleFloat = "right";
    closeButton.style.cursor = "pointer";
    
    // Append Elements to header div
    headerDiv.appendChild(titleDiv);
    headerDiv.appendChild(closeButton);
    
    // Add header div
    this.getEl().appendChild(headerDiv);
    
    // Create chat window
    var chatDiv = document.createElement("div");
    chatDiv.className = "PrivScrollDiv WindowStyle";
    chatDiv.id = "WebcamControl";
    
    // Append chat window
    this.getEl().appendChild(chatDiv);
    
    this.getEl().style.top = "42px";
    this.getEl().style.left = "380px";
    var style = this.getEl().style;

    // The z-index needs to be set very high so the element will indeed be on top
    z = z+1;
    style.zIndex = z;
    
    // embed webcam control
	var s1 = new SWFObject("flash/player.swf","ply","220","140","9","#FFFFFF");
	s1.addParam("flashvars", "autostart=true&bufferlength=2&streamer=rtmp://" + ServerHost + "/videochat&file=" + MyGuid.replace(/[^a-zA-Z 0-9]+/g,'') + "&type=camera");
	s1.addParam("allowfullscreen","false");
	s1.addParam("wmode", "transparent");
	s1.addParam("allowscriptaccess","always");
	s1.write("WebcamControl");
	
	this.setHandleElId("VideoWindowHandle");
};

// YAHOO.example.DDOnTop.prototype = new YAHOO.util.DD();
YAHOO.extend(ChatWindow, YAHOO.util.DD);
YAHOO.extend(VideoWindow, YAHOO.util.DD);

/**
 * The inital z-index of the element, stored so we can restore it later
 *
 * @type int
 */
var z = 100;
ChatWindow.prototype.onMouseDown = function(x, y) {

    var style = this.getEl().style;

    // The z-index needs to be set very high so the element will indeed be on top
    z = z+1;
    style.zIndex = z;
};
VideoWindow.prototype.onMouseDown = function(x, y) {

    var style = this.getEl().style;

    // The z-index needs to be set very high so the element will indeed be on top
    z = z+1;
    style.zIndex = z;
};

ChatWindow.prototype.onMouseUp = function()
{
    document.getElementById("input_" + this.target).focus();
}