﻿
$(document).ready(function() {
	// append &status= message to the end of our url
	$('a.replyHref').click(function() {
		var statusMessage = $('#statusMessage').val();
		var hrefUrl = $(this).attr('href');
		if (statusMessage == null) {
			statusMessage = '';
		}
		$(this).attr('href', hrefUrl + '&status=' + statusMessage);
	});

	// autopostback for character drop down list - sets default character
	$('#playerCharacters').change(function() {
		var charId = $('#playerCharacters option:selected').val();
		var returnUrl = window.location;
		var getUrl = "/Character/SetDefault?characterId=" + charId + "&dd=1" + "&ReturnUrl=" + returnUrl;
		window.location = getUrl;
	});

	// set the tooltips for mouseover on character profile picture
	$(".profileAnchor").tooltip({
		tip: '.profileTooltip',
		position: ['center', 'right'],
		opacity: 0.92,
		offset: [10, 10]
	});
	// set tooltips for image mouseovers in tweets
	$("a.screenshotIcon").tooltip({
		tip: 'div.screenshotTooltip',
		position: ['bottom', 'right'],
		opacity: 0.92,
		offset: [-10, 5],
		delay: 250
	});
	// set tooltips for image mouseovers in tweets
	$("a.moreCharacters").tooltip({
		tip: 'span.moreCharactersTooltip',
		position: ['center', 'right'],
		opacity: 0.92,
		offset: [5, 5],
		delay: 1
	});
});

// autopostback when drop down is changed on screenshots listings page
function sortScreenshotsDDL() {
	var sortBy = $('#sortScreenshotsBy').val();
	var hrefUrl = window.location.pathname + '?sort=' + sortBy;
	// include the tid if it's present
	var tid = $(document).getUrlParam("tid");
	if (tid != null)
		hrefUrl += "&tid=" + tid;
	window.location = hrefUrl;
}

// updates the character counter
function update_count(field_name, div_name) {
	var new_count = 140 - field_name.value.length;
	document.getElementById(div_name).innerHTML = new_count;
}

// a more generic confirmation that will accept a custom message
function confirmMessage(message) {
	return confirm(message);
}
// confirmation dialog for reporting screenshots/tweets
function confirmReport() {
	return confirm("Are you sure you want to report this?");
}
// confirmation dialog for deleting screenshots
function confirmDeleteSS() {
	return confirm("Permanently delete screenshot?");
}
// confirmation dialog for deleting tweets
function confirmTweetDelete() {
	return confirm("Permanently delete this tweet?");
}

// returns a timestamp. Used to avoid IE caching Ajax requests
function nocacheTimestamp() {
	tstmp = new Date();
	return tstmp.getTime();
}

// check to make sure input isn't potentially dangerous
function checkIfDangerous(msg) {
	var isBadValue = false;

	// <3 is allowed and < followed by space too, everything else isn't
	var firstCharIndex = msg.indexOf('<');
	var firstChar = '';
	if (firstCharIndex < msg.length)
		firstChar = msg.charAt(firstCharIndex + 1);
	if ((firstCharIndex != -1) && !(firstChar == '3' || firstChar == ' ')) {
		isBadValue = true;
	}

	// if > is preceeded by a space allow it
	var firstCharIndex2 = msg.indexOf('>');
	var firstChar2 = '';
	if (firstCharIndex2 > 0)
		firstChar2 = msg.charAt(firstCharIndex2 - 1);
	if ((firstCharIndex2 != -1) && !(firstChar2 == ' ')) {
		isBadValue = true;
	}

	// if it looks like <tag> don't allow it
	if (msg.indexOf('<') != -1 && (msg.indexOf('<') < msg.indexOf('>'))) {
		isBadValue = true;
	}
	return isBadValue;
}