var searchSentinel = '';

function descriptionFix() {
    small = $('input ~ small, select ~ small, textarea ~ small');
    small.each(function (i, row) {
        $(row).attr("title",  $(row).html());
        $(row).html("&nbsp;");
        $(row).removeClass("hidden");
    })
}

function showPrintDialog() {
	$("div#services div.email-form").toggle('slow');
}

function searchRegister() {
	if(!$('#search-field').length) {
		return;
	}
	
	searchSentinel = $('#search-field').val();
	
	var diff = $('#search-field').position().top  - $('#search-button').position().top;
	if(diff != 0) {
		$('#search-field').css('position', 'relative');
		$('#search-field').css('top', -diff+'px');
	}
}

function searchOn() {
	$('#search-field').addClass('focused');
	
	if($('#search-field').val() == searchSentinel) {
		$('#search-field').val('');
		$('#search-button').removeClass('blocked');
	}
}

function searchOut() {
	if(!$('#search-field').val()) {
		$('#search-field').removeClass('focused');
		$('#search-field').val(searchSentinel);
		$('#search-button').addClass('blocked');
	}
}

function searchFire() {
	return ($('#search-field').val() != searchSentinel);
}

function doAlert(validated_element, message) {
	var id = $(validated_element).attr('id');
	//alert(id);
	
	if(!$(validated_element).parents('table').next('div[class=error-box]').length) {
		$(validated_element).parents('table').after('<div class="error-box"></div>');
	}
	
	var box = $(validated_element).parents('table').next('div[class=error-box]');
	
	$(validated_element).addClass('form-control-error');
	
	if($('#err-msg-'+id).length) {
		if($('#err-msg-'+id).html() != message) {
			$('#err-msg-'+id).html(message);
		}
	}
	else {
		box.append('<div id="err-msg-'+id+'">'+message+'</div>');
	}
}

function doRemove(validated_element) {
	var id = $(validated_element).attr('id');
	
	$(validated_element).removeClass('form-control-error');
	$('#err-msg-'+id).remove();
}

$(document).ready(function() {
	// fancybox
	$("#content a > img").parent().each(function (i, o) {
		if($(o).width() > 20) {
			if(!$(o).attr('title')) {
				$(o).attr('title', $(o).children('img').attr('alt'));
			}
			
			$(o).fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	600, 
				'speedOut'		:	200, 
				'overlayShow'	:	true,
				'onStart'       :   function() {if($.browser.msie) {$("object").css('visibility', 'hidden')}},
				'onClosed'       :  function() {if($.browser.msie) {$("object").css('visibility', 'visible')}}
			});
			
			$(o).addClass('img');
			
			/*var title = $(o).attr('title');
			
			if(title) {
				$(o).wrap('<div class="img-wrap" style="float: left;" />');
				$(o).after('<span>'+title+'</span><div class="cleaner"></div>');
			}*/
		}
	});
	
	searchRegister();
});

