/*********************************************************************
  Copyright 2006-2009 .
  
  Auteur : Boudchicha naime -- WWW.NEW6TM.COM
           Manager@new6tm.com
***********************************************************************/
// -----------------------------------------------------------------------------------
NEW6TM.UI.Window.infobox = Class.create(NEW6TM.UI.Options, {
  options: {
	labelImage: "AAHP-Photo",
	labelOf: "de"
  },
    imageArray: [],
    activeImage: undefined,
	MyBody     : null,

    initialize: function(options) {    
        this.setOptions(options);
		if (!_NuiWindow) {
			 var _NuiWindow = document._NuiWindow = new NEW6TM.UI.Window(options);
		  } 
		this._window = _NuiWindow;
        this.updateInfoList();
		
    },
	
    updateInfoList: function() {   
        this.updateInfoList = Prototype.emptyFunction;
		
		 $$('a[href][rel^="window["]').each(function(element) {												  
				var rWnd = element.rel.match(/window[^[]*([)([\S\s]*?)]/)[1];
   				(rWnd != "undefined") ? rWnd = rWnd.replace(/\[/,'') : rWnd = "#";
				Object.extend(element, {target : rWnd});
							 });
        document.observe('click', (function(event){
            var target = event.findElement('a[rel^=window]') || event.findElement('area[rel^=window]');
            if (target) {
                event.stop();  
                this.showWindowEx(event,target);
            }
        }).bind(this));
    },
    
    //
    //  start()
    //  Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
    //
    showWindowEx: function(event,target) {	
		// connect		
		var _caller = this;
		var url = "service/infoService.php";
		new Ajax.Request(url, {
                    method: 'post', parameters : {target : target.target},
					onCreate: function(){
				   },
	          onSuccess: function(transport) {					   
				       var responseText = transport.responseText;
					   
					   _caller.inConnect(event,responseText); 
                   },onFailure: function(transport){
					     _caller.inConnect(null,'Une erreur est survenue :(' + transport.status + ') Verifier votre connection')
						 },
				     on404: function(){
					     _caller.inConnect(null,'Desolé la Page est introuvable , le serveur ne repond pas')
						 }
                    });    
		
		//this.resizeImageContainer(800,400);
		//this.enableKeyboardNav();
    },
	inConnect : function(state,message){		
			this._window.Wnd_Temp.update(message);
			var h =this._window.Wnd_Temp.getHeight();
			if (h > 500) h=500;
			//var e = Event.element(state)
			//alert(e.tagName);
			this._window.showWindow(state,800,h);
	},

    //
    //  updateDetails()
    //  Display caption, image number, and bottom nav.
    //
    updateDetails: function() {
    
        // if caption is not null
        if (this.imageArray[this.activeImage][1] != ""){
            this.caption.update(this.imageArray[this.activeImage][1]).show();
        }
        
        // if image is part of set display 'Image x of x' 
        if (this.imageArray.length > 1){
            this.numberDisplay.update( this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + '  ' + this.imageArray.length).show();
        }

        new Effect.Parallel(
            [ 
                new Effect.SlideDown(this.imageDataContainer, { sync: true, duration: this.options.resizeDuration, from: 0.0, to: 1.0 }), 
                new Effect.Appear(this.imageDataContainer, { sync: true, duration: this.options.resizeDuration }) 
            ], 
            { 
                duration: this.resizeDuration, 
                afterFinish: (function() {
	                // update overlay size and update nav
	                var arrayPageSize = this.getPageSize();
	                this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
	                this.updateNav();
                }).bind(this)
            } 
        );
    },

    //
    //  updateNav()
    //  Display appropriate previous and next hover navigation.
    //
    updateNav: function() {

       this.hoverNav.show();               

        // if not first image in set, display prev image button
        if (this.activeImage > 0) this.prevLink.show();

        // if not last image in set, display next image button
        if (this.activeImage < (this.imageArray.length - 1)) this.nextLink.show();
        
        this.enableKeyboardNav();
    },
	

    //
    //  preloadNeighborImages()
    //  Preload previous and next images.
    //
    preloadNeighborImages: function(){
        var preloadNextImage, preloadPrevImage;
        if (this.imageArray.length > this.activeImage + 1){
            preloadNextImage = new Image();
            preloadNextImage.src = this.imageArray[this.activeImage + 1][0];
        }
        if (this.activeImage > 0){
            preloadPrevImage = new Image();
            preloadPrevImage.src = this.imageArray[this.activeImage - 1][0];
        }
    
    }
});

