﻿
function walkTheDOM(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {
        walkTheDOM(node, func);
        node = node.nextSibling;
    }

}

function showloggedout() {
    walkTheDOM(document, function(node) {
        if (node.attributes && node.attributes['loginstatus']) {
            if (node.attributes['loginstatus'].value == 'loggedin') {
                node.style.display = 'none';
            }
            if (node.attributes['loginstatus'].value == 'loggedout') {
                node.style.display = '';
            }
        }
    });
}
function showloggedin() {
    walkTheDOM(document, function(node) {
        if (node.attributes && node.attributes['loginstatus']) {
            if (node.attributes['loginstatus'].value == 'loggedin') {
                node.style.display = '';
            }
            if (node.attributes['loginstatus'].value == 'loggedout') {
                node.style.display = 'none';
            }
        }
    });
}




function MyDialog(titletext, contentelement) {
    var _titletext = titletext;
    //var _contenttext = contenttext;
    var _contentelement = contentelement;
    var thisObj = this;
    this.oncancel = null;


    var table = document.createElement("table");

    function popup(time) {
        var tbody = document.createElement("tbody");

        table.style.display = "none";
        table.style.opacity = 0;
        table.style.left = "auto";
        table.style.top = "auto";
        table.style.bottom = "-11px";
        table.style.right = "20px";
        table.style.position = "fixed";
        table.className = "my_pop_dialog_table my_popup";
        table.appendChild(tbody);


        var tr1 = document.createElement("tr");
        var tr1td1 = document.createElement("td");
        var tr1td2 = document.createElement("td");
        var tr1td3 = document.createElement("td");
        tr1td1.className = "my_pop_topleft";
        tr1td2.className = "my_pop_border";
        tr1td3.className = "my_pop_topright";
        tr1.appendChild(tr1td1);
        tr1.appendChild(tr1td2);
        tr1.appendChild(tr1td3);

        tbody.appendChild(tr1);



        var tr2 = document.createElement("tr");
        var tr2td1 = document.createElement("td");
        var tr2td2 = document.createElement("td");
        var tr2td3 = document.createElement("td");
        tr2td1.className = "my_pop_border";
        tr2td2.className = "my_pop_content";
        tr2td3.className = "my_pop_border";
        tr2.appendChild(tr2td1);
        tr2.appendChild(tr2td2);
        tr2.appendChild(tr2td3);


        var container = document.createElement("div");
        container.className = "my_pop_content_container";

        var header = document.createElement("h2");
        var title = document.createElement("span");
        var xbutton = document.createElement("a");
        var content = document.createElement("div");
        title.className = "my_dialog_header";
        xbutton.className = "my_dialog_cancel_button";
        xbutton.innerHTML = "&nbsp;"
        xbutton.onclick = function() { thisObj.close(time); if (thisObj.oncancel) thisObj.oncancel(); };

        content.className = "my_dialog_content";
        header.appendChild(title);
        header.appendChild(xbutton);

        container.appendChild(header);
        container.appendChild(content);
        tr2td2.appendChild(container);


        tbody.appendChild(tr2);




        var tr3 = document.createElement("tr");
        var tr3td1 = document.createElement("td");
        var tr3td2 = document.createElement("td");
        var tr3td3 = document.createElement("td");
        tr3td1.className = "my_pop_bottomleft";
        tr3td2.className = "my_pop_border";
        tr3td3.className = "my_pop_bottomright";
        tr3.appendChild(tr3td1);
        tr3.appendChild(tr3td2);
        tr3.appendChild(tr3td3);

        tbody.appendChild(tr3);




        title.innerHTML = _titletext;
        if (typeof _contentelement === "object")
            content.appendChild(_contentelement);
        else
            content.innerHTML = _contentelement;


        document.getElementsByTagName("body")[0].appendChild(table);
    }

    function fadein(opacity) {
        var mo = opacity / 100;
        var oo = parseFloat(table.style.opacity);
        if (oo < 1) {
            var no = oo + mo;
            if (no > 1) no = 1;
            table.style.opacity = no;
            setTimeout(function() { fadein(opacity); }, 10);
        }
    }

    function fadeout(opacity) {
        var mo = opacity / 100;
        var oo = parseFloat(table.style.opacity);
        if (oo > 0) {
            var no = oo - mo;
            if (no < 0) {
                table.parentNode.removeChild(table);
                //no = 0;
            } else {
                table.style.opacity = no;
                setTimeout(function() { fadeout(opacity); }, 10);
            }
        }
    }

    this.get_domElement = function() {
        return table;
    }

    this.show = function(time) {
        popup(time);
        table.style.display = "";
        if (time) {
            var steps = time / 10;
            var opacitysteps = 100 / steps;

            setTimeout(function() { fadein(opacitysteps); }, 0);


        } else {
            table.style.opacity = 1;
        }
    }



    this.close = function(time) {
        if (time) {
            var steps = time / 10;
            var opacitysteps = 100 / steps;
            setTimeout(function() { fadeout(opacitysteps); }, 0);


        } else {
            table.parentNode.removeChild(table);
        }

    }

};


