/**
Ajax functions
**/

var lastActionObj = null;
var icons = 'small';

/**
*	Add friend
*
*	@param int friendId
*	@return boolean
*
**/
function ajaxAddFriend(obj, friendId, iconpack) {
	lastActionObj = obj;

	if (typeof iconpack != 'undefined') icons = iconpack;

	var url = '/friends/add_friend/'+parseInt(friendId)+'/L2FqYXg=';
	new Ajax.Request(url, {method: 'get', onSuccess: doAjaxAddFriend});

	return false;
}
function setErrorMessage(innerText) {
	errorMessage = innerText;
}

function hidePops() {
	hideModelPopup();
	hideOtherPopup();
}

var doAjaxAddFriend = function (t) {
	hidePops();
	fillPopup(t.responseText);
	showCenteredPopup();
	popupLocked = true;
	setTimeout('popupLocked = false;hidePops()', 3*1000);

	if (lastActionObj.tagName == "A") {
		if (t.responseText.match(languages['friend_request_sent'])) {
			lastActionObj.className	= (icons == 'small') ? "s20x20 pending_friend_small" : "s40x20 pending_friend_big";
			lastActionObj.title 	= 'Your friend request to this user is pending';
			lastActionObj.href		= '';
			lastActionObj.onclick	= function (e) {return false}
		}
	}
}

function acceptFriendRequest(obj, friendId) {
	lastActionObj = obj;

	hidePops();
	fillPopup('<img src=\"/images/preloader.gif\" />');
	showCenteredPopup();
	popupLocked = true;

	var url = '/friends/afid/'+parseInt(friendId)+'/L2FqYXg=';
	new Ajax.Request(url, {method: 'get', onSuccess: doAcceptFriendRequest});
	return false;
}

var doAcceptFriendRequest = function (t) {
	if (t.responseText == '') {
		$('all_friends').innerHTML = parseInt($('all_friends').innerHTML) + 1;
		$('pending_friends').innerHTML = parseInt($('pending_friends').innerHTML) - 1;
		//$('topMenuFriends').innerHTML = parseInt($('topMenuFriends').innerHTML) - 1;
		//if (parseInt($('topMenuFriends').innerHTML)==0) $('topMenuFriends').className = '';

		lastActionObj.parentNode.parentNode.parentNode.removeChild(lastActionObj.parentNode.parentNode);

		if (parseInt($('pending_friends').innerHTML)==0) $('friends_ajax').innerHTML = "<br class=\"clear\"><div class=\"empty\">You have no requests.</div>"; //<br style=\"clear: both\">
		popupLocked = false;
		hidePops();
	}else{
		popupLocked = false;
		hidePops();
		fillPopup(t.responseText);
		showCenteredPopup();
		popupLocked = true;
		//setTimeout('popupLocked = false;hidePops()', popupTimeout);
	}
}

function rejectFriendRequest(obj, friendId) {
	lastActionObj = obj;

	if (confirm('Are You sure You want to Reject this friend request?')) {
		hidePops();
		fillPopup('<img src=\"/images/preloader.gif\" />');
		showCenteredPopup();
		popupLocked = true;

		var url = '/friends/rjfid/'+parseInt(friendId)+'/L2FqYXg=';
		new Ajax.Request(url, {method: 'get', onSuccess: doRejectFriendRequest});
	}
	return false;
}

var doRejectFriendRequest = function (t) {
	if (t.responseText == '') {
		$('pending_friends').innerHTML = parseInt($('pending_friends').innerHTML) - 1;
		lastActionObj.parentNode.parentNode.parentNode.removeChild(lastActionObj.parentNode.parentNode);
		popupLocked = false;hidePops()
	}else{
		hidePops();
		fillPopup(t.responseText);
		showCenteredPopup();
		popupLocked = true;
		setTimeout('popupLocked = false;hidePops()', 3*1000);
	}
}

function showPreloader() {
	hidePops();
	fillPopup("<img src='/images/preloader.gif' alt=\"preloader\" />");
	showCenteredPopup();
}

