
var oTimer;
var oFirstTimer;

/* -------- Object() --------------*/
var mySettings = new ObjectSettings();
function ObjectSettings()
{
    this.read = readSettings;
    this.save = saveSettings;
    this.refreshXml = refreshXmlObject;
    
    this.oXmlIndex = new ActiveXObject( "MSXML2.DomDocument" );
    this.oXmlIndex.async = false;
    this.bXmlIndex = false;
    this.oXml = new ActiveXObject( "MSXML2.DomDocument" );
    this.oXml.async = false;
    this.oXmlImage = new ActiveXObject( "MSXML2.DomDocument" );
    this.oXmlImage.async = false;
    
    this.sXml = "";             // path du fichier de la galerie
    this.sImage = "";           // nom de l'imagette
    this.sImageVisu = "";       // nom de l'image en visu
    this.sImageVisuWidth = "";
    this.sImageVisuHeight = "";
    this.sImageText = "";
    this.sVisu = "";            // path de la visu
    this.sPhoto = "";           // path de l'imagette
    this.iTimer = 0;            // temps du timer
    this.sDiaporama = "";       // nom du diaporama : JO2006, WC2006, etc...
    this.sTransition = "";      // effet de style de la transition des imagettes et visu
    this.iPhotoMax = 0;         // nombre de photo dans la galerie
    this.iPhoto = 0;
    this.bStarted = false;
}

function refreshXmlObject()
{
    this.oXml.load( this.sXml );
    var o = this.oXml.selectNodes( "//NewsComponent[NewsItemRef/@NewsItem]" );
    this.iPhotoMax = o.length;
}


function readSettings()
{
    var temp;
    temp = System.Gadget.Settings.read("xTRA");
    if (temp != "")
        this.sTransition = temp ;
    else this.sTransition = "pixelate";
    //
    temp = System.Gadget.Settings.read("xDIA");
    if (temp != "")
        this.sDiaporama = temp ;
    else this.sDiaporama = "photossport";   
    //
    temp = System.Gadget.Settings.read("tURL");
    if (temp != "")
        this.iTimer = temp ;
    else this.iTimer = "30000";
    //
    this.bXmlIndex = this.oXmlIndex.load( "http://test.afp.com/vista-data/francais/topics/index.xml" );
    if( this.bXmlIndex ) {
        var oTopic = this.oXmlIndex.selectSingleNode( "//topics/topic[@name='" + this.sDiaporama + "']" );
        if( oTopic == null ) {
            oTopic = this.oXmlIndex.selectSingleNode( "//topics/topic" );
        }
        this.sXml = oTopic.getAttribute( "reference" ) + "/" + oTopic.getAttribute( "index" );
        this.sPhoto = oTopic.getAttribute( "reference" );
        this.sVisu = oTopic.getAttribute( "reference" );
    }
}

function saveSettings()
{
    System.Gadget.Settings.write("xDIA", this.sDiaporama );
    System.Gadget.Settings.write("xTRA", this.sTransition );
    System.Gadget.Settings.write("tURL", this.iTimer );
}


/* ---------------- Gadget -----------------*/

function init()
{
    System.Gadget.settingsUI = "settings.html";
    System.Gadget.onShowSettings = SettingsOpen;
    System.Gadget.onSettingsClosed = SettingsClosed;
    System.Gadget.onUndock = WhenUndocked;
    System.Gadget.onDock = WhenDocked;
    System.Gadget.Flyout.file = "flyout.html";
    System.Gadget.Flyout.onShow = flyoutOnShow;
    System.Gadget.Flyout.onHide = flyoutOnHide;
    WhenDocked();
    //
    mySettings.read();
    mySettings.refreshXml();
    oFirstTimer = setTimeout( initFirstLoad, 3000 );
    dvImgPhoto.style.display = "none";
    document.body.style.backgroundImage = "url('../images/cadre_intro_vignette.png')";
}
function initFirstLoad()
{
    reLoad();
}
function SettingsOpen(event)
{
    timerSetOff();
}
function SettingsClosed(event)
{
    if (event.closeAction == event.Action.commit)
    {
        reLoad();
        if( System.Gadget.docked ) {
            WhenDocked();
        }
        else {
            WhenUndocked();
        }
    }
    timerSetOn();
}

