﻿var OCF = {};
OCF.ChapterMap = function(clientID) {
    this.ClientID = clientID;
    this.Chapters = new Array();    
    this.FilteredChapters = new Array();
    this.AddChapter = function(name, url, avatar, address1, address2, city, province, postalcode, country, latitude, longitude) {
        this.Chapters.push({"name":name,"url":url,"avatar":avatar,"address1":address1,"address2":address2,"city":city,"province":province,"postalcode":postalcode,"country":country,"latitude":latitude,"longitude":longitude});  
    }
    this.Map = null;
    this.ProvinceSelector = null;
    this.ChapterList = null;
    this.ItemsPerPage = 18;
    this.CurrentPage = 0;
    this.CreateMap = function() {
        if (GBrowserIsCompatible()) {
            var thisObject = this;
            this.Map = new GMap2(document.getElementById(this.ClientID));
            this.Map.setCenter(new GLatLng(45.0890355, -99.31640625), 3);
            this.Map.setMapType(G_NORMAL_MAP);
            for(i = 0; i < this.Chapters.length; i++) {
                var chapter = this.Chapters[i];
                var marker = this.CreateMarker(chapter);
                this.Map.addOverlay(marker);
            }
            this.Map.addControl(new GLargeMapControl());
            if (this.ProvinceSelector != null)
                GEvent.addDomListener(this.ProvinceSelector, "change", function() {thisObject.SelectProvince();});
            if (this.ChapterList != null)
            {
                GEvent.addListener(this.Map, "moveend", function() { thisObject.UpdateChapterList();});
                GEvent.addListener(this.Map, "zoomend", function() { thisObject.UpdateChapterList();});
                this.UpdateChapterList();
            }
        }
    }
    this.CreateMarker = function(chapter) {
        var point = new GLatLng(chapter.latitude,chapter.longitude);
        var marker = new GMarker(point);
        var thisObject = this;
        GEvent.addListener(marker, 'click', function() { thisObject.Map.openInfoWindowHtml(point, thisObject.GetChapterSummaryHtml(chapter, true));});
        return marker;
    }
    
    this.SelectProvince = function() {
        if (this.ProvinceSelector != null)
        {
            var index = this.ProvinceSelector.selectedIndex;
            var value = this.ProvinceSelector.options[index].value;
            var values = value.split(",");
            this.Map.setCenter(new GLatLng(values[2], values[3]), parseInt(values[4]));
        }
    }
    
    this.DisplayPage = function(page)
    {
            this.CurrentPage = page;
            var inHtml = "";
            var pageHtml = "";
            var count = this.ItemsPerPage;
            
            if (this.FilteredChapters.length <= count)
                count = this.FilteredChapters.length;
            else 
            {
                if (this.FilteredChapters.length - (this.ItemsPerPage * this.CurrentPage) < this.ItemsPerPage)
                    count =  this.FilteredChapters.length - (this.ItemsPerPage * this.CurrentPage);
                pageHtml = "<div style='clear:both'></div>";
                pageHtml += "<div class='CommonContentBoxFooter'>";
                var pageCount = this.FilteredChapters.length/this.ItemsPerPage; 
                for(var p=0;p<pageCount;p++)
                {
                    if (p == this.CurrentPage)
                        pageHtml += (p+1) + "&nbsp;";
                    else
                        pageHtml += "<a href='javascript:" + this.ClientID + ".DisplayPage(" + p + ");'>" + (p+1) + "</a>&nbsp;";
                }
                if (this.CurrentPage < pageCount-1)
                    pageHtml += "<a href='javascript:" + this.ClientID + ".DisplayPage(" + (this.CurrentPage + 1) + ");'>Next ></a>";
                pageHtml += "</div>";
            }
            for(var i=(this.CurrentPage*this.ItemsPerPage);i<(this.CurrentPage*this.ItemsPerPage)+count;i++) {
                inHtml += this.GetChapterSummaryHtml(this.FilteredChapters[i]);
            }
            this.ChapterList.innerHTML = inHtml + (pageHtml.length > 0 ? pageHtml : "");
    }
    this.UpdateChapterList = function() {
        if (this.ChapterList != null)
        {
            var bounds = this.Map.getBounds();
            var sw = bounds.getSouthWest();
            var ne = bounds.getNorthEast();
            this.FilteredChapters = new Array();
            
            for(var i=0; i< this.Chapters.length; i++)
            {
                var chapter = this.Chapters[i];
                if (chapter.latitude < ne.lat() && chapter.latitude > sw.lat() &&
                    chapter.longitude > sw.lng() && chapter.longitude < ne.lng())
                    this.FilteredChapters.push(chapter);
            }
            this.DisplayPage(0);
        }
    }
    
    this.GetChapterSummaryHtml = function(chapter, withAddress) {
        if (!withAddress && chapter.SummaryHtml != null)
            return chapter.SummaryHtml;
        var html = "<div class='CommonSideListArea'><div class='CommonSideListImage'><a href='" + chapter.url + "'><img src='" + chapter.avatar + "' style='border-width:0px;max-height:60px;max-width:60px;' /></a></div>";
        html += "<div class='CommonSideListContent'><strong style='font-size: 110%;'><a href='" + chapter.url + "'>" + chapter.name + "</a></strong>";
        if (withAddress) 
            html += "<br/>" + chapter.address1 + "<br/>" + chapter.city + ", " + chapter.province + " " + chapter.postalcode
        html += "</div><div style='clear: both;'></div></div>";
        if (!withAddress)
            chapter.SummaryHtml = html; 
        return html;
    }
}

