/*---- Diaporama.1.1.js ----*/

function getID(swfID) {
    if ($.browser.msie) {
        return getFlashMovieObject(swfID);
    }
    else if ($.browser.mozilla && navigator.appName.indexOf("Netscape") != -1) {
        return document[swfID];
    }
    else if ($.browser.safari) {
        return window.document.myFlashMovie;
    }
    return undefined;
}


function getFlashMovieObject(movieName) {
    if (window[movieName]) {
        return window[movieName];
    }
    else if (window.document[movieName]) {
        return window.document[movieName];
    }
    else if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName];
    }
    else if (navigator.appName.indexOf("Microsoft Internet")!= -1)
    {
        return document.getElementById(movieName);
    }
}

function callJavascript(num) {
    var sendText = num;
    var me = getID("myFlashMovie");
    if (me != undefined) {
        me.SetVariable("testValue", sendText);
    }
}

var PJPAIntervalAfficheFlash = null;
var PJPACptTentativeAfficheFlash = 0;
function callJavascriptLoadXML(nomGlobal) {
    var sendText = "charge";
    var me = getID("myFlashMovie");
    if (me != undefined) {
        me.setAttribute("testValue", sendText);
    }
}

function callJSGetCurrentImg() {
    var currentImage = 0;
    var me = getID("myFlashMovie");
    if (me != undefined) {
        try {
            if ($.browser.msie) {
                var flashMovie = getFlashMovieObject("myFlashMovie");
                currentImage = flashMovie.GetVariable("/:currentImage");
            }
            else {
                currentImage = me.GetVariable("/:currentImage");
            }
        }
        catch (ex) {

        }
    }
    return currentImage;
}

function PJPAFlashDemarre() {
    //Ne pas Effacer
}

function PJPAAfficheFlash(nomGlobal) {
    var chtFlash = eval(nomGlobal + ".ChargementFlash");
    if (chtFlash != undefined) {
        if (!chtFlash) {
            eval(nomGlobal + ".ChargementFlashOk();");
        }
        else {
            clearInterval(PJPAIntervalAfficheFlash);
        }
    }
}

/******* Debut PJPADiaporama ***/
function PJPADiaporama(nomGlobal, conteneurDiv, listeImage, canPlayAndStop, canZoom, withMax, heightMax) {
    
    this.ID = nomGlobal; 
    this.ConteneurDiv = document.getElementById(conteneurDiv);
    this.BtnPlay = null;

    this.Intervall = 5000;

    this.ListeImage = listeImage;
    this.CanPlayAndStop = canPlayAndStop;
    this.CanZoom = canZoom;
    this.WidthMax = withMax;
    this.HeightMax = heightMax;

    this.CurrentImgPlay = 0;
    this.IsLoading = false;

    var loadAnimation = PJPAReadCookie('DiapoLoading');
    this.InitDiaporama(loadAnimation);
}

PJPADiaporama.prototype.InitDiaporama = function(loadAnimation) {

    if (this.CanPlayAndStop) {
    	var DivBtnPlay = '<div id="BtnPlay" onmousedown="' + this.ID + '.Play(true)" class="BoutonPlay_Off" onmouseover="mld(this,\'BoutonPlay\')" ></div>';
    	var DivBtnPause = '<div id="BtnPause" onmousedown="' + this.ID + '.Stop(true, true)" class="BoutonPause_Off" onmouseover="mld(this,\'BoutonPause\')" ></div>';
    	var DivBtnZoom = '<div id="BtnZoom" onmousedown="' + this.ID + '.Zoom(true)" class="BoutonZoom_Off" onmouseover="mld(this,\'BoutonZoom\')" ></div>';
        var DivBtn = '<div id="BtnAction" class="Btn_Action">' + DivBtnPlay + DivBtnPause + DivBtnZoom + '<div class="the_breaker"></div></div>';

        $(this.ConteneurDiv).html($(this.ConteneurDiv).html() + DivBtn);
        this.BtnPlay = "BtnPlay";
        this.BtnPause = "BtnPause";
        this.BtnZoom = "BtnZoom";

        if (this.CanZoom) {
            for (var i = 0; i < this.ListeImage.length - 1; i++) {
                this.SetActionClick(this.ListeImage[i]);
            }
        }
    }

    if (this.ListeImage.length > 1) {
        if (loadAnimation == "1") {
            this.IsLoading = true;
            setTimeout(this.ID + ".Play(false);", 1000);
        }
        else {
            $("#" + this.BtnPause).hide();
            this.ShowBtnPlay();
        }
    }

    if (this.ListeImage.length == 0) {
        $("#" + this.BtnPause).hide();
        $("#" + this.BtnPlay).hide();
        $("#" + this.BtnZoom).hide();
    }
    else if (this.ListeImage.length == 1) {
        $("#" + this.BtnPause).hide();
        $("#" + this.BtnPlay).hide();
    }

}

