bqc.PAGES_MAX = 10;

bqc.resultList={
    
    }
function ListControl(mapId) {
    if(typeof mapId == "undefined"){
        mapId = "";
    }
    this.mapId = mapId;
}

ListControl.prototype = new GControl();

ListControl.prototype.initialize = function(map) 
{

    try{
        bqc.resultList.container=$('resultsList');

        var msgDiv = document.createElement("div");
        msgDiv.innerHTML= bqc.locale.resultList.loading;
        msgDiv.className="loading";
        bqc.resultList.container.appendChild(msgDiv);
        bqc.resultList.loadingDiv=msgDiv;
        bqc.resultList.loadingDiv.style.display="none";

        var errorDiv = document.createElement("div");
        errorDiv.className="error";
        bqc.resultList.container.appendChild(errorDiv);
        bqc.resultList.errorDiv=errorDiv;
        bqc.resultList.errorDiv.style.display="none";


        var contentDiv = document.createElement("div");
        bqc.resultList.container.appendChild(contentDiv);
        bqc.resultList.entriesDiv=contentDiv;


        var container = document.createElement("div");
        var imageContainer=document.createElement("div");
    
        var imageHide = new Image();
        imageHide.src = "images/hideList.png";
        imageHide.title = bqc.locale.resultList.close;

        var image = new Image();
        image.id="listControlImg"
        image.src = "images/showList.png";
        image.title = bqc.locale.resultList.open;
        bqc.resultList.img=image;
        image.style.cursor='pointer';
        image.onclick=function(){
            bqc.resultList.toogle();
        }
        imageContainer.appendChild(image);

        var borderContainer=document.createElement("div");
        borderContainer.className="resultListBorder";
        borderContainer.id="resultListBorder";
        bqc.resultList.borderDiv=borderContainer;

        bqc.resultList.borderDiv.style.display="none";

    
        container.appendChild(imageContainer);
        container.appendChild(borderContainer);
        map.getContainer().appendChild(container);

        return container;
    }catch(e){
        //console.debug(e);
    }  
}

ListControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}


bqc.resultList.hide = function() {
    bqc.resultList.container.style.display="none";
    bqc.resultList.container.style.visibility = "hidden";

    bqc.resultList.img.src="images/showList.png";
    bqc.resultList.img.title = bqc.locale.resultList.open;

    bqc.resultList.borderDiv.style.display    = "none";
    bqc.resultList.borderDiv.style.visibility = "hidden";
    $('map').style.left = "0px";
    
    resizeMap();
}

bqc.resultList.toogle =function(){
    try {
        
        if( bqc.resultList.container.style.visibility == "visible"){
            bqc.resultList.hide();
        }else{
            
            bqc.resultList.container.style.display = "block";
            bqc.resultList.container.style.visibility = "visible";
            bqc.resultList.borderDiv.style.display = "block";
            bqc.resultList.borderDiv.style.visibility = "visible";

            bqc.resultList.img.src="images/hideList.png";
            bqc.resultList.img.title = bqc.locale.resultList.close;
            $('map').style.left = $('resultsList').getDimensions().width + "px";
            
            resizeMap();
        }
    } catch(e) {
        console.debug(e);
    }

}

bqc.resultList.showLoading =function(){

    bqc.resultList.entriesDiv.innerHTML="";
    bqc.resultList.loadingDiv.style.display="";
    bqc.resultList.errorDiv.style.display="none";
}

bqc.resultList.showError =function(text){
    bqc.resultList.hideLoading();
    bqc.resultList.entriesDiv.innerHTML="";
    bqc.resultList.errorDiv.style.display="";
    bqc.resultList.errorDiv.innerHTML="";
    bqc.resultList.errorDiv.appendChild(document.createTextNode(text));
}

bqc.resultList.clear = function() {
    bqc.unitToggler.removeMember(this.member);
    bqc.resultList.entriesDiv.innerHTML = '';
    bqc.resultList.loadingDiv.style.display = 'none';
    bqc.resultList.hide();
}

bqc.resultList.hideLoading =function(){
    bqc.resultList.loadingDiv.style.display="none";
}

bqc.resultList.showList =function(){
    if(bqc.resultList.container.style.visibility != 'visible'){
        bqc.resultList.toogle();
    }
}