//Profile actions
function profileAddFriend(friend_id) {
	showPreloader();

	new Ajax.Request('/friends/add_friend/' + parseInt(friend_id) + '/L2FqYXg=', {onSuccess: doProfileAddFriend});

	return false;
}

var doProfileAddFriend = function (t) {

	if (t.responseText.match(languages['friend_request_sent'])) {
		$('fr_img').innerHTML = '<img src="/images/ico/pending_friend_small.gif" alt="'+languages['is_pending_fr_alt']+'" title="'+languages['is_pending_fr_alt']+'" border="0" width="20" height="20" />';
		$('fr_txt').innerHTML = languages['is_pending_fr'];
		hidePops();
	} else{
		if (t.responseText.match(languages['you_are_friends'])) {
			$('fr_img').innerHTML = '';
			$('fr_txt').innerHTML = '';
			fillPopup(t.responseText);
			reCenterPopup();
			setTimeout('hidePops()', popupTimeout);
		}else{
			fillPopup(t.responseText);
			reCenterPopup();
			setTimeout('hidePops()', popupTimeout);
		}
	}
}

function composeMessage(toUserId, parameters) {
	if (typeof parameters == 'undefined') parameters = '';
	hidePops();
	showPreloader();

	new Ajax.Request('/messages/ajax_compose/'+toUserId + parameters, {onSuccess: doComposeMessage});

	return false;
}

var doComposeMessage = function (t) {
	if (!pageLoaded) return false;
	if (t.responseText.match(/^<div class="flashmsg/)) {
		 fillPopup(t.responseText);
		 reCenterPopup();
		 popupLocked=true;
		 setTimeout('popupLocked=false;hidePops()', popupTimeout);
	}else {
		fillPopup(getCloseButton() + t.responseText);
		reCenterPopup();

		if ($('photoshoot_date')) {
			Calendar.setup({
				inputField     : "photoshoot_date",     // id of the input field
				ifFormat       : "%m/%d/%Y %I:%M %p",    // the date format
				button         : "photoshoot_date",  // trigger for the calendar (button ID)
				align          : "B1",           // alignment (defaults to "Bl")
				singleClick    : true,
				showsTime	   : true,
				time24         : false
			});
		}

//	if ($('recaptcha')) {
//		var RecaptchaOptions = {theme : 'blackglass'};
//
//		var head= document.getElementsByTagName('head')[0];
//		var script= document.createElement('script');
//		script.type= 'text/javascript';
//		script.src= 'http://api.recaptcha.net/challenge?k='+CAPTCHA_PUBLIC_KEY;
//		head.appendChild(script);
//
//	}
		popupLocked=true;
	}
}

function sendMessage() {
	var to_user_id 				= $('to').value;
	var subject 				= $('subject').value;
	var message 				= $('msg').value;
	//Tfp
	var tfp 					= $('tfp') ? $('tfp').value : 0;
	var photoshoot_compensation = $('photoshoot_compensation') ? $('photoshoot_compensation').value : '';
	var photoshoot_date 		= $('photoshoot_date') ? $('photoshoot_date').value : '';

	popupLocked=false;
	waitProcess();
	popupLocked=true;

	var url = "to="+parseInt(to_user_id)+"&subject="+encodeURIComponent(subject)+"&msg="+encodeURIComponent(message)+"&msg_id=0&tfp_id="+parseInt(tfp)+"&photoshoot_compensation="+encodeURIComponent(photoshoot_compensation)+"&photoshoot_date="+encodeURIComponent(photoshoot_date);

	new Ajax.Request("/messages/send/1", {method: "POST", postBody: url, onSuccess: doSendMessage});
}

var doSendMessage = function (t) {
	popupLocked=false;
	fillPopup(t.responseText);
	reCenterPopup();
	popupLocked=true;
	setTimeout('popupLocked=false;hideDimmer(true)', popupTimeout);
}
