﻿function changeEmbedSize(parameter) {
    var theWrapper = $('#tinyChatControl .wrapper');
    if (parameter == 0) {
        theWrapper.css('height', '450px');
    }
    else if (parameter == 1 || parameter == 2) {
        theWrapper.css('height', '660px');
    }
    else if (parameter == 3) {
        theWrapper.css('height', '600px');
    }
    else if (parameter == 4) {
        theWrapper.css('height', '560px');
    }
    else if (parameter >= 5 && parameter <= 8) {
        theWrapper.css('height', '680px');
    }
    else if (parameter >= 8 && parameter <= 12) {
        theWrapper.css('height', '810px');
    }
    else if (parameter >= 5) {
        var height = 680;
        var divi = ((parameter - 5) / 4);
        height = height + (120 * (divi));
        theWrapper.css('height', height + "px");
    }
}

function do_sc(user, uid, roomVar) {
    var un = user;
    var id = uid;
    var room = roomVar;
    var roompass = 'hi';

    var cap = document.createElement("div");
    cap.id = "cap";
    var b = document.getElementsByTagName("body");
    b[0].appendChild(cap);

    cap.innerHTML = "<applet archive='http://tinychat.com/ScreenCap.jar' code='TinyChat.Main.class' width='0' height='0' name='TinyChat ScreenCap' MAYSCRIPT>" +
	"<param name='username' value='" + un + "'/>" +
	"<param name='userid' value='" + id + "'/>" +
	"<param name='roomname' value='" + room + "'/>" +
	"<param name='roompass' value='" + roompass + "'/>" +
	"<param name='postdomain' value='upload.tinychat.com'/>" +
	"<param name='postlocation' value='/sc'/>" +
	"<param name='capturefps' value='1'/>" +
	"<param name='java_arguments' value='-d32'/>" +
	"<param name='savetofile' value='false'/>" +
	"</applet>";
}

function undo_sc(user, uid, roomVar) {
    var cap = document.getElementById("cap");
    if (cap) cap.innerHTML = '';
}

function fbPop(fburl) {
    var fb = document.createElement("div");
    fb.id = 'fbframediv';
    fb.style.display = 'none';
    fb.innerHTML = '<div style="width: 100%; background: #666666; text-align: right; float: top; font-family: Arial, Helvetica, sans-serif;"><a style="padding: 3px; decoration: none; color: #efefef;" href="#" onClick="fbHide(); return false;">X</a><iframe width="100%" height="450" id="fbframe" frameborder="0"></iframe>';
    var b = document.getElementsByTagName("body");
    b[0].insertBefore(fb, b[0].firstChild);

    var fb = document.getElementById("fbframe");
    fb.src = fburl;
    var fbd = document.getElementById("fbframediv");
    fbd.style.display = 'block';
    if (window.postMessage) {   // newer browsers
        window.addEventListener("message", function(e) {  // e.data, e.origin, e.source
            if (e.origin.search("^http://tinychat.com") == -1) return;
            var args = e.data.split('&');
            if (args.length == 3) {
                fbHide(args[0], args[1], args[2]);
            } else {
                fbHide();
            }
        }, false);
    } else {
        (function() {    // hacky stuff
            var cnt = 2 * 60 * 10; // 10mins
            var timer = setInterval(function() {
                var fb = document.getElementById("fbframe").contentWindow;
                if (cnt--) {
                    try {
                        var args = document.location.substring(1).split('&');
                        if (args.length == 3) {
                            clearInterval(timer);
                            fbHide(args[0], args[1], args[2]);
                        } else {
                            if (document.location.search(/fbfail/)) fbHide();
                        }
                    } catch (x) {
                        //						   console.log ( x );
                    }
                } else {
                    clearInterval(timer);
                }
            }, 1000);

        })();
    }
}

var objects = document.getElementsByTagName("object");
for (var i = 0; i < objects.length; i++) {
    objects[i].outerHTML = objects[i].outerHTML;
}

function fbHide(n, p, pe) {
    var fbd = document.getElementById("fbframediv");
    fbd.style.display = 'none';
    var name = unescape(n);
    var pic = unescape(p);
    var perms = unescape(pe);
    try {
        var fm = navigator.appName.indexOf("Microsoft") != -1 ? window['tinyembed'] :
	    document['tinyembed'];
        fm.toFlash(name, pic, perms);
    } catch (x) {
        console.log(x);
    }
}

var tinychat = function() {
    var flashvars = { roomVar: tinychat.room, embedMode: "true", pageVar: document.location, pageVarSuper: tinychat.urlsuper, askJoin: tinychat.join, nickVar: tinychat.nick, noOper: tinychat.oper, noOwner: tinychat.owner, nickChange: tinychat.change, loginVar: tinychat.login, bcastVar: tinychat.bcast, apiVar: tinychat.api, autoop: tinychat.autoop, colorVar: tinychat.colorbk, fbautoVar: tinychat.fbauto, noguestVar: tinychat.noguest, topicVar: tinychat.topic, extDomain: document.domain, logoVar: tinychat.logo, proSiteVar: tinychat.site, keyVar: tinychat.key, vidVar: tinychat.tcdisplay };
    var params = { quality: "high", scale: "noscale", allowFullScreen: "true", wmode: "transparent", allowScriptAccess: "always", menu: "false", salign: "t" };
    var attributes = { id: "tinyembed", align: "top", width: "100%", height: "100%" };
    swfobject.embedSWF("http://tinychat.com/tinychat.swf?" + Math.random(), "client","100%", "100%","9.0.0", "expressInstall.swf",flashvars, params, attributes);
} ();
