
// Method to apply JS mouseover. Always apply to IMG object itself. If you 
// want to bind the event to the closest parent A object instead of the IMG 
// object, pass the second var as true. File naming/saving convention is 
// APP/plugins/adm/vendors/img/{base_filename}[_over].png
$.fn.extend({
	applyMouseover: function(base_filename, closestA) {
		return this.each(function() {
			
			// Preload mouseover image
			$('<img>').attr('src', '/adm/img/'+base_filename+'_over.png');
			
			// Gather vars
			var img = $(this);
			if (typeof closestA !== 'undefined' && closestA) {
				obj = $(this).closest('a');
			} else {
				obj = $(this);
			}
			
			// Bind events
			obj
			.mouseover(function() {
				img.attr('src', '/adm/img/'+base_filename+'_over.png');
			})
			.mouseout(function () {
				img.attr('src', '/adm/img/'+base_filename+'.png');
			});
		});
	}
});

function d(v) {
	if (appConfig.debug > 0) {
		if (typeof console !== 'undefined') {
			console.log(v);
		} else {
			alert(v);
		}
	}
}

// Tooltips
$(function() {
	var defaultContent = {
		text: false
	};
	var defaultPosition = {
		adjust: { 
			screen: true 
		}
	};
	var defaultStyle = {
		tip: true, // Give it a speech bubble tip
		border: {
			width: 1, 
			radius: 2, 
			color: '#B2DF61'
		}
	};
	$('.help img').each(function () {
		var message = $(this).next('span').html();
		$(this).qtip({
			content: message,
			position: defaultPosition,
			style: defaultStyle
		});
	});
	$('.help .handle').each(function () {
		var message = $(this).next('span').html();
		$(this).qtip({
			content: message,
			position: defaultPosition,
			style: defaultStyle
		});
	});
	$('a[href][title], input[title]').qtip({
		content: defaultContent,
		position: defaultPosition,
		style: defaultStyle
	});
	$('.site_search input').qtip({
		content: { text: 'Enter your search terms and press Enter.' },
		position: defaultPosition,
		show: { when: { event: 'focus' } },
		hide: { when: { event: 'unfocus' } },
		style: defaultStyle
	});
});
	
// Site search
$(function() {
	$('.site_search input').each(function() {
		if (this.value === '') {
			this.value = 'Site search';
			$(this).removeClass('full');
		}
		if (this.value !== 'Site search') {
			$(this).addClass('full');
		}
	})
	.focus(function() {
		if (this.value === 'Site search') {
			this.value = '';
		}
		$(this).addClass('full');
	})
	.blur(function() {
		if (this.value === '') {
			this.value = 'Site search';
			$(this).removeClass('full');
		}
	});
});
	
// Form feel
$(function() {

	// Flash message highlight
	$('.flash_message').each(function() {
		var color = $(this).hasClass('bad') ? '#DF2929' : '#008000';
		$(this).animate({
			backgroundColor: color
		}, 2500);
	});
	
	// Change the style of the focused TD
	$('td.write')
	.find('input, select, textarea')
	.focus(function() {
		$(this).closest('td').addClass('active');
	})
	.blur(function() {
		$(this).closest('td').removeClass('active');
	});

	// Toggle checkboxes by clicking on their TD
	$('td.write > div.input.checkbox > input[type=checkbox]').each(function() {
		var cb = $(this);
		cb
		.click(function() {
			cb.attr('checked', !cb.attr('checked'));
			cb.closest('form').data('changed', true);
		})
		.closest('td, th')
		.click(function() {
			cb.attr('checked', !cb.attr('checked')).focus();
			cb.closest('form').data('changed', true);
		});
	});

	// Focus text boxes by clicking on their TD
	$('td.write').find('input[type=text]:first').each(function() {
		var input = $(this);
		input
		.closest('td')
		.click(function() {
			input.focus();
		});
	});

	// Form controls follow page
	$('.controls').each(function() {
		var el = $(this);
		var offset = el.offset().top;
		var width = el.css('width');
		var form = el.closest('form');
		var formBottom = form.offset().top + form.height() - el.height();
		el.wrap('<div style="height: '+el.height()+'px;"></div>');
		$(window).scroll(function() {
			var scroll = $(document).scrollTop();
			if (scroll > offset && scroll < formBottom) {
				if (el.css('position') === 'static') {
					el.css({
						'position' : 'fixed',
						'top' : '0',
						'width' : width,
					});
				}
			} else {
				if (el.css('position') === 'fixed') {
					el.css('position', 'static');
				}
			}
		});
	});
});

// Preload/mouseover
$(function() {
	$('.help img').applyMouseover('help');
	$.each(['edit', 'delete', 'close', 'view', 'help', 'add', 'download'], function() {
		var key = this;
		$('<img>').attr('src', '/adm/img/'+key+'_over.png');
		$('a img.'+key).each(function() {
			var img = $(this);
			$(this).applyMouseover(key, true);
		});
	});
	$('.direction').each(function() {
		$(this).css('padding-left', '0').prepend('<img src="/adm/img/direction.png" width="16" height="16" alt="Info" class="icon" />');
	});
	$.each(['edit', 'delete', 'close', 'view', 'help', 'add', 'download'], function() {
		var key = this;
		$('<img>').attr('src', '/adm/img/'+key+'_over.png');
		$('a img.'+key).each(function() {
			var img = $(this);
			$(this).applyMouseover(key, true);
		});
	});
});

// Quick edit jumper
$(function() {
	$('.record_selector').change(function() {
		if (this.value !== '') {
			window.location.href = '/adm/adm_edit/select/'+appConfig.modelAlias+'/'+this.value;
		}
	});
});

// Delete confirmation
$(function() {
	$('.delete_link').click(function() {
		return confirm(appConfig.deletionMessages[this.rel]);
	});
});
function deleteConfirmRedirect(href) {
	if (!confirm(appConfig.deletionMessage)) {
		return false;
	}
	window.location.href = href;
}

// Dirty form
$(function() {
	var main_form = $('form.main');
	main_form
	.data('changed', false)
	.find('input, select, textarea')
	.change(function() {
		main_form.data('changed', true);
	});
	main_form.submit(function() {
		window.onbeforeunload = null;
	});
	window.onbeforeunload = function() {
		if (main_form.data('changed')) {
			return 'If you leave this page before saving your changes, your changes will be lost.';
		}
	};
});

// Target _new
$(function() {
	$('a.new').attr('target', '_blank');
});

