/**
 * Find/invite friends functionalities
 */

// setting a global variable be used for accessing the images hosted at s3
///var s3_asset_address = ((location.host == 'devpune.socialmedian.com' || location.host == 'pune.socialmedian.com') ? 'http://devpune.s3.amazonaws.com' : 'http://socialmedian.s3.amazonaws.com');
///var s3_static_asset_address = 'http://socialmedian.s3.amazonaws.com';

 /**
 * find friends in socialmedian
 */
var findFriends = function (){
	if($('search_friend').value.strip() == "") {
		return false;
	}
	var postParameters = Form.serialize("find_friends_form");
	var ajaxRequestOptions = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: postParameters,
		// Handle successful response
		onSuccess: findFriendsCallback,
		// Handle 404
		on404: findFriends404Callback,
		// Handle other errors
		onFailure: findFriendsFailureCallback
	}
	new Ajax.Request('/find-friends', ajaxRequestOptions);
}
/**
 * find friends in socialmedian success callback function
 */
var findFriendsCallback = function (response){
	//eval the json object
	response_data = eval('(' + response.responseText + ')');
	// Google Analytics - code to track 'vote for feed' action
	tracAjaxFunctionalities('/find-friends');
	if(response_data.error == 1) {
		alert(response_data.message);
	} else {
		htmlHolder = "";
		htmlHolder += '<div style="margin:0px;padding:5px 0px 5px 10px;font-size:14px;font-weight:bold;">Search Results</div>'+
		'<div style="margin:0px;padding:5px 0px 5px 6px;background-color:#CCCCCC;">'+
			'<input type="checkbox" id="my_friends_check_all" name="my_friends_check_all" onclick="if($(\'my_friends_check_all\').checked == true) {checkAll(\'findFriendsCheck\');} else {uncheckAll(\'findFriendsCheck\');}"> <span style="font-weight:bold;">Select/Deselect all</span>'+
		'</div>'+
		'<div style="margin:0px;padding:0px;max-height:300px;overflow:auto;border:1px solid #B7B7B7;">';
			if(response_data.result.length > 0) {
				htmlHolder += '<form id="findFriendsResult" name="findFriendsResult" style="margin:0px;padding:0px;">';
				for(i=0;i<response_data.result.length;i++) {
					htmlHolder += '<div style="margin:0px;padding:10px 0px 10px 0px;'+(i<response_data.result.length-1 ? 'border-bottom:1px solid #CCCCCC;':'')+'">'+
							'<div style="margin:0px;padding:0px 0px 0px 5px;float:left;">'+
								'<input class="findFriendsCheck" type="checkbox" name="invite_friends[]" value="'+response_data.result[i].email+'">'+
							'</div>'+
							'<div style="margin:0px;padding:2px 0px 0px 5px;float:left;width:20px;">'+
								'<a href="/'+response_data.result[i].socialmedian_url+'"><img src="'+ S3_DYNAMIC_ASSETS_PATH +'/user-images/'+response_data.result[i].id+'-small.jpg"></a>'+
							'</div>'+
							'<div style="margin:0px;padding:2px 0px 0px 5px;float:left;">'+
								response_data.result[i].socialmedian_url.capitalize()+
							'</div>'+
							'<div style="margin:0px;padding:2px 0px 0px 5px;float:left;">'+
								' -- '+response_data.result[i].email+
							'</div>'+
							'<br clear="all">'+
					'</div>';
				}
				htmlHolder += '</form>';
			} else {
				htmlHolder += '<div align="center" style="margin:0px;padding:10px 0px 10px 0px;color:red;">Result not found</div>';
			}
		htmlHolder += '</div>'+
			'<input type="button" value="Send" onclick="inviteFriends(0)">';
		$('find_friends_result_holder').innerHTML = htmlHolder;
	}
}

/**
 * find friends in socialmedian failure callback function
 */
var findFriendsFailureCallback = function (){
	alert("Error on page");
}
/**
 * find friends in socialmedian 404 callback function
 */
var findFriends404Callback= function (){
	alert("Error page is not found");
}

/**
 * invite friends to socialmedian/perticular news network
 */
function inviteFriends(ncId) {
	var postParameters = "";
	postParameters = Form.serialize('findFriendsResult');
	if(postParameters == "") {
		alert("Select your friends to send invitation");
		return false;
	}
	var ajaxRequestOptions = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: postParameters,
		// Handle successful response
		onSuccess: inviteFriendsCallback,
		// Handle 404
		on404: inviteFriends404Callback,
		// Handle other errors
		onFailure: inviteFriendsFailureCallback
	}
	new Ajax.Request('/send-invitation', ajaxRequestOptions);
}

/**
 * invite friends to socialmedian/perticular news network success callback function
 */
var inviteFriendsCallback = function (response){
	//eval the json object
	response_data = eval('(' + response.responseText + ')');
	// Google Analytics - code to track 'vote for feed' action
	tracAjaxFunctionalities('/send-invitation');
	if(response_data.error == 1) {
		alert(response_data.message);
	} else {
		alert(response_data.message);
	}
}

/**
 * invite friends to socialmedian/perticular news network failure callback function
 */
var inviteFriendsFailureCallback = function (){
	alert("Error on page");
}
/**
 * invite friends to socialmedian/perticular news network 404 callback function
 */
var inviteFriends404Callback= function (){
	alert("Error page is not found");
}