//------------------------------------------------------[MJ]
// Public: JS
//------------------------------------------------------[MJ]

// On Ready --------------------------------------------[MJ]
$(document).ready(function() {
	// Hover state for links -------------------------------[MJ]
	$('.Link').hover(
		function() {
			$(this).children('div').fadeIn('fast');
		},
		function() {
			if ( $(this).children('div').hasClass('Current'))
				return;

			$(this).children('div').fadeOut('fast');
		}
	);
	//------------------------------------------------------[MJ]
});
//------------------------------------------------------[MJ]

// Login -----------------------------------------------[MJ]
function SubmitLogin() {
	var myVars = {
		'Username'		: $('#Login_Username').val(),
		'Password'		: $('#Login_Password').val(),
		'RememberMe'	: ($('#Login_RememberMe').attr('checked') ? 'Yes' : 'No')
	};

	$.post('/ajx.login.process/', myVars, LoginFormCallback, 'json');
}
//------------------------------------------------------[MJ]

// Login User ------------------------------------------[MJ]
function LoginFormCallback(myReturn) {
	if(myReturn['Status'] == 'Success') {
		// Do something if no errors
		window.location.reload();
	} else {
		// Error
		$('.LoginCloud').fadeIn('fast');		

		var HTML = '';
		for(i = 0; i < myReturn['ErrorMsg'].length; i++) {
			HTML += myReturn['ErrorMsg'][i] + '<br />';
		}

		$('#LoginError').html(HTML);
	}
	return false;
}
//------------------------------------------------------[MJ]

// Top Rail Login --------------------------------------[MJ]
function SubmitTopRailLogin() {
	var myVars = {
		'Username'		: $('#Login_Username').val(),
		'Password'		: $('#Login_Password').val(),
		'RememberMe'	: ($('#Login_RememberMe').attr('checked') ? 'Yes' : 'No')
	};

	$.post('/ajx.login.process/', myVars, LoginFormTopRailCallback, 'json');
}
//------------------------------------------------------[MJ]

// Top Rail Login User ---------------------------------[MJ]
function LoginFormTopRailCallback(myReturn) {
	if(myReturn['Status'] == 'Success') {
		// Do something if no errors
		window.location.reload();
	} else {
		// Error
		$('.LoginCloud').fadeIn('fast');

		var HTML = '';
		for(i = 0; i < myReturn['ErrorMsg'].length; i++) {
			HTML += myReturn['ErrorMsg'][i] + '<br />';
		}

		$('#LoginError').html(HTML);
	}
	return false;
}
//------------------------------------------------------[MJ]

// Logout User -----------------------------------------[MJ]
function SubmitLogout() {
	var myVars = {};
	$.post('/ajx.logout.process/', myVars, function(){ window.location.reload(); });
}
//------------------------------------------------------[MJ]

// Add Comment -----------------------------------------[MJ]
function PostComment() {
	var myVars = {
		'Username'		: $('#Comment_Username').val(),
		'Comment'		: $('#Comment_Comment').val(),
		'EntryID'		: $('#Comment_EntryID').val(),
		'ImageCode'		: $('#Comment_Captcha').val()
	};

	$.post('/blog/addcomment.process/', myVars, PostCommentCallback, 'json');
}
//------------------------------------------------------[MJ]

// Add Comment Callback --------------------------------[MJ]
function PostCommentCallback(myReturn) {
	if(myReturn['Status'] == 'Success') {
		// Do something if no errors
		$('#Comment_Comment').val('');
		$('#Comment_Captcha').val('');
		window.location.reload();
	} else {
		// Error
		$('#BlogError').fadeIn('fast');

		var HTML = '';
		for(i = 0; i < myReturn['ErrorMsg'].length; i++) {
			HTML += myReturn['ErrorMsg'][i] + '<br />';
		}		
		$('#BlogErrorTxt').html(HTML);
		$('.CaptchaImg img').attr('src', '/blog/captcha/?Rand=' + Math.random());
	}
	return false;
}
//------------------------------------------------------[MJ]


