function pseudo_random(min, max) {
	var diff = Math.abs(max - min);
	var i = Math.round(Math.random() * diff);
	i = i + Math.min(min, max);
	return i;
}

function popupcentre(adresse, nomFenetre, largeur, hauteur, scroll) {
	// On force la scroll quand c'est la popup de documentation
	if ((610 == largeur) && (780 == hauteur)) {
		scroll = 1;
	}
	openFormFromFlash("hiddenForm", nomFenetre, adresse, largeur, hauteur,
			scroll);
}

function openFormFromFlash(formId, windowId, url, largeur, hauteur, scroll) {

	var myMatch = url.search('video=');
	var myMatch2 = url.search('/mailing');

	if (myMatch2 != -1 && myMatch == -1) {
		window.open("", windowId, "width=" + largeur + ", height=" + hauteur
				+ ", scrollbars=" + scroll);

		var form = $("#" + formId);

		form.attr("target", windowId);
		form.attr("action", url);
		form.submit();
	} else {

		if (largeur === '') {
			largeur = 410;
		}
		if (hauteur === '') {
			hauteur = 510;
		}
		if (scroll === '') {
			scroll = 'no';
		}
		var largeur_popin = 695;
		var hauteur_popin = parseInt(hauteur);
		var random_number = pseudo_random(10, 10000);

		var myMatch = url.search('video=');
		if (myMatch != -1) {
			largeur_popin = parseInt(largeur) + 20;
			hauteur_popin = parseInt(hauteur) + 80;
			url += '&l=' + largeur + '&h=' + hauteur + '&lp=' + largeur_popin
					+ '&hp=' + hauteur_popin;
		}

		$('body').append('<a id="invisible_link' + random_number + '"></a>');
		$('#invisible_link' + random_number).popin( {
			width : largeur_popin,
			height : hauteur_popin,
			className : "myPopinCogedim",
			opacity : .4
		});

		$('#invisible_link' + random_number).attr('href', url);
		$('#invisible_link' + random_number).click();
		$('#invisible_link' + random_number).remove();

	}
}

// un window.open est fais avant l'appel de la fonction
// on ne fais pas le window.open dans cette fonction car il y a un risque
// que certains navigateur le bloque
function openForm(formId, windowId, url) {
	var hauteur_popin = 1000;
	var random_number = pseudo_random(10, 10000);

	$('body').append('<a id="invisible_link' + random_number + '"></a>');
	$('#invisible_link' + random_number).popin( {
		width : 695,
		height : hauteur_popin,
		className : "myPopinCogedim",
		opacity : .4
	});

	$('#invisible_link' + random_number).attr('href', url);
	$('#invisible_link' + random_number).click();
	$('#invisible_link' + random_number).remove();

	/*
	 * var form = $("#" + formId); form.attr("target", windowId);
	 * form.attr("action", url); form.submit();
	 */
}

/*
 * article_form.tpl.html
 */

function verif_formulaire(idChamp, longChamp) {

	if (idChamp !== '' && longChamp > 0) {
		var texte = document.getElementById('#' + idChamp).val();
		texte = texte.replace(/<strong>/g, "");
		texte = texte.replace(/<\/strong>/g, "");
		texte = texte.replace(/<i>/g, "");
		texte = texte.replace(/<\/i>/g, "");
		texte = texte.replace(/<br \/>/g, "");
		texte = texte.replace(/<br\/>/g, "");
		texte = texte.replace(/<br>/g, "");

		var longueur = texte.length;

		if (longueur > longChamp) {
			alert('Longueur maximale du descriptif est de '
					+ longChamp
					+ ' caractères \n Longueur actuelle de votre descriptif : '
					+ longueur);
			return false;
		}
	}
	if (!check_form('fr'))
		return false;
	else
		return true;
}

/*
 * Ajout de la liste des lots dans "Ma séléction" de l'espace perso
 * 
 */

function addToSelection(idBddProg, idBddLot, idForm, action) {
	formulaire = $("#" + idForm);
	$("#" + idForm + " #idBddProg").val(idBddProg);

	$("#" + idForm + " #idBddLot").val(idBddLot);
	$("#" + idForm + " #returnUrl").val(document.location);
	formulaire.attr("action", action);
	formulaire.submit();
}

/*
 * Suppression dans "Ma séléction" de l'espace perso
 */

function deleteSelectedItem(progLotName, idx) {
	if (confirm("Etes vous certain(e) de vouloir supprimer " + progLotName
			+ " de vos sélections?")) {
		$("#hiddenFormDelete #SeleIdx").val(idx);
		$("#hiddenFormDelete").submit();
	} else {
		return false;
	}
}

