/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function FBConnect(){
    this.login = function(){
        FB.getLoginStatus(function(response) {
          if (response.status != "connected") {
	     FB.login(function(response) {
		   FBConnect.getProfile();
	     }, {scope: 'email,user_location,publish_stream'});
	  } else {
	      FBConnect.getProfile();
	  }
	});
    }

    this.getProfile =  function (){
	var profile, loc, country;
	FB.api('/me', function(response) {
                if (response.location == undefined) {
                    country = "United States";
                }
                else {
                    loc = response.location.name.split(", ");
                    if (loc[1] == undefined)
                        country = "United States";
                    else
                        country = loc[1];
                }

               profile = {
		   strUserName: (response.username)?response.username: response.first_name+"."+response.last_name,
		   strFirstName: response.first_name,
		   strLastName: response.last_name,
		   strEmail: response.email,
		   strZipcode: 0,
		   strQuestion: "",
		   strCountry: country
	       };
	       $.get('http://www.showoff.com/apps/facebooksignup.aspx', profile, function (data){
		   if (data != 0){
                        setCookie('fbShowoffSignup',data,3);
                        var mySplitResult = data.split(",");
                        
                        if(mySplitResult[1] == "Register"){
                            register_user(data[0], response.email, true, 0);
                            FBConnect.welcome_share(response.id);
                        }
                        login_user(data[0], response.email, true, 0);
                   }
	       });
	 });
    };
    
    this.logout = function(){
        FB.getLoginStatus(function(response) {
          if (response.status == "connected") {
            FB.logout(function(response) {
               // alert(response.toSource());
		window.location.reload();
            });
          } else {
               
          }
        });
    }

    this.welcome_share = function(uid){
           var data = {
               name : "Welcome to the ShowOff Home Visualizer!",
               caption: "www.showoff.com",
               description:"See your design dreams come to life as you experience a new level of home improvement with the Showoff.com Visualizer. Paint, test out products and more all without leaving your computer!",
               link:"http://www.showoff.com/",
               picture:"http://www.showoff.com/apps/images/mainlogo.jpg",
               method:"feed"               
           }
        
            FB.api('/me/feed', 'post', data,
                function(response){
                        if (response && response.id) {
//                          //  alert(response.id);
                        }else{
//                          //  alert("feed not published.");
                        }
                }
            );
        
    }

    this.publish = function(title, desc, href, image){
            var data = {
               name : title,
               caption: "www.showoff.com",
               description: desc,
               link: href,
               picture: image,
               method:"feed"               
           };
            FB.ui(
                data
               ,
              function(response) {
                if (response && response.post_id) {
                  //alert('Post was published.');
                } else {
                  //alert('Post was not published.');
                }
              }
            );
    }
}