PJPAInterfaceDiaporama.WriteCookie = function() {
    if (_PJPAInterfaceDiaporama.Diapo.IsLoading == true)
        var load = 1;
    else
        var load = 0;
        
    PJPACreateCookie('DiapoLoading', load, 1);
}

PJPADiaporama.prototype.Play = function(TagXiti) {
    callJavascript("lecture");
    this.CurrentImgPlay = callJSGetCurrentImg();
//    if (TagXiti) {
//        xt_med('C', '2', 'Immo::FD_Immo::l_Lanc_Animation', 'N');
//    }
    this.IsLoading = true;
    $("#" + this.BtnPlay).hide();
    this.ShowBtnPause();
    PJPAInterfaceDiaporama.WriteCookie();
}

PJPADiaporama.prototype.Stop = function(TagXiti, UserStop) {
    callJavascript("stop");
    this.CurrentImgPlay = callJSGetCurrentImg();
//    if (TagXiti) {
//        xt_med('C', '2', 'Immo::FD_Immo::l_Arret_Animation', 'N');
//    }
    this.IsLoading = false;

    $("#" + this.BtnPause).hide();
    this.ShowBtnPlay();
    if (UserStop) {
        PJPAInterfaceDiaporama.WriteCookie();
    }
}

PJPADiaporama.prototype.ShowBtnPlay = function() {
    if (this.ListeImage.length > 1) {
        $("#" + this.BtnPlay).show();
    }
}

PJPADiaporama.prototype.ShowBtnPause = function() {
    if (this.ListeImage.length > 1) {
        $("#" + this.BtnPause).show();
    }
}

PJPADiaporama.prototype.Zoom = function(TagXiti) {
	this.Stop(false, false);
	this.CurrentImgPlay = callJSGetCurrentImg();
	document.getElementById(CurrentImageSelected).value = this.CurrentImgPlay;
	//    if (TagXiti) {
	//        xt_med('C', '2', 'Immo::FD_Immo::l_Zoom', 'N');
	//    }
	
	$("#" + ButtonOpenPopUp).click();
}

PJPADiaporama.prototype.SetCurrentImgPlay = function(i) {
    this.CurrentImgPlay = i;
}

PJPADiaporama.prototype.ActionStop = function() {
    this.IsLoading = false;
    $("#" + this.BtnPause).hide();
    $("#" + this.BtnPlay).show();
}

/*** Fin PJPADiaporama ***/


/*---- InterfaceDiaporama.1.1.js ----*/