function pdfSendMail(sendToFriend) {
	// closePopin();
	$("#openPopinPdfSendMail").attr('href', sendToFriend);
	$("#openPopinPdfSendMail").click();
}

function pdfRechercheRappel(rappel) {
	// closePopin();
	$("#openPopinPdfRechercheRappel").attr('href', rappel);
	$("#openPopinPdfRechercheRappel").click();
}

$(document).ready(
		function() {

			/*
			 * jQuery.ajaxSetup ( { 'beforeSend' : function(xhr) {
			 * if(xhr.overrideMimeType){ xhr.overrideMimeType('text/html;
			 * charset=ISO-8859-1'); } } });
			 */

			var oConteneurFlash_banner = $('#flash_ban_header');
			var oSrc_banner = $('#src_flash_ban_header');

			var oUrl = $('#lienDeFond');
			var oFond = $('#background-feuillage');

			if (oSrc_banner) {
				loadFlash(oSrc_banner.val(), "expand_banner", "1005", "400",
						'', '', {});
			}

			if (oUrl.size() > 0 && oFond.size() > 0) {
				oFond.bind('mouseover', function(oEvent) {
					var oTarget = oEvent.target.id;
					if (oFond.attr("id") === oTarget) {
						oFond.css("cursor", "pointer");
					} else {
						oFond.css("cursor", "auto");
					}
				});

				oFond.bind('click', function(oEvent) {
					var oTarget = oEvent.target.id;
					if (oFond.attr("id") === oTarget) {
						// window.open(oUrl.val(), '_blank');
						document.location.href = oUrl.val();
					}
				});
			}

			if ($('#iframeptzplus').length > 0) {
				BodyOnLoad(0);
			}

			load_all_popins();

			$('.popinPdfLot').popin( {
				width : 1010,
				height : 1200,
				className : "myPopinCogedim",
				opacity : .4,
				onComplete : function() {
					load_all_popins();
				}
			});

			var olightBoxSettings = {
				// Configuration related to overlay
				overlayBgColor : '#18417D', // (string) Background color to
											// overlay; inform a hexadecimal
											// value like: #RRGGBB. Where RR,
											// GG, and BB are the hexadecimal
											// values for the red, green, and
											// blue values of the color.
				overlayOpacity : 0.5, // (integer) Opacity value to overlay;
										// inform: 0.X. Where X are number from
										// 0 to 9
				// Configuration related to navigation
				fixedNavigation : true, // (boolean) Boolean that informs if the
										// navigation (next and prev button)
										// will be fixed or not in the
										// interface.
				// Configuration related to images
				imageLoading : '/images/lightbox-ico-loading.gif', // (string)
																	// Path and
																	// the name
																	// of the
																	// loading
																	// icon
				imageBtnPrev : '/images/prev.png', // (string) Path and the
													// name of the prev button
													// image
				imageBtnNext : '/images/next.png', // (string) Path and the
													// name of the next button
													// image
				imageBtnClose : '/images/close.png', // (string) Path and the
														// name of the close btn
				imageBlank : '/images/lightbox-blank.gif', // (string) Path and
															// the name of a
															// blank image (one
															// pixel)
				btnPrevNextPos : '150px',
				// Configuration related to container image box
				containerBorderSize : 10, // (integer) If you adjust the
											// padding in the CSS for the
											// container,
											// #lightbox-container-image-box,
											// you will need to update this
											// value
				containerResizeSpeed : 400, // (integer) Specify the resize
											// duration of container image.
											// These number are miliseconds. 400
											// is default.
				// Configuration related to texts in caption. For example: Image
				// 2 of 8. You can alter either "Image" and "of" texts.
				txtImage : 'Image', // (string) Specify text "Image"
				txtOf : 'sur' // (string) Specify text "of"
			};

			// Affichage Photos programme
			$('.lightBox').lightBox(olightBoxSettings);

			var olightBoxSettingsSolo = olightBoxSettings;
			olightBoxSettingsSolo.fixedNavigation = false;
			$('.lightBoxSolo').lightBox(olightBoxSettingsSolo);

			/*
			 * Affiche les boutons de contact au rollover
			 */

			$('.listeResultats').mouseleave(function() {
				cacherListeBoutonsCallToAction($(this).attr('id'));
			});

			$('.listeResultats').mouseenter(function() {
				afficherListeBoutonsCallToAction($(this).attr('id'));
			});

			if ($('#src_flash_banniere').length > 0) {
				$('<div id="c_est_beau">&nbsp;</div>').insertAfter(
						'#contenu_text_cest_beau');
				changerFlash($('#src_flash_banniere').val(), '');
			}

			if ($('.specialEnteteParis7').length > 0) {
				$('.newPrintButton > img').attr('src',
						'/image/paris7/bouton_imprimer_paris7.png');
				$('.newSendToFriendButton > img').attr('src',
						'/image/paris7/bouton_envoyer_ami_paris7.png');
			}
			if ($('.specialEnteteViry').length > 0) {
				$('.newPrintButton > img').attr('src',
						'/image/Viry/bouton_imprimer_viry.png');
				$('.newSendToFriendButton > img').attr('src',
						'/image/Viry/bouton_envoyer_ami_viry.png');
			}

			if ($('.specialEnteteCannes').length > 0) {
				$('.newPrintButton > img').attr('src',
						'/image/Viry/bouton_imprimer_viry.png');
				$('.newSendToFriendButton > img').attr('src',
						'/image/Viry/bouton_envoyer_ami_viry.png');
				$('.retour_liste_prog_img').attr('src',
						'/image/Cannes/retour_a_la_liste_marron.png');

			}
			$('#bloc_milieu .programmesSimilaires #imageCarousel img').each(
					function() {
						var iThisImgWidth = $(this).width();
						var iThisImgHeight = $(this).height();
						var iThisRatio = iThisImgWidth / iThisImgHeight;
						var iRealWidth = 346;
						var iRealHeight = iRealWidth / iThisRatio;
						var sThisTop = (iRealHeight < 250) ? sThisTop = '0'
								: '-50%';
						$(this).css('top', sThisTop);
					});

			/*
			 * var image_path=''; $('.programmesSimilaires
			 * #imageCarousel').each( function(){
			 * image_path=$(this).children('a').children('img').attr('src');
			 * $(this).children('a').children('img').hide();
			 * $(this).children('a').css('background-image',
			 * "url('"+image_path+"')") });
			 */

			// si il y a un interstitiel et une banniere expand sous menu alors
			// on cache la bannière au chargement de la page
			if ($('#sPopup').length > 0 && $('#flash_ban_header').length > 0) {
				hide_expand_banner();
			}

		});

