var TNT = {

    config: {
		ie: false,
		tooltips: [
			'#nav a'
		]
    },

    init: function () {
        $( 'body' ).addClass( 'js' );

		if ( $( '#page-home' ).length ) TNT.home.init();
		if ( $( '#page-services' ).length ) TNT.services.init();
		if ( $( '#page-contact' ).length ) TNT.contact.init();

		TNT.general.initTooltips();
		
    },

	general: {

		initTooltips: function () {
			$( TNT.config.tooltips.join( ',' ) ).tipTip();
		}
		
	}
}

TNT.home = {

	init: function () {
		TNT.home.work.init();
	},

	work: {

		init: function () {
			TNT.home.work.initTestimonials();
			TNT.home.work.addLabels();

			$( '.work .info' ).addClass( 'callout' ).prepend( '<div class="arrow" />' ).prepend('<span class="close">x</span>');
			$( '#work>ul>li>img, #work>ul>li .view' ).click( TNT.home.work.select );
			$( '#work .callout .close').click( TNT.home.work.off )
		},

		addLabels: function () {
			var label = $( '<span class="view">View</span>' ).fadeTo(0, 0);
			$( '#work>ul>li' ).append( label ).hover( TNT.home.work.hovered, TNT.home.work.unhovered );
		},

		hovered: function () {
			$( this ).children( 'img' ).stop().fadeTo( 500, 0.9 );
			$( this ).find( '.view' ).stop().fadeTo( 500, 1 );
		},

		unhovered: function () {
			$( this ).children( 'img' ).stop().fadeTo( 500, 1 );
			$( this ).find( '.view' ).stop().fadeTo( 500, 0 );
		},

		select: function ( e ) {
			var $this = $( this ).parents( '.work' );
			if ( $this.hasClass( 'on' ) ) {
				TNT.home.work.off.call($this);
			} else {
				TNT.home.work.on.call($this);
			}

		},

		on: function ( e ) {
			var el = $( this ), winW, winH, winY, info, infoX, infoY, infoW, infoH, cutoff, infoArrowX;

			info = el.children( '.info' );

			// Hide open callouts
			el.siblings('li').each( function ( i, j ) {
				TNT.home.work.off.call( $( j ) );
			});
			// Show this callout
			el.addClass( 'on' );
			info.stop().show().fadeTo( 0, 0 ).fadeTo( 500, 1 );

			// Bring horizontally cutoff callouts back onscreen
			infoX = info.offset().left;
			infoW = info.outerWidth();
			winW = $( window ).width();
			cutoff = infoX + infoW - winW;
			if ( cutoff > 0 ) {
				infoArrowX =  parseInt( info.children('.arrow').position().left );
				info.offset({
					'left': infoX - cutoff - 40
				});
				info.children( '.arrow' ).css({
					'left': infoArrowX + cutoff + 40
				});
			}
			// Scroll down to show all of vertically-cutoff callouts
			infoY = info.offset().top;
			infoH = info.outerHeight();
			winH = $( window ).height();
			winY = $( window ).scrollTop();
			cutoff = parseInt(infoY + infoH - winH - winY);
			if ( cutoff > 0 ) {
				$.scrollTo( cutoff + winY, { duration: 500, easing: 'swing' });
			}
		},

		off: function ( e ) {
			var el = $( this ).hasClass( 'close' ) ? $( this ).parents( '.work' ) : $( this );

			el.addClass('out');

			el.children( '.info' ).stop().fadeTo( 500, 0, function () {
				$( this ).hide();
				$( this ).parents( '.work' ).removeClass('out').removeClass( 'on' )
			});
		},

		initTestimonials: function () {
			$('#work .info').each( function ( i, el ) {
				el = $(el);

				// Add testimonial button
				if ( el.find( '.testimonial' ).length ) el.children( '.buttons' ).append('<li> <a class="button button2 button-testimonial" href="#" title="What they said about our work"> <span>Client\'s comments</span> </a> </li>');
			});

			$('#work li .button-testimonial').click( TNT.home.work.toggleTestimonial );
		},

		toggleTestimonial: function ( e ) {
			var btn = $(this), info, desc, test, dt;

			info = btn.parents( '.info' );
			desc = info.find( '.desc' );
			test = info.find( '.testimonial' );
			dt = desc.parent();

			if ( test.hasClass( 'on' ) ) {
				// Hide testimonial
				if ( typeof( TNT.IE6 ) != 'undefined' ) {
					// IE6
					desc.show();
					test.hide().removeClass( 'on' );
				} else {
					// Everything else
					test.stop().fadeOut( 500, function () {
						desc.stop().fadeIn( 500 );
						test.removeClass( 'on' );
					} );

					
				}

				btn.children( 'span' ).text( 'Client comments' );
			} else {
				// Show testimonial
				dt.height( desc.outerHeight() );
				if ( typeof( TNT.IE6 ) != 'undefined' ) {
					// IE6
					test.show().addClass( 'on' );
					desc.hide();
				} else {
					// Everything else
					desc.stop().fadeOut( 500, function () {
						test.stop().fadeIn( 500 );
						test.addClass( 'on' );
					} );
				}

				btn.children( 'span' ).text( 'Project info' );
				
			}

			e.preventDefault();
		}

	}
}