function PJPADiaporamaCreateDivInterface(nomGlobal, idannonce) {

    var nomFlash = "PJPAdiaporamaNoJS.swf";
    var html = '<div id="' + nomGlobal + '_diaporama">';

    html += '<div class="Flash" >';
    html += '<div id="' + nomGlobal + '_CadreFlash" class="cadreFlash" onmousedown="' + nomGlobal + '.Diapo.Zoom();" >';
    html += '<img  id="' + nomGlobal + '_ImgPix" src="/_Images/pix.gif" width="341" height="255" style="position:absolute;top:0px; left:0px" class="pixtransp" >';
    html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=7,0,0,0" ';
    html += 'id="myFlashMovie" width="341" height="255"> ';
    html += '<param name="movie" value="/_Swf/Diaporama/' + nomFlash + '?varjs=' + nomGlobal + '&id=' + idannonce + '"> ';
    html += '<param name="quality" value="high"> ';
    html += '<param name="play" value="false"> ';
    html += '<param name="bgcolor" value="#FFFFFF"> ';
    html += '<param name="allowScriptAccess" value="always">';
    html += '<param name="swLiveConnect" value="true"> ';
    html += '<param name="wmode" value="transparent" />';
    html += '<embed play="false" wmode="transparent" allowScriptAccess="always" swLiveConnect="true" name="myFlashMovie" src="/_Swf/Diaporama/' + nomFlash + '?varjs=' + nomGlobal + '&id=' + idannonce + '" ';
    html += 'quality="high" bgcolor="#FFFFFF" width="341" height="255" type="application/x-shockwave-flash" ';
    html += 'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed >';
    html += '</object > ';
    html += '<img id="' + nomGlobal + '_CadreNonDispo" src="/_Images/Non_Dispo/FicheDetail.jpg" width="341" height="255" alt="Photo non disponible" >';

    //        html += '<div id="' + nomGlobal + '_DetectFlash" ></div>';

    html += '</div>';//fin div cadreFlash
    html += '<div id="' + nomGlobal + '_CadreBtnAction" ></div>';
    html += '</div>'; //fin div Flash
	html += '<div class="the_breaker"></div>';

	html += '<div id="' + nomGlobal + '_DivCarrPhotos"></div>';
    html += '<div class="the_breaker"></div>';
    html += '</div>';//fin div carrouselFD
    html += '<div class="the_breaker"></div>';

    document.writeln(html);
}

function PJPAInterfaceDiaporama(conteneurDiv, secteur,  listeImg,  canPlayAndStop, canZoom, widthMax, heightMax, withDiapo, withCarroussel, linkDiapoAndCarr, withDescription, nomGlobal, idannonce, idCadrePhoto) {
    this.ID = nomGlobal;

    this.ListeImage = listeImg;
    this.Secteur = secteur;
    this.ConteneurDiv = document.getElementById(conteneurDiv);
    this.ConteneurFlash = document.getElementById(nomGlobal + "_CadreFlash");
    this.ConteneurNonDispo = document.getElementById(nomGlobal + "_CadreNonDispo");
    this.ConteneurChargement = document.getElementById(nomGlobal + "_CadreChargement");
    this.ConteneurBtnAction = document.getElementById(nomGlobal + "_CadreBtnAction");
    this.ConteneurImgPix = document.getElementById(nomGlobal + "_ImgPix");
    this.ConteneurDetectFlash = document.getElementById(nomGlobal + "_DetectFlash");
    this.Flash = document.getElementById("myFlashMovie");

    this.IdCadrePhoto = document.getElementById(idCadrePhoto);
    this.WithAlerteEmail = false;
/*
    $(this.ConteneurDetectFlash).flash({ src: '/_swf/DetectFlash.swf' }, { version: '11' });
    var innerHtmlDetect = $(this.ConteneurDetectFlash).html();
    if (innerHtmlDetect.indexOf('This content requires the Flash') == -1) {
        $(this.ConteneurDetectFlash).hide();
        
    */    
        this.ChargementFlash = false;
        this.CurrentZoom = 0;

        var nbVignettes = 4;
        this.linkDiapoAndCarr = linkDiapoAndCarr;

        if (this.ListeImage.length > 0) {

            if (withDiapo) {
                this.Diapo = new PJPADiaporama(this.ID + ".Diapo", this.ID + "_CadreBtnAction", this.ListeImage, canPlayAndStop, canZoom, widthMax, heightMax);
            }
            if (this.ListeImage.length > 1) {
            	if (withCarroussel) {
            		/*
            		this.Carroussel = new PJPACarroussel(this.Secteur, this.ListeImage, conteneurDiv, 4, 60, 45, false, this.ID + ".Carroussel", this.ID, PJPA_MODE_DIAPO, this.idAnnonce, true, "");
            		this.Carroussel.DiaporamaFlash = this.ConteneurFlash;

            		if (this.ListeImage.length > 4) {
						$(this.IdCadrePhoto).css("height", "340px");
            		}
            		else {
            			$(this.IdCadrePhoto).css("height", "315px");
            		}
					*/

            		var divimages = this.ID + "_DivCarrPhotos";
            		var paramWScarrPhotoAd = ' "idannonce" : "' + idannonce + '" ';
            		this.Carroussel = new SmartCarroussel(this.ID + ".Carroussel", this.Secteur, divimages, null, MODE_WITH_JS, MODE_HORIZONTAL, false);
            		this.Carroussel.SetParametresWS(paramWScarrPhotoAd, "GetDiaporamaAnnonce");
            		this.Carroussel.SetVignetteConfig(4, 56, 42, true);
            		this.Carroussel.SetIsVisible(true);
            		this.Carroussel.SetCallWsByPage(false);
            		this.Carroussel.SetParent(this.ID);
            		this.Carroussel.CreateIHM();
            		this.Carroussel.DiaporamaFlash = this.ConteneurFlash;

            		if (this.ListeImage.length > 4) {
            			$(this.IdCadrePhoto).css("height", "340px");
            		}
            		else {
            			$(this.IdCadrePhoto).css("height", "315px");
            		}       		
            	}
            }
        }
        else {
            this.HideFlash();
        }
        /*
    }
    else {
        this.HideFlash();
        this.HideNonDispo();
    }*/
}