function publishFeed(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback) {
        FB.ensureInit(function() {
        FB.Connect.get_status().waitUntilReady(function() {
                var a = showFeedDialog(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback);
                if (a) {
                    a.set_placement(FB.UI.PopupPlacement.hidden);
                    a.sizing();
                    setTimeout(function() {
                        a.set_placement(FB.UI.PopupPlacement.hidden);
                        a.sizing();
                    }, 100);

                    var e = document.createElement("div");
                    var text = document.createElement("div");
                    var div = document.createElement("div");
                    var show = document.createElement("input");
                    var cancel = document.createElement("input");

                    e.style.padding = "5px";
                    e.style.height = "20px";

                    text.innerHTML = "You have a social feed to publish in Facebook";

                    div.style.float = "right";
                    div.style.styleFloat = "right";
                    div.style.cssFloat = "right";

                    show.className = "inputbutton";
                    show.type = "button";
                    show.value = "Show me!";
                    cancel.style.marginLeft = "10px";
                    cancel.className = "inputbutton inputaux";
                    cancel.type = "button";
                    cancel.value = "No Thanks";

                    div.appendChild(show);
                    div.appendChild(cancel);
                    e.appendChild(div);
                    e.appendChild(text);


                    var dialog = new MyDialog("Publish Facebook Feed?", e)
                    dialog.oncancel = function() {
                        a.close();
                    }
                    dialog.show(300);

                    show.onclick = function() {
                        a.set_placement(FB.UI.PopupPlacement.topCenter);
                        a.sizing();
                        dialog.close(100);
                    };
                    cancel.onclick = function() {
                        dialog.close(100);
                        dialog.oncancel();
                    };

                } else {
                }
            });
        });

}


