//Watch or unwatch a user and set watch notice filters.
unwatchpulse = false;
watchpulse = false;

watchfiltersubmissions_on_pulse = false;
watchfilterjournals_on_pulse = false;
watchfilterstreams_on_pulse = false;

watchfiltersubmissions_off_pulse = false;
watchfilterjournals_off_pulse = false;
watchfilterstreams_off_pulse = false;

watchlock = false;
watchfiltersubmissionslock = false;
watchfilterjournalslock = false;
watchfilterstreamslock = false;

watchoptionsstate = false;

//Close options box if user clicks outside it.
Event.observe(document, 'click', function (event) {
	
	if (!event.element().parentNode) return;
	//event.element().up searches for any parents of the thing we clicked that have these given properties. In this case we look for parents with a certain ID.
	if (event.element().up('#unwatch_block') || event.element().up('#watch_general_block') || event.element().id == 'unwatch_button') {
		//Clicked inside options box so do nothing.
	} else {
		watchSetOptionsState(false); 
	}
});

function watchToggleOptionsState() {
	
	//If locked (processing ajax request) then do not show options box.
	if (watchlock) return;
	
	watchSetOptionsState(!watchoptionsstate);
}

function watchSetOptionsState(state) {
	
	//If locked (processing ajax request) then do not show options box.
	if (watchlock) return;
	
	if (state) {
		$('watchoptionsbox').setStyle({ display: 'block' });
	} else {
		$('watchoptionsbox').setStyle({ display: 'none' });
	}
	watchoptionsstate = state;
}

function watchStopEffect(mode) {
	
	switch (mode) {
		case 'watch':
		default:
			if (unwatchpulse) unwatchpulse.cancel();
			if (watchpulse) watchpulse.cancel();
			$('unwatch_button').setStyle({ opacity: 1 });
			$('watch_button').setStyle({ opacity: 1 });
			
			break;
		case 'submissions':
			if (watchfiltersubmissions_on_pulse) watchfiltersubmissions_on_pulse.cancel();
			if (watchfiltersubmissions_off_pulse) watchfiltersubmissions_off_pulse.cancel();			
			$('watchfilter_submissions_on').setStyle({ opacity: 1 });
			$('watchfilter_submissions_off').setStyle({ opacity: 1 });
			break;
		case 'journals':
			if (watchfilterjournals_on_pulse) watchfilterjournals_on_pulse.cancel();
			if (watchfilterjournals_off_pulse) watchfilterjournals_off_pulse.cancel();
			$('watchfilter_journals_on').setStyle({ opacity: 1 });
			$('watchfilter_journals_off').setStyle({ opacity: 1 });
			break;
		case 'streams':
			if (watchfilterstreams_on_pulse) watchfilterstreams_on_pulse.cancel();
			if (watchfilterstreams_off_pulse) watchfilterstreams_off_pulse.cancel();
			$('watchfilter_streams_on').setStyle({ opacity: 1 });
			$('watchfilter_streams_off').setStyle({ opacity: 1 });
			break;
	}
}

function watchFilterSetState(type, state) {
	switch (type) {
		case 'submissions':
			if (state) {
				$('watchfilter_submissions_on').setStyle({ display: '' });
				$('watchfilter_submissions_off').setStyle({ display: 'none' });
				$('watchfilter_submissions_off_smallicon').setStyle({ display: 'none' });
				
			} else {
				$('watchfilter_submissions_on').setStyle({ display: 'none' });
				$('watchfilter_submissions_off').setStyle({ display: '' });
				$('watchfilter_submissions_off_smallicon').setStyle({ display: '' });
			}
			break;
		case 'journals':
			if (state) {
				$('watchfilter_journals_on').setStyle({ display: '' });
				$('watchfilter_journals_off').setStyle({ display: 'none' });
				$('watchfilter_journals_off_smallicon').setStyle({ display: 'none' });
				
			} else {
				$('watchfilter_journals_on').setStyle({ display: 'none' });
				$('watchfilter_journals_off').setStyle({ display: '' });
				$('watchfilter_journals_off_smallicon').setStyle({ display: '' });
			}
			break;
		case 'streams':
			if (state) {
				$('watchfilter_streams_on').setStyle({ display: '' });
				$('watchfilter_streams_off').setStyle({ display: 'none' });
				$('watchfilter_streams_off_smallicon').setStyle({ display: 'none' });
				
			} else {
				$('watchfilter_streams_on').setStyle({ display: 'none' });
				$('watchfilter_streams_off').setStyle({ display: '' });
				$('watchfilter_streams_off_smallicon').setStyle({ display: '' });
			}
			break;
	}
}