PJPAInterfaceDiaporama.prototype.SetWithAlerEmail = function(val) {
	this.WithAlerteEmail = val;
}

PJPAInterfaceDiaporama.Chargement = function(nomGlobal) {
    PJPAIntervalAfficheFlash = setInterval("PJPAAfficheFlash('" + nomGlobal + "')", 500);
}

PJPAInterfaceDiaporama.prototype.ChargementFlashOk = function() {
    this.ChargementFlash = true;
    if (this.ListeImage.length > 0) {
        this.HideNonDispo();
    }
}

PJPAInterfaceDiaporama.prototype.HideChargement = function() {
    $("#" + this.ConteneurChargement.id).hide();
}

PJPAInterfaceDiaporama.prototype.HideFlash = function() {
    this.ConteneurFlash.onmousedown = "";
    $("#" + this.ConteneurImgPix.id).hide();
    $("#" + this.Flash.id).hide();
}

PJPAInterfaceDiaporama.prototype.HideNonDispo = function() {
    $("#" + this.ConteneurNonDispo.id).hide();
}

PJPAInterfaceDiaporama.SetCurrentImgPlay = function(nomGlobal, i) {
    var obj = PJPAGetEvalObj(nomGlobal);
    if (obj != null && obj != undefined) {
        obj.Diapo.SetCurrentImgPlay(i);
    }
}

function PJPAGetEvalObj(id) {
    try {
        return eval(nomGlobal);
    }
    catch (ex) {
        return undefined;
    }
}

PJPAInterfaceDiaporama.ActionStop = function(nomGlobal) {
    var obj = PJPAGetEvalObj(nomGlobal);
    if (obj != null && obj != undefined) {
        obj.Diapo.ActionStop();
    }    
}

PJPAInterfaceDiaporama.prototype.InitPagination = function() {
	var innerhtml = "";
	var nbimg = this.ListeImage.length;
	for (var i = 0; i < nbimg; i++) {
		innerhtml += "<a class='divPageNumber divPageNumberOne' id='lkImgZoom" + i + "' href='javascript:" + this.ID + ".ChangeZoomImage(" + i + ");'   >" + (i + 1) + "</a>";
	}
	if (this.WithAlerteEmail) {
		innerhtml += "<a class='divPageNumber divPageNumberOne' id='lkImgZoom" + (nbimg) + "' href='javascript:" + this.ID + ".ChangeZoomImage(" + (nbimg) + ");'   >@</a>";
	}

	var ajust = 0;
	if (this.WithAlerteEmail) {
		ajust = 1;
	}

	$("#PaginationZoom").html(innerhtml);
	$("#PaginationZoom").width(30 * (nbimg + ajust));
	this.CurrentZoom = this.Diapo.CurrentImgPlay;
	this.ChangeZoomImage(this.CurrentZoom);

	this.AffCacheLienPrecSuiv();
}

