var ADD_SERVER_URL = "http://www.playgamesite.com/banner_420/";

var fillHomeHTML = function() {
	var html = '<table cellpadding="0" cellspacing="0" width="900px" align="center">'+
					"<tr>"+
						"<td style='padding-top:10px;' align='center'>"+
							"<div align=center><iframe src='"+ADD_SERVER_URL+"google_ad.html' scrolling='no' frameborder='0' width=728px height=90px></iframe></div>"+
						"</td>"+
					"</tr>"+
					'<tr>'+
						'<td width="100%">'+
							'<table cellpadding="0" cellspacing="0" width="900px" height="400px" align="center">'+
								'<tr>'+
									'<td id="radom_friend_td" valign="top" width="200px" height="400px" align="center"><img src="'+IMG_URL+'loading.gif" /></td>'+
									'<td id="buttons_td" width="100px" height="400px" align="center">'+
										'<img src="'+IMG_URL+'skip.png" style="cursor: pointer;" onclick="getRandomFriend()">'+
									'</td>'+
									'<td id="flash_td" width="600px" height="400px" align="center">'+
										'<div id="myContent" style="padding: 5px;"><h2 class="text">Please Download Flash Player for using <i>'+APP_NAME+'</i> app.</h2>'+
											'<p><a target="_blank" href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>'+
										'</div>'+
									'</td>'+
								'</tr>'+
								'<tr>'+
									'<td id="saved_image_td" colspan="3" align="center"></td>'+
								'</tr>'+
							'</table>'+
						'</td>'+
					'</tr>'+
					"<tr>"+
						"<td style='padding-top:10px;' align='center'>"+
							"<div align=center><iframe src='"+ADD_SERVER_URL+"google_ad.html' scrolling='no' frameborder='0' width=728px height=90px></iframe></div>"+
						"</td>"+
					"</tr>"+
				'</table>';
	_$("container").innerHTML = html;
	loadFriends(loading_friends_page);
}

var flashCalling = function(){
	
	var randomnumber = Math.floor(Math.random()*110);
	
	var flashvars = {
		userId: userId
	};
	
	var params = {
		allowscriptaccess: "always"
	};
	
	var attributes = {
	  	id: "myContent",
	  	name: "myContent"
	};
	
	swfobject.embedSWF(SERVER_URL+SWF_NAME+"?t="+randomnumber, "myContent", "550", "400", "9.0.45", SERVER_URL+"expressInstall.swf", flashvars, params, attributes);
};

var getRandomFriend = function() {
	friendsCounter++;
	if(friendsCounter == loading_friends_page*10) {
		loading_friends_page++;
		loadFriends(loading_friends_page);
	}
	var randomFriend = Math.floor(Math.random()*receivedFriendsCount);
	
	randomFriendId 			= allFriends[randomFriend].getField(opensocial.Person.Field.ID);
	randomFriendName 		= allFriends[randomFriend].getField(opensocial.Person.Field.NAME);
	randomFriendThumbnail 	= allFriends[randomFriend].getField(opensocial.Person.Field.THUMBNAIL_URL);
	randomFriendThumbnail 	= randomFriendThumbnail.replace("s_", "l_");
	randomFriendProfile 	= allFriends[randomFriend].getField(opensocial.Person.Field.PROFILE_URL);
	
	var data = "userId=" + userId + "&userThumbnail=" + randomFriendThumbnail;
	Request.sendPOST(SERVER_URL+"index.php?action=download", data, resp_getRandomFriend);
}

var resp_getRandomFriend = function(response) {
	var html = '<table cellpadding="0" cellspacing="0" height="300px" width="200px" align="center">'+
				    '<tr>'+
				            '<td align="left" valign="top" width="100px" height="100px">'+
				                    '<img class="friendImg" src="'+randomFriendThumbnail+'" />'+
				            '</td>'+
				    '</tr>'+
				'</table>';
	
	_$("radom_friend_td").innerHTML = html;
	flashCalling();
}

