var DEFAULT_TIMEOUT = 4 * 1000; // 4 seconds * 1000 milliseconds

var customPresetRadioBtn = null;
var relationshipsToBlockSelect = null;

var friendFollowerRatioOnChk = null;
var maxFriendFollowerRatioInput = null;
var maxFriendFollowerRatioSlider = null;

var tweetNumberOnChk = null;
var minTweetsInput = null;
var minTweetsSlider = null;

var onOffSwitches = null;
var yesNoSwitches = null;
var CUSTOM_CHECKBOX_EVENT_TYPE = "checkboxchanged";

function selectCustom() {
	customPresetRadioBtn.attr("checked", "checked");
	selectPreset(customPresetRadioBtn);
	$('.reset_values').fadeIn();
}
function selectPreset(preset) {
	$('.presets label').removeClass('selected');
	$(preset).parent().addClass('selected');
}
function setSliderValue(event) {
	var curId = $(this).attr('id');
	var curVal = $(this).val();
	$('#'+curId+'_slider').slider('value', curVal);
	selectCustom();
}
$(function () {
	customPresetRadioBtn = $("input:radio[value=custom]");
	relationshipsToBlockSelect = $('#block_setting_relationships_to_block');

	friendFollowerRatioOnChk = $('#block_setting_friend_follower_ratio_on');
	maxFriendFollowerRatioInput = $('#block_setting_max_friend_follower_ratio');
	maxFriendFollowerRatioSlider = $('#block_setting_max_friend_follower_ratio_slider');

	tweetNumberOnChk = $("#block_setting_tweet_number_on");
	minTweetsInput = $('#block_setting_min_tweets');
	minTweetsSlider = $('#block_setting_min_tweets_slider');

	onOffSwitches = $(':checkbox.toggleSwitch:not(.yesNo)');
	onOffSwitches.iphoneStyle();

	yesNoSwitches = $(':checkbox.toggleSwitch.yesNo');
	yesNoSwitches.iphoneStyle({
		checkedLabel: 'YES',
		uncheckedLabel: 'NO'
	});

	maxFriendFollowerRatioSlider.slider({
		animate: true,
		max: 100,
		min: 1,
		step: 1,
		value: (maxFriendFollowerRatioInput.val() > 0 ? maxFriendFollowerRatioInput.val() : 0),
		slide: function(event, ui) {
			maxFriendFollowerRatioInput.val(ui.value);
			selectCustom();
		}
	});

	minTweetsSlider.slider({
		animate: true,
		max: 30,
		value: (minTweetsInput.val() > 0 ? minTweetsInput.val() : 0),
		slide: function(event, ui) {
			minTweetsInput.val(ui.value);
			selectCustom();
		}
	});

	/*
	 if ($.browser.msie) {
	 $('.presets :radio').click(function () {
	 var btn = $(this);
	 btn.blur();
	 btn.focus();
	 });
	 }
	 */

	// set actual values to those of presets when they're clicked
	$('.presets :radio').change(function () {
		selectPreset(this);
		var preset = presets[$(this).val()];
		setValues(preset);

		$('.reset_values').fadeIn();
	});

	// set the preset to custom when the input value is manually changed 
	$.each([maxFriendFollowerRatioInput,minTweetsInput], function() {
		$(this).keyup(selectCustom);
	});

	// set the preset to custom when the relationship select is changed
	$.each([relationshipsToBlockSelect], function () {
		$(this).change(selectCustom);
	});

	$('a[rel*=facybox]').each(function () {
		var anchor = $(this);
		var link = anchor.attr('href');
		if (link.indexOf('?') != -1) {
			link = link + "&popover=true";
		} else {
			link = link + "?popover=true";
		}
		anchor.attr('href', link);
		anchor.facybox();
	});

	setTimeout(function () { $('.messages').slideUp(); }, DEFAULT_TIMEOUT);

	$('.help_icon').qtip({
		style: {
			name: 'blue',
			tip: true
		},
		position: {
			corner: {
				target: 'rightMiddle',
				tooltip: 'leftMiddle'
			}
		}
	});
	$('.bottom_right_help_icon').qtip({
		style: {
			name: 'blue',
			tip: true
		},
		position: {
			corner: {
				target: 'bottomRight',
				tooltip: 'topRight'
			}
		}
	});
	$('.middle_left_help_icon').qtip({
		style: {
			name: 'blue',
			tip: true
		},
		position: {
			corner: {
				target: 'leftMiddle',
				tooltip: 'rightMiddle'
			}
		}
	});

	$('#share_btn').live("click", function () {
		var dataStr = 'status='+encodeURIComponent($('#share_message').val());
		$.post('/share/update_status', dataStr, function () {
			$.facybox("<div id=\"popover_content\"><h3>Thank you for sharing StopTweet</h3><p>We now return to your regularly scheduled broadcast.</div>");
			setTimeout(closeSharePopover, DEFAULT_TIMEOUT);
		});
		var follow_us = $('#follow_us:checked').val();
		if (follow_us) {
			$.get('/share/follow_stoptweet');
		}
	});

	$('.highlight_when_selected').focus(function () {
		var cur = $(this);
		if (cur.val() == cur.attr('title')) cur.val('').css('color','#000');
	}).blur(function () {
		var cur = $(this);
		if (cur.val() == '') cur.val(cur.attr('title')).css('color','#777');
	}).each(function () {
		var cur = $(this);
		if (cur.val() == cur.attr('title'))
			cur.css('color','#777');
		else
			cur.css('color','#000');
	});

	$('.reset_values a').click(function () {
		resetValues();
		return false;
	});

	$('.edit_notification_prefs').click(function () {
		$("#existingPrefs").fadeOut(function () {
			$("#notificationsForm").slideDown();
		})
	});
});