// cache la banniere expand sous menu si elle existe
function hide_expand_banner() {
	if ($('#flash_ban_header').length > 0) {
		$('#flash_ban_header').hide();
		$('#fake_flash_ban_header').show();
	}
}

// affiche la banniere expand sous menu si elle existe
function show_expand_banner() {
	if ($('#flash_ban_header').length > 0) {
		$('#flash_ban_header').show();
		$('#fake_flash_ban_header').hide();
	}
}

function load_all_popins() {

	// envoyer a un ami
	$('.popinEnvoiAmi').popin( {
		width : 695,
		height : 900,
		className : "myPopinCogedim",
		opacity : .4
	});

	// demande d'info AP
	$('.popinAvantPremiere').popin( {
		width : 695,
		height : 1200,
		className : "myPopinCogedim",
		opacity : .4
	});

	// newsletter
	$('.popinNewsletter').popin( {
		width : 695,
		height : 750,
		className : "myPopinCogedim",
		opacity : .4
	});

	// alertes
	$('.popinCreerAlerte').popin( {
		width : 695,
		height : 1200,
		className : "myPopinCogedim",
		opacity : .4
	});

	// Demande de contact (sans flash)
	$('.popinRappel').popin( {
		width : 695,
		height : 1085,
		className : "myPopinCogedim",
		opacity : .4
	});

	// Demande de contact (sans flash)
	$('.popinDemandeRDV').popin( {
		width : 695,
		height : 1200,
		className : "myPopinCogedim",
		opacity : .4
	});

	// Demande de documentation (sans flash)
	$('.popinDemandeDoc').popin( {
		width : 695,
		height : 1200,
		className : "myPopinCogedim",
		opacity : .4
	});

	// Intéréssé par un lot
	$('.popinInteresseLot').popin( {
		width : 695,
		height : 1200,
		className : "myPopinCogedim",
		opacity : .4
	});

	// Demande d'étude personnalisée
	$('.popinEtudePerso').popin( {
		width : 750,
		height : 1400,
		className : "myPopinCogedim",
		opacity : .4,
		onComplete : function() {
			$('img.tTip').tinyTips('title');
		}
	});

	// Popin contact
	$('.popinContact').popin( {
		width : 695,
		height : 750,
		className : "myPopinCogedim",
		opacity : .4
	});
}

function openMenuBannerExpandable() {
	$('#flash_ban_header').animate( {
		height : '400px'
	}, 1000, function() {
	});

}

function closeMenuBannerExpandable() {
	$('#flash_ban_header').animate( {
		height : '190px'
	}, 100, function() {
	});

}

$(document).ajaxStart(function(e, xhr, settings) {
	self.location.hash="#";
});