function watchFilterChange(watched_user_id, IBmode, IBstate, IBtoken) {
	
	//Prevent multiple clicks.
	switch (IBmode) {
		case 'submissions':
			if (watchfiltersubmissionslock) return;
			break;
		case 'journals':
			if (watchfilterjournalslock) return;
			break;
		case 'streams':
			if (watchfilterstreamslock) return;
			break;
	}
	
	//Display loading effect.
	switch (IBmode) {
		case 'submissions':
			watchfiltersubmissions_on_pulse = new Effect.Pulsate('watchfilter_submissions_on', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
			watchfiltersubmissions_off_pulse = new Effect.Pulsate('watchfilter_submissions_off', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
			break;
		case 'journals':
			watchfilterjournals_on_pulse = new Effect.Pulsate('watchfilter_journals_on', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
			watchfilterjournals_off_pulse = new Effect.Pulsate('watchfilter_journals_off', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
			break;
		case 'streams':
			watchfilterstreams_on_pulse = new Effect.Pulsate('watchfilter_streams_on', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
			watchfilterstreams_off_pulse = new Effect.Pulsate('watchfilter_streams_off', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
			break;
	}
	
	//Turn on click lock while processing.
	switch (IBmode) {
		case 'submissions':
			watchfiltersubmissionslock = true;
			break;
		case 'journals':
			watchfilterjournalslock = true;
			break;
		case 'streams':
			watchfilterstreamslock = true;
			break;
	}
	
	submissions_out = '';
	journals_out = '';
	streams_out = '';
	
	switch (IBmode) {
		case 'submissions':
			submissions_out = (IBstate == true ? 'yes' : 'no');
			break;
		case 'journals':
			journals_out = (IBstate == true ? 'yes' : 'no');
			break;
		case 'streams':
			streams_out = (IBstate == true ? 'yes' : 'no');
			break;
	}
	
	new Ajax.Request('watchuser_process.php',
	{	
		parameters: { token: IBtoken, mode: 'change', submissions: submissions_out, journals: journals_out, streams: streams_out, user_id: watched_user_id },
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			
			//If watchlock is on, a +watch or -unwatch is in progress, so don't perform any filter change actions.
			//If watchstate is false, we have unwatched this user, so don't perform any filter change actions.
			if (watchlock || !watchstate) return;
			
			var response = transport.responseText || "{'error':'no response'}";
			
			if (!transport.responseText) {
				//Stop all loading effects.
				switch (IBmode) {
					case 'submissions':
						watchStopEffect('submissions');
						watchfiltersubmissionslock = false;
						break;
					case 'journals':
						watchStopEffect('journals');
						watchfilterjournalslock = false;
						break;
					case 'streams':
						watchStopEffect('streams');
						watchfilterstreamslock = false;
						break;
				}
			}
			
			var jsondata = response.evalJSON(true);
			if (jsondata.success=='yes') {
				switch (IBmode) {
					case 'submissions':
						watchFilterSetState('submissions', IBstate);
						watchStopEffect('submissions');
						watchfiltersubmissionslock = false;
						break;
						
					case 'journals':
						watchFilterSetState('journals',IBstate);
						watchStopEffect('journals');
						watchfilterjournalslock = false;
						break;
						
					case 'streams':
						watchFilterSetState('streams', IBstate);
						watchStopEffect('streams');
						watchfilterstreamslock = false;
						break;
				}
				
				
			} else if (!jsondata.success || jsondata.success != 'yes') {
				
				//Stop all loading effects.
				switch (IBmode) {
					case 'submissions':
						watchStopEffect('submissions');
						watchfiltersubmissionslock = false;
						break;
					case 'journals':
						watchStopEffect('journals');
						watchfilterjournalslock = false;
						break;
					case 'streams':
						watchStopEffect('streams');
						watchfilterstreamslock = false;
						break;
				}
			}
		},
		onFailure: function() { 
			
			//Stop all loading effects.
			switch (IBmode) {
				case 'submissions':
					watchStopEffect('submissions');
					watchfiltersubmissionslock = false;
					break;
				case 'journals':
					watchStopEffect('journals');
					watchfilterjournalslock = false;
					break;
				case 'streams':
					watchStopEffect('streams');
					watchfilterstreamslock = false;
					break;
			}
		}
	});
}

function watchProcess(watched_user_id, IBmode, IBtoken) {
	
	//Prevent multiple clicks.
	if (watchlock) return;
	
	if (IBmode == 'remove' && !confirm('Are you sure you want to unwatch this user?')) return;
	
	//Hide options box on remove.
	if (IBmode == 'remove') watchSetOptionsState(false);
	
	//Display loading effect.
	unwatchpulse = new Effect.Pulsate('unwatch_button', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
	watchpulse = new Effect.Pulsate('watch_button', { pulses: 100, duration: 60, from: 1.0, to: 0.5 });
	watchlock = true;
	
	new Ajax.Request('watchuser_process.php',
	{
		parameters: { token: IBtoken, mode: IBmode, user_id: watched_user_id },
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			var response = transport.responseText || "{'error':'no response'}";
			
			if (!transport.responseText) {
				//Stop loading effect.
				watchStopEffect('watch');
				
				watchlock = false;
			}
			
			var jsondata = response.evalJSON(true);
			if (jsondata.success=='yes') {
				
				//NOTE: watchstate global is used by Friends button to check Watch status when friending, and by watch filter options ajax.
				watchstate = IBmode=='add';
				
				if (IBmode=='add') {
					$('unwatch_button').setStyle({ display: '' });
					$('watched_tick').setStyle({ display: '' });
					$('watch_button').setStyle({ display: 'none' });
					if ($('unwatch_block')) $('unwatch_block').setStyle({ display: '' });
					if ($('watch_block')) $('watch_block').setStyle({ display: 'none' });
					//Show the suggested members div
					Effect.BlindDown('suggestedmembersbox');
					Effect.Pulsate('loadingsuggestedmembers', { pulses: 10, duration: 10 });
					suggestedmembersProcess(watched_user_id);
				} else {
					$('unwatch_button').setStyle({ display: 'none' });
					$('watched_tick').setStyle({ display: 'none' });
					$('watch_button').setStyle({ display: '' });
					if ($('unwatch_block')) $('unwatch_block').setStyle({ display: 'none' });
					if ($('watch_block')) $('watch_block').setStyle({ display: '' });
				}
				
				//Stop loading effects.
				watchStopEffect('watch');
				watchStopEffect('submissions');
				watchStopEffect('journals');
				watchStopEffect('streams');
				
				watchlock = false;
				watchfiltersubmissionslock = false;
				watchfilterjournalslock = false;
				watchfilterstreamslock = false;
				
				//Show the options box on watch, otherwise hide it.
				//watchSetOptionsState(IBmode=='add');
				
				//Always hide options box
				watchSetOptionsState(false);
				
				//Set all filter options back to the default of "on".
				watchFilterSetState('submissions', true);
				watchFilterSetState('journals', true);
				watchFilterSetState('streams', true);
				
			} else if (!jsondata.success || jsondata.success != 'yes') {
				
				//Stop loading effect.
				watchStopEffect('watch');
				
				watchlock = false;
			}
		},
		onFailure: function() { 
			
			//Stop loading effect.
			watchStopEffect('watch');
			
			watchlock = false;
		}
	});
}