PJPAInterfaceDiaporama.prototype.ChangeZoomImage = function(i) {
	if (i == null) {
		i = 0;
	}
	if (i < this.ListeImage.length) {
		if (this.WithAlerteEmail) {
			$("#EmailAlert").hide();
			$("#ctl00_ContentPlaceHolder1_DescriptionFD_Diapo_PopUp__ZoomImage_UCPopUp_conteneur").show();
		}

		$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber divPageNumberOne");
		this.CurrentZoom = i;

		var index = this.ListeImage[this.CurrentZoom].ImageUrl.lastIndexOf("_");
		var url = this.ListeImage[this.CurrentZoom].ImageUrl.substring(0, index) + "_Zoom.jpg";
		$("#" + ImageAgrandie).attr("src", url);

		$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber orange divPageNumberOne");

		if (document.getElementById("photo_legend_num") != undefined) {
			numPhoto = parseInt(i) + 1;
			document.getElementById("photo_legend_num").innerHTML = " - Photo n&deg;" + numPhoto;
		}

	}
	else if (i == this.ListeImage.length && this.WithAlerteEmail) {
		$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber divPageNumberOne");
		this.CurrentZoom = i;
		$("#ctl00_ContentPlaceHolder1_DescriptionFD_Diapo_PopUp__ZoomImage_UCPopUp_conteneur").hide();
		$("#EmailAlert").show();
		$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber orange divPageNumberOne");
	}
	this.AffCacheLienPre + cSuiv();
}

PJPAInterfaceDiaporama.prototype.AffCacheLienPrecSuiv = function() {
	var ajust = 0;
	if (this.WithAlerteEmail) {
		ajust =1;
	}

	if (this.ListeImage.length > 1 + ajust) {
		$("#" + HyperLink__Previous).css("visibility", "visible");
		$("#" + HyperLink__Next).css("visibility", "visible");
		
		if (this.CurrentZoom == 0) {
			$("#" + HyperLink__Previous).css("visibility", "hidden");
		}
		else if (this.CurrentZoom == this.ListeImage.length - 1 + ajust) {
			$("#" + HyperLink__Next).css("visibility", "hidden");
		}
		else {
			$("#" + HyperLink__Previous).css("visibility", "visible");
			$("#" + HyperLink__Next).css("visibility", "visible");
		}
	}
	else {
		$("#" + HyperLink__Previous).css("visibility", "hidden");
		$("#" + HyperLink__Next).css("visibility", "hidden");
	}
}


PJPAInterfaceDiaporama.prototype.PreviousZoomImage = function() {
    if (this.CurrentZoom > 0) {
    	$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber divPageNumberOne");
        this.CurrentZoom--;
        this.ChangeZoomImage(this.CurrentZoom);
        $("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber orange divPageNumberOne");
    }
    this.AffCacheLienPrecSuiv();
}

PJPAInterfaceDiaporama.prototype.NextZoomImage = function() {
	var ajust = 0;
	if (this.WithAlerteEmail) {
		ajust = 1;
	}

	if (this.CurrentZoom < this.ListeImage.length - 1 + ajust) {
		$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber divPageNumberOne");
		this.CurrentZoom++;
		this.ChangeZoomImage(this.CurrentZoom);
		$("#lkImgZoom" + this.CurrentZoom).attr("class", "divPageNumber orange divPageNumberOne");
	}
	this.AffCacheLienPrecSuiv();
}

/*** Fin PJPAInterfaceDiaporama ***/
function PJPACreateCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function PJPAReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return '1';
}

function PJPAEraseCookie(name) {
    createCookie(name, "", -1);
}



