/*
 * Activate template effects
 * @author	Display:inline <contact@display-inline.fr>
 * @url		http://display-inline.fr
 * @version	1.0
 */

// Define site file extension here : html, php, asp...
template_fileExt = 'html';

// Template setup
jQuery(document).ready(function()
{
	// Keywords
	if (jQuery(document.body).hasClass('reversed'))
	{
		jQuery('#keywords li a').hover(function()
		{
			jQuery(this).css({'padding-left':'0'}).animate({paddingLeft:'8px'}, 100);
		}, function()
		{
			jQuery(this).css({'padding-left':'8px'}).animate({paddingLeft:'0'}, 100);
		});
	}
	else
	{
		jQuery('#keywords li a').hover(function()
		{
			jQuery(this).css({'padding-right':'0'}).animate({paddingRight:'8px'}, 100);
		}, function()
		{
			jQuery(this).css({'padding-right':'8px'}).animate({paddingRight:'0'}, 100);
		});
	}
	
	// Header links
	jQuery('#header-links > li > a').hover(function()
	{
		jQuery(this).css({'padding-bottom':'6px', 'border-bottom-width':'0'}).animate({borderBottomWidth:'6px', paddingBottom:'0'}, 100);
	}, function()
	{
		jQuery(this).css({'padding-bottom':'0', 'border-bottom-width':'6px'}).animate({borderBottomWidth:'0', paddingBottom:'6px'}, 100);
	});
	
	// Opera and IE6 have small problems with this effect
	if (!jQuery.browser.opera && !jQuery.browser.msie)
	{
		jQuery('#header-links li').hover(function()
		{
			var submenu = jQuery(this).children('ul');
			if (submenu.length > 0)
			{
				var final_height = submenu.height();
				submenu.css({height:0, opacity:0}).animate({height:final_height, opacity:1}, 'fast');
			}
		}, function()
		{
			var submenu = jQuery(this).children('ul');
			if (submenu.length > 0)
			{
				submenu.stop().css('display', '').css('height', '');
			}
		});
	}
	
	// Highlight
	jQuery('#highlights li').hover(function()
	{
		jQuery(this).css('background-position', '0 0').animate({backgroundPosition:'0 -300px'}, 500);
	}, function()
	{
		jQuery(this).css('background-position', '0 -300px').animate({backgroundPosition:'0 0'}, 500);
	});
	
	// Nav
	var nav = jQuery('.nav');
	var navElements = nav.children('li');
	navElements.hover(function()
	{
		jQuery(this).children('a').css({'padding-left':'7px', 'padding-right':'17px'}).animate({paddingLeft:'17px', paddingRight:'7px'}, 100);
	}, function()
	{
		jQuery(this).children('a').css({'padding-left':'17px', 'padding-right':'7px'}).animate({paddingLeft:'7px', paddingRight:'7px'}, 100);
	});
	
	// Nav elements with submenu
	var navChildren = navElements.children('ul');
	navChildren.parent().each(function(i)
	{
		this._height = jQuery(this).height();
		var a = jQuery(this).children('a');
		a.get(0)._opened = false;
		this._aHeight = (parseInt(a.height())+17)+'px';
		jQuery(this).css('height', this._aHeight)
	}).children('a').click(function(e)
	{
		var a = jQuery(this);
		var li = a.parent();
		var liNode = li.get(0);
		if (!this._opened)
		{
			e.preventDefault();
			li.animate({height:liNode._height}).parent().find('li ul').parent().each(function()
			{
				if (this != liNode)
				{
					jQuery(this).animate({height:this._aHeight}).children('a').get(0)._opened = false;
				}
			});
			this._opened = true;
		}
	});
	
	// Sub-nav
	navChildren.children('li').children('a').hover(function()
	{
		jQuery(this).css({'padding-left':'0', 'padding-right':'15px', 'border-left-width':'0'}).animate({paddingLeft:'5px', paddingRight:'10px'}, 100).animate({borderLeftWidth:'5px', paddingRight:'5px'}, 100);
	}, function()
	{
		jQuery(this).css({'padding-left':'5px', 'padding-right':'5px', 'border-left-width':'5px'}).animate({paddingLeft:'0', borderLeftWidth:'0', paddingRight:'15px'}, 100);
	});
	
	// Ajax sections
	jQuery('.ajax .pagination a').live('click', function(event)
	{
		var ajaxDiv = jQuery(this).parents('.ajax:first');
		if (ajaxDiv.length > 0)
		{
			// Stop click event
			event.preventDefault();
			
			// Effect
			var initHeight = ajaxDiv.height();
			ajaxDiv.children().animate({opacity:0});
			
			// Load ajax content
			var date = new Date();
			ajaxDiv.load(this.href, 'ajax=1&refresh='+date.getTime(), function()
			{
				var newHeight = ajaxDiv.height();
				if (newHeight != initHeight)
				{
					ajaxDiv.height(initHeight).animate({height:	newHeight+'px'}, 'slow', 'swing', function() { jQuery(this).css('height', ''); });
				}
				ajaxDiv.children().css('opacity', 0).animate({opacity:1});
				
				// Fancybox reload
				if (jQuery.fn.fancybox)
				{
					ajaxDiv.find('a[rel="gallery"]').fancybox();
				}
				
				// Portfolio
				if (jQuery.fn.projectSlideshow)
				{
					jQuery('.project-images').projectSlideshow();
				}
			});
		}
	});
	
	// Forms input field auto-tip
	jQuery('.search-form input, .form input[type=text], .form textarea').focus(function()
	{
		var input = jQuery(this);
		var title = input.attr('title');
		input.removeClass('input-tip');
		if (title && input.val() == title)
		{
			input.val('');
		}
	}).blur(function()
	{
		var input = jQuery(this);
		var title = input.attr('title');
		if (title && (input.val() == '' || input.val() == title))
		{
			input.val(title);
			input.addClass('input-tip');
		}
	}).blur();
	
	// Prevent tips to be submitted
	jQuery('.search-form, .form').submit(function()
	{
		jQuery(this).find('input[type=text]').each(function(i)
		{
			var input = jQuery(this);
			var title = input.attr('title');
			if (title && input.val() == title)
			{
				input.val('');
			}
		});
	});
	
	/*
	 * Navigation
	 */
	
	// Detection of current page if none specified
	var current = nav.find('.current');
	if (current.length == 0)
	{
		// Detects from current url, with hash
		var selection = navElements.find('a');
		var href = document.location.href;
		if (href.substr(-1, 1) == '#')
		{
			href = href.substr(0, href.length-1);
		}
		if (jQuery.browser.safari)
		{
			href = href.replace(' ', '%20');			// Webkit leaves spaces unencoded in document.location	
		}
		var found = template_markLinks(selection, href);
		if (!found)
		{
			// If hash, remove it
			if (document.location.hash && document.location.hash.length > 0)
			{
				href = document.location.href.substr(0,  document.location.href.length-document.location.hash.length);
				found = template_markLinks(selection, href);
			}
		}
		if (!found)
		{
			// If folder, search with index.html
			if (document.location.pathname.substr(-1, 1) == '/')
			{
				found = template_markLinks(selection, href+'index.'+template_fileExt);
			}
		}
		
		// Reload selection
		if (found)
		{
			current = nav.find('.current');
		}
	}
	
	// If current
	if (current.length > 0)
	{
		current.each(function(i)
		{
		  	// Check level
			var element = jQuery(this);
			var parent = element.parent().parent();
			if (parent.get(0).nodeName.toLowerCase() == 'li')
			{
				element = parent;
			}
			element.children('a').click();
		});
	}
	
	/*
	 * Contact form
	 */
	if (!jQuery.browser.msie || jQuery.browser.version > 6)	// IE6 lack of support for position:fixed does not allow animation
	{
		jQuery('#contact').addClass('fixed').find('button[type=button]').click(function()
		{
			jQuery('#contact').animate({top:'-365px'});
		});
		jQuery('#contact-close').click(function()
		{
			jQuery('#contact').animate({top:'-365px'});
		});
		jQuery('a[href$="#contact"]').click(function(event)
		{
			jQuery('#contact').animate({top:0});
			event.preventDefault();
		});
		
		// If a close message link already exists
		jQuery('#contact-message a:last').live('click', function(event)
		{
			jQuery('#contact').animate({top:'-365px'}, 'normal', 'swing', function()
			{
				jQuery('#contact-message').html(sendMessage).hide('normal');
				jQuery('#contact-form').show();
				jQuery('#message').val('');
			});
			event.preventDefault();
		});
		
		// If in url hash, open
		if (document.location.hash == '#contact' || document.location.hash == '#contact-top')
		{
			jQuery('#contact').animate({top:0});
		}
	}
	
	// Ajax sending
	jQuery('#contact').submit(function(event)
	{
		// Prevent standard sending
		event.preventDefault();
		
		// Check fields
		var name = jQuery.trim(jQuery('#name').val());
		var mail = jQuery.trim(jQuery('#mail').val());
		var message = jQuery.trim(jQuery('#message').val());
		var mailRegExp = new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', 'i');
		if (name == '' || mail == '' || message == '')
		{
			alert('Please fill in all fields');
		}
		else if (!mail.match(mailRegExp))
		{
			alert('Invalid mail, please check.');
		}
		else
		{
			jQuery('#contact-form').hide();
			jQuery('#contact-message').show().load(document.location.href, {ajax:1, 'contact-sent':1, name:name, mail:mail, message:message});
		}
	});
});

/*
 * Add class 'current' to links matching an url
 * @param	Array		selection		jQuery nodes selection
 * @param	string		url				the url to find
 * @return	boolean		true if one or more links found, else false
 */
function template_markLinks(selection, url)
{
	var found = false;
	selection.each(function()
	{
		if (this.href === url)
		{
			jQuery(this).parent().addClass('current');
			found = true;
		}
	});
	return found;
}