/*
	Blanko.
	Projet WLA.
	js file.
	Version 1.0
	Rev. xxx
*/

var fixeHeight = true;

function init()
{
	jQuery('a[rel*=external]').live('click', function() { window.open(this.href); return false; });
	
	jQuery('.btn').mousedown   ( function(){ jQuery(this).css({'margin-top' : '+=1', 'margin-bottom' : '-=1'}); });
	jQuery('.btn').mouseup	  ( function(){ jQuery(this).css({'margin-top' : '-=1', 'margin-bottom' : '+=1'}); });
	
	
	if ( fixeHeight )
	{
		// Fixe Content height;
		height = Math.max(0,jQuery(window).height()-455);	
		jQuery('.content').css('min-height', height + 'px');
	}
	
	jQuery(".close").click( function () 
	{
		jQuery(this).parent().fadeTo(400, 0, function () 
		{
			// Links with the class "close" will close parent
			jQuery(this).slideUp(400);			
		});
		return false;
	});
}

function initFiche()
{
	jQuery("a.imgplan").fancybox(
	{		
		'showNavArrows'		: false,
		'padding'			: 6,
		'showCloseButton'	: true,
		'titleShow'			: false,
		'overlayColor'		: '#051310',
		'overlayOpacity'	: '0.9'
	});
	
	jQuery("a.imgthmb").click( function()
	{
		id = jQuery(this).attr('id');
		if ( id != imgplan )
		{
			jQuery('#' + imgplan + 'plan').fadeOut(700);
			jQuery('#' + id + 'plan').fadeIn(700);
			imgplan = id;
		}
		
		return false;
	});
		
	jQuery("a.imgthmbfy").click( function()
	{
		id = jQuery(this).attr('id');
		if ( id != imgplany )
		{
			//swicth plan
			jQuery('#' + imgplany + 'plany').fadeOut(500);
			jQuery('#' + id + 'plany').fadeIn(500);			
			
			//switch overlay
			jQuery('#' + imgplany + ' span').hide();
			jQuery('#' + id + ' span').show();
			
			imgplany = id;			
		}
		
		return false;
	});
	
	
	jQuery("a.imgplan").click( function()
	{
		id = jQuery(this).attr('id').substring(4,5);
		jQuery('.imgplanfy').hide();		
		jQuery('#fyimg' + id + 'plany').show();
		
		jQuery('.imgthmbfy span').hide();
		jQuery('#fyimg' + id + ' span').show();
		imgplany = 	'fyimg' + id ;
	});
}

function initStatic()
{
	idx = 1;
	jQuery('.wallimg').each(function(index) 
	{		
		jQuery(this).delay(idx*250).fadeIn(700);		
		idx++;
	});
}

function initEmploye()
{
	for( idx = 1; idx < 6; idx++ )
	{		
		jQuery('#eimg' + idx).delay(idx*250).fadeIn(700);		
	}
}

function initProduits()
{
	for( idx = 1; idx < 7; idx++ )
	{		
		jQuery('#pimg' + idx).delay(idx*250).fadeIn(700);		
	}
}

function mainNav()
{	
	jQuery('#mainnav ul li').hover ( function()
	{
		jQuery('a:first', this).addClass('hackie6');	/* IE6 Menu Hack */
		jQuery('ul', this).show();
	},
	function ()
	{
		jQuery('a:first', this).removeClass('hackie6'); 	/* IE6 Menu Hack */
		jQuery('ul', this).hide();
	});
	
	jQuery('#header ul li').hover ( function()
	{		
		jQuery('ul', this).show();
	},
	function ()
	{		
		jQuery('ul', this).hide();
	});	
}

function loadFancyBox()
{
	jQuery("a.btninfolettre").fancybox(
	{		
		'overlayColor'		: '#051310',
		'overlayOpacity'	: '0.9',
		'hideOnContentClick': false,
		'hideOnOverlayClick': false
	});
}

/* ------------ Contact Form ---------------- */
function contactvalidateform()
{
	showsuccesmessage(false);

	/* Name field */
	if ( jQuery('#nom').val() == '' )
	{			
		jQuery('#errormsg').html('Vous devez entrer votre nom.');
		jQuery('#nom').focus();
		showerrmessage(true);
		return false;
	}
						
	/* Email field */			
	if ( jQuery('#courriel').val() == '' )
	{			
		jQuery('#errormsg').html('Vous devez entrer votre courriel.');
		jQuery('#courriel').focus();
		showerrmessage(true);
		return false;
	}
	
	/* Email validation */
	if ( !validate_email(jQuery('#courriel').val()) )
	{			
		jQuery('#errormsg').html('Votre courriel est invalide.');
		jQuery('#courriel').focus();
		showerrmessage(true);
		return false;
	}
	
	/* Message field */			
	if ( jQuery('#message').val() == '' )
	{			
		jQuery('#errormsg').html('Vous devez entrer votre message.');
		jQuery('#message').focus();
		showerrmessage(true);
		return false;
	}
	
	showerrmessage(false);
	
	jQuery('#contactspinner').show();
	
	jQuery.post('sendmail.php',  { nom: jQuery('#nom').val(), courriel: jQuery('#courriel').val(), message:  jQuery('#message').val()}, function(data, success) 
	{
		jQuery('#contactspinner').hide();
		if ( data == 'true')
			showsuccesmessage(true);
		else
		{
			jQuery('#errormsg').html('Désolé, une erreur s\'est produite. Recommencez ou écrivez-nous à l\'adresse <a href="mailto:info@wlaframboise.com" title="info@wlaframboise.com">info@wlaframboise.com</a>');
			showerrmessage(true);
		}
	})    
    .error(function() 
	{
		jQuery('#contactspinner').hide();
		jQuery('#errormsg').html('Désolé, une erreur s\'est produite. Recommencez ou écrivez-nous à l\'adresse <a href="mailto:info@wlaframboise.com" title="info@wlaframboise.com">info@wlaframboise.com</a>');
		showerrmessage(true);
	})
}

