function add_subscribe_placeholder() {
	var i = $('#subscribe');
	var t = 'Subscribe to our updates';
	var c = 'placeholder';
	i.addClass(c);
	i.val(t);
	i.focus( function() {
		$(this).removeClass(c);
		if( $(this).val() === t ) {
			$(this).val('');
		}
	});
	i.blur( function() {
		if( $(this).val() === '' || $(this).val() === t ) {
			$(this).val(t);
			$(this).addClass(c);
		}
	});
}

function add_rotator_effect() {
	if( $('#welcome-container').length > 0 ) {
		$('#welcome-container').after('<div id="welcome-pager"></div>');
		$('#welcome-container').cycle({
			pager: '#welcome-pager',
			activePagerClass: 'active',
			timeout: 8000,
			pause: 1
		});
		$('#welcome-pager a').click( function() {
			$(this).blur();
		});
	}
}

function add_scroll_effect() {
	$('#quick-links a').click( function(e) {
		var h = this.href.split('#')[1];
		var t = $( '#' + h ).offset().top;
		$('html, body').animate({
			scrollTop: t
		}, 500);
		$(this).blur();
		e.preventDefault();
	});
}

function add_position_fix() {
	var a = $('#aside');
	var w = $(window);
	var s = 423; // a.offset().top
	a.css( 'position', 'absolute' );
	$(window).scroll( function() {
		if( w.scrollTop() < s ) {
			a.css({
				'position': 'absolute',
				'top': s
			});
		} else {
			a.css({
				'position': 'fixed',
				'top': 0
			});
		}
	});
}

function add_error_message(m) {
	var e = $('#contact-form-error');
	if( m === '' ) {
		e.remove();
	} else {
		if( e.length > 0 ) {
			e.text(m);
		} else {
			var p = $('<p></p>');
			p.text(m);
			p.attr( 'id', 'contact-form-error' );
			p.addClass('error');
			$('#contact-form').before(p);
		}
	}
}

function validate_form() {
	var e = [];
	var c = $('#contact-form .text-input');
	c.each( function() {
		if( $(this).hasClass('required') && $(this).val() === '' ) {
			e.push( $(this).siblings('input[type="hidden"].validation').first().val() );
		} else if( $(this).hasClass('email') ) {
			var f = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
			var a = /[\(\)<\>\,\;\:\\\"\[\]]/;
			if( (!f.test( $(this).val() )) || $(this).val().match(a) ) {
				e.push( 'a valid email address' );
			}
		}
	});
	if( e.length > 0 ) {
		var m = 'Please enter ';
		m += e[0];
		if( e.length > 1 ) {
			if( e.length > 2 ) {
				for( var i = 1; i < ( e.length - 1 ); i++ ) {
					m += ', ';
					m += e[i];
				}
			}
			m += ' and ';
			m += e[e.length-1];
		}
		m += '.';
		add_error_message(m);
		return false;
	}
}

function add_validation() {
	var f = $('#contact-form');
	if ( f.length > 0 ) {
		f.submit( function() {
			return validate_form();
		});
		f.bind('reset', function() {
			add_error_message('');
		});
	}
}

function add_map() {
	var f = $('#address');
	if( f.length > 0 ) {
		var c = $('<div id="map"></div>');
		f.prepend(c);
		var l = new google.maps.LatLng(53.975847,-1.565778);
		var o = {
			zoom: 14,
			center: l,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var m = new google.maps.Map( document.getElementById('map'), o );
		var k = new google.maps.Marker({
			position: l,
			title: 'Adtec Software',
			icon: 'css/images/icon.png'
		});
		k.setMap(m);
	}
}

$(document).ready( function() {
	add_subscribe_placeholder();
	add_rotator_effect();
	add_scroll_effect();
	add_position_fix();
	add_validation();
	add_map();
});

