/*
 * MASKS
 */
 
$.mask.masks = $.extend($.mask.masks,{ 
    number: {mask: '9999999999999999'},
    year: {mask: '2999'},
    date: {mask: '2999/19/39'},
    date_yyyymm: {mask: '2999/19'},
    time: {mask: '29:59'},
    phone: {mask: '999.999.9999'},
    zipcode: {mask: '99999-9999'},
    decimal: {mask: '99.999,999,999,999,9', type: 'reverse', defaultValue: '000' }, 
    percentage: {mask: '999-9', type: 'reverse', defaultValue: '0'}
});


/*
 * FORMAT CURRENCY
 *
 * formatNumber(input, decimanl places, thousands separator, decimanl separator, currency prefix, currency suffix, negative prefix, negative suffix) 
 */
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1=n2='';
	var y = (''+Math.abs(x)).split('');
	var z = y.length - dec; if (z<0) z--;
	for(var i = z; i < 0; i++) y.unshift('0');
	if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0');
	while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;
}


// form element hint text
function input_hints() {
	// set up
	$('.hint:input').each(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('title')).addClass('active');
		}
	});
	
	// remove on focus
	$('.hint:input').focus(function() {
		if($(this).val() == $(this).attr('title')) {
			$(this).val('').removeClass('active');
		}
	});
	
	// return on blur
	$('.hint:input').blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('title')).addClass('active');;
		}
	});
}

// form input helpers
function input_helpers() {
	// time picker
	$('input.time').timepicker();
	
	// date picker
	$('input.date').DatePicker({
		format:'Y/m/d',
		date: $(this).val(),
		current: $(this).val(),
		starts: 0,
		onBeforeShow: function() {
			if($(this).hasClass('active')) {
				$(this).DatePickerSetDate($(this).val(), true);
			}
			$(this).addClass('picker_active');
		},
		onChange: function(formated, dates) {
			$('input.date.picker_active').val(formated);
			$('input.date.picker_active').removeClass('active');
			$('input.date.picker_active').DatePickerHide();
		},
		onHide: function() {
			$('input.date.picker_active').removeClass('active');
			$('input.date.picker_active').removeClass('picker_active');
		}
	});
	
	$(window).keydown(function(event) {
		switch(event.keyCode) {
			case 27:
			case 9:
				if($('input.date.picker_active').length > 0) {
					$('input.date.picker_active').DatePickerHide();
					$('input.date.picker_active').removeClass('active');
					$('input.date.picker_active').removeClass('picker_active');
				}
				break;
		}
	});
}

function forms_setup() {
	$('input.masked:text').setMask();
	input_hints();
	input_helpers();
	$('form[validate]').validator();

	// set focus for log in form
	if($('#authentication input[name=username]').val() == '') {
		$('#authentication').find('input:first').focus();
	} else {
		$('#authentication input[name=password]').focus();
	}
}

function row_shading() {
	$('table.data').not('.paginate, .rowdetails, .noshading').find('tbody tr:even').addClass('odd');
	$('table.rowdetails tbody').find('tr:nth-child(4n+1), tr:nth-child(4n+2)').addClass('odd');
}

function row_details() {
	$('table.rowdetails tr').not('.details, .nodetails').click(function(e) {
		e.stopPropagation();
		
		if(e.target.tagName != 'A') {		
			$(this).toggleClass('expanded');
			$(this).closest('tr').next('.details').toggle();
		}
	});
}

function row_select() {
	$('table.selectrow tr').not('.disabled').click(function() {
		$(this).find('td:first input').attr('checked', 'checked');
	});
}

function datatable_setup() {
	row_shading();
	row_details();
	row_select();
}

function global_setup() {
	datatable_setup();
	forms_setup();
}

function cleanup() {
}

//$(document).ready(setup);
$(window).load(global_setup);