bqc.resultList.applyDistances = function() {
    for (var i = 0; i < bqc.resultList.distances.length; ++i) {
        bqc.resultList.distances[i]();
    }
}

bqc.resultList.appendTitle=function(nb){
    try {
    var titleDiv = document.createElement("div");
    var titleMsg;
    if(bqc.resultList.isProximity){
       titleMsg=nb+bqc.locale.resultList.proximTitle.replace('%s', bqc.proximityName).truncate(135);
    }else{
        if (bqc.searchManager.searchCriterions) {
            titleMsg=bqc.locale.resultList.resultTitle.replace('%s', bqc.searchManager.searchCriterions).truncate(80);
        } else {
            titleMsg=bqc.locale.resultList.resultsMiniTitle;
        }
    }
    titleDiv.appendChild(document.createTextNode(titleMsg));
    titleDiv.className="title";
    bqc.resultList.entriesDiv.appendChild(titleDiv);
    }catch(e){
        console.error(e);
    }
}

bqc.resultList.appendUnitToggler = function() {
    var unitToggler = bqc.resultList.entriesDiv.appendChild(document.createElement('div'));
    unitToggler.style.marginBottom="5px";
    var kmButton = unitToggler.appendChild(document.createElement('span'));
    var pipe = unitToggler.appendChild(document.createElement('span'));
    var milesButton = unitToggler.appendChild(document.createElement('span'));

    kmButton.innerHTML = bqc.locale.itinary.details.km;
    pipe.className = 'lcPipe';
    pipe.innerHTML = '|';
    milesButton.innerHTML = bqc.locale.itinary.details.miles;

    this.member = {
        id: 'listControl',
        applyDistances: bqc.resultList.applyDistances,
        setToKM: function() {
            kmButton.onclick = function() {};
            kmButton.className = 'nolink itinaryText';
            milesButton.onclick = bqc.unitToggler.setToMiles;
            milesButton.className = 'link itinaryTextLink';
        },
        setToMiles: function() {
            milesButton.onclick = function() {};
            milesButton.className = 'nolink itinaryText';
            kmButton.onclick = bqc.unitToggler.setToKM;
            kmButton.className = 'link itinaryTextLink';
        }
    };
    bqc.unitToggler.addMember(this.member);
}

bqc.resultList.createPageNumber = function(index, pos, paging) {
    var pageNb = document.createElement('a');
    pageNb.setAttribute('href', 'javascript:void(0);');
    bqc.resultList.pagingToEvent(pageNb, index, paging);
    pageNb.appendChild(document.createTextNode(index));
    if (pos == index) {
        pageNb.className = "disabled";
    } else {
        pageNb.className = "smallLink";
    }
    paging.appendChild(pageNb);
}

