var OAuth = {}, oauthWin, swfId, locUrl, tokenSent;
tokenSent = false;

OAuth.setFlashElement = function (id) {
    swfId = id;
};
 
OAuth.openLoginLoader = function (url) {
     oauthWin = window.open(url, "oauthWin", "width=780, height=500");
     OAuth.urlCheckDelay();
}
 
OAuth.urlCheckDelay = function() {
    setTimeout('OAuth.checkUrl()',1000);
}

checkOAuthWindowClosing = function() {
        if(!oauthWin.closed) {
            setTimeout(checkOAuthWindowClosing,2000);
        } else {
            if (!swfId) {
                       alert("You never called OAuth.setFlashElement!");
                }
                else {
                       flash = document.getElementById(swfId);
                       if (!tokenSent) {
                            flash.windowClosed();
                       }
                }
        }

}

OAuth.checkUrl = function() {
    if (!swfId) { alert("You never called OAuth.setFlashElement!"); }
    else {
       flash = document.getElementById(swfId);
       locUrl = flash.getLocationUrl();
        if(locUrl) {
            oauthWin.location = locUrl;
            setTimeout(checkOAuthWindowClosing,2000);
        } else {
            OAuth.urlCheckDelay();
        }
    }
}
 
OAuth.callAuthorize = function (url) {
  oauthWin.location = url;
};
 
 
OAuth.verify = function () {
 
    var href, searchString, startIndex, sSLength, verifierToken;
    href = window.location.href;
    searchString = "oauth_verifier=";
    sSLength = searchString.length;
    startIndex = href.indexOf(searchString);
    verifierToken = href.substr(startIndex+sSLength, href.length);
    tokenSent = true;
    window.opener.OAuth.passVerifier(verifierToken);
    window.close();
};

OAuth.passVerifier = function (token) {
    if (!swfId) { alert("You never called OAuth.setFlashElement!"); }
    else {
        tokenSent = true;
        flash = document.getElementById(swfId);
        flash.setVerifier(token);
    }
};