function reLoad()
{
    if( System.Gadget.docked ) 
        WhenDocked()
    else {
        WhenUndocked();
    }
    
    mySettings.iPhoto = 0;
    timerSetOff();
    mySettings.read();
    mySettings.refreshXml();
    update();
    timerSetOn();
}

function update()
{
    mySettings.iPhoto++;
    if( !System.Gadget.docked ) {
        var o;
        for(var i=1; i<=mySettings.iPhotoMax; i++) 
        {
            o = document.getElementById( "p" + i ) ;
            o.className = ( i == mySettings.iPhoto ? "imgDiapoSelected" : "imgDiapo" );
        }
    }
    if( mySettings.bStarted == false ){
        mySettings.bStarted = true;
    }
    imgPhoto.className = mySettings.sTransition;
    var oImages = mySettings.oXml.selectNodes( "//NewsComponent/NewsComponent" );
    if( oImages.length == 0 ) {
        imgPhoto.src = "../images/afp.jpg";
    }
    else if( oImages.length>0 ) {
        var oImage = oImages[mySettings.iPhoto-1].selectSingleNode( "NewsItemRef" );
        if( oImage != null ) {
            var sNewsItem = oImage.getAttribute( "NewsItem" );
            try {
                mySettings.oXmlImage.load( mySettings.sPhoto + "/" + sNewsItem );
                mySettings.sImage = mySettings.oXmlImage.selectSingleNode( "//NewsComponent[Role/@FormalName='Thumbnail']/ContentItem" ).getAttribute( "Href" );
                var oVisu = mySettings.oXmlImage.selectSingleNode( "//NewsComponent[Role/@FormalName='Preview']/ContentItem" );
                if( oVisu == null ) {
                    oVisu = mySettings.oXmlImage.selectSingleNode( "//NewsComponent[Role/@FormalName='Quicklook']/ContentItem" );
                }
                mySettings.sImageVisu = oVisu.getAttribute( "Href" );
                mySettings.sImageVisuHeight = oVisu.selectSingleNode( "Characteristics" ).selectSingleNode( "Property[@FormalName='Height']" ).getAttribute( "Value" );
                mySettings.sImageVisuWidth = oVisu.selectSingleNode( "Characteristics" ).selectSingleNode( "Property[@FormalName='Width']" ).getAttribute( "Value" );
                var oText = mySettings.oXmlImage.selectSingleNode( "//ContentItem/DataContent/p" );
                if( oText == null ) {
                    mySettings.sImageText = "";
                }
                else {
                    mySettings.sImageText = oText.text;
                }
                dvImgText.innerText = mySettings.sImageText;
                //
                imgPhoto.filters.item(0).Apply();                        
                if( System.Gadget.docked ) {
                    imgPhoto.src = mySettings.sPhoto + "/" + mySettings.sImage;
                    if( parseInt( mySettings.sImageVisuWidth ) > parseInt( mySettings.sImageVisuHeight )) {
                        imgPhoto.style.width = "98px";
                        imgPhoto.style.height = "auto";
                    }
                    else {
                        imgPhoto.style.height = "98px";
                        imgPhoto.style.width = "auto";
                    }
                }
                else {
                    imgPhoto.style.width = mySettings.sImageVisuWidth;
                    imgPhoto.style.height = mySettings.sImageVisuHeight;
                    imgPhoto.src = mySettings.sVisu + "/" + mySettings.sImageVisu;
                }
                imgPhoto.xVisu = mySettings.sVisu + "/" + mySettings.sImageVisu;
                imgPhoto.xHeight = mySettings.sImageVisuHeight;
                imgPhoto.filters.item(0).Play();
                //
                if( mySettings.iPhoto == mySettings.iPhotoMax ) {
                    mySettings.iPhoto = 0;
                    mySettings.oXml.load( mySettings.sXml );
                }
            }
            catch(e) {
                reLoad();
            }
        }
    }
}

function timerSetOn()
{  
    if( oTimer == null ) {
        oTimer = setInterval( update, parseInt( mySettings.iTimer ));   
    }
}

function timerSetOff()
{
    if( oTimer != null ) {
        clearInterval( oTimer );
        oTimer = null;
    }
}

