﻿function VerifForm(formulaire)
{
	adresse = formulaire.email.value;
	var place = adresse.indexOf("@",1);
	var point = adresse.indexOf(".",place+1);
	if ((place > -1)&&(adresse.length >2)&&(point > 1))
	{
		formulaire.submit();
		return(true);
	}
	else
	{
		alert('Entrez une adresse e-mail valide !');
		return(false);
	}
}

function HiddenTable(tid1, tid2)
{
	if (document.getElementById(tid1).style.display == "none")
	{
	    document.getElementById(tid2).style.display = "none";
	    document.getElementById(tid1).style.display = "";
	}
	else if (document.getElementById(tid2).style.display == "none")
	{
	    document.getElementById(tid2).style.display = "";
	    document.getElementById(tid1).style.display = "none";
	}
}

function HiddenShowTable(tid)
{
	if (document.getElementById(tid).style.display == "none")
	{
	    document.getElementById(tid).style.display = "";
	}
	else 
	{
	    document.getElementById(tid).style.display = "none";
	}
}

function log_out()
{
	if (confirm('Êtes vous sûr de vouloir vous déconnecter ?'))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function confirm_submit(message)
{
	if (confirm(message))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function confirm_action(message)
{
	if (confirm(message))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function affichage_popup(nom_de_la_page, nom_interne_de_la_fenetre, width, height)
{
   window.open(nom_de_la_page, nom_interne_de_la_fenetre, config='height='+height+', width='+width+', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
}

function sendData(page, param, elementID)
{
	elementID = (elementID == "") ? "contenu" : elementID;
	
	if(document.all)
	{
		var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
	}
	else
	{
		var XhrObj = new XMLHttpRequest();
	}

	var content = document.getElementById(elementID);
		
	XhrObj.open("POST", page);

	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200)
		{
			content.innerHTML = XhrObj.responseText;
		}
	}

	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XhrObj.send(param);
}

function change_photo(photo_name)
{
	document.getElementById('main_photo').src = photo_name;
}

function donner_focus(chp)
{
	document.getElementById(chp).focus();
	UpdateEmailField(false);
}








function SetSearchResultsWrapperVisible(visible)
{
	if (visible)
	{
		$('div#div_instantSearchResults').show('slow');
	}
	else
	{
		$('div#div_instantSearchResults').hide('slow');
	}
}
function UpdateEmailField(focusing)
{
	var tmpElement = document.getElementById("txtbx_instantSearch");
	var tmpString = "";
	if (focusing)
	{
		if (tmpElement.value == tmpString)
		{
			tmpElement.value = "";
			tmpElement.style.color = "";
		}
	}
	else
	{
		if (tmpElement.value == "")
		{
			tmpElement.value = tmpString;
			tmpElement.style.color = "#777";
		}
	}
}




/*
$(document).keyup(function(e) {
 // if (e.keyCode == 13) { $('.save').click(); }     // enter
  if (e.keyCode == 27)
  {
	  //document.getElementById("txtbx_instantSearch").value = "";
	  //SetSearchResultsWrapperVisible(false);
  }   // esc
});
*/





	/************************** ADVANCED SEARCH **************************/
	
	$.ajax("/ajax/advanced_search.php?catID=" + CURRENT_CATEGORY_ID, {complete:function(j,t){
		if (t == "success")
		{
			$("#div_advancedSearchWrapper").html(j.responseText);
			applyjQueryStyles();
		}
		}});



$(document).ready(function() {
	
	$(function() {
          $("img").lazyload({
              effect : "fadeIn"
          });
      });
	
	
	$('#toggle4').click(function() {
		$('.toggle4').slideToggle('fast');
		return false;
	});
	
	
	
	$("a[rel=example_group]").fancybox({
			'titleShow'     : true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});
	$(".various5").fancybox({
		'width'				: 610,
		'height'			: 570,
		'autoScale'     	: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	$("#various6").fancybox({
	    'padding'           : 0,
        'autoScale'     	: true,
        'transitionIn'		: 'none',
		'width'				: 785,
		'height'			: 575,
		'transitionOut'		: 'none'
	});


	/************************** AJAX SEARCH **************************/
	var runningRequest = false;
	var request;
	
	$(document).click(function(){SetSearchResultsWrapperVisible(false);});
	

	$('input#txtbx_instantSearch').click(function(e){
		if ($(this).val() != "")
		{
			SetSearchResultsWrapperVisible(true);
			e.stopPropagation();
		}
	});
	
	
	var searchQuickFct = function(e){

		e.preventDefault();
		var tmpTxtbxInstSrch = $("input#txtbx_instantSearch");
		var tmpSelectCatID = $('select#select_catID');
		
		if (tmpTxtbxInstSrch.val() == '')
		{
			$('div#div_instantSearchResults').html('');
			return false;
		}

		//Abort opened requests to speed it up
		if (runningRequest)
		{
			request.abort();
		}
		
		runningRequest=true;
		request = $.getJSON(
			'/ajax/search.php',
			{
				searchKeywords:tmpTxtbxInstSrch.val(),
				searchCatID:tmpSelectCatID.val()
			},
			function(data){
				showResults(data,tmpTxtbxInstSrch.val());
				runningRequest = false;
				}
			);

		//Create HTML structure for the results and insert it on the result div
		function showResults(data, highlight)
		{
			var tmpTricksHTML = '';
			var tmpAnnoncesHTML = '';
			
			$.each(data, function(i,item)
			{
				var tmpHTML =
					  '<li><a href="'+item.ViewLink+'">'
						+ '<h2>'+item.Title+'</h2>'
						//+ '(pertinence '+item.Score+')' // ## pertinence
						//+ '<p>'+(item.Content.replace(highlight, '<span class="highlight">'+highlight+'</span>'))+'</p>'
						+ '<p>'+item.Content+'</p>'
					+ '</a></li>';
					
				if (item.Class == "Trick")
				{
					tmpTricksHTML += tmpHTML;
				}
				else if (item.Class == "Annonce")
				{
					tmpAnnoncesHTML += tmpHTML;
				}
			});
			
			
			tmpTricksHTML = (tmpTricksHTML != "")
				? '<ul>' + tmpTricksHTML + '</ul>'
				: "Aucune astuce trouvée";
			
			tmpAnnoncesHTML = (tmpAnnoncesHTML != "")
				? '<ul>' + tmpAnnoncesHTML + '</ul>'
				: "Aucune annonce trouvée";
			
			var resultHtml =
				  '<div class="instantSearchResult_wrapper">'
					+ '<a href="#" onclick="javascript:SetSearchResultsWrapperVisible(false); return false;" class="closeButton">Fermer</a>'
					+ ((tmpAnnoncesHTML != "") ? '<div class="instantSearchResult iSR_annonces"><h1>Annonces</h1>' + tmpAnnoncesHTML + '</div>' : "")
					+ ((tmpTricksHTML != "") ? '<div class="instantSearchResult iSR_tricks"><h1>Astuces</h1>' + tmpTricksHTML + '</div>' : "")
					+ '<div style="clear: both;"></div>'
				+ '</div>';
			
			$('div#div_instantSearchResults').html(resultHtml);
			SetSearchResultsWrapperVisible(true);
			
			$("div#div_instantSearchResults div.instantSearchResult_wrapper").click(function(e){e.stopPropagation();});
		}
		
		/*$('form').submit(function(e)
		{
			e.preventDefault();
		});*/
	}
	
	$('input#txtbx_instantSearch').keyup(searchQuickFct);
	$('select#select_catID').change(searchQuickFct);
	
	
	(function(d, s, id) {
	var js, fjs = d.getElementsByTagName(s)[0];
	if (d.getElementById(id)) return;
	js = d.createElement(s); js.id = id;
	js.src = "//connect.facebook.net/fr_FR/all.js#xfbml=1";
	fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));



});

$(document).pngFix();


var sliderHeight = "15px";

	$(document).ready(function(){
	$('.slider').each(function () {
				var current = $(this);
				current.attr("box_h", current.height());
			}
	 );
	
	$(".slider").css("height", sliderHeight);
	
	$(".slider_menu").html('<a href="#">Lire toute la description (+)</a>');
	$(".slider_menu a").click(function() { openSlider() })
});

function openSlider()
{
	var open_height = $(".slider").attr("box_h") + "px";
	$(".slider").animate({"height": open_height}, {duration: "slow" });
	
	$(".slider_menu").html('<a href="#">Fermer (-)</a>');
	$(".slider_menu a").click(function() { closeSlider() })
}

function closeSlider()
{
	$(".slider").animate({"height": sliderHeight}, {duration: "slow" });
	
	$(".slider_menu").html('<a href="#">Lire toute la description (+)</a>');
	$(".slider_menu a").click(function() { openSlider() })
}





function ExecuteJQueryAtEnd()
{
	$.fn.extend({customStyle : function(options){
		if (!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
		return this.each(function() {
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				selectBoxSpanInner.text($(this).val()).parent().addClass('changed');
			});
		});
		}
	}});
	
	applyjQueryStyles();
	$('.slider1').bxSlider({auto: true});
	$('.slider2').bxSlider({auto: true});
}

function applyjQueryStyles()
{
	$('select.cat-select').selectmenu(/*{style:'popup'}*/);
	$('select.superStyle').selectmenu(/*{style:'popup'}*/);
	$('input:checkbox:not([safari])').checkbox();
	$('input[safari]:checkbox').checkbox({cls:'jquery-safari-checkbox'});
	$('input:radio').checkbox();
	$('select.styled').customStyle();
}


function showOneByOne(itemsSelector, speed)
{
	if (speed == null)
	{
		speed = 90;
	}
	
	var itemsCnt = $(itemsSelector).length;
	var list = $(itemsSelector).hide();
	var index = 0;
	ShowImage(list, index);
	
	function ShowImage()
	{
		$(list[index]).fadeIn(speed,
			function()
			{ 
				if (itemsCnt > 0)
				{
					++index; --itemsCnt;
					ShowImage(list, index);
				}
			});
	} 
}

 
function developAdvancedSearch()
{
	showOneByOne("#formAdvancedSearch div.toDevelop", 30);
}


 
$(document).ready(function() {

// fade in images already loaded:
$('#floatimgtn img ').fadeIn(1500);
// and tell pending images to do the same, once they've finished loading:
$('#floatimgtn img ').load(function () {
    $(this).fadeIn(1500);
});

});

$(document).ready(function() {

// fade in images already loaded:
$('img#main_photo').fadeIn(2000);
// and tell pending images to do the same, once they've finished loading:
$('img#main_photo').load(function () {
    $(this).fadeIn(2000);
});

});


$(document).ready(function() {
	showOneByOne('#gallerylist li');
});


