// Show / Hide of an element (elem's innerHTML must be "[+]" or "[-]"
function toggleShowHide(elem, targetElem) {
	if (elem.innerHTML == '[+]') { // Show
		$('#'+targetElem).slideDown();
		elem.innerHTML = '[-]';
	}
	else {
		$('#'+targetElem).slideUp();
		elem.innerHTML = '[+]';
	}
	return false;
}

function validatePassword() {
	if (validate_field(document.frmPassword.chgpwd_password, "New Password", TEXT) &&
			validate_field(document.frmPassword.chgpwd_password2, "Confirm New Password", TEXT) &&
			matchPasswords(document.frmPassword.chgpwd_password, document.frmPassword.chgpwd_password2)) {
		return true;
	}
	return false;
}

// Timer for session time out
var stoCount = 0; // in seconds
var stoTimer = null;
var stoTarget = 0;
function tick() {
	stoCount += 1;
	if (stoCount == stoTarget - 300 - 10) { // 10 is buffer
		alert("This session is going to expire in 5 minutes if you do not do anything, please remember to save your works!");
	}
	if (stoCount == stoTarget - 10) {
		alert("Session time out. Please login again");
	}
}

function startTicking(target) {
	stoTarget = target;
	stoTimer = setInterval(tick, 1000);
}

// On load
$(document).ready(function() {
	$('#loadingWrapper').hide();
	
	jQuery.validator.addMethod('ckeditor', function(value, element) {
		if (CKEDITOR.instances[element.name].getData() != '') {
			return true;
		}
		else {
			return false;
		}
	}, 'This field is required.'); 

	// Change password functions
	$("#dialogChangePassword").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false, 
		resizable: false,
		title: 'Message',
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});

	$('#inlineChangePassword').dialog({
		title:'Change Administrator Password',
		autoOpen: false, 
		width: 300,
		modal: true
	});

	$('#frmPassword').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			return validatePassword();
		},

		success: function(responseText, statusText) {
			$('#inlineChangePassword').dialog('close');
			$("#dialogChangePassword").dialog('open');
		}
	});

	$('#changePasswordLink').click(function() {
		$('#inlineChangePassword').dialog('open');
	});
	
	// System info
	$('#systemInfoLink').colorbox({
		iframe:true, 
		width:850, 
		height:550
	});

	// Animated menu
	var menuTimer;
	$('ul.menu > li').hover(
		function() {
			if (menuTimer) {
				clearTimeout(menuTimer);
			}
			$('ul.menu > li ul').hide();
			$(this).children('ul').fadeIn(150);
		},
		function() {
			menuTimer = setTimeout(function() {
				$('ul.menu > li ul').slideUp(200);
			}, 400);
		}
	);
	
	// Smooth scrolling
	$(function() {
		$('a[href*=#]').click(function() {
			var hostname = location.hostname;
		
			jQuery.each(jQuery.browser, function(i, val) {
				if($.browser.safari){
					hostname = location.host; // location.host / location.hostname
				}
			});

			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
					&& hostname == this.hostname 
			) {
				var $target = $(this.hash);
				if (this.hash.slice(1) == '') {
					return true;
				}
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 800);
					return false;
				}
			}
		});
	});
});

window.onbeforeunload = function() {
	$('#loadingWrapper').show();
	var mTimer = 0;
	mTimer = setTimeout(function() {
		$('#loadingWrapper').hide();
	}, 3000);
}