/* ---- Flyout ----------------*/
function showFlyout()
{
    if( System.Gadget.docked ) {
        System.Gadget.Flyout.show = !( System.Gadget.Flyout.show );
    }
}
function flyoutOnShow()
{
    timerSetOff();
}
function flyoutOnHide()
{
    update();
    timerSetOn();
}

/* ---- Docked / Undocked -----*/

function WhenUndocked()
{
    System.Gadget.beginTransition();
    
    if( oFirstTimer != null ) 
    {
        clearTimeout( oFirstTimer );
        timerSetOn();
    }

    with(document.body.style)
    { 
        margin = "0px";
        width = "562px";
        height = "548px";
        backgroundImage = "url('../images/cadre_slide_horizon.png')";
        backgroundColor = "black";
    }
    with(dvImgPhoto.style ) 
    {
        width = "520px";
        top = "22px";
        left = "17px";
        height = "530px";
        display = "block";
    }
    with(imgPhoto.style)
    {
        height = "auto";
        width = "auto";
        cursor = "default";
    }

    imgPhoto.style.height="auto";
    imgPhoto.style.verticalAlign = "bottom";
    dvImgText.style.display = "block";
    dvThumbnails.innerHTML = "";
    dvThumbnails.style.display = "block";
    dvThumbnails.style.backgroundColor = "black";
    imgButton.style.display = "block";
    
    var oTd;
    var o;
    var oImage;
    var oImages;
    for( var i=1; i<=mySettings.iPhotoMax; i++ ) {
        o = document.createElement( "a" );
        o.className = ( i == mySettings.iPhoto ? "imgDiapoSelected" : "imgDiapo" );
        o.href = "";
        o.xId = i;
        o.id = "p" + i;
        dvThumbnails.appendChild( o );
        o.innerText = ( i<10 ? "0" + i : i );
        o.attachEvent( "onmouseover", thumbnailShow );
        o.attachEvent( "onmouseout", thumbnailHide );
        o.attachEvent( "onclick", thumbnailClickOn );
    }

    if( mySettings.iPhotoMax == 0 ) {
	    imgPhoto.src = "../images/afp.jpg";
	}
	else if( mySettings.iPhoto != 0 ) {
        imgPhoto.src = mySettings.sVisu + "/" + mySettings.sImageVisu;
    }
    else {
        update();
    }
    

    System.Gadget.endTransition(System.Gadget.TransitionType.none,0);
}
function thumbnailClickOn()
{
    var o = event.srcElement;
    buttonPlaySet( "play" );
    imgButton.src = "../images/play.gif";
    timerSetOff();
    mySettings.iPhoto = o.xId-1;
    update();
}
function buttonPlay()
{
    if( imgButton.xState == "play" ) {
        buttonPlaySet( "play" );
        timerSetOff();
    }
    else {
        buttonPlaySet( "pause" );
        update();
        timerSetOn();
    }
}
function buttonPlaySet( s )
{
    imgButton.xState = ( s == "play" ? "pause" : "play" );
    imgButton.src = "../images/" + s + ".gif";
}
function thumbnailShow()
{
    var o = event.srcElement;
    var oImages = mySettings.oXml.selectNodes( "//NewsComponent/NewsComponent" );
    if( oImages.length>0 ) {
        var oImage = oImages[parseInt( o.xId )-1].selectSingleNode( "NewsItemRef" );
        if( oImage != null ) {
            var sNewsItem = oImage.getAttribute( "NewsItem" );
            try {
                mySettings.oXmlImage.load( mySettings.sPhoto + "/" + sNewsItem );
                var oContentItem = mySettings.oXmlImage.selectSingleNode( "//NewsComponent[Role/@FormalName='Thumbnail']/ContentItem" );
                var sImage = oContentItem.getAttribute( "Href" );
                var oChar = oContentItem.selectSingleNode( "Characteristics" );
                var sWidth = oChar.selectSingleNode( "Property[@FormalName='Width']").getAttribute( "Value" );
                var sHeight = oChar.selectSingleNode( "Property[@FormalName='Height']").getAttribute( "Value" );

                dvThumbnail.style.top = 480 - parseInt( sHeight );
                dvThumbnail.style.left = event.clientX - parseInt( sWidth ) / 2 ;
                imgThumbnail.src = mySettings.sPhoto + "/" + sImage;
                imgArrow.style.top = 104;
                imgArrow.style.left = 45;
                dvThumbnail.style.display = "block";
            }
            catch(e) {
                reLoad();
            }
        }
    }
}
function thumbnailHide()
{
    var o = event.srcElement;
    dvThumbnail.style.display = "none";
}