function closeSharePopover() {
	$.facybox.close();
}

function setValues(values) {
	if (typeof values != 'undefined') {
		relationshipsToBlockSelect.val(values["relationships_to_block"]);

		if (friendFollowerRatioOnChk.attr("checked") != values["friend_follower_ratio_on"]) {
			if (values["friend_follower_ratio_on"]) {
				friendFollowerRatioOnChk.attr("checked", "checked");
			} else {
				friendFollowerRatioOnChk.removeAttr("checked");
			}
			friendFollowerRatioOnChk.parent('.iPhoneCheckContainer').mouseup();
		}
		maxFriendFollowerRatioInput.val(values["max_friend_follower_ratio"]);
		maxFriendFollowerRatioSlider.slider('value', values["max_friend_follower_ratio"]);

		if (tweetNumberOnChk.attr("checked") != values["tweet_number_on"]) {
			if (values["tweet_number_on"]) {
				tweetNumberOnChk.attr("checked", "checked");
			} else {
				tweetNumberOnChk.removeAttr("checked");
			}
			tweetNumberOnChk.parent('.iPhoneCheckContainer').mouseup();
		}
		minTweetsInput.val(values["min_tweets"]);
		minTweetsSlider.slider('value', values["min_tweets"]);
	}
}

function resetValues() {
	if (currentSettings.is_light) {
		selectPreset($('.presets :radio[value=light]'));
		$('.presets :radio[value=light]').attr("checked", "checked");
	} else if (currentSettings.is_medium) {
		selectPreset($('.presets :radio[value=medium]'));
		$('.presets :radio[value=medium]').attr("checked", "checked");
	} else if (currentSettings.is_aggressive) {
		selectPreset($('.presets :radio[value=aggressive]'));
		$('.presets :radio[value=aggressive]').attr("checked", "checked");
	} else { // set custom
		selectCustom();
		$('.presets :radio[value=custom]').attr("checked", "checked");
	}
	setValues(currentSettings["custom_settings"]);
	$('.reset_values').fadeOut();
}

