$j(document).bind('OnBeforeLoadReferences', function(evt, reference) { } );
$j(document).bind('OnAfterLoadReferences', function(evt, reference) { } );
$j(document).bind('OnReferencesImagesLoaded', function(evt, reference) { });

var ReferencesApicius = $j.inherit(
{
    init : false,
    type : "abstract",
    currentPage : 1,
    currentTotalPage : 1,
    InitComponents : function(){},
    Update : function(){ this._Update(); },
    OnBeforeUpdate : function(params){ return params; },
    OnBtnPageFirstClick : function(){
        this.currentPage = 1;
        this.Update();     
    },
    OnBtnPagePreviousClick : function(){
        this.currentPage--;
        if(this.currentPage < 1) this.currentPage = 1;
        this.Update();       
    },
    OnBtnPageNextClick : function(){
        this.currentPage++;
        if(this.currentPage > this.currentTotalPage) this.currentPage = this.currentTotalPage;
        this.Update();       
    },
    OnBtnPageLastClick : function(){
        this.currentPage = this.currentTotalPage;
        this.Update();       
    },
    __constructor : function(){
        this._InitComponents();
        $j(document).bind('onLoadHtmlCompleted', $j.proxy(this._ChargePageInit, this));
    }, 
    _InitComponents : function(){
        if($j('#References'+this.type).length >0 && !this.init){
            this.Update();
            this.init = true;
        }
        if($j('.References_PanelPagination').length > 0){
            $j('#References_BtnPageFirst_'+this.type).bind('click', $j.proxy(this._HandlePaginationClick,this));
            $j('#References_BtnPagePrevious_'+this.type).bind('click', $j.proxy(this._HandlePaginationClick,this));
            $j('#References_BtnPageNext_'+this.type).bind('click', $j.proxy(this._HandlePaginationClick,this)); 
            $j('#References_BtnPageLast_'+this.type).bind('click', $j.proxy(this._HandlePaginationClick,this));
            this.currentTotalPage = $j('#References_TotalPageCount_'+this.type).val(); 
        }
        this.InitComponents();
    },
    _ChargePageInit : function(){
        if($j('#References'+this.type).length >0){
            this.Update();
            this.init = true;
            this._InitComponents();
        }
    },
    _HandlePaginationClick : function(e){
        var methodToCall = 'On' + e.currentTarget.id.replace(this.type,'').replace('References_','').replace('_','') + 'Click';
        this[methodToCall](); 
        return false;   
    },
    _Update : function(){
        $j(document).trigger('OnBeforeLoadReferences', this);
        var dataPost = {
            'PageNumber' : this.currentPage,
            'ReferenceType' : this.type
        };
        dataPost = this.OnBeforeUpdate(dataPost)
        $.ajax({
            url: "../references_apicius/references.php",
            dataType : 'html',
            type : 'POST',
            data : dataPost,
            success: $j.proxy(this._UpdateHtml, this)
        });   
    },
    _UpdateHtml : function(response){
        var el = $j('#References'+this.type);
        $j(el).html(response);
        this._InitComponents();  
        $j(document).trigger('OnAfterLoadReferences', this); 
        this._CheckImagesLoadStatus();   
    },
    _CheckImagesLoadStatus: function(){

        var imgs = $j('#References'+this.type).find('img');
        imgs.references = this;
        imgs.triggered = false;
        for(var i = 0; i<imgs.length; i++){
            $j(imgs[i]).load($j.proxy(this._ImageLoaded, imgs));
        }
    },
    _ImageLoaded: function(){
        var complete = true;
        for(var i = 0; i<this.length; i++){
            if(!this[i].complete) complete = false;
        }
        if(complete && !this.triggered){
            this.triggered = true;
            $j(document).trigger('OnReferencesImagesLoaded', this.references);  
        }
    }  
});

var ReferencesApiciusSites = $j.inherit(ReferencesApicius,
{
    type : "Sites",
    currentFilterSecteur : 0,
    currentFilterCategorie : '',
    currentFilterPays : '',
    currentFilterRegion : '',
    InitComponents : function(){
        if($j('#References_PanelFilter_Sites').length > 0){
            $j('#FilterSecteur').bind('change', $j.proxy(this.OnFilterSecteurChange, this));
            $j('#FilterCategorie').bind('change', $j.proxy(this.OnFilterCategorieChange, this));
            $j('#FilterPays').bind('change', $j.proxy(this.OnFilterPaysChange, this));
            $j('#FilterRegion').bind('change', $j.proxy(this.OnFilterRegionChange, this));
        }
    },
    OnBeforeUpdate : function(params){
        params.FilterSecteur = this.currentFilterSecteur;
        params.FilterCategorie = this.currentFilterCategorie;
        params.FilterPays = this.currentFilterPays;
        params.FilterRegion = this.currentFilterRegion; 
        return params;
    }, 
    OnFilterSecteurChange : function(e){
        this.currentFilterSecteur = e.target.value; 
        this.currentFilterCategorie = ''; 
        this.currentPage = 1;  
        this.Update();  
    },
    OnFilterCategorieChange : function(e){
        this.currentFilterCategorie = e.target.value;
        this.currentPage = 1;
        this.Update();  
    },
    OnFilterPaysChange : function(e){
        this.currentFilterPays = e.target.value;
        this.currentPage = 1;
        this.currentFilterRegion = '';    
        this.Update();  
    },
    OnFilterRegionChange : function(e){
        this.currentFilterRegion = e.target.value;  
        this.Update();  
    }
});
var ReferencesApiciusPortails = $j.inherit(ReferencesApicius,
{
   type : "Portails",
   InitComponents : function(){
   }      
});
var ReferencesApiciusBoutiques = $j.inherit(ReferencesApicius,
{
   type : "Boutiques",
   InitComponents : function(){
   }
});

$j(document).ready(function(){
    var referencePortails  =  new ReferencesApiciusPortails();
    var referenceSites = new ReferencesApiciusSites();
    var referenceBoutiques = new ReferencesApiciusBoutiques(); 
});