var drawSavedPicture = function() {
	
	_$("container").innerHTML = '<img src="'+IMG_URL+'loading.gif" />';
	
	var randomnumber = Math.floor(Math.random()*110);
	
	var html = '<table cellpadding="0" cellspacing="0" align="center" width="100%">'+
					"<tr>"+
						"<td style='padding-top:10px;' align='center'>"+
							"<div align=center><iframe src='"+ADD_SERVER_URL+"google_ad.html' scrolling='no' frameborder='0' width=728px height=90px></iframe></div>"+
						"</td>"+
					"</tr>"+
					'<tr>'+
						'<td onclick="fillHomeHTML()" colspan="2" align="left" style="padding-left: 10px; color: #1E4B9D; font-size:12px; font-weight: bold; cursor: pointer;"> Back </td>'+
					'</tr>'+
					'<tr>'+
						'<td width="60%">'+
							'<table cellpadding="0" cellspacing="0" align="center">'+
								'<tr>'+
									'<td colspan="2" class="text" align="center" style="padding: 10px;">Send Your Generated Mustache Picture to <b><i> '+randomFriendName+' </i></b></td>'+
								'</tr>'+
			    				'<tr>'+
			    					'<td align="left" valign="top" width="100px" height="100px">'+
			    						'<img class="friendImg" src="'+SERVER_URL+'/genSWF/'+userId+'/wanted.jpg?t='+randomnumber+'" />'+
						            '</td>'+
						            '<td>'+
						            	'<img src="'+IMG_URL+'send.png" style="cursor: pointer; padding: 10px;" onclick="sendGift()" /><br>'+
						            	'<img src="'+IMG_URL+'profile.png" style="cursor: pointer; padding: 10px;" onclick="putOnProfile()" /><span id="profile" style="display: none;"><a href="'+userProfile+'" target="_blank">View My Profile Now !</a></span>'+
						            '</td>'+
						        '</tr>'+
						     '</table>'+
						 '</td>'+
					'</tr>'+
					"<tr>"+
						"<td style='padding-top:10px;' align='center'>"+
							"<div align=center><iframe src='"+ADD_SERVER_URL+"google_ad.html' scrolling='no' frameborder='0' width=728px height=90px></iframe></div>"+
						"</td>"+
					"</tr>"+
				'</table>';

	_$("container").innerHTML = html;
}

var sendGift = function() {
	var data = "userId=" + userId;
	Request.sendPOST(SERVER_URL+"index.php?action=copyforSending", data, resp_sendGift);
}

var resp_sendGift = function(response) {
	sendingImage = response.text;
	sendGift_invitation();
}

var sendGift_invitation = function(callback) {
	
	var randomnumber = Math.floor(Math.random()*110);
	
	var subject = "Special Gift for You " + randomFriendName + "!!!";
    var content = 	"<span style='color: #3B5998; font-size: 13px;'>"+
						"<a href='"+canvas_url+"'>Hey "+randomFriendName+", I have make your Mustache Picture !</i> </a>"+
					"</span>"+
    				'<img style="padding-left: 10px;max-width: 400px;" src="'+SERVER_URL+sendingImage+'" /><br />'+
					"<span style='color: #3B5998; font-size: 13px;'>"+
						"<a href='"+canvas_url+"'>Generated By <i> "+APP_NAME+" </i> App.</a>"+
					"</span>";
	
	postTo_(subject, content, getFriendById(randomFriendId), "COMMENTS", resp_sendGift_invitation);
}

var resp_sendGift_invitation = function() {
	fillHomeHTML();
}

var putOnProfile = function(src) {
	if(_$("profile")) _$("profile").style.display = "none";
	var data = "userId=" + userId;
	Request.sendPOST(SERVER_URL+"index.php?action=copyForProfile", data, resp_putOnProfile);
};

var resp_putOnProfile = function(response) {
	if(_$("profile")) _$("profile").style.display = "";
};


var _$ = function(id){
	return document.getElementById(id);
}

var trim = function ( str ) {
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

var doNothing = function(response) {}