﻿var app_fbid = "";
var app_flashvars = "";
var api_key = "";
var app_title = "";
var app_source = "";
var canvas_url = "";
var iphone_link = "";
var require_permissions = "";

function init() {
    var currentDomain = document.domain.replace('www.', '');
    var fb_chrome = getQuerystring('fb_sig_in_iframe');

    var domainFound = false;
    for (var x in applications) {
        if (currentDomain == applications[x].Domain) {
            domainFound = true;
            api_key = applications[x].ApiKey;
            app_title = applications[x].Title;
            app_fbid = applications[x].FBID;
            app_source = applications[x].Source;
            iphone_link = applications[x].IphoneURL;
            require_permissions = applications[x].Perms;
            canvas_url = applications[x].CanvasURL;
            document.title = app_title;
            if (canvas_url != "" && fb_chrome != 1) {
                window.location.href = "http://apps.facebook.com/" + canvas_url;
            }
            else {
                FB_RequireFeatures(["Api"], setup);
                if (fb_chrome == 1) {
                    document.getElementById('FanBox').src = "http://www.facebook.com/connect/connect.php?connections=0&stream=0&id=" + app_fbid;
                }
                if (iphone_link != "") {
                    //document.getElementById('AppLinkDiv').innerHTML = "<a target='_new' href='" + iphone_link + "'>Download iPhone Version</a>";
                }
            }
        }
    }
    
    if (domainFound == false) {
        alert('Onoko App Runner encountered an error');
        window.location.href = "http://www.facebook.com";
    }
}
function setup() {
    FB.init(api_key, "xd_receiver.htm");
    api = FB.Facebook.apiClient;
    api.requireLogin(handleLogin);
}
function handleLogin() {
    runApplication();
}
function fbCookie(name) {
    var qsValue = getQuerystring('fb_sig_' + name, "");
    return getCookie(api_key + '_' + name, qsValue);
}
function runApplication() {
    var session_key = fbCookie('session_key');
    var ss = fbCookie('ss');
    var user = fbCookie('user');

    app_flashvars = "fb_sig_ss=" + ss + "&fb_sig_api_key=" + api_key + "&fb_sig_session_key=" + session_key + "&fb_sig_user=" + user + "&locale=" + getCountry() + "&app_id=" + app_fbid;

    var requiredMajorVersion = 9;
    var requiredMinorVersion = 0;
    var requiredRevision = 124;

    // Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
    var hasProductInstall = DetectFlashVer(6, 0, 65);

    // Version check based upon the values defined in globals
    var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

    if (hasProductInstall && !hasRequestedVersion) {
        // DO NOT MODIFY THE FOLLOWING FOUR LINES
        // Location visited after installation is complete if installation is required
        var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
        var MMredirectURL = window.location;
        document.title = document.title.slice(0, 47) + " - Flash Player Installation";
        var MMdoctitle = document.title;
        AC_FL_RunContent(
            "src", "playerProductInstall",
            "FlashVars", "MMredirectURL=" + MMredirectURL + '&MMplayerType=' + MMPlayerType + '&MMdoctitle=' + MMdoctitle + "",
            "width", "100%",
            "height", "100%",
            "align", "middle",
            "id", "FriendPlayer",
            "quality", "high",
            "bgcolor", "#000000",
            "name", "FriendPlayer",
            "allowScriptAccess", "sameDomain",
            "type", "application/x-shockwave-flash",
            "pluginspage", "http://www.adobe.com/go/getflashplayer"
        );
    } else if (hasRequestedVersion) {
        // if we've detected an acceptable version
        // embed the Flash Content SWF when all tests are passed
        AC_FL_RunContent(
            "src", app_source,
            "width", "100%",
            "height", "100%",
            "align", "middle",
            "id", "FriendPlayer",
            "quality", "high",
            "bgcolor", "#333333",
            "name", "FriendPlayer",
            "allowscriptaccess", "always",
            "type", "application/x-shockwave-flash",
            "pluginspage", "http://www.adobe.com/go/getflashplayer",
            "wmode", "transparent",
            "FlashVars", app_flashvars
        );
        document.body.style.margin = "0px";
        document.body.style.padding = "0px";
    } else {  // flash is too old or we can't detect the plugin
        var alternateContent = 'This application requires Adobe Flash Player. <a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
        document.write(alternateContent);  // insert non-flash content
    }
}

function getMyApp(appName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[appName];
    } else {
        return document[appName];
    }
}

function requirePermissions(perms) {
    FB.Connect.showPermissionDialog(perms, requirePermissionsCallback);
}
function requirePermissionsCallback(perms) {
    if (!perms) {
        getMyApp("FriendPlayer").requirePermissionsCallback("");
    }
    else {
        getMyApp("FriendPlayer").requirePermissionsCallback(perms);
    }
}
function streamPublish(title, caption, description, thumbnail, target, link, actionName, silentMode) {
    var attachment = { 'name': title, 'description': description, 'href': link, 'media': [{ 'type': 'image', 'src': thumbnail, 'href': link}] };
    var actionLinks = [{ "text": actionName, "href": link}];
    FB.Connect.streamPublish('', attachment, actionLinks, target, title, streamPublishCallback, silentMode);
}
function streamPublishCallback(post_id, exception) {
    if (post_id) {
        getMyApp("FriendPlayer").streamPublishCallback(post_id);
    }
    else {
        getMyApp("FriendPlayer").streamPublishCallback(0);
    }
}
function getCookie(c_name, default_) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return default_;
}
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
function showInterstitialAd() {
    window.location.href = "interstitial.htm";
    //alert('Interstitial Ad');
}
function refreshAds() {
    document.getElementById('HeaderAd').src = "ads/fb.htm?slot=siapp_top";
    //document.getElementById('FooterAd').src = "http://www.onokoads.com/google.htm?slot=siapp_bottom";
}
function displayAd(url) {
    if (url == "") {
        url = "http://www.socialinterview.com/ads/fb.htm?slot=siapp_top";
        parent.frames['FooterAd'].location.href = 'http://www.socialinterview.com/ads/fb.htm?slot=siapp_bottom';
    }
    else {
        url = "http://www.socialinterview.com/ads/as.htm?r=" + url;
        parent.frames['FooterAd'].location.href = 'http://www.socialinterview.com/blank.htm';
        //document.getElementById('FooterAd').src = 'blank.htm';
    }
    parent.frames['HeaderAd'].location.href = url;
    //document.getElementById('HeaderAd').src = url;
}
function getCountry() {
    try {
        return getQuerystring("fb_sig_country").toUpperCase();
    }
    catch (e) {
        return "UNKNOWN";
    }
}
function publishActivity(message, action_link_text, action_link_href) {
    var params = { "activity": { "message": message, "action_link": { "text": action_link_text, "href": action_link_href} }, "image": null };
    api.callMethod("Dashboard.publishActivity", params, function(result, exception) {});
}