﻿
/*
	Website: adventistai.lt
	Author: Osvaldas Valutis, www.osvaldas.info - freelancer web / print / brand identity designer and web developer
*/



/*
	drop down navigation
*/

var navDrop = function()
{
	var parent		= $( 'ul#nav li' );
	var maxHeight 	= 405;
	var container;
	var drop;
	var hide;
	var defaultPos;

	parent.hover( function()
	{
		hide 		= true;
		container 	= $( this );
		drop 		= container.children( 'ul' );
      	defaultPos	= container.offset().top;

		drop.stop( true, false ).css( 'top', defaultPos ).fadeIn( 200, function()
		{
			$( this ).css( 'opacity', 1 );
		});


		var $container = $( this ),
			$list = drop,
			height = $list.height() * 1.1,
			multiplier = height / maxHeight;

        if( multiplier > 1 )
        { 
            $list.mousemove( function( e ) 
            {
				var offset = $container.offset();
				var relativeY = ( ( e.pageY - offset.top ) * multiplier ) - ( $container.height() * multiplier );
				if( relativeY > $container.height() )
					$list.css( 'top', -relativeY + $container.height() + 60 );
			});
        }
	},
	function()
	{
		drop.hover( function()
		{
			hide = false;
		},
		function()
		{
			$( this ).fadeOut( 200 );
		});

		if( hide )
			drop.fadeOut( 200 );

	});
	
	$( 'ul#nav li ul li[class~=current_page_item]' ).each( function()
	{
		$( this ).parents( 'li' ).addClass( 'current_page_item' );
	});


	// small independent tweak
	$( 'a:has(img)' ).css( 'padding', '0' );
};

$( window ).load( navDrop );



/*
	slideshow
*/

var slideShow = function()
{
	var nav		= $( '#slideshow ul.pagination li a' );
	var slides	= $( '#slideshow .slide' );

	nav.click( function()
	{
		nav.filter( '.selected' ).removeClass( 'selected' );
		$( this ).addClass( 'selected' );

		var navIndex = $( this ).parent().index();

		slides.filter( ':visible' ).fadeOut( 300, function()
		{
			slides.eq( navIndex ).fadeIn( 300 );
		});

		return false;
	});
};

$( window ).load( slideShow );



/*
	tooltip
*/

var toolTip = function()
{
	var target = $( '[rel~=tooltip]' );
	var tip;
	var title;
	var pos;

	target.hover( function()
	{
		title = $( this ).attr( 'title' );

		if( title == '' )
			return false;

		$( this ).removeAttr( 'title' );

		tip	= $( '<span class="tooltip"><span>' + title + '</span></a>' );

		tip.appendTo( 'body' ).fadeIn( 300 );
	}, 
	function()
	{
		tip.fadeOut( 300, function()
		{
			$( this ).remove();
		});

		$( this ).attr( 'title', title );
	});

	target.mousemove( function( mouse )
	{
		tip.css({ left: mouse.pageX, top: mouse.pageY + 20 });
	});
};

$( window ).load( toolTip );



/*
	forms AJAX
*/

var formsAJAX = function()
{
	$( 'form.ajax' ).submit( function()
	{
		var thisForm = 		this;
		var responseObj =	$( 'div.response', this );
		var inputs = 		[];


		$( 'html, body' ).animate( {scrollTop: responseObj.offset().top - 80 }, 500 );

		responseObj.html( '<p><em><strong>Luktelkite...</strong></em></p>' ).fadeIn( 500 ).fadeOut( 500 ).fadeIn( 500 );


		$( 'input[type!=radio], textarea, select', this ).each( function() 
		{
			inputs.push( this.name + '=' + escape( this.value ) );
		});

		$( 'input[type=radio]:checked', this ).each( function()
		{
			if( this.value != null )
				inputs.push( this.name + '=' + this.value );
		});


		jQuery.ajax(
		{
			url: 			this.action,
			type:			this.method,
			data: 			inputs.join( '&' ),
			dataType:		'json',
			timeout: 		10000,

			error: function( request, status, thrown ) 
			{
				responseObj.fadeOut( 150, function()
				{
					responseObj.fadeIn( 300 ).html( '<p class="fail"><strong>Klaida</strong> <em>&bdquo;' + status + '&ldquo;</em>. Pamėginkite dar kartą!</p>' );
				});
			},

			success: function( data ) 
			{
				responseObj.fadeOut( 150, function()
				{
					responseObj.fadeIn( 300 ).html( data.response );
				});

				if( data.cleanForm == true )
				{
					$( 'input, select, textarea', thisForm ).each( function()
					{
						if( this.type != 'submit' && this.type != 'radio' && this.type && 'select' )
							$( this ).val( '' );

						if( this.type == 'radio' )
							$( this ).attr( 'checked', false );

						if( this.type == 'select' )
							$( this ).attr( 'selected', false );
					});
				}

				if( data.hideForm == true )
				{
					function doHideForm()
					{
						$( thisForm ).fadeOut( 1000 );
					}

					setTimeout( doHideForm, 4000 );
				}

				if( data.redirectTo != false )
					window.location = data.redirectTo;
			}
		});

		return false;
	});
};

$( window ).load( formsAJAX );



/*
	send email dialog
*/

var sendMail = function()
{
	var isActive = false;
	var sendForm = $( 'form#send-mail' );

	$( 'a[href*=mailto:]' ).click( function()
	{
		if( isActive == true )
			return false;
		else
			isActive = true;

		var mailTo	= $( this ).attr( 'href' );
		mailTo 		= mailTo.replace( 'mailto:', '' );

		$( 'a#send-recipient' ).text( mailTo ).attr( 'href', 'mailto:' + mailTo );
		$( 'input#f-recipient' ).attr( 'value', mailTo );

		var pos	= $( this ).offset();
		sendForm.css({ left: pos.left - 110 + 'px', top: pos.top + 40 + 'px' });
		sendForm.fadeIn( 500 );

		return false;
	});

	$( 'a.send-close' ).click( function()
	{
		sendForm.fadeOut( 500 );
		isActive = false;

		return false;
	});
};

$( window ).load( sendMail );



/*
	scroller - on link click scrolls to and nudges the hot zone
*/

var scrollerTo = function()
{
	$( 'a[href|=#to]' ).click( function()
	{
		var objName			= $( this ).attr( 'href' ).substr( 4 );
		var target			= $( 'a[name=' + objName + ']');

		if( target.size() != 1 )
			target = $( '#' + objName );

		var targetOffset 	= target.offset().top - 60;

		$( 'html, body' ).animate( { scrollTop: targetOffset }, 600, function()
		{
			target.css( 'position', 'relative' );

			for( var i = 0; i < 3; i++ )
			{
				posTop	= rand( 6 );
				posLeft	= rand( 6 );
				time	= rand( 5 ) * 20;

				target
				.animate( { top: '+=' + posTop + 'px', left: '+=' + posLeft + 'px' }, time )
				.animate( { top: '-=' + posTop + 'px', left: '-=' + posLeft + 'px' }, time );
			}

			setTimeout( makeStatic, 2000 );
		});

		function makeStatic()
		{
			target.css( 'position', 'static' );
		}

		return false;
	});

	$( 'a#to-top' ).click( function()
	{
		$( 'html, body' ).animate( { scrollTop: 0 }, 600 );

		return false;
	});
};

$( window ).load( scrollerTo );



/* random number from 1 to N */

function rand ( n )
{
	return ( Math.floor ( Math.random ( ) * n + 1 ) );
}