TNT.services = {

	init: function () {
		// Equalise rate card columns
		TNT.services.equaliseRateCard();
		$( window ).resize( TNT.services.resize )
		setTimeout("$( window ).resize()", 500); // For FF Linux
	},

	resize: function () {
		$( '.rate-card>li' ).removeAttr( 'style' ).removeClass( 'equalised' );
		TNT.services.equaliseRateCard();
	},

	equaliseRateCard: function () {
		var h, maxH = 0;
		$( '.rate-card>li' ).each( function ( i, el ) {
			el = $( el )
			h = el.outerHeight();
			maxH = ( h > maxH ) ? h : maxH;
		});
		$( '.rate-card>li' ).height( maxH ).addClass( 'equalised' );
	}

}

TNT.contact = {

	init: function () {

		TNT.contact.form.init();

	},

	form: {

		init: function () {
			$( 'input, textarea' ).innerLabel({ once: true });
			// Auto-resize textarea
			$( 'textarea' ).autoResize({
				// Quite slow animation:
				animateDuration : 600
			});

			// Set up validation
			$('#name').attr('defaultValue', 'Name');
			$('#email').attr('defaultValue', 'Email address');
			$('#message').attr('defaultValue', 'Message');
			$.validator.addMethod('defaultInvalid', function ( val, el ) {
				return val != $( el ).attr( 'defaultValue' );
			}, 'Please enter something!');
			$( '#page-contact form' ).validate({
				rules: {
					name: { required: true, defaultInvalid: true },
					email: { required: true, email: true },
					message: { required: true, defaultInvalid: true }
				},
				messages: {
					name: 'Aw, don&rsquo;t be shy now - what should we call you?',
					email: 'Please enter a valid email address',
					message: 'Cat got your tongue? Please enter a message!'
				}
			})


			$( '#page-contact form' ).submit( TNT.contact.form.submit );
		},

		submit: function ( e ) {
			var f;
			f = $( '#page-contact form' );
			dataString = f.serialize();

			$.ajax({
				type: 'POST',
				url: f.attr( 'action' ) + '/send/true',
				data: dataString,
				dataType: 'json',
				success: TNT.contact.form.submitComplete
			});

			e.preventDefault();
		},

		submitComplete: function ( data, textStatus, jqXHR ) {
			var feedback = $( '<ul id="feedback" />' ), feedbackH;
			$( '#feedback' ).remove();
			$( '#page-contact form' ).prepend( feedback );

			if( data.valid ) {
				$( '#feedback' ).append('<li class="success">' + data.general + '</li>');
			} else {
				$( '#feedback' ).append('<li class="error">' + data.general + '</li>');
			}

			feedbackH = $( '#feedback' ).height();
			$( '#feedback' ).fadeTo( 0, 0).height( 0 );
			$( '#feedback' ).animate( { opacity: 1, height: feedbackH }, { duration: 500 } );
		}

	}

}



$(function () {
    TNT.init();
});

