﻿
function encode(str) {
	var result = "";
	
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) == " ") result += "+";
		else result += str.charAt(i);
	}
	
	return escape(result);
}

function decode(str) {
 
 	var result = str.replace(/\+/g, " ");
 	return unescape(result);
}

function CheckFBStatus(access_token)
{

    FB.api('/me', function(response) {
   
        if(window.location.href.indexOf('login.aspx') > -1)
        {
            if(window.location.href.indexOf('?') > -1)
            {
                 if(window.location.href.indexOf('fid=') > -1 && window.location.href.indexOf('at=') > -1)
                 {
                    window.location.href = window.location.href ;
                 }
                 else
                 {
                      window.location.href = window.location.href + '&fid=' + response.id + '&at=' + access_token;
                 }
            }
            else
            {
                 
                if(window.location.href.charAt(window.location.href.length - 1) == '#')
                {
                     window.location.href = window.location.href.substring(0, window.location.href.length - 1) + '?fid=' + response.id + '&at=' + access_token;
                }
                else
                {
                     window.location.href = window.location.href + '?fid=' + response.id + '&at=' + access_token;
                }
                
               
            }
        }
        else
        {
            window.location.href = 'http://www.leopardgeckomorphcalculator.co.uk/login.aspx?fid=' + response.id + '&at=' + access_token + '&ru=' + encode(window.location.href);
        
        }
   
        
   
   
    });


        
        
    
};



function FBLI()
{
 

    if(window.location.href.indexOf('fid') == -1)
    {

    FB.getLoginStatus(function(response) {
      if (response.session) 
      {
        
            CheckFBStatus(response.session.access_token);
      } 
      else
      {
        FB.Event.subscribe('auth.login', function(response) {
            CheckFBStatus(response.session.access_token);

      });
      }
    });

    }
};

function FBLogin()
{
FB.login(function(response) {
  if (response.session) {
      CheckFBStatus(response.session.access_token);
  } else {
        
  }
});


};


function FBLoginPublishPerms()
{
FB.login(function(response) {
  if (response.session) {
      CheckFBStatus(response.session.access_token);
  } else {
        
  }
}, {perms:'publish_stream'});



};





function FBLoginFBC()
{
FB.login(function(response) {
  if (response.session) {
      CheckFBStatusFBC(response.session.access_token);
  } else {
        
  }
});


};


function CheckFBStatusFBC(access_token)
{

    FB.api('/me', function(response) {
   
        if(window.location.href.indexOf('login.aspx') > -1)
        {
            if(window.location.href.indexOf('?') > -1)
            {
                 if(window.location.href.indexOf('fid=') > -1 && window.location.href.indexOf('at=') > -1)
                 {
                    window.location.href = window.location.href ;
                 }
                 else
                 {
                      window.location.href = window.location.href + '&fid=' + response.id + '&at=' + access_token;
                 }
            }
            else
            {
                 
                if(window.location.href.charAt(window.location.href.length - 1) == '#')
                {
                     window.location.href = window.location.href.substring(0, window.location.href.length - 1) + '?fid=' + response.id + '&at=' + access_token;
                }
                else
                {
                     window.location.href = window.location.href + '?fid=' + response.id + '&at=' + access_token;
                }
                
               
            }
        }
        else
        {
            window.location.href = 'login.aspx?fid=' + response.id + '&at=' + access_token + '&ru=' + encode(window.location.href);
        
        }
   
        
   
   
    });


        
        
    
};



function FBLIFBC()
{
     
    FB.getLoginStatus(function(response) {
      if (response.session) 
      {
      
            CheckFBStatusFBC(response.session.access_token);
      } 
      else
      {
          
           FBLoginFBC();
      }
    });
 
};



function GoToLGMC()
{
    document.location.href = 'http://leopardgeckomorphcalculator.co.uk/FBC/';
};



function PostToProfileWhatMorph(PostIt, GeckoID)
{
if(PostIt == true)
{
    FB.ui(
   {
     method: 'feed',
     name: 'The Leopard Gecko Morph Calculator',
     link: 'http://apps.facebook.com/leogeckomorphcalc/WhatMorph.aspx?ID=' + GeckoID,
     picture: 'http://www.leopardgeckomorphcalculator.co.uk/images/fbl.jpg',
     caption: 'What Morph Is My Leopard Gecko?',
     description: 'Help other Leopard Gecko owners decide what morph their geckos are',
     message: 'has helped someone try and decide what morph their Leopard Gecko is with the Leopard Gecko Morph Calculator.'
   },
   function(response) {
     if (response && response.post_id) {
        
     } else {
        
     }
   }
 );

}
 

};




function PostToProfileWhatMorphV2(PostIt, GeckoID)
{
if(PostIt == true)
{
    FB.ui(
   {
     method: 'feed',
     message: 'has helped someone try and decide what morph their Leopard Gecko is with the Leopard Gecko Morph Calculator.',
     name: 'The Leopard Gecko Morph Calculator',
     link: 'http://apps.facebook.com/leogeckomorphcalc/WhatMorph.aspx?ID=' + GeckoID,
     picture: 'http://www.leopardgeckomorphcalculator.co.uk/images/fbl.jpg',
     caption: 'What Morph Are These Leopard Geckos?',
     description: 'The "What Morph" League - A Friendly and Helpful Competition to Help Other Leopard Gecko Owners!'
   },
   function(response) {
     if (response && response.post_id) {
        
     } else {
        
     }
   }
 );

}
 

};