function showFeedDialog(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback) {
    var singleton = FB.Connect._singleton;
    if (singleton._feedformDialog) {
        if (callback) {
            callback.invoke();
        }
        return undefined;
    }
    if (singleton._feedStatusMustBeRefetched) {
        singleton._dialogInfo = FB.$create_FeedDialogInfo(template_bundle_id, template_data, target_id, body_general, story_size, callback);
        singleton._feedStatusRefetch();
        FB.Connect.requireSession(null);
        return singleton._feedformDialog;
    }
    FB.Connect.get_status().waitUntilReady(Delegate.create(null, function(result) {
        var login_state = result;
        if (require_connect === FB.RequireConnect.promptConnect && login_state !== FB.ConnectState.connected) {
            singleton._dialogInfo = FB.$create_FeedDialogInfo(template_bundle_id, template_data, target_id, body_general, story_size, callback);
            FB.Connect.requireSession(null);
        } else if (require_connect === FB.RequireConnect.require && login_state !== FB.ConnectState.connected) {
            if (callback) {
                callback.invoke();
            }
        } else if (login_state !== FB.ConnectState.userNotLoggedIn) {
            var story_setting;
            var targetFeed = false;
            if (target_id && target_id.length > 0) {
                targetFeed = true;
                story_setting = FB.FeedStorySetting.preview;
            } else if (story_size === FB.FeedStorySize.oneLine) {
                story_setting = singleton.get__userInfo().oneLineStorySetting;
            } else if (story_size === FB.FeedStorySize.full) {
                story_setting = singleton.get__userInfo().fullStorySetting;
            } else {
                story_setting = singleton.get__userInfo().shortStorySetting;
            } if (story_setting === FB.FeedStorySetting.doNotSend) {
                if (callback) {
                    callback.invoke();
                }
            } else if (story_setting === FB.FeedStorySetting.autoaccept) {
                FB.Connect._sessionlessPublishUserAction(template_bundle_id, template_data, target_id, body_general, story_size, callback);
            } else {
                FB.IFrameUtil.CanvasUtilServer.run(false);
                var feedInfo = { template_id: template_bundle_id.toString() };
                if (template_data) {
                    feedInfo.template_data = template_data;
                } if (body_general) {
                    feedInfo.body_general = body_general;
                }
                singleton._feedformNum++;
                var iframeName = "fb_feedIFrame_" + singleton._feedformNum;
                var switchUserHandlerName = "switchUser";
                if (!FB.XdComm.Server.singleton.isDataHandlerRegistered(switchUserHandlerName)) {
                    FB.XdComm.Server.singleton.registerDataHandler(switchUserHandlerName, Delegate.create(null, function(data, endpoint) {
                        singleton.switchUser();
                    }));
                }
                var switchUserUrl = FB.XdComm.Server.singleton.createUdpUrl(switchUserHandlerName, null, new (FB.XdComm.EndPoint)(null, FB.XdComm.PageRelation.self, FB.XdComm.Server.singleton.get_receiverUrl()));
                var iframe = FB.XdComm.Server.singleton.createNamedHiddenIFrame(iframeName, FB.XdComm.Server.singleton.get_receiverUrl(), "fb_feedIFrame", "frameborder=\"0\"");
                var dialogTitle;
                if (targetFeed) {
                    dialogTitle = "Publish this story to your friend's Facebook profile?";
                } else {
                    dialogTitle = "Publish this story to your Facebook profile?";
                }
                singleton._feedformDialog = new (FB.UI.PopupDialog)(dialogTitle, iframe, true, true);
                singleton._feedformDialog.set_placement(FB.UI.PopupPlacement.topCenter);
                singleton._feedformDialog.add_closing(Delegate.create(null, function(closingResult) {
                    singleton._feedformDialog = null;
                    if (callback) {
                        callback.invoke();
                    }
                }));
                singleton._feedformDialog.setContentWidth(600);
                singleton._feedformDialog.show();
                var parameters = { preview: "true", feed_info: FB.JSON.serialize(feedInfo), feed_target_type: targetFeed ? "target_feed" : "self_feed", api_key: FB.Facebook.apiKey, callback: singleton._feedformDialog._createCrossDomainClosingLink(false), switch_user_url: switchUserUrl, channel_url: FB.XdComm.Server.singleton.get_receiverUrl() };
                if (login_state === FB.ConnectState.connected) {
                    parameters.session_key = FB.Facebook.apiClient.get_session().session_key;
                }
                if (target_id) {
                    parameters.to_ids = FB.JSON.serialize(target_id);
                }
                if (story_size > 0) {
                    parameters.size = story_size.toString();
                }
                FBIntern.Utility.getIFrameDocument(iframeName, iframe, Delegate.create(null, function(iframeDoc) {
                    singleton._facebookIFrame(iframeDoc, "connect/prompt_feed.php", parameters);
                }));
            }
        } else {
            if (callback) {
                callback.invoke();
            }
        }
    }));


    return singleton._feedformDialog;
}
       