function contactvalidateform_en()
{
	showsuccesmessage(false);

	/* Name field */
	if ( jQuery('#nom').val() == '' )
	{			
		jQuery('#errormsg').html('You must enter your name.');
		jQuery('#nom').focus();
		showerrmessage(true);
		return false;
	}
						
	/* Email field */			
	if ( jQuery('#courriel').val() == '' )
	{			
		jQuery('#errormsg').html('You must enter your email.');
		jQuery('#courriel').focus();
		showerrmessage(true);
		return false;
	}
	
	/* Email validation */
	if ( !validate_email(jQuery('#courriel').val()) )
	{			
		jQuery('#errormsg').html('Your email is invalid.');
		jQuery('#courriel').focus();
		showerrmessage(true);
		return false;
	}
	
	/* Message field */			
	if ( jQuery('#message').val() == '' )
	{			
		jQuery('#errormsg').html('You must enter your message.');
		jQuery('#message').focus();
		showerrmessage(true);
		return false;
	}
	
	showerrmessage(false);
	
	jQuery('#contactspinner').show();
	
	jQuery.post('sendmail.php',  { nom: jQuery('#nom').val(), courriel: jQuery('#courriel').val(), message:  jQuery('#message').val()}, function(data, success) 
	{
		jQuery('#contactspinner').hide();
		if ( data == 'true')
			showsuccesmessage(true);
		else
		{
			jQuery('#errormsg').html('Sorry, an error occurred. Repeat or write us at <a href="mailto:info@wlaframboise.com" title="info@wlaframboise.com">info@wlaframboise.com</a>');
			showerrmessage(true);
		}
	})    
    .error(function() 
	{
		jQuery('#contactspinner').hide();
		jQuery('#errormsg').html('Sorry, an error occurred. Repeat or write us at <a href="mailto:info@wlaframboise.com" title="info@wlaframboise.com">info@wlaframboise.com</a>');
		showerrmessage(true);
	})
}

function showerrmessage(show)
{
	if ( show )
	{
		if ( jQuery('#errormsg').is(':hidden') )
		{
			jQuery('#errormsg').slideDown('fast', 'easeOutExpo');
		}
	}
	else
	{
		if ( !jQuery('#errormsg').is(':hidden') )
		{
			jQuery('#errormsg').slideUp('fast', 'easeOutExpo');
		}
	}
}

function showsuccesmessage(show)
{
	if ( show )
	{
		if ( jQuery('#successmsg').is(':hidden') )
		{
			jQuery('#successmsg').slideDown('fast', 'easeOutExpo');
		}
	}
	else
	{
		if ( !jQuery('#successmsg').is(':hidden') )
		{
			jQuery('#successmsg').slideUp('fast', 'easeOutExpo');
		}
	}
}

function validate_email(value) 
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;			
	return reg.test(value);
}

/* Recherche */
function searchformclick()
{
	if ( jQuery('#searchvalue').val() == '' || jQuery('#searchvalue').val() == 'Rechercher' || jQuery('#searchvalue').val() == 'Search' )
	{
		return false;
	}	
	jQuery('#searchform').submit();
}

function setsearchpage(idx)
{
	jQuery('#pageidx').val(idx)
	jQuery('#searchpageform').submit();
	return false;
}

function autocomplete()
{
	jQuery( "#searchvalue" ).autocomplete(
	{		
		source: baseurl + "/search.php",
		position: { my : "left top", at: "left bottom", offset: "-11 0" },
		minLength: 2,				
		open: function() 
		{
			jQuery( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() 
		{
			jQuery( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		},
		select: function(event, ui) 
		{
			jQuery('#searchvalue').val(ui.item.value);
			jQuery('#searchform').submit();
		}
	});
}

/* Home slider */
function slider()
{
	cycleslide();
	setInterval("cycleslide()",5000);
}

function cycleslide()
{
	if ( slideIdx != 0 )
	{
		jQuery('#slide' + slideIdx ).fadeOut(800, function()
		{
			jQuery('.bannertagline', this).css('bottom', '-60px');
			jQuery('.banneradd', this).css('right', '-400px');
		});
	}
	
	slideIdx++;
	if ( slideIdx > nbSlide ) slideIdx = 1;
	
	jQuery('#slide' + slideIdx).fadeIn(800, function()
	{		
		jQuery('.bannertagline', this).animate({bottom: '0px'}, 400, 'easeOutExpo', function()
		{
			jQuery('#slide' + slideIdx + ' .banneradd').animate({right: '5px'}, 600, 'easeOutExpo');
		});		
	});
}
