﻿
function fblogout() {
    var divFBPic = document.getElementById("divFBPic");
    if (divFBPic != null) {
        divFBPic.innerHTML = "<fb:profile-pic size='square' uid='loggedinuser' facebook-logo='true'></fb:profile-pic>";
    }

    FB.Connect.logout();

    var spanLogout = document.getElementById("divLogout");
    if (spanLogout != null) {
        spanLogout.style.display = "none";
    }
}
function fbconnect_dff() {
    SetFBCInfoHTML_DFF("block")
}
function fbnotconnect_dff() {
    SetFBCInfoHTML_DFF("none");
}
function fbnotconnect() {
    SetFBCInfoHTML("none");
}
function fbconnect() {
    SetFBCInfoHTML("block");
}

function SetFBCInfoHTML_DFF(DisplayLogoutLink) {
    var spanInfo = document.getElementById("spanInfo");
    if (spanInfo != null) {
        spanInfo.innerHTML = "<strong>Share this review with your friends through Facebook.</strong> <img width='22' height='11' src='/fbconnect/new.gif'>";
    }

    var divFBPic = document.getElementById("divFBPic");
    if (divFBPic != null) {
        divFBPic.innerHTML = "<fb:profile-pic size='square' uid='loggedinuser' facebook-logo='true'></fb:profile-pic>";
    }

    var divPublishReview = document.getElementById("divPublishReview");
    if (divPublishReview != null) {
        divPublishReview.innerHTML = "<a href='#' onclick='javascript:PublishToFacebook(document.SearchForm.txtHid_FBC_ReviewTemplateID.value,getDFFTemplateData, \"FBPostReview\");'>Publish to Facebook</a>";
    }

    var divLogout = document.getElementById("divLogout");
    if (divLogout != null) {
        divLogout.style.display = DisplayLogoutLink;
    }
    //if we got to this point, FB init was successful and lets show the div
    if (document.getElementById("divShareFB") != null) {
        document.getElementById("divShareFB").style.display = "block";
    }

    FB.XFBML.Host.parseDomTree();
}

function SetFBCInfoHTML(DisplayLogoutLink) {
    var divNewText = document.getElementById("divNewText");
    if (divNewText != null) {
        divNewText.innerHTML = "<strong>Share your dining experience with your friends through Facebook <img WIDTH='22' HEIGHT='11' src='fbconnect/new.gif'></strong>";
    }

    var divFBPic = document.getElementById("divFBPic");
    if (divFBPic != null) {
        divFBPic.innerHTML = "<fb:profile-pic size='square' uid='loggedinuser' facebook-logo='true'></fb:profile-pic>";
    }

    var spanTxtInfo = document.getElementById("spanTxtInfo");
    if (spanTxtInfo != null) {
        var str = 'Now OpenTable makes it simple to post your upcoming reservation at ' + document.Form.txtHid_RestaurantName.value + ' and also to share your restaurant review through your Facebook profile. ';
        spanTxtInfo.innerHTML = str + "<a href='javascript:ShowLearnMore(true)'<span id='spanLearnMore' style='text-decoration: underline;'>Learn More</span> </a>";
    }

    var divPublish = document.getElementById("divPublish");
    if (divPublish != null) {
        divPublish.innerHTML = "<fb:login-button onlogin=\"PublishToFacebook(document.Form.txtHid_FBC_ResoTemplateID.value,getConfTemplateData,'FBPostReservation');\" size=\"large\" background=\"light\" length=\"long\"></fb:login-button>";
    }

    var divLogout = document.getElementById("divLogout");
    if (divLogout != null) {
        divLogout.style.display = DisplayLogoutLink;
    }

    //if we got to this point FB init was successful and we can show the appropriate divs
    if (document.getElementById("divFBconnectpanel") != null) {
        document.getElementById("divFBconnectpanel").style.display = "block";
    }

    if (document.getElementById("fb_separator") != null) {
        document.getElementById("fb_separator").style.display = "block";
    }

    FB.XFBML.Host.parseDomTree();
}

function launchLearnMoreWindow(sUrl) {
    openWindow(sUrl, 564, 530, 'DinersChoiceLearnMore', 0, 0);
}
//clean redundant code in array, just add the images portion if we need to
function getDFFTemplateData() {
    var jsonObj = { "post-title": document.SearchForm.txtHid_RestaurantName.value,
        "post-url": "http://www.opentable.com/rest_profile.aspx?rid=" + document.SearchForm.txtHid_RestaurantID.value + "&ref=5263",
        "post-url-mobile": "http://www.opentable.com/mobile",
        "post-url2": "http://www.opentable.com/rest_profile.aspx?rid=" + document.SearchForm.txtHid_RestaurantID.value + "&rnr=1&ref=5263",
        "opentable-url": "http://www.opentable.com/home.aspx?ref=5263",
        "category-text": document.SearchForm.txtHid_ReviewCategories.value,
        "overall-rating": document.SearchForm.txtHid_ReviewRatings.value,
        "review": document.SearchForm.txtHid_ReviewText.value
    };

    if (document.SearchForm.txtHidHasProfImage.value == "1")
        jsonObj.images = [{ "src": "http://www.opentable.com/img/restimages/" + document.SearchForm.txtHid_RestaurantID.value + ".jpg", "href": "http://www.opentable.com/rest_profile.aspx?rid=" + document.SearchForm.txtHid_RestaurantID.value + "&ref=5263"}];

    return jsonObj;
}
//clean redundant code in array, just add the images portion if we need to
function getConfTemplateData() {
    var jsonObj = { "post-title": document.Form.txtHid_RestaurantName.value,
        "post-url-mobile": "http://www.opentable.com/mobile",
        "post-url2": "http://www.opentable.com/rest_profile.aspx?rid=" + document.Form.txtHid_RestaurantID.value + "&ref=5062",
        "opentable-url": "http://www.opentable.com/home.aspx?ref=5062",
        "post-url": "http://www.opentable.com/rest_profile.aspx?rid=" + document.Form.txtHid_RestaurantID.value + "&rnr=1&ref=5062"
    };

    if (document.Form.txtHidHasProfImage.value == "1")
        jsonObj.images = [{ "src": "http://www.opentable.com/img/restimages/" + document.Form.txtHid_RestaurantID.value + ".jpg", "href": "http://www.opentable.com/rest_profile.aspx?rid=" + document.Form.txtHid_RestaurantID.value + "&ref=5062"}];

    return jsonObj;
}

function ShowLearnMore(close) {
    var divLearnMorePopup = document.getElementById("LearnMorePopup");

    if (close)
        divLearnMorePopup.style.visibility = "visible";
    else
        divLearnMorePopup.style.visibility = "hidden";
}

function PublishToFacebook(templateID, getTemplateFunc, propValue) {
    FB.Connect.showFeedDialog(templateID, getTemplateFunc(), null, null, null, FB.RequireConnect.promptConnect, null);

    FBButtonTrack(propValue);
}

function FBButtonTrack(propValue) {
    var sObj = s_gi(s_account);
    sObj.linkTrackVars = 'prop41';
    sObj.linkTrackEvents = 'None';
    sObj.prop41 = propValue;
    sObj.tl(true, 'o', propValue);
}

function LogVisitorOverlapMetric() {
    FB.Connect.get_status().waitUntilReady(function(status) {
        switch (status) {
            case FB.ConnectState.connected:
                ottrack('FB_Connected', '72942')
                break;
            case FB.ConnectState.appNotAuthorized:
                ottrack('FB_Not_Authorized', '72943');
                break;
            case FB.ConnectState.userNotLoggedIn:
                break;
        }
    });
}