function WhenDocked()
{
	System.Gadget.beginTransition();
	with(document.body.style)
	{
		width = "122px";
        height = "144px";
		backgroundImage = "url('../images/cadre_vignette.png')";
		border="none";
		margin = "0px";
	}
    with(dvImgPhoto.style ) 
    {
        height = "100px";
        width = "100px";
        left = "10px";
        top = "10px";
        display = "block";
    }
    with(imgPhoto.style)
    {
        height="98px";
        width="98px";
        cursor = "hand";
    }
	
	if( mySettings.iPhotoMax == 0 ) {
	    imgPhoto.src = "../images/afp.jpg";
	}
	else if( mySettings.iPhoto != 0 ) {
	    imgPhoto.src = mySettings.sPhoto + "/" + mySettings.sImage;
	}
	
    
	dvThumbnails.innerHTML = "";
	dvImgText.style.display = "none";
	dvThumbnails.style.display = "none";
    imgButton.style.display = "none";
    
    System.Gadget.endTransition(System.Gadget.TransitionType.none,0);
}





/*---- Setting ----*/
function SettingsInit()
{
    System.Gadget.onSettingsClosing = SettingsClosing;
    mySettings.read();
    loadSelDia();
    selCombo( selDIA, mySettings.sDiaporama );
    selCombo( selTRA, mySettings.sTransition );
    selCombo( selTPS, mySettings.iTimer );
    libActualVersion.innerText = "Actuelle : " + System.Gadget.version;
    var oVersion = mySettings.oXmlIndex.selectSingleNode( "//application" );
    if( oVersion != null ) {
        var sVersion = oVersion.getAttribute( "version" );
        libServerVersion.innerText = "Dernière mise à jour : " + sVersion;
        btUpdate.disabled = (sVersion == System.Gadget.version );
    }
}
function SettingsClosing(event)
{
    if (event.closeAction == event.Action.commit) {
        mySettings.sDiaporama = getSelComboValue( selDIA );
        mySettings.sTransition = getSelComboValue( selTRA );
        mySettings.iTimer = getSelComboValue( selTPS );

        mySettings.save();
    }
    event.cancel = false;
}
function loadSelDia()
{
    var oXml = new ActiveXObject( "MSXML2.DomDocument" );
    oXml.async = false;
    
    razCombo( selDIA );
    var oIndexes = mySettings.oXmlIndex.selectNodes( "//topics/topic" );
    var oOption, sReference, sIndex, oSel, sName;
    for(var i=0; i<oIndexes.length; i++ ) {
        sReference = oIndexes[i].getAttribute( "reference" );
        sIndex = oIndexes[i].getAttribute( "index" );
        sName = oIndexes[i].getAttribute( "name" );
        oXml.load( sReference + "/" + sIndex ) ;
        oSel = oXml.selectNodes( "//NewsComponent/NewsItemRef/@NewsItem" );
        if( oSel.length >1 ) {
            oOption = document.createElement("option");
            oOption.text = oIndexes[i].getAttribute( "label" );
            oOption.value = sName;
            selDIA.options.add( oOption );
        }
    }
}
function applicationUpdate()
{
}
function selCombo(oSel, nVal)
{
	var n = oSel.options.length;
	for (var i = 0; i < n; i++) {
		if (oSel.options[i].value == nVal) {
			oSel.selectedIndex = i;
			return;
		}
	}
}
function getSelComboValue(oSel)
{
	var vRet = null;
	var nSel = oSel.options.selectedIndex;
	if (nSel > -1)
		vRet = oSel.options[nSel].value;
	return vRet;
}
function razCombo(oSel)
{
	var n = oSel.options.length;
	for (var i = 0; i < n; i++) {
		oSel.remove(0);
	}
}
