﻿var FeaturedList = function(par,p,m){
    var x = this;
    this.prevLink=p;
    this.moreLink=m;
    this.parent=par;
    this.proxyEl=null;
    this.pageList=new Array();
    this.cls='featured-client';
    this.clsOdd=this.cls+'-odd';
    this.page=null;
    this.visible=null;
    this.currentPage=1;
    this.MAX_PER_PAGE=3;
    this.INCREMENT=1;
    this.PAGE_START=this.INCREMENT;
    this.itemCount=this.PAGE_START;
    this.add=function(el){
        if(this.itemCount==this.PAGE_START){
            var np = document.createElement('div');
            np.className='featured-list-proxy-page';
            this.proxyEl.appendChild(np);
            this.page=Ext.get(np);
            this.page.enableDisplayMode();
            this.pageList.push(this.page);
            this.onElementCreated(this.page);
        }
        el.className=((this.itemCount%2)==0)?this.cls:this.clsOdd;
        this.page.dom.appendChild(el);
        this.itemCount=(this.itemCount==this.MAX_PER_PAGE)?this.PAGE_START:this.itemCount+this.INCREMENT;
        this.onAfterAdd();
    };
    this.updateLinks=function(){
        if(this.currentPage==this.PAGE_START){
            this.prevLink.setDisabled(true);
        } else {
            this.prevLink.setDisabled(false);
        }
        if(this.currentPage==(this.pageList.length)){
            this.moreLink.setDisabled(true);
        } else {
            this.moreLink.setDisabled(false);
        }
    };
    this.onInit=function(){
        var i = this.PAGE_START-1;
        this.pageList[i].setVisible(true);
        this.updateLinks();
    };
    this.onAfterAdd=function(){};
    this.onElementCreated=function(el){
        el.setVisible(false);
    };
    this.showNext=function(){
        this.pageList[this.currentPage-1].slideOut('l');
        this.currentPage=this.currentPage+this.INCREMENT;
        this.pageList[this.currentPage-1].slideIn('r');
        this.updateLinks();
    };
    this.showPrev=function(){
        this.pageList[this.currentPage-1].slideOut('r');
        this.currentPage=this.currentPage-this.INCREMENT;
        this.pageList[this.currentPage-1].slideIn('l');
        this.updateLinks();
    }
    this.init=function(){
        var pEl = Ext.get(this.parent);
        this.proxyEl=Ext.DomQuery.selectNode('div#featured-list-proxy');
        var children = pEl.select('div');
        for(var a=0;a<children.elements.length;a++){
            this.add(children.elements[a]);
        }
        this.moreLink.onclick=function(){
            x.showNext();
        }
        this.prevLink.onclick=function(){
            x.showPrev();
        }
        this.onInit();
    };
    this.init();
    
}

var MoreLink = function(el){
    var x = this;
    this.disabled=false;
    this.cls='featured-link-more';
    this.clsDisabled=this.cls+'-dis';
    this.el=el;
    this.setDisabled=function(b){
        this.disabled=b;
        if(b){
            this.el.className=this.clsDisabled;
        }else{
            this.el.className=this.cls;
        }
    };
    this.onclick=function(){};
    var m = this;
    el.onclick=function(){
        if(!x.disabled){
            x.onclick();
        }
        return false;
    }
}

var PrevLink = function(el){
    var x = this;
    this.disabled=false;
    this.cls='featured-link-prev';
    this.clsDisabled=this.cls+'-dis';
    this.el=el;
    this.setDisabled=function(b){
        this.disabled=b;
        if(b){
            this.el.className=this.clsDisabled;
        }else{
            this.el.className=this.cls;
        }
    };
    this.onclick=function(){};
    var m = this;
    el.onclick=function(){
        if(!x.disabled){
            x.onclick();
        }
        return false;
    }
}

Ext.onReady(function(){
    //var itm = Ext.DomQuery.selectNode("div#featured-list");

    var more = new MoreLink(Ext.DomQuery.selectNode("a[class='featured-link-more']"));
    var prev = new PrevLink(Ext.DomQuery.selectNode("a[class='featured-link-prev']"));
    var fl = new FeaturedList(Ext.DomQuery.selectNode("div#featured-list"),prev,more);
    
    /*for(var a=0;a<itm.length;a++){
        fl.add(itm[a]);
    }*/

})