bqc.resultList.fillList = function(markers, marked, nbtotal, pos, nbtotalfound) {

    try{
        bqc.resultList.marked=marked;
        bqc.resultList.markers=markers;
        bqc.resultList.nbtotal=nbtotal;
        bqc.resultList.nbtotalfound=nbtotalfound;
        bqc.resultList.distances = [];
        bqc.unitToggler.removeMember(this.member);
        bqc.resultList.entriesDiv.innerHTML = '';
        bqc.resultList.hideLoading();
        bqc.resultList.appendTitle(nbtotal);
        for (var i = 0; i < marked.length; ++i) {
            
            bqc.resultList.entriesDiv.appendChild(bqc.resultList.createEntry(marked[i],i));
        }
        if (bqc.resultList.isProximity) {
            bqc.resultList.appendUnitToggler();
        }
        if(nbtotal>10){
            if (pos == null) {
                pos = 1;
            }

            if (nbtotalfound > nbtotal) {
                var resultsFound=document.createElement("div");
                resultsFound.className="pagingContainer"
                var resultsFoundTitle=document.createElement("span");
                resultsFoundTitle.appendChild(document.createTextNode(nbtotalfound+bqc.locale.resultList.resultsFound))
                resultsFound.appendChild(resultsFoundTitle);

                bqc.resultList.entriesDiv.appendChild(resultsFound);
                bqc.resultList.entriesDiv.appendChild(document.createElement("br"));
            }

            var nbPages=Math.ceil(nbtotal/10);
            var resultsPaging=document.createElement("div");
            resultsPaging.className="pagingContainer"
            var resultsTitle=document.createElement("span");
            resultsTitle.appendChild(document.createTextNode(nbtotal+bqc.locale.resultList.results))
            resultsPaging.appendChild(resultsTitle);


            var paging=document.createElement("span");
            //fix ie
            paging=$(paging);

            var backAhref = document.createElement('a');
            backAhref.setAttribute('href', 'javascript:void(0);');
            backAhref.appendChild(document.createTextNode("«"));
            backAhref.title = bqc.locale.resultList.previous;
            paging.appendChild(backAhref);
            if (pos == 1) {
                backAhref.className = "disabled";
            } else {
                backAhref.className = "smallLink";
                bqc.resultList.pagingToEvent(backAhref, pos - 1, paging);
            }
            var shownPages = Math.min(bqc.PAGES_MAX, nbPages);
            var start = Math.max(1, pos - bqc.PAGES_MAX / 2 + 1);
            var end = Math.min(nbPages, pos + bqc.PAGES_MAX / 2);
            if (end - start + 1 < shownPages) {
                if (end < shownPages) {
                    end += shownPages - (end - start + 1);
                } else {
                    start -= shownPages - (end - start + 1);
                }
            }
            for(var k=start; k<=end ;k++){
                bqc.resultList.createPageNumber(k, pos, paging);
            }

            var nextAhref = document.createElement('a');
            nextAhref.setAttribute('href', 'javascript:void(0);');
            nextAhref.title = bqc.locale.resultList.next;
            bqc.resultList.pagingToEvent(nextAhref,2,paging);
            nextAhref.appendChild(document.createTextNode("»"));
            nextAhref.className="smallLink";
            paging.appendChild(nextAhref);
            resultsPaging.appendChild(paging);

            if (pos == Math.ceil(bqc.resultList.nbtotal / 10)) {
                nextAhref.className = "disabled";
                nextAhref.onclick = function() {}
            } else {
                nextAhref.className = "smallLink";
                bqc.resultList.pagingToEvent(nextAhref, pos + 1, paging);
            }

            bqc.resultList.entriesDiv.appendChild(resultsPaging);
        } else {
            var resultsFound=document.createElement("div");
            resultsFound.className="pagingContainer"
            var resultsFoundTitle=document.createElement("span");
            resultsFoundTitle.appendChild(document.createTextNode(nbtotalfound+bqc.locale.resultList.resultsFound))
            resultsFound.appendChild(resultsFoundTitle);

            bqc.resultList.entriesDiv.appendChild(resultsFound);
            bqc.resultList.entriesDiv.appendChild(document.createElement("br"));
        }
    } catch(e) {
        console.debug(e);
    }
}


