// stealing my code, huh?
// well, if you do that, then you are the biggest brainless asshole in the world

var loadAttempt = 0;
var firstLoad = 1; 
document
.write(unescape("%3Cscript src='http://www.google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var pageTracker = null;

function loadURL(url) {

wait(true);

if (fs_visible) {
fs_Container.insertBefore(friendSelectorDiv, null);
fs_visible = false;
}

var param = {};
param[opensocial.ContentRequestParameters.AUTHORIZATION] = opensocial.ContentRequestParameters.AuthorizationType.SIGNED;
param[opensocial.ContentRequestParameters.METHOD] = opensocial.ContentRequestParameters.MethodType.GET;
param[opensocial.ContentRequestParameters.CONTENT_TYPE] = opensocial.ContentRequestParameters.ContentType.HTML;
gadgets.io
.makeRequest(
url + '&_t=' + Math.random(),
function(response) {
if (response.text) {
$('main').innerHTML = response.text;
response.text.evalScripts();
wait(false);
loadAttempt = 0;
} else {
if (loadAttempt < 5) {
loadAttempt++;
loadURL(url);
} else {
$('main').innerHTML = '<div style="font-size:20px;">Page load from MySpace failed. <a style="font-size:140%;" href="javascript:loadURL(\'' + url + '\')">Click here</a> to try again or refresh the entire page.</div>';
wait(false);
loadAttempt = 0;
firstLoad=0; 
}
}
gadgets.window.adjustHeight(3000);
}, param);

if (!pageTracker) {
var pageTracker = _gat._getTracker(googana);
}
pageTracker._trackPageview();
}

function wait(flag) {
if (flag) {
if (loadAttempt > 0) {
$('loading').innerHTML = '<img src="http:///img/tp/nv33tt.gif"> Loading, please wait...';
} else if (loadAttempt > 2) {
$('loading').innerHTML = '<img src="http:///img/tp/nv33tt.gif"> Still loading, please wait...';
} else {
$('loading').innerHTML = '<img src="http:///img/tp/nv33tt.gif">';
}
$('loading').style.display = '';
$('main').style.cursor = 'wait';
} else {
$('loading').innerHTML = '';
$('loading').style.display = 'none';
$('main').style.cursor = '';
}

}

function submitAJAXForm(obj) {

if (window.attachEvent && navigator.userAgent.indexOf('Opera') === -1) {
Element.extend(obj);
}
if (obj.tagName == 'FORM') {
var f = obj;
}
else {
var f = obj.up('form');
}
var url;

if (f.action != '') {
url = f.action;
} else {
url = app_domain;
}

if (f.action.indexOf('?') == -1) {
var url = url + '?' + f.serialize();
} else {
var url = url + '&' + f.serialize();
}

loadURL(url);
}

var initApp = function() {

var params = gadgets.views.getParams();

if (params.ownerid <= 0) {
top.location = "http://www.myspace.com";
return;
}
if (params.installState == 0) {
$('main').innerHTML = '<img src="http:///img/tp/i5r8jt.png" style="padding-left:290px">';
return;
} else {
fs_loadFriends();
var ini_url = app_domain + '?app_id=' + app_id;
var params = opensocial.getEnvironment().getParams();
if ('undefined' === typeof (params)
|| 'undefined' === typeof (params['target'])) {
;
} else {
	
	if ( firstLoad )
	{
		for (i in params)
		{
			ini_url += '&'+i+'=' + params[i];
		}
		firstLoad=0;
	}
	else 
		ini_url += '&target=' + params['target'];
}

loadURL(ini_url);
}
}

var FBJSEmulator = {
setStyle2 : function(element, key, val) {
eval("$(element).style." + key + "='" + val + "';");
},
getStyle : function(element) {
return $(element).style;
},
setDisabled : function(element, flag) {
$(element).disabled = flag;
},
setTextValue : function(element, val) {
$(element).innerHTML = val;
},
setValue : function(element, val) {
$(element).value = val;
},
getName : function(element) {
return $(element).name;
},
getChecked : function(element) {
return $(element).checked;
}
}

Element.addMethods(FBJSEmulator);

var fs_SINGLE_INVITE_MODE = 1;

var fs_currentPosition = null;
var fs_maxFriends = null;
var fs_FRIEND_LIMIT = 6000;
var fs_numPages = 1;
var fs_excludedIds = [];
var fs_callbackURL = null;
var fs_allFriends = {};

var fs_selectedFriends = {};
var fs_selectedFriendsArray;

var fs_filter_started = 0;

var friendsDiv;
var friendSelectorDiv;
var searchBoxLabel;
var progress_div;
var searchBoxInput;
var infoBox;
var fs_Container = new Element('span');
fs_visible = false;

function fs_init() {

MyOpenSpace.DefaultPageSize = 500;

friendsDiv = new Element('div', {
'class' :'box'
});

friendSelectorDiv = new Element('div', {
'class' :'friendSelector'
});

searchBoxLabel = new Element('span', {
'class' :'text_label'
}).update('Find Friends: &nbsp;');

progress_div = new Element(
'div',
{
'class' :'fsProgressInner',
'style' :'width:0px;background-color:#3b5998;font-size:2px;height:100%;'
}).update('&nbsp;');

searchBoxInput = new Element('input', {
'class' :'fsProgressInner',
value :'Start Typing a Friend\'s Name',
'style' :'color:#888888;',
type :'text',
id :'filterBox'
});

searchBoxInput.observe('keyup', function(e) {
fs_filter_started++;
infoBox.innerHTML = 'working...';
setTimeout(fs_filter_list, 500);
});
searchBoxInput.observe('focus', function() {
if (this.value == 'Start Typing a Friend\'s Name') {
this.value = '';
this.style.color = '';
}
});

searchBoxInput.observe('blur', function(e) {
if (this.value == '') {
this.value = 'Start Typing a Friend\'s Name';
this.style.color = '#888888';
}
});

infoBox = new Element('span', {
'id' :'infoBox'
});

var toolBox = new Element('div', {
'class' :'tool_box'
});

var table = new Element('table', {
'cellpadding' :'2',
'cellspacing' :'0'
});

var tr = new Element('tr');

var allButton = new Element('button', {
'type' :'button',
'id' :'inviteAll'
}).update('Invite All Friends!')

allButton.observe('click', function() {
fs_selectedFriendsArray = null;
fs_invite_all();
});

var td = new Element('td', {
style :'padding-right:20px;'
}).update(allButton);
tr.insert(td);

var td = new Element('td');
td.insert(searchBoxLabel);
tr.insert(td);

var td = new Element('td');
td.insert(searchBoxInput);
tr.insert(td);

infoBox.insert(new Element('div', {
id :'loadingLabel'
}).update('Loading Friends'));

var div = new Element('div', {
id :'fsProgressOuter'
});

div.insert(progress_div);
infoBox.insert(div);

var td = new Element('td', {
style :'padding-left:12px;'
});
td.insert(infoBox);
tr.insert(td);

var tbody = new Element('tbody');
tbody.insert(tr);
table.insert(tbody);
toolBox.insert(table);

var inviteButton = new Element('button', {
'id' :'inviteButton',
'type' :'button'
}).update("Send Invites!");

inviteButton.observe('click', function() {
fs_selectedFriendsArray = Object.values(fs_selectedFriends);
fs_invite_selected();
});

inviteDiv = new Element('div', {
'id' :'inviteDiv'
});
inviteDiv.insert(inviteButton);

friendSelectorDiv.insert(toolBox);
friendSelectorDiv.insert(friendsDiv);
if (fs_SINGLE_INVITE_MODE == 0) {
friendSelectorDiv.insert(inviteDiv);
}

fs_Container.insertBefore(friendSelectorDiv, null);
}

function fs_invite_selected() {
if (fs_selectedFriendsArray.length > 0) {
var friend = fs_selectedFriendsArray.shift();
id = friend.substr(4);
opensocial
.requestShareApp(
id,
message,
function(responseCode) {
if (responseCode == MyOpenSpace.PostTo.Result.SUCCESS) {
fs_callback(id);
delete (fs_selectedFriends[friend]);
$(friend).remove();
delete (fs_allFriends[id]);
infoBox.innerHTML = '<div style="padding:4px;background-color:#117711;color:white;">Great Job! Keep inviting your friends until they all disappear from the list!</div>'
} else {
infoBox.innerHTML = '<div style="padding:4px;background-color:#ee2222;color:white;">ERROR! You need to click &quot;Send&quot; on each friend in order to invite them!</div>'
}

fs_invite_selected();
});
}
}

function fs_invite_all() {

if (fs_selectedFriendsArray == null) {
fs_selectedFriendsArray = Object.values(fs_allFriends);
}

var friend = fs_selectedFriendsArray.shift();
if (friend != null) {
id = friend.id;
opensocial
.requestShareApp(
id,
message,
function(responseCode) {
if (responseCode == MyOpenSpace.PostTo.Result.SUCCESS) {
fs_callback(id);
$('fsid' + id).remove();
delete (fs_allFriends[id]);
infoBox.innerHTML = '<div style="padding:4px;background-color:#117711;color:white;">Great Job! Keep inviting your friends until they all disappear from the list!</div>'
fs_invite_all();
} else if (responseCode == MyOpenSpace.PostTo.Result.ERROR) {
$('fsid' + id).remove();
delete (fs_allFriends[id]);
fs_invite_all();
} else {
infoBox.innerHTML = '<div style="padding:4px;background-color:#ee2222;color:white;">ERROR! You need to click &quot;Send&quot; on each friend in order to invite them!</div>'
}
});
}
}

function fs_invite_one(friend) {
id = friend.substr(4);
opensocial
.requestShareApp(
id,
message,
function(responseCode) {
if (responseCode == MyOpenSpace.PostTo.Result.SUCCESS) {
fs_callback(id);
$(friend).remove();
delete (fs_allFriends[id]);
infoBox.innerHTML = '<div style="padding:4px;background-color:#117711;color:white;">Great Job! Keep inviting your friends until they all disappear from the list!</div>'
} else {
infoBox.innerHTML = '<div style="padding:4px;background-color:#ee2222;color:white;">ERROR! You need to click &quot;Send&quot; on each friend in order to invite them!</div>'
$(friend).className = 'user';
}

});

}

function fs_set_message(msg) {
	message = opensocial.newMessage(msg, '');
	var title = "Play this game";
	message.setField(opensocial.Message.Field.TITLE, title);
	}

function fs_filter_list() {
fs_filter_started--
if (fs_filter_started == 0) {

var j = 0;
if (searchBoxInput.value == '') {
for (i in fs_allFriends) {
if ($('fsid' + i))
$('fsid' + i).show();
if (j++ > 500) {
break;
}
}
} else {
var reg = new RegExp(searchBoxInput.value, "i");
for (i in fs_allFriends) {
if (fs_allFriends[i].name == null) {
continue;
}

if (reg.exec(fs_allFriends[i].name)) {
if ($('fsid' + fs_allFriends[i].id))
$('fsid' + fs_allFriends[i].id).show();

} else {
if ($('fsid' + fs_allFriends[i].id))
$('fsid' + fs_allFriends[i].id).hide();
}

if (j++ > 500) {
break;
}
}
}
infoBox.innerHTML = '';
}
}

function fs_addFriend(id) {

var friend = fs_allFriends[id];

var e = new Element('div', {
id :'fsid' + friend.id,
'class' :'user'
});

e.observe('mouseover', function() {
if (fs_selectedFriends[this.id] == null) {
this.className = 'user user_hover';
}
});
e.observe('mouseout', function() {
if (fs_selectedFriends[this.id] == null) {
this.className = 'user'
}
});
e.observe('click', function() {
if (fs_SINGLE_INVITE_MODE == 0) {
if (fs_selectedFriends[this.id] == null) {
fs_selectedFriends[this.id] = this.id;
this.className = 'user user_sel';
} else {
fs_selectedFriends[this.id] = null;
delete (fs_selectedFriends[this.id]);
this.className = 'user';
}
} else {
this.className = 'user user_sel';
fs_invite_one(this.id);
}
});

e_img1 = new Element('div', {
'class' :'img_outer'
});

e_img2 = new Element('div', {
'class' :'img_inner'
});

e_img = new Element('img', {
src :friend.img
});

e_name = new Element('div', {
'class' :name
}).update(friend.name);

e_img2.insert(e_img);
e_img1.insert(e_img2);
e.insert(e_img1);
e.insert(e_name);

friendsDiv.insert(e);

}

function fs_loadFriends(response) {

if (response == null) {
fs_init();
fs_currentPosition = 1;
} else {
var friendData = response.get('friends').getData();

var hasFriends = false;
friendData.each( function(data) {
var friendID = data.getField(opensocial.Person.Field.ID);
var friendName = data.getDisplayName();

if (friendID != 6221 && friendName != null
&& fs_excludedIds.indexOf(Number(friendID)) == -1) {

var friendImg = data
.getField(opensocial.Person.Field.THUMBNAIL_URL);

fs_allFriends[friendID] = {
'id' :friendID,
'name' :friendName,
'img' :friendImg
};

fs_addFriend(friendID);

hasFriends = true;
}
});

if (fs_maxFriends == null) {
fs_maxFriends = friendData.getTotalSize();
if (fs_maxFriends > fs_FRIEND_LIMIT) {
fs_maxFriends = fs_FRIEND_LIMIT;
}
}

progress_div
.setStyle( {
'width' :Math.round(100 * fs_currentPosition
/ fs_maxFriends) + 'px'
});

fs_currentPosition += 100;
if (fs_currentPosition >= fs_maxFriends) {
if (fs_SINGLE_INVITE_MODE == 0) {
infoBox.innerHTML = '<span style="color:black">Select friends below and click "Send Invites!" button</a>';
} else {

infoBox.innerHTML = '<span style="color:black">Click on each friend below to invite them!</span>';
}
return null;
}
}

var req = opensocial.newDataRequest();
var params = {};
params[opensocial.DataRequest.PeopleRequestFields.FIRST] = fs_currentPosition;
params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
req.add(req.newFetchPeopleRequest(
opensocial.DataRequest.Group.VIEWER_FRIENDS, params), 'friends');
req.send(fs_loadFriends);

}

function fs_exclude_ids(ids) {
fs_excludedIds = ids;
ids.each( function(id) {
if ($('fsid' + id)) {
$('fsid' + id).remove();
}
delete (fs_allFriends[id]);
});
}

function fb_setCallbackURL(url) {
fs_callbackURL = url;
}

function fs_callback(id) {
if (fs_callbackURL != null) {
var param = {};
param[opensocial.ContentRequestParameters.AUTHORIZATION] = opensocial.ContentRequestParameters.AuthorizationType.SIGNED;
param[opensocial.ContentRequestParameters.METHOD] = opensocial.ContentRequestParameters.MethodType.GET;
param[opensocial.ContentRequestParameters.CONTENT_TYPE] = opensocial.ContentRequestParameters.ContentType.HTML;
gadgets.io.makeRequest(fs_callbackURL + '&ids[]=' + id + '&_t='
+ Math.random(), function(response) {
}, param);
}
}

function fs_bindTo(id) {
$(id).update(friendSelectorDiv);
fs_visible = true;
}