function publishFeed2(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback) {

    var mousetimeout = null;

    function fadein(e, opacity) {
        var mo = opacity / 100;
        var oo = parseFloat(e.style.opacity);
        if (oo < 1) {
            var no = oo + mo;
            if (no > 1) no = 1;
            e.style.opacity = no;
            setTimeout(function() { fadein(e, opacity); }, 10);
        }
    }

    function fadeout(e, opacity) {
        var mo = opacity / 100;
        var oo = parseFloat(e.style.opacity);
        if (oo > 0) {
            var no = oo - mo;
            if (no < 0) {
                table.parentNode.removeChild(table);
                //no = 0;
            } else {
                e.style.opacity = no;
                setTimeout(function() { fadeout(e, opacity); }, 10);
            }
        }
    }

   function scroll(e, pixel) {
        var mo = pixel;
        var oo = parseInt(e.style.bottom, 10);
        
        if (oo > (-e.clientHeight + 42)) {
            var no = oo - mo;
            if (no < (-e.clientHeight + 42)) no = (-e.clientHeight + 42);
            e.style.bottom = no + "px";
            setTimeout(function() { scroll(e, pixel); }, 10);
        }
    }

    function reposition(e, oldheight) {
        try {
            if (e.style.top != "auto") {
                popup.style.position = "fixed";
                e.style.left = "auto";
                e.style.top = "auto";

                //if ((-e.clientHeight + 42) != lastbottomposition) {
                    //lastbottomposition = (-e.clientHeight + 42);
                //}

                e.style.bottom = (-e.clientHeight + 42) + "px";
            }
            else {
                setTimeout(function() { reposition(e, oldheight); }, 10);
            }
            
            if (e.clientHeight == oldheight) {
                //var newheight = e.clientHeight;
                //setTimeout(function() { reposition(e, newheight); }, 10);
            } else {
                //var steps = 300 / 10;
                //var opacitysteps = 100 / steps;

                //setTimeout(function() { reposition(e, newheight); }, 10);

                //setTimeout(function() { fadein(e, opacitysteps); }, 0);

                //setTimeout(function() { scroll(e, 10); }, 1000);

            }
            
        } catch (e) {
        }


    }

    FB_RequireFeatures(["XFBML"], function() {
        FB.ensureInit(function() {
            var e = showFeedDialog(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback);
            if (e) {
                var popup = e.get_domElement();

                //popup.parentNode.style.display = "none";
                popup.style.position = "fixed";
                popup.style.left = "auto";
                popup.style.top = "auto";
                popup.style.right = "20px";
                popup.style.bottom = "-11px"; //(-popup.parentNode.clientHeight + 42) + "px";
                popup.style.opacity = 0;

                var steps = 300 / 10;
                var opacitysteps = 100 / steps;

                setTimeout(function() { fadein(popup, opacitysteps); }, 0);
                setTimeout(function() { scroll(popup, 10); }, 1000);

                reposition(popup, popup.clientHeight);

                popup.onmouseover = function() {
                    if (mousetimeout) {
                        clearTimeout(mousetimeout);
                    }

                    mousetimeout = setTimeout(function() {
                        popup.style.bottom = "-11px";
                    }, 300);
                };
                popup.onmouseout = function() {
                    if (mousetimeout) {
                        clearTimeout(mousetimeout);
                    }
                    mousetimeout = setTimeout(function() {
                        popup.style.bottom = (-popup.clientHeight + 42) + "px";
                    }, 300);
                }

            }

            //var frame = document.getElementById("fb_dialog_content").childNodes[0];
            //frame.onload = function() { alert(frame.contentWindow.location); };

            //setTimeout(function() { alert(document.getElementById("fb_feedIFrame_1")); }, 10000);


            //setTimeout(function() { for (var a in window.frames["fb_feedIFrame_1"]) { alert(a); }; }, 10000);

            //window.frames["fb_feedIFrame_1"].onload = function() { alert('a'); }

            //setTimeout(function() { alert(window.frames["fb_feedIFrame_1"].location); }, 10000);

        });
    });
}