bqc.resultList.createEntry=function(json,index){
    try{
        var entryDiv = document.createElement("div");
        var IconDiv = document.createElement("div");
        var mainInfo = document.createElement("div");
        var title = document.createElement("div");
        var imgAddr = document.createElement("div");
        var addr = document.createElement("div");

        var iconImage = new Image();
        iconImage.src = bqc.markedIcons[json.type][index].image;

        iconImage.style.width = '25px';
        iconImage.style.height = '30px';
        IconDiv.appendChild(iconImage);

        IconDiv.style.styleFloat="left";
        IconDiv.style.cssFloat="left";
        IconDiv.style.marginRight="5px";
        IconDiv.style.cursor = 'pointer';
        IconDiv.onclick = function() {
            bqc.searchManager.showMarker(json.addId);
        }
        IconDiv.title = json.category;
        entryDiv.appendChild(IconDiv)

        var titleAhref = document.createElement('a');
        titleAhref.setAttribute('href', 'javascript:bqc.searchManager.showMarker(' + json.addId + ')');
        var titleText = json.name;
        if (json.poiName != null && json.poiName.length > 0) {
            titleText += ' - ' + json.poiName;
        }
        titleAhref.appendChild(document.createTextNode(titleText.truncate(34)));
        titleAhref.title = IconDiv.title;
        title.appendChild(titleAhref);



        if (json.rating != "") {
            var ratingImage=new Image();
            ratingImage.src = json.rating;
            ratingImage.title = json.ratingDesc;
            title.appendChild(ratingImage);
        }
        mainInfo.appendChild(title)


        addr.appendChild(document.createTextNode(json.address));

        addr.style.styleFloat="left";
        addr.style.cssFloat="left";
        addr.style.width="250px";
        if (json.reservable) {
            var reserveAhref = document.createElement('a');
            reserveAhref.setAttribute('href', 'javascript:bqc.tooltipManager.popup(' + json.addId + ')');
            reserveAhref.appendChild(document.createTextNode(bqc.locale.resultList.reserve));
            reserveAhref.className="smallLink";
            addr.appendChild(reserveAhref);
        }
        if(json.img!=""){
            var image = new Image();
            image.src = json.img;
            image.style.paddingTop = '4px';
            image.style.width = '75px';
            image.style.height = '45px';

            image.style.styleFloat="left";
            image.style.cssFloat="left";
            imgAddr.appendChild(image);
            image.style.cursor="pointer";
            image.onclick = function() {
                bqc.searchManager.showMarker(json.addId);
            }
            image.style.marginRight="2px";
            addr.style.width="172px";
        }

        imgAddr.appendChild(addr);
        mainInfo.appendChild(imgAddr);
        mainInfo.style.width="250px";
        mainInfo.style.styleFloat="left";
        mainInfo.style.cssFloat="left";

        if (bqc.resultList.isProximity) {
            var distance = mainInfo.appendChild(document.createElement('div'));
            distance.className = 'routingListDetailDist';

            var metersDistance = json.distance * 1000;
            var applyDistance = function() {
                distance.innerHTML = metersToCurrentUnit(metersDistance);
            }
            applyDistance();
            bqc.resultList.distances.push(applyDistance);
        }

        entryDiv.appendChild(IconDiv);
        entryDiv.appendChild(mainInfo)
        entryDiv.className="clearfix";
        entryDiv.style.marginBottom="5px";
        return entryDiv;
    
    }catch(e){
        console.debug(e);
    }
  
}

bqc.resultList.pagingToEvent=function(obj,pos,container){
    try{
        obj.onclick=function(){
            bqc.resultList.goToPage(pos,container,bqc.resultList.nbtotal)
        }
    }catch(e){
        console.debug(e);
    }
}
bqc.resultList.goToPage=function(pos,container){
    try{
        bqc.resultList.showLoading();
        var add_ids=new Array();
        if (bqc.resultList.isProximity) {
            bqc.searchManager.setPage(pos, function(json) {
                for (i = 0; i < bqc.resultList.markers.length; ++i) {
                    add_ids.push(bqc.resultList.markers[i].addId);
                }
                bqc.resultList.fillList(bqc.resultList.markers,
                    json.marked, bqc.resultList.nbtotal, pos, bqc.resultList.nbtotalfound);
            });
        } else {
            for(var i=(pos-1)*10;(i<(pos-1)*10+10 && i<bqc.resultList.nbtotal );i++){
                add_ids.push(bqc.resultList.markers[i].addId);
            }
            new Ajax.Request('marked.do', {
                method: 'post',
                evalJSON: true,
                parameters: {
                    "p_add_ids":add_ids.join(",")
                    },
                onSuccess: function(transport) {
                    bqc.resultList.fillList(bqc.resultList.markers, transport.responseJSON, bqc.resultList.nbtotal,pos, bqc.resultList.nbtotalfound)
                    bqc.searchManager.setPage(pos);
                }
            });
        }
        

    }catch(e){
        console.debug(e);
    }
}

bqc.resultList.enableAllPaging=function(pos,container){
    try{
        //browse the dom to enable all
        for(var i=0;i<container.descendants().length;i++){
            container.descendants()[i].className="smallLink";
            if(i==0){
                bqc.resultList.pagingToEvent(container.descendants()[i],pos-1,container,bqc.resultList.nbtotal);
            }else{
                if(i==container.descendants().length-1){
                    bqc.resultList.pagingToEvent(container.descendants()[i],pos+1,container,bqc.resultList.nbtotal);
                }else{
                    bqc.resultList.pagingToEvent(container.descendants()[i],i,container,bqc.resultList.nbtotal);
                }
            }
        }
    }catch(e){
        console.debug(e);
    }
}
