/*
    $Id: frontend_programme_viewLot.js 4310 2010-08-17 13:09:32Z  $
*/
dojo.require("dojo.NodeList-fx");

var currentPage = '';
var pages = new Object();

/**
 Initialisation de la page
 */
var init = function() {
	
	function openForm(src, target)
	{
		return document.getElementById('hidPopUp').value
				+ fct_urlEncode(fct_urlEncode(src + 'images_700_500/' + target));
	}	// function openForm
	
		// Redirection des liens des miniatures pour affichage du grand format
		dojo.query('.vign').query('a').connect('onclick', function(e) {
		// désactive l'action par défaut
		e.preventDefault();
		// récuperation du nom de l'image
		target = e.target.src.substring(e.target.src.lastIndexOf("/") +1 );

		// mise à jour de l'adresse de la grande image
		image = dojo.query('.programme_image img')[0];
		image.src = image.src.substring(0, image.src.lastIndexOf("/") + 1) + target;

		// Traitement des grandes images pour faire le zoom
		var aImages	= document.getElementsByTagName('img');

		for (i in aImages)
		{
			if (aImages[i].className == "image_to_up grande_image")
			{
				aImages[i].onclick = function () {
					window.open(this.src, 'image', config='height=500, width=700, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, directories=no, status=no');
				};
			}
		}
	 });
	
	// A l'initialisation on récupère l'image à afficher en grand du nouvel onglet
	target = document.location.href.substring(document.location.href.lastIndexOf("/page/") +6 );
	if (target.indexOf('/') != -1)
	{
		target = target.substring(0, target.indexOf('/'));
	}	// if
	image = dojo.query('.programme_image img')[0];
	image_new = dojo.query('#' + target + 'Vign0')[0];
	
	if (image_new == null)
	{
		// Récupère l'image à afficher en grand du nouvel onglet
		image_new = dojo.query('#residenceVign0')[0];
	}
	if (image_new != undefined)
	{
		source_new = image_new.src.substring(image_new.src.lastIndexOf("/") + 1, image_new.src.length);
		// Affiche l'image en grand du nouvel onglet
		image.src = image.src.substring(0, image.src.lastIndexOf("/") + 1) + source_new;
	}
	
	// A l'initialisation on rend zoomable la première image
	// Traitement des grandes images pour faire le zoom 
	imageZoom = dojo.query('.programme_image img')[0];
	imageZoom.className = '';
	imageZoom.onclick = function () {
	// récuperation du nom de l'image
	target = this.src.substring(this.src.lastIndexOf("/") + 1);
	imagesrc = this.src.substring(0, (this.src.substring(0, this.src.lastIndexOf("/"))).lastIndexOf("/") + 1);
	//http://site-bmi.dev/media/images_700_500/DhwEVb7v.jpg
	window.open(openForm(imagesrc, target), 'image', config='height=500, width=700, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, directories=no, status=no');
	};

    // Redirection vers la page identifiée dans l'url :
    if (document.location.href.lastIndexOf("/page/") != -1)
    {
    	href = document.location.href;
		currentPage = href.substring(href.lastIndexOf("/page/") +6 );
		if (currentPage.indexOf('/') != -1)
		{
			currentPage = currentPage.substring(0, currentPage.indexOf('/'));
		}
	}
	else
	{
		currentPage = 'residence';
	}
};

function fct_urlEncode(text)
{
	var txt = text;
	txt = escape(txt);
	
	txt = txt.replace(/[/]/g,"%2F");
	txt = txt.replace(/[+]/g,"%2B");
	txt = txt.replace(/[ ]/g,"+");
	return txt;
}

dojo.addOnLoad(init);
