$(document).ready(function() {
	
	// Submit button - disable on click
	$('form').submit(function(){
		$('input[type=submit]:not(.no-disable)', this).attr('disabled', 'disabled');
	});
	
	// Error class on inputs before a field-error
	$('span.field-error').each(function() {
		$(this).parent().find('input,select,textarea').addClass('field-error');
		$(this).parent().next('td.field-info').prepend(this);
	});
	
	
	$('input[title]').each(function() {
		// on focus - make text empty if it is set to emptytext
		$(this).focus(function() {
			if ($(this).attr('value') == $(this).attr('title')) {
				$(this).attr('value', '').removeClass('placeholder-text');
			}
		});
		
		// on blur - make text emptytext if it is empty
		$(this).blur(function() {
			if ($(this).attr('value') == '') {
				$(this).attr('value', $(this).attr('title')).addClass('placeholder-text');
			}
		});
		
		// run the blur event
		$(this).blur();
	});
	
	// on submit - make text empty if it is set to emptytext 
	$('input[title]').closest('form').submit(function() {
		$(this).find('input[title]').each(function() {
			if ($(this).attr('value') == $(this).attr('title')) {
				$(this).attr('value', '');
			}
		});
	});
	
	
	// Expando divs
	$('div.expando').each(function() {
		var $div = $(this);
		var $expander = $(this).prev('h2,h3');
		
		if ($expander.length == 0) {
			$(this).before('<p><a href="javascript:;">' + ($(this).attr('title') ? $(this).attr('title') : 'More information') + '</a></p>');
			$expander = $(this).prev().find('a');
		}
		
		
		
		if ($expander.is('a')) {
			$expander.addClass('expando-opener-link');
			$expander.parent().addClass('expando-opener-para');
			
			$expander.click(function() {
				$div.toggle();
				$div.toggleClass('expanded');
				$expander.toggle();
				$expander.toggleClass('expanded');
				return false;
			});
			
			
		} else if ($expander.is('h2,h3')) {
			$expander.addClass('expando-opener-heading');
			$expander.css('cursor', 'pointer');
			
			$expander.click(function() {
				$div.toggle();
				$div.toggleClass('expanded');
				$expander.toggleClass('expanded');
				return false;
			});
			
		}
		
		
		$div.hide();
		
		var $close = $('<p><a href="javascript:;">Close</a></p>');
		$close.appendTo($div);
		$close.addClass('expando-closer-para');
		$close.find('a').addClass('expando-closer-link');
		
		$close.click(function() {
			$expander.click();
		});
	});
	
	
	
	// Refine add
	$('div.refine-bar div.refine-add select').change(function() {
		if ($(this).val() == '') return;
		
		$('div.refine-bar div.refine-list > div.' + $(this).val()).show().find('input,select').focus();
		
		$('div.refine-bar div.refine-submit').show();
		
		$(this).val('');
	});
	
	// Refine remove
	$('div.refine-bar div.refine-list img.remove').click(function() {
		$(this).parent().find('input,select').val('');
		$(this).parent().hide();
		
		if ($('div.refine-bar div.refine-list > div:visible').length == 0) {
			$('div.refine-bar div.refine-submit').hide();
			$('div.refine-bar').parent('form').submit();
		}
	});
	
	// Hide the button if no refine fields
	if ($('div.refine-bar div.refine-list > div:visible').length == 0) {
		$('div.refine-bar div.refine-submit').hide();
	}
	
	$('div.toggle-strip div.ts-item').hover(function() {
		$(this).addClass('ts-over');
	}, function() {
		$(this).removeClass('ts-over');
		
	}).click(function () {
		$(this).parent().find('div.ts-item').removeClass('ts-on');
		$(this).addClass('ts-on');
		$(this).parent().find('input').val($(this).attr('data-id'));
	});
	
});

