$(document).ready(function(){

	// @target: external window
	$('a.external').attr({
		target: '_blank'
	});
	// @modal
	var t = $('#ajaxContact');
	if($('#ajaxContact').size() > 0){
		$('#ajaxContact').jqm({
			trigger: 'a.jqmContactUs',
			ajax: '@href',
			target: t,
			modal: true,
			onLoad: jqmSetupContactForm,
			onHide: function(h) { 
				t.html('Please Wait...');	// Clear Content HTML on Hide.
				h.o.fadeOut();						// remove overlay
				h.w.hide();								// hide window
			},
			overlay: 75
		});
	}
	// Work around for IE's lack of :focus CSS selector
	if($.browser.msie){
		$('input')
		.focus(function(){$(this).addClass('iefocus');})
		.blur(function(){$(this).removeClass('iefocus');});
	}
	// @login
	$('#username').focus(function(){
		if($(this).val().toLowerCase() == 'username'){
			$(this).val('')
		}
	});
	$('#username').blur(function(){
		if($(this).val() == ''){
			$(this).val('Username')
		}
	});
	$('#password').focus(function(){
		if($(this).val().toLowerCase() == 'password'){
			$(this).val('')
		}
	});
	$('#password').blur(function(){
		if($(this).val() == ''){
			$(this).val('Password')
		}
	});
});

/*-- @modal: made available when the Contact Form is loaded -*/
function jqmSetupContactForm(hash){
	// prepare the form when the DOM is ready 
	$(document).ready(function(){
		$('#contactUsForm').submit(function(){
			if($("#contactUsForm").validate({
				rules: {
					email: {
						required: true,
						email: true
					},
					confirmEmail: {
						required: true,
						email: true,
						equalTo: '#email'
					}
				}
			}).form()){
				// processing
				$('div#contactUsFormContainer').hide()
				$('div#processing').fadeIn()
				// get all the inputs/values
				var inputs = [];
				$(':input', this).each(function(){
					inputs.push(this.name + '=' + escape(this.value));
				})
				// display all inputs/values
				//alert(inputs.join('&'));
				jQuery.ajax({
					type: 'POST',
					data: inputs.join('&'),
					url: this.action,
					timeout: 2000,
					error: function(){
						$('div#processing').hide()
						$('div#contactUsFormContainer').fadeIn()
						$('p.error').fadeIn('slow')
					},
					success: function(r){
						$('div#processing').hide()
						$('div#formSubmitted').fadeIn()
					}
				})
			}
			return false;
		});
	});
	
	// Work around for Safari
	if($.browser.safari){
		$('div#processing').addClass('safari')
		$('div#formSubmitted').addClass('safari')		
	}

}