function publishFeed2(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback) {

    var mousetimeout = null;

    function fadein(e, opacity) {
        var mo = opacity / 100;
        var oo = parseFloat(e.style.opacity);
        if (oo < 1) {
            var no = oo + mo;
            if (no > 1) no = 1;
            e.style.opacity = no;
            setTimeout(function() { fadein(e, opacity); }, 10);
        }
    }

    function fadeout(e, opacity) {
        var mo = opacity / 100;
        var oo = parseFloat(e.style.opacity);
        if (oo > 0) {
            var no = oo - mo;
            if (no < 0) {
                table.parentNode.removeChild(table);
                //no = 0;
            } else {
                e.style.opacity = no;
                setTimeout(function() { fadeout(e, opacity); }, 10);
            }
        }
    }

    function scroll(e, pixel) {
        var mo = pixel;
        var oo = parseInt(e.style.bottom, 10);

        if (oo > (-e.clientHeight + 42)) {
            var no = oo - mo;
            if (no < (-e.clientHeight + 42)) no = (-e.clientHeight + 42);
            e.style.bottom = no + "px";
            setTimeout(function() { scroll(e, pixel); }, 10);
        }
    }

    function reposition(e, oldheight) {
        try {
            if (e.clientHeight == oldheight) {
                var newheight = e.clientHeight;
                setTimeout(function() { reposition(e, newheight); }, 10);
            } else {
                //e.style.bottom = (-e.clientHeight + 42) + "px";
                var steps = 300 / 10;
                var opacitysteps = 100 / steps;

                setTimeout(function() { fadein(e, opacitysteps); }, 0);

                setTimeout(function() { scroll(e, 10); }, 1000);

            }
        } catch (e) {
        }


    }

    function findpopup(func) {
        var popup = document.getElementById("RES_ID_fb_pop_dialog_table");
        if (!popup) {
            setTimeout(function() { findpopup(func); }, 10);
        } else {
            func(popup);
        }
    }


    FB_RequireFeatures(["XFBML"], function() {
        FB.ensureInit(function() {
            if (FB.Connect.showFeedDialog(template_bundle_id, template_data, target_id, body_general, story_size, require_connect, callback)) {
                findpopup(function(popup) {
                    popup.parentNode.style.display = "none";
                    var frame = document.getElementById("fb_dialog_content").childNodes[0];
                    frame.onload = function() {
                        popup.parentNode.style.display = "";
                        popup.parentNode.style.position = "fixed";
                        popup.style.position = "static";
                        popup.style.left = "auto";
                        popup.style.top = "auto";
                        popup.style.right = "0px";
                        popup.style.bottom = "0px";
                        popup.parentNode.style.left = "auto";
                        popup.parentNode.style.top = "auto";
                        popup.parentNode.style.right = "20px";
                        popup.parentNode.style.bottom = "-11px"; //(-popup.parentNode.clientHeight + 42) + "px";
                        popup.parentNode.style.opacity = 0;


                        //popup.parentNode.style.overflow = "hidden";
                        //popup.parentNode.style.width = "400px";
                        //popup.parentNode.style.height = "200px";
                        //popup.parentNode.style.background = "#ff0000";

                        reposition(popup.parentNode, popup.parentNode.clientHeight);

                        popup.parentNode.onmouseover = function() {
                            if (mousetimeout) {
                                clearTimeout(mousetimeout);
                            }
                            mousetimeout = setTimeout(function() {
                                popup.parentNode.style.bottom = "-11px";
                            }, 300);
                        };
                        popup.parentNode.onmouseout = function() {
                            if (mousetimeout) {
                                clearTimeout(mousetimeout);
                            }
                            mousetimeout = setTimeout(function() {
                                popup.parentNode.style.bottom = (-popup.parentNode.clientHeight + 42) + "px";
                            }, 300);


                        };
                    };
                });
            }

            //var frame = document.getElementById("fb_dialog_content").childNodes[0];
            //frame.onload = function() { alert(frame.contentWindow.location); };

            //setTimeout(function() { alert(document.getElementById("fb_feedIFrame_1")); }, 10000);


            //setTimeout(function() { for (var a in window.frames["fb_feedIFrame_1"]) { alert(a); }; }, 10000);

            //window.frames["fb_feedIFrame_1"].onload = function() { alert('a'); }

            //setTimeout(function() { alert(window.frames["fb_feedIFrame_1"].location); }, 10000);

        });
    });
}





