var help_contents;
var help_container;
var onhome;

function displayWelcome()
{
    var cookie = YAHOO.util.Cookie.get("stampsconnect")
    onhome = true;
    //if( true )
    if( cookie != "welcome" )
    {
        YAHOO.util.Cookie.set("stampsconnect", "welcome", {
            expires: new Date("January 12, 2025")
        });
        displayHelp();
    }
		
    return false;
}

function displayHelp() {
    if (!YAHOO.lang.isObject(help_container))
    {
        initHelpContainer();
    }

    if (!YAHOO.lang.isObject(help_contents))
    {
        getHelpContents();
    } else {
        showHelpContents();
    }
}

function showHelpContents()
{
    help_container.setHeader(help_contents.head);
    help_container.setBody(help_contents.body);
    help_container.setFooter(help_contents.footer);
    help_container.render();
    help_container.show();
}

function getHelpContents()
{
    var handleSuccess = function(o){
        if(o.responseText !== undefined){
            help_contents = YAHOO.lang.JSON.parse(o.responseText);
            showHelpContents();
        }
    }
    var handleFailure = function(o){}

    var query = location.pathname.split("/");
    var url = "/ajax/help/" + query[1] + "/";
    var callback = { success:handleSuccess,failure:handleFailure };
    var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
}

function initHelpContainer()
{ 
    /*if( onhome )
        help_container = new YAHOO.widget.Panel("win", {
            width:"627px",
            close:true,
            context:["help","tr","br"],
            underlay:"none",
            visible:false,
            modal:true
        } );
    else*/
    help_container = new YAHOO.widget.Panel("win", {
        width:"454px",
        close:true,
        context:["help","tr","br"],
        underlay:"none",
        visible:false,
        modal:true
    } );

    help_container.setHeader("&nbsp;");
    help_container.setBody("&nbsp;");
    help_container.setFooter("&nbsp;");
    //help_container.subscribe.hideEvent(hideOverlay);
    help_container.render(document.body);
    help_container.show();
}

function reportVideo(id)
{
    var handleSuccess = function(o){
        if(o.responseText !== undefined){
            document.getElementById("report").innerHTML = "Reported";
        }
    }
    var handleFailure = function(o){}

    var url = '/ajax/report_video/'+id+'/';
    var callback = { success:handleSuccess,failure:handleFailure };
    var request = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function reportPhoto(id, r)
{
    var handleSuccess = function(o){
        if(o.responseText !== undefined){
            document.getElementById("report"+r).innerHTML = "Reported";
        }
    }
    var handleFailure = function(o){}

    var url = '/ajax/report_photo/'+id+'/';
    var callback = { success:handleSuccess,failure:handleFailure };
    var request = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function removeVideo(id)
{
    var handleSuccess = function(o){
        if(o.responseText !== undefined){
            document.getElementById("report").innerHTML = "Removed";
        }
    }
    var handleFailure = function(o){}

    var url = '/ajax/remove_video/'+id+'/';
    var callback = { success:handleSuccess,failure:handleFailure };
    var request = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function removePhoto(id, r)
{
    var handleSuccess = function(o){
        if(o.responseText !== undefined){
            document.getElementById("report"+r).innerHTML = "Removed";
        }
    }
    var handleFailure = function(o){}

    var url = '/ajax/remove_photo/'+id+'/';
    var callback = { success:handleSuccess,failure:handleFailure };
    var request = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function removeTweet(id, r)
{
    var handleSuccess = function(o){
        if(o.responseText !== undefined){
            document.getElementById("report"+r).innerHTML = "Removed";
        }
    }
    var handleFailure = function(o){}

    var url = '/ajax/remove_tweet/'+id+'/';
    var callback = { success:handleSuccess,failure:handleFailure };
    var request = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function updateUserBox() {
    var tabView = new YAHOO.widget.TabView("afterLogin");

    document.getElementById("afterLogin").style.display = "block";
    document.getElementById("groupLink").style.visibility = "visible";
    document.getElementById("beforeLogin").style.display = "none";
    document.getElementById("profile").innerHTML = ''
        + '<div class="user_name">'
        + '<fb:name uid="loggedinuser" useyou="false" ></fb:name>'
        + '</div>'
        //+ '<div class="status">'
        //+ '<fb:user-status linked="false" uid="'+FB.Facebook.apiClient.get_session().uid+'" ></fb:user-status>'
        //+ '<?= !is_null($status) ? $status : ""; ?>'
        //+ '</div>'
        + '<div class="logout">'
        + '<a href="#" onclick="popupFacebookInvite(); return false;" style="color: #cc0921;"> Invite Friends</a>'
        + ' | <a href="#" onclick="FB.Connect.logout(function() { reload(); }); return false;" style="color: #cc0921;">Logout</a>'
        + '</div>';		
		

    FB.XFBML.Host.parseDomTree();
}

function reload() {
    window.location.reload();
}

function ensure_init(callback) {
  if(!window.api_key) {
    window.alert("api_key is not set");
  }

  if(window.is_initialized) {
    callback();
  } else {
    foo = new FB_RequireFeatures(["XFBML", "CanvasUtil"], function() {
        FB.FBDebug.logLevel = 4;
        FB.FBDebug.isEnabled = true;
        FB.Facebook.init(window.api_key, "/xd_receiver.php");

        window.is_initialized = true;
        callback();
      });
  }
}


function popupRequestForm(exclude_ids) {
  ensure_init(function() {
    var home_link;
    var action_text;
    var content_text;

    home_link = "http://stampsconnect.com";
    action_text = "Invite your friends to StampsConnect";
    content_text = "I thought you would be interested in StampsConnect - the platform that brings Calgary Stampeder fans together.";

    FB.IFrameUtil.CanvasUtilServer.run(true);

    var container=document.createElement("div");
    container.setAttribute("iframeHeight","510px");
    container.setAttribute("iframeWidth","486px");
    
    var dialog=new FB.UI.PopupDialog(action_text,container,false,false);
    dialog.setContentWidth(486);
    dialog.setContentHeight(510);
    dialog.set_placement(FB.UI.PopupPlacement.center);

    var fbml = ''
        + '<fb:fbml>'
        + '<fb:request-form action="'+document.location.href+'" method="POST" invite="true" type="StampsConnect" content="'+content_text+' <fb:req-choice url=\''+home_link+'\' label=\'View\' />">'
        + '<fb:multi-friend-selector rows="3" cols="3" showborder="false" actiontext="'+action_text+'" exclude_ids="'+exclude_ids+'" />'
        + '</fb:request-form></fb:fbml>'

    container.setAttribute("fbml",fbml);

    dialog.show();
    tmp = new FB_RequireFeatures(["XFBML"], function()
            {
                var node=new FB.XFBML.ServerFbml( container);
                FB.XFBML.Host.addElement(node);
            });
    });
}

function popupFacebookInvite(exclude_ids) {
  ensure_init(function() {
    FB.Facebook.get_sessionState().waitUntilReady(Delegate.create(null, function(_d5) {
      FB.Facebook.apiClient.connect_getUnconnectedFriendsCount(function(o,e) {
        if (o > 1) {
          FB.Connect.inviteConnectUsers();
        } else {
          popupRequestForm(exclude_ids);
        }
      });
    }));
  });

  return false;
}
