/**
 * Presqu'île Habitat
 * http://www.presquile-habitat.fr
 *
 * Copyright (c) 2010 Julien Le Bren (julien.lebren@createurdimage.fr)
 * Créateur d'Image - http://www.createurdimage.fr
 *
 * Version: 1.0
 * Création: 2010-04-22
 * Modification : 2010-09-01
 */
 
var _that;
var _name;
var _open = 0;

var _rub = String(window.location).substr(String(window.location).lastIndexOf('/')+1,String(window.location).lastIndexOf('.')-String(window.location).lastIndexOf('/')-1);

$(function () 
{
	/* Actu */
	$('#actu marquee').marquee('pointer').mouseover(function () {
		$(this).trigger('stop');
	}).mouseout(function () {
		$(this).trigger('start');
	}).mousemove(function (event) {
		if($(this).data('drag') == true) {
			this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
		}
	}).mousedown(function (event) {
		$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	}).mouseup(function () {
		$(this).data('drag', false);
	});
	
	/* Scroll */
	$('#scrollUp a').click(function() { 
		$('#text').scrollTo('-=250px',800);
	});
		
	$('#scrollDown a').click(function() { 
		$('#text').scrollTo('+=250px',800);
	});
	
	/* Conseiller à un ami */
	$('a.friend').click(function() 
	{
		var url = $(this).attr('href');
		var the_html = '<h1>Conseiller cette page à un ami</h1>'+
			'<form method="post">'+
			'<p>Votre adresse e-mail :<br /><input type="text" size="80" name="user_email" /></p>'+
			'<p>Adresse e-mail de votre ami :<br /><input type="text" size="80" name="friend_email" /></p>'+
			'<p>Message accompagnant le lien :<br /><textarea name="user_message" cols="80" rows="12">Voici une page à visiter...</textarea></p>'+
			'<input type="submit" value="Envoyer le message" />'+
			'</form>';
		
		$('#modal').html('<div id="modal-content"><div>'+the_html+'<a class="close" href="javascript:void(0)"></a></div></div>');
		$('#modal').show('scale');
		
		$('#modal input[type="submit"]').click(function() 
		{
			$.post('friend.php', { user_email:$('#modal input[name="user_email"]').val(), friend_email:$('#modal input[name="friend_email"]').val(), url:url, 
				user_message:$('#modal textarea[name="user_message"]').val() }, function(data) {
				if(data == 'ok') {
					$('#modal h1').after('<p class="success">Votre message a bien été envoyé !</p>');
					setTimeout('$("#modal a.close").trigger("click");',4000);
				}
				else {
					alert(data);
				}
			});
			return false;
		});
		
		$('#modal a.close').unbind('click');
		$('#modal a.close').click(function() {
			$('#modal div').html('');
			$('#modal').hide('scale');
		});
		return false;
	});
	
	$('#round-content h2').each(function()
	{
		if($(this).parent().find('ul').is('ul')) {
			$(this).addClass('hover');
		}
	});
	
	$('#round-content h2').click(function()
	{
		$(this).parent().find('ul').slideToggle();
	});
	
	$('#menu li a').hover(function()
	{
		_that = this;
		_name = this.href.substr(this.href.lastIndexOf('/')+1,this.href.lastIndexOf('.')-this.href.lastIndexOf('/')-1);
		
		if(_name == 'devenir-client') { _name = 'client'; }
		if(_name == 'espace-client') { _name = 'locataire'; }
		if(_name == 'espace-pro') { _name = 'pro'; }
		
		$('.sousmenu').hide();
		$('#menu-'+_name).show();
		$(this).css('background-position','bottom');
	},
	function() {
		setTimeout('closeMenu("'+_name+'")',100);	
		//closeMenu(_name);
		if(_name != _rub) { $(_that).css('background-position','top'); }
		_open = 0;
	});
	
	$('.sousmenu').hover(function()
	{
		_open = 1;
		$(_that).css('background-position','bottom');
	},
	function() {
		//setTimeout('closeMenu("'+_name+'")',1000);
		_open = 0;
		closeMenu(_name);
		if(_name != _rub) { $(_that).css('background-position','top'); }
	});
	
	if($('#message').is('div')) {
		setTimeout("$('#message').fadeOut('slow');",5000);
	}
});

function closeMenu(__name)
{
	if(_open == 0) {		
		$('#menu-'+__name).hide();
	}
}
