/* Used to debug, do not remove
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
*/

DR.extend('Social', {
    
    getData: function(id){
      if((window.globalSocialData == undefined) || (window.globalSocialData[id] == undefined)){
          //we have a problem. No key defined.
          return false;
      }else{
          //do a deep copy.
          var newObject = jQuery.extend(true, {}, window.globalSocialData[id]);
         return newObject; 
      } 
    },
    
    getAllGifts: function(id){
      if((window.globalGiftData == undefined)){
          //we have a problem. No key defined.
          return false;
      }else{
          //do a deep copy.
          var newObject = jQuery.extend(true, {}, window.globalSocialData[id]);
         return newObject; 
      } 
    },
    
    getGiftData: function(id){
      if((window.globalGiftData == undefined) || (window.globalGiftData[id] == undefined)){
          //we have a problem. No key defined.
          return false;
      }else{
          //do a deep copy.
          var newObject = jQuery.extend(true, {}, window.globalGiftData[id]);
         return newObject; 
      } 
    },
    
    invites: function(id, track) {

        var instanceData = DR.Social.getData(id);
        
        //if(FBto     == undefined || FBto     == false){ FBto     = null; }
       
		// Customize various params to make the invite more personal
       instanceData.message = instanceData.message.replace("%name%", window.Arcade.user.first_name);
       instanceData.link = instanceData.link.replace("%fbid%", window.Arcade.user.fbid);
       
		DR.log('method: ' + instanceData.method);
		DR.log('title: ' + instanceData.title);
		DR.log('message: ' + instanceData.message);
		DR.log('link: ' + instanceData.link);
		DR.log('filters: ' + instanceData.filters);
		//DR.log('FBto: ' + FBto);
       
            FB.ui({
                method  : instanceData.method,
                title   : instanceData.title,
                message : instanceData.message,
                to      : instanceData.to, //FBto, Having this creates an error
                link    : instanceData.link,
                filters : instanceData.filters

            }, function (response) {
            	DR.log("Facekbook response:", response);
                DR.Sva.trackInvite();
				
				//If invite is sent via app calls this function for backend to track
				if(response != null && track === true) {
					DR.Sva.appInvite(response);
				}
                // logging purposes.
            });
    }, //end function invite

    feed: function(id, communityName, link, FBto, itemId, message, tracking, title) {
    
    	//DR.log('array ' + window.globalSocialData[id]);
        DR.log('I made it!');
        var instanceData = DR.Social.getData(id);
        
		//if (instanceData.source = instanceData.source || instanceData.source = null);
		
		DR.log('Return Link: ' + link);
		
        // Customize various params to make the streampost more unique/personal
        if(window.Arcade.user.first_name == undefined) {}else{instanceData.caption = instanceData.caption.replace("%name%", window.Arcade.user.first_name);}
        if(window.Arcade.user.first_name == undefined) {}else{instanceData.title   = instanceData.title.replace("%name%", window.Arcade.user.first_name);}
		 if(link == undefined) {}else{instanceData.link   = instanceData.link.replace("%link%", link);}
        if(communityName == undefined) {}else{instanceData.title   = instanceData.title.replace("%community%", communityName);}
        if(communityName == undefined) {}else{instanceData.caption   = instanceData.caption.replace("%community%", communityName);}

                FB.ui({
                    method  : instanceData.method,
                    display : 'iframe',
                    caption : instanceData.caption,
                    name    : instanceData.title,
                    to      : FBto,
                    link    : instanceData.link,
                    picture : instanceData.picture, 
                    source  : instanceData.source,
                    //message : instanceData.message,
                    data : {
                    },
                    actions : [{
                            name : instanceData.actions,
                            link : instanceData.link
                        }]
                }, function (response) {
                    if(response) {
                        DR.Sva.trackPost(response.post_id);
                    }
                });
       
    } //end function feed
});

