﻿
// FareListing : Component for fare listing

FareListMgr = createSingleton(FareListMgr = {});
FareListMgr.prototype.init = function(args) {
    /*
		faresPerPage	: Number of fares to be shown per page ( -1 will be used to show all fares )
		layout			: Inner-layout for listing 
		fares			: Reference for list of fares to be displayed
		pageNum			: Current page number
		pagingCells		: Maximum number of page-number cells that can be shown
    */
	if(!isDefined(args))
		args = {};
	defaults = { faresPerPage: -1, fares: [], fareMgr: null, pageNum: 1, 
	             pagingCells: 5, parentDiv: "ListingDiv", onSelect: null };
	for(var pr in defaults)
		this[pr] = isDefined(args[pr]) ?args[pr] :defaults[pr];
	this.pagingParams = null;
    var listing = this;
    
    // Method for updating fares in listing
    this.update = function(mode) {
	    if(/init/i.test(mode) || /fareUpdate/i.test(mode)) {
		    this.pageNum = 1;
        }

	    this.setPagingParams();		
	    this.domMgr.update(false);
    };

    // Method for updating fares on pageClick
    this.onPageClick = function(txt) {
	    var newNum = 0;
	    if(/first/i.test(txt))
	        newNum = 1;
	    if(/prev/i.test(txt))
		    newNum = this.pageNum-1;
	    else if(/next/i.test(txt))
		    newNum = this.pageNum+1;
	    else if(/last/i.test(txt))
		    newNum = this.pagingParams.noOfPages;
		if(newNum != this.pageNum) {
		    this.pageNum = newNum;
		    $("topHeaderDiv").scrollIntoView();
	        this.update("pageClick");
	    }
    };

    // Method for setting the paging-params
    this.setPagingParams = function() {
	    if(this.faresPerPage == -1) {
		    this.pagingParams = { 
			    startIndex: 0, stopIndex: this.fares.length-1, 
			    showPrev: false, showNext: false, 
			    pageNumStart: 1, pageNumEnd: 1, 
			    noOfPages: 1 
		    };
	    }
	    else {
		    this.pagingParams = Util.paging( this.fares, this.faresPerPage, this.pageNum, this.pagingCells);
	    }
    };

    // Method for getting count of the required fare-divs
    this.getReqFareDivsCnt = function() {
	    return (this.pagingParams.stopIndex - this.pagingParams.startIndex + 1);
    };
    
   	// Get count of the required fare-divs
	this.getFareDivsCnt = function() {
		var reqCnt = this.getReqFareDivsCnt();
		var actualCnt = 0;
		if(isArray(this.domMgr.elements["fares"])) 
			actualCnt = this.domMgr.elements["fares"].length;
		return { 'req': reqCnt, 'actual': actualCnt };
	};

    this.hide = function() {
        this.domMgr.parent.style.display = "none";        
    };

    this.show = function() {
        this.domMgr.parent.style.display = "";
    };
    
    // Method for updating fares-div for show/hide itenerary
    this.onFareSelect = function(i) {
	    var fareIndex = this.pagingParams.startIndex + i;
	    var mgr = this.domMgr;
	    if(this.onSelect)
	        this.onSelect(this.fares[fareIndex]);
    };

    var fareMgr = this.fareMgr;
    
    var fareDiv = {
        tagName: "div",
        name: "fares",
        fareMgrList: [],
        layout: function(el, mgr) {
            mgr = new fareMgr({ fare: null, parentDiv: el});
            fareDiv.fareMgrList.push(mgr);
            mgr.create(el);
        }, 
        updateFunc: function(el, mgr) {
            var fareIndex = (listing.pagingParams.startIndex + mgr.indices.fare);
            var fare = null;
            if(fareIndex < listing.fares.length)
                fare = listing.fares[fareIndex];
            if(isDefined(fare)) {
                el.style.display = "";                
                var mgr = fareDiv.fareMgrList[mgr.indices.fare];
                mgr.fare = fare;
                mgr.resetModes("compact");
                mgr.update();                
            }
            else
                el.style.display = "none";
        }
    };
    
    var selectBtn = {
	    tagName: "div",
	    style: { textAlign: "right", paddingRight: "5px", paddingTop: "7px", paddingBottom: "22px" },
	    attributes: { index: function(mgr) { return mgr.indices.fare; } },
	    layout: {
		    tagName: "img",
		    style: { cursor: "pointer" },
		    attributes: { src: "images/book-flight-button.gif" }
	    },
	    handlers: { 
		    click: function(ev, mgr) {
			    var el = DOM.getTargetElement(ev);
			    if(!/div/i.test(el.tagName))
			    {
				    el = el.parentNode;
			        listing.onFareSelect(parseInt(el.getAttribute("index")));
			    }
			    return false;
		    }
	    },
        updateFunc: function(el, mgr) {
            var fareIndex = (listing.pagingParams.startIndex + mgr.indices.fare);
            var fare = null;
            if(fareIndex < listing.fares.length)
                fare = listing.fares[fareIndex];
            if(isDefined(fare))
                el.style.display = "";
	        else
	            el.style.display = "none";
        }
    };
        
    var body = {
        tagName: "div",
        style: { width: "576px" },
	    foreach: { 
		    startIndex: 0,
		    stopIndex: function(mgr) {
			    var c = listing.getFareDivsCnt();
			    return (c.req > c.actual) ?c.req-1 :c.actual-1;
		    },
		    indexKey: "fare"
	    },
	    layout: [ fareDiv, selectBtn ],
	    updateFunc: function(el, mgr) {
		    var c = listing.getFareDivsCnt();		
		    for(var i=c.actual; i<c.req; i++) {
			    mgr.indices.fare = i;
			    mgr.createLayout([ fareDiv, selectBtn ], el);
		    }
	    }
    };
    
    var updatePagingCell = function(p, el, n) {
	    if(p.pageNumStart <= n && n <= p.pageNumEnd) {
		    el.innerHTML = n;
		    el.style.fontWeight = (p.curPageNum == n) ?"bold" :"normal";
		    el.style.display = "";
	    }
	    else 
		    el.style.display = "none";
    };
    
    var paging = {
	    tagName: "div",
	    style: { width: "576px", fontFamily: "verdana", fontSize: "12px", textAlign: "right", paddingBottom: "10px" },
	    foreach: {
		    startIndex: 0, 
		    stopIndex: function(mgr) {
			    return 4;
		    },
		    indexKey: "paging"
	    },
	    layout: {
		    tagName: "span",
		    style: { textAlign: "center", margin: "3px" },		    
			layout: {
			    tagName: "a",
			    attributes: { href: "javascript:void(0)" },
			    updateFunc: function(el, mgr) {
				    var p = listing.pagingParams;
				    var i = mgr.indices.paging;				
            
			        if (i == 0 || i == 1) {
			            el.className = (!p.showPrev) ?"txt10Black" :"txt10Green";
			            el.innerHTML = (i==0) ?"&lt;&lt;First" :"&lt;&lt;Previous";			        
			        }
			        else if(i == 3 || i == 4) {
			            el.className = (!p.showNext) ?"txt10Black" :"txt10Green";
			            el.innerHTML = (i==3) ?"Next&gt;&gt;" :"Last&gt;&gt;";
			        }
			        else {
			            el.className = "txt10Black";
			            el.innerHTML = "|";
			        }
			        if(listing.fares.length<5)
			            el.style.display='none';
			        /*else
			            el.style.display='block';*/
		        }
		    }
	    },
	    handlers: { 
		    click: function(ev, mgr) {
			    var el = DOM.getTargetElement(ev);
			    if(/a$/i.test(el.tagName)) {
				    listing.onPageClick(el.innerHTML);				    
			    }
			    return false;
		    }
	    }
    };
    
    this.layout = {
	    parent: listing.parentDiv,
	    layout: {
	        tagName: "div",
	        style: { width: "576px" },
	        updateFunc: function(el, mgr) {
		        el.style.display = (listing.fares.length == 0) ?"none" :"";
	        },
	        layout: [ paging, body, paging ]
        }
    };

	// Initiate the component
	this.setPagingParams();
	this.domMgr = new DomMgr(this.layout);
	this.domMgr.create();
	this.update("init");
};