// Poll --------------------------------------------[MJ][LB]
function PollLoaded() {
	$('.PollSubmit').click(PollSubmit);
	$('.PollResult').click(PollResult);
}

function PollSubmit() {
	// Error check
	

	// Submit to process
	var	PollData = {
		'PollID'	: $('#PollID').val(),
		'ChoiceID'	: $('.PollChoice:checked').val()
	};

	$.post('/poll.process/', PollData, PollResult);

	return false;
}

function PollResult() {
	$('.Poll').load('/poll.result/?PollID=' + $('#PollID').val());
	return false;
}
/*
function PollLoaded() {
	$('.PollSubmit').click(PollSubmit);
	$('.PollResult').click(PollResult);
}

function PollSubmit() {
	var FormID = '#' + $(this).parent().parent().attr('id');

	if($(FormID + ' input:checked').length == 0) {
		alert('Please choose a poll option.');
		return false;
	}

	var	PollData = {
		'PollID'	: $(FormID + ' .PollID').val(),
		'ChoiceID'	: $(FormID + ' .PollChoice:checked').val()
	};

	$.post('/poll.process/', PollData, function(){
		PollResult(PollData.PollID);
	});

	return false;
}

function PollResult(PollID) {
	if (!parseInt(PollID)) {
		PollID = $(this).parent().parent().parent().find('#PollID').val();
	}

	$('#Poll_' + PollID).load('/poll.result/?PollID=' + PollID);

	return false;
}
*/
// -------------------------------------------------[MJ][LB]


// Back Issue Class ------------------------------------[TG]
var VPI_BackIssue = function( Config ) {
	var This = function() {

		// Private Vars ----------------------------------------[TG]

		//------------------------------------------------------[TG]
		
		// Public Vars -----------------------------------------[TG]
		This._Config = { };
		//------------------------------------------------------[TG]

		// Constructor -----------------------------------------[TG]
		(function() {
			This._Config = $.extend(This._Config, Config);
		})();
		//------------------------------------------------------[TG]
		
		// Public Functions ------------------------------------[TG]
		This.Scroll = function(Direction) {
			if (!$('.BackIssues:animated').get(0))
				$('.BackIssues').animate( { scrollTop: ((Direction == 'Up') ? 0 : $('.BackIssues').height()) }, 500);
		}
		//------------------------------------------------------[TG]

		// Private Functions -----------------------------------[TG]
		function Debug() {

		}
		//------------------------------------------------------[TG]

		// Send Back Reference ---------------------------------[TG]
		return This;
		//------------------------------------------------------[TG]

	};
	return This();
};
var VBI = new VPI_BackIssue();
//------------------------------------------------------[TG]

//-----------------------------------------------------[ADL]
function setDays() {
	var y = document.getElementById('Year').options[document.getElementById('Year').selectedIndex].value;
	var m = document.getElementById('Month').selectedIndex;
	var d;

	// find number of days in current month
	if ( (m == 4) || (m == 6) || (m == 9) || (m == 11) ) {
		days = 30;
	}
	else if (m == 2) {
		// check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
		if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
		  days = 29
		else
		  days = 28
	}
	else {
		days = 31;
	}

	// if (days in new month > current days) then we must add the extra days
	if (days > document.getElementById('Day').length) {
		for (i = document.getElementById('Day').length; i < days; i++) {
			document.getElementById('Day').length = days;
			document.getElementById('Day').options[i].text = i + 1;
			document.getElementById('Day').options[i].value = i + 1;
		}
	}
  
	// if (days in new month < current days) then we must delete the extra days
	if (days < document.getElementById('Day').length) {
		document.getElementById('Day').length = days;
		if (document.getElementById('Day').selectedIndex == -1) 
			document.getElementById('Day').selectedIndex = days - 1;
	}
}
//-----------------------------------------------------[ADL]

// Event Delegation ------------------------------------[TN]
jQuery.delegate = function(Rules) {
	return function(e) {
		var Target = $(e.target);
		for (var Selector in Rules)
			if (Target.is(Selector)) return Rules[Selector].apply(this, $.makeArray(arguments));
	}
}
//------------------------------------------------------[TN]