if (ie != 7)
	Cufon.replace('.tab-header a', {hover: true});


$(document).ready( function() {
		$('#critere-prix input').arcadimPrixInput();

		if ( ie && ie < 7 )
			$('#feature-recherche').css('cursor', 'pointer').click( function()
				{
					$(location).attr('href', $(this).find('a').attr('href'));
				});

    $('#formulaire-arcapass')
        .submit(function(event) {

            if ($('#champ-arcapass input').val() == 'Référence de l\'annonce...') {
                event.preventDefault();
                arcadimMsgBox.error({message:'Merci de renseigner le champ de l\'arcapass'});
            }
        });

		$('#formulaire-vente')
			.submit(function(event)
				{
					var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					var theForm = $(this);
					var errors = new Array();

					if (theForm.find('input[type=radio]:checked').length == 0)
						errors.push(
							{
								name: 'type_bien',
								message: 'Vous devez spécifier le type de bien'
							});

					theForm.find('input[type!=radio], textarea[type!=radio]').each(function ()
						{
							var theName = $(this).attr('name');
							var title = $(this).attr('title');
							var val = $(this).val().trim();

							if ( val == '' )
								errors.push(
									{
										name: theName,
										message: 'Vous devez spécifier ' + (title ? title.toLowerCase() : name)
									});

							if ( val != '' && theName.indexOf('email') != -1 && ! emailReg.test(val) )
								errors.push(
									{
										name: theName,
										message: (title ? title : name) + ' n\'est pas une adresse mail valide.'
									});

							if ( val != '' && theName.indexOf('tel') != -1 && (val.stripNonNumeric().length < 7 || val.stripNonNumeric().length > 20) )
								errors.push(
									{
										name: theName,
										message: (title ? title : name) + ' n\'est pas un numéro de téléphone valide.'
									});
						});

					if ( errors.length > 0 )
						{
							event.preventDefault();
							var msgs = new Array();

							theForm.find('input[type!=radio], textarea[type!=radio]').removeClass('error');
							$(errors).each( function()
								{
									var self = this;
									msgs.push( self.message );
									theForm.find('input[name=' + self.name + '], textarea[name=' + self.name + ']')
										.addClass('error');
								});

							arcadimMsgBox.error({message: msgs.join('</p><p>')});
							theForm.find('.error').first().focus();
							return false;
						}
				})
		; // $('#formulaire-vente')

		$('#champ-arcapass input')
			.data('default', $('#champ-arcapass input').val())
			.click( function()
				{
					$(this).val('');
				})
			.blur( function()
				{
					if ( $(this).val == '')
						$(this).parents('form').first().get(0).reset();
				})
			.focus( function()
				{
					$(this).val('');
				});

		// Calculs de hauteurs de box
		//$('.widget-home .widget-body').sameHeight();
		//var theOneHeight = $('.tab-content .widget-body').sameHeight();
		var aToolbar = $('#tabs-wrapper .widget-toolbar').first();
		var aTabItem = $('#tabs-wrapper .tab-header a').first();
		var aFooter = $('#tabs-wrapper .widget-footer').first();

		//var subtracted = theOneHeight + (aToolbar.outerHeight() + aTabItem.outerHeight() + aFooter.outerHeight());

		/*$('#feature')
			.css(
				{
					height: subtracted + 'px',
					clip: 'rect(0px 300px ' + subtracted + 'px 0px)'
				});

		*/
	var slideshow = $('#feature-articles');
	slideshow.css('z-index', 999);

	var tabs = $('#tabs-wrapper');

		$('.tab-header a').not('.basic').each( function()
			{
				var li = $(this).parents('li').first();
				if ( ! li.hasClass('tab-active') )
					{
						var panel = $(this).attr('href');
						$(panel).css('display', 'none');
						var feature = $(this).attr('rel');
						$(feature).css('display', 'none');
					}
			})
		; // $('.tab-header a')

		$('.tab-header a')
			.not('.basic')
			.click( function(event)
				{
					event.preventDefault();
					var li = $(this).parents('li').first();

					if ( li.hasClass('tab-active') )
						return;

					var nextPanel = $(this).attr('href');
					var activePanel = $('.tab-active a').attr('href');

					slideshow
						.fadeOut(150, function() {

							$(activePanel)
								.stop()
								.animate({opacity: 0}, function()
									{
										$(this).css('display', 'none');

										slideshow.css('z-index', -2);

										tabs.find('#criteres-plus')
											.hide();

										tabs.find('#criteres-plus-actifs').val('no');

										if ($(nextPanel).hasClass('widepanel')) {
											tabs.css('width', '940px');
										} else {
											tabs.css('width', '620px');
										}


										$('.tab-active').removeClass('tab-active');
										li.addClass('tab-active');
										Cufon.refresh();

										$(nextPanel)
											.css('display', 'block')
											.css('opacity', 0)
											.animate({opacity: 1}, function() {

												if ($(this).attr('id') == 'tab-recherche'){
													slideshow.css('z-index', 999);
												}
												slideshow.fadeIn(150);
											});

									});
						});
				})
		; // $('.tab-header a')

	tabs
		.css({
			borderLeft: 0,
			borderRight: 0,
			borderBottom: 0,
			width: '620px'
		});

	var bodyHeight = 350 - (tabs.find('.widget-footer').outerHeight() + 1 + 20 * 2);
	tabs
		.find('.widget-body')
		.css('height', bodyHeight+'px');


	$('#criteres-plus').hide();
	$('#criteres-plus-actifs').val('no');

	tabs.find('#details')
		.click(function(event) {

			event.preventDefault();

			var self = $(this);
			if ($(this).hasClass('expanded')) {

				$(this).removeClass('expanded');
				$('#criteres-plus-actifs').val('no');
				tabs.find('#criteres-plus')
					.fadeOut(function() {
						tabs
							.stop()
							.animate({ width: '620px' }, function(){
								slideshow.css('z-index', '9999');
							});
						self.find('.button-text').text('Plus de critères');
						Cufon.replace('#details');
					});

			} else {
				// alert('ICI');				
				slideshow.css('z-index', -2);
				tabs
					.stop()
					.animate({ 'width' : '940px' }, function() {
						$('#criteres-plus-actifs').val('yes');
						tabs.find('#criteres-plus')
							.fadeIn(function() {
								tabs.find('#details')
									.addClass('expanded');
								self.find('.button-text').text('Moins de critères');
								Cufon.replace('#details');
							});
					});
			}
		});


	$('.widget-home .widget-body').sameHeight();

	$('#exclusivites').find('.exclu-box .widget-body').sameHeight();

	$('#exclusivites').cycle({
		next: '.exclu-nav .next',
		prev: '.exclu-nav .prev',
		fx: 'scrollHorz',
		pause: true,
		timeout: 11000
	});

	$('#feature-articles')
		.find('.feature-text')
			.each(function() {
				var
					textItem 	= $(this),
					topMargin 	= 350 - textItem.outerHeight();
				
				textItem.css('marginTop', topMargin+'px');

			});
	
	$('#feature-articles')
		.cycle({
			pause: true,
			timeout: 8000
		});

});

