
var $j = jQuery.noConflict();



function styleFileInputs() {

	if(!$j.browser.msie) {

	$j('input[type=file]')
		.css({'opacity' : '0', 'position' : 'relative', 'z-index' : '99'})
		.before('<div class="fake-file"></div>')
		.each(function(i){
	
			var input_id = $j(this).attr('id');
			var the_value = $j(this).attr('value');
			var the_class = $j(this).attr('class');
			var the_label = $j('label[for='+input_id+']').text();
		
			$j('p.' + input_id +' div.fake-file').text(the_label).addClass(the_class);
	
		}).change(function() {
		
			var input_id = $j(this).attr('id');
			var the_value = $j(this).attr('value');
		
			$j('p.' + input_id +' div.fake-file').text(the_value);
			
		});
	}

}

function labelsInInputs() {

	//place labels inside inputs
	$j('input[type=text], input[type=password], textarea.reg').each(function(i){
	
		var input_id = $j(this).attr('id');
		var the_value = $j(this).attr('value');
		var the_label = 'label[for='+input_id+']';
		var the_label_text = $j(the_label).text();
		
		$j(the_label).css({'color' : '#999', 'display' : 'block', 'font-size': '1.2em', 'left' : '13px', 'position' : 'absolute', 'top' : '8px'});
		
		if(the_value != '') {
			//$j(this).attr({ value : the_label_text });
			$j(the_label).css({'display' : 'none'});
		}	
	
	}).focus(function(){
	
		var input_id = $j(this).attr('id');
		var the_label = 'label[for='+input_id+']';
		var the_label_text = $j(the_label).text();
	
		if ($j(this).val() == '') {
			$j(the_label).css({'color' : '#DDD'});
		}
	
	}).keyup(function(){
	
		var input_id = $j(this).attr('id');
		var the_label = 'label[for='+input_id+']';
		var the_label_text = $j(the_label).text();
	
		if ($j(this).val() != '') {
			$j(the_label).css({'display' : 'none'});
		}
	
	}).blur(function(){
	
		var input_id = $j(this).attr('id');
		var the_label = 'label[for='+input_id+']';
		var the_label_text = $j(the_label).text();
	
		if ($j(this).val() == '') {
			$j(the_label).css({'color' : '#AAA', 'display' : 'block'});	
		}
	
	});


}



$j(document).ready(function(){

	// initialise label states
	$j('label').css({'display' : 'none'});
	$j('p.showlabel label').css({'display' : 'block'});
	$j('p.checkbox label, p.radio label, div.checkbox label').css({'display' : 'inline'});

	// place labels inside inputs
	labelsInInputs();
	
	
	// clear labels from empty fields when forms are submitted
	$j('form').submit(function() {
		$j('input[type=text]').each(function(i) {
			var input_id = $j(this).attr('id');
			var the_label = 'label[for='+input_id+']';
			var the_label_text = $j(the_label).text();
			
			if ($j(this).val() == the_label_text) {
				$j(this).attr({ value : '' });
			}		
		
		});
	
	});
	


	$j('div#extended').css({ display : 'none'});
	$j('p#expand a').click(function() {
		$j('div#extended').toggle('fast');

		var keyword = $j('p#expand a span').text();
		
		if(keyword == 'Expand') {
			$j('p#expand a span').text('Collapse');
			$j(this).addClass('down');
		} else {
			$j('p#expand a span').text('Expand');
			$j(this).removeClass('down');
		}
		return false;	
	});


	$j('div#stripe').append('<div id="people"></div>');
	$j('p.find-options.overlapped').after('<div id="glass"></div>');



	/*
	Safari completely refuses to even acknowledge
	the existence of the legend element, making it
	un-manipulatable. The following function replaces
	<legend> with <p class="legend"> as a hack that 
	partially fixes the problem.
	*/
	$j('legend').each(function(i){
		var legend_content = $j(this).html();
		$j(this).replaceWith('<p class="legend">' + legend_content + '</p>');

	});
	

	/* when variable 'scroll_to' is defined,
	the page will smooth-scroll to the element
	with that ID
	*/
	if(typeof(scroll_to) != 'undefined') {
		$j('html, body').animate({
				scrollTop: $j('#' + scroll_to).offset().top
		}, 1000);
	}


	/*zebra tables*/
	$j('table tr:nth-child(even)').css({background : 'white'});


	/*close generic boxes */
	$j('div.box').after('<div class="box-bottom"></div>');

	/* make pretty file inputs */
	styleFileInputs();
	
	/*print pages */
	$j('li.print a').click(function() {
		window.print();
		return false;
	});
	
	
});