
var flexId = "";

function fbInit( apikey, crossdomain, id )
{
	FB.init( apikey, crossdomain );
	flexId = id;
}

function fbLogin( isAuto, SpotsLeft )
{
	FB.ensureInit(function()
	{
	    //if ( isAuto == "false" )
		    FB.Connect.requireSession(onLogin(SpotsLeft));
            
            document.getElementById("fb_dialog_cancel_button").onclick = function()
            {
                top.location.reload();
            };
	    /*
	    else
	    {
		    FB.Facebook.get_sessionState().waitUntilReady(function(session) 
				    {
					    if(session)
						    onLogin(SpotsLeft);
				    });
	    }
	    */
	});
}
function onLogin(SpotsLeft)
{
	//flexDispatcher( "onLoggedIn", FB.Facebook.apiClient._session );
	//if (typeof(facebookiframe) != "undefined")
	//    facebookiframe.location.href = "/facebook/friendselector.htm?EntryId=" + EntryId;
	
	friendselector(SpotsLeft);
}

function friendselector(SpotsLeft)
{
    var fbml = "<fb:fbml><fb:request-form action=\"http://" + document.location.host + "/scripts/FriendsSelection.aspx\" \tmethod=\"GET\" invite=\"true\" type=\"Cruze with Pink\" content=\"Cruze with Pink: <fb:req-choice url='http://www.cruzewithpink.com/' label='Cruze with Pink' />\"><fb:multi-friend-selector\tshowborder=\"false\" max=\"" + SpotsLeft + "\" exclude_ids=\"\" actiontext=\"Invite your friends\" rows=\"3\" bypass=\"cancel\"\tshowborder=\"false\" />"+"</fb:request-form>"+"</fb:fbml>";
    FB.Facebook.get_sessionState().waitUntilReady(function(session)
    {
        if(session)
            inviteFriends("Select " + SpotsLeft + " of your friends to Cruze with", fbml);
    });
}

function fbUserInfo( uid, params )
{
	FB.ensureInit(function()
	{
	    FB.Facebook.apiClient.users_getInfo( uid, params, onUserInfo );
	});
}
function onUserInfo( result, ex )
{
	//flexDispatcher( "onUserInfo", result[ 0 ] );
}

function fbLogout()
{
	FB.Connect.logout( onLogout );
}
function onLogout()
{
	flexDispatcher( "onLoggedOut" );
}

function inviteFriends( title, fbmlContent )
{
  FB.ensureInit(function()
  {
        var dialog = new FB.UI.FBMLPopupDialog( title, '' );
        var fbml = fbmlContent;
        
        dialog.setFBMLContent(fbml);
        dialog.setContentWidth(760);
        dialog.setContentHeight(400);
        dialog.set_placement(FB.UI.PopupPlacement.topCenter);
        dialog.show();
        //document.getElementById("RES_ID_fb_pop_dialog_table").style.top = 0;
        
        document.getElementById("fb_dialog_cancel_button").onclick = function()
        {
            top.location.reload();
        };        
    });
}

//publish to the wall
function fbPublish( template, FriendsCount, TargetIDs)
{
    //fbPublish(104465355459, 3, "");
	var comment_data = { 	
							"images":[{"src":"http://www.cruzewithpink.com/facebook/images/pinknewsfeed.jpg", "href":"http://www.cruzewithpink.com"}],
							"FriendsCount":"3"
						};					
	
  FB.ensureInit(function()
  {	
	/*if (TargetIDs.indexOf(",") > -1)
	{
	    //alert(TargetIDs.split(","));
	    FB.Connect.showFeedDialog( template , comment_data, TargetIDs.split(","), null, null, FB.RequireConnect.promptConnect, fbPublishWall);
    }
	else
	    FB.Connect.showFeedDialog( template , comment_data, TargetIDs, null, null, FB.RequireConnect.promptConnect, fbPublishWall);
	    */
    FB.Connect.showFeedDialog( template , comment_data, null, null, null, FB.RequireConnect.promptConnect, fbPublishWall);	    
    });
}

function fbPublishWall(title, fbml)
{
    FB.ensureInit(function()
    {
        FB.Facebook.apiClient.profile_setFBML(FB.Facebook.apiClient.get_session().uid, "", null, "", "<div style='text-align: center;'><a href='http://www.cruzewithpink.com.au' target='_blank'><img src='http://www.cruzewithpink.com/facebook/images/pinkprofile.jpg' height='190' /></a><p>Enter the draw to WIN the ultimate<br />Cruze with Pink experience thanks<br />to the all-new Holden Cruze.<br /><a href='http://www.cruzewithpink.com.au' style='text-decoration: underline; text-align: center;' target='_blank'>www.cruzewithpink.com.au</a></p></div>", function(){});
          FB.ensureInit(function()
          {
                var dialog = new FB.UI.FBMLPopupDialog( "Add to your Profile", "");
                var fbml = "<fb:add-section-button section=\"profile\" />";
                dialog.setFBMLContent(fbml);
                dialog.setContentWidth(650);
                dialog.setContentHeight(480);
                dialog.set_placement(FB.UI.PopupPlacement.topCenter);
                //dialog.show();
            });
    });
}

function flexDispatcher( func )
{
	if( arguments.length > 1 )
		swfobject.getObjectById( flexId )[func]( Array.prototype.slice.call(arguments).slice(1)[0]);
	else
		swfobject.getObjectById( flexId )[func]();
}

function querySt(ji)
{
    var hu = document.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++)
    {
        ft = gy[i].split("=");
        if (ft[0] == ji)
        {
            return ft[1];
        }
    }
}