
var bloosee = bloosee || {};

bloosee.Client = function(options) {
    options = options || {};
    options.categories = options.categories || '1,2,3,4,5,6,7,8,9,10,11,12'; 
    options.users = options.users ||  false;
    options.tags = options.tags || false;
    this.options = options;
    
    
    // private variables
    this.infopoints_ =[]; // store map infopoints
    this.clusters_ =[]; // store map clusters
};


bloosee.Client.prototype.setOptions = function(options) {
    jQuery.extend(this.options, options);
    return this;
}


/**
 * get data from server
 */
bloosee.Client.prototype.getData = function() {
     var me = this;  
     var bounds = me.map.getBounds();
     var southWest = bounds.getSouthWest();
     var northEast = bounds.getNorthEast();
     var zoom = me.map.getZoom();     
     var hash = new Date().getTime();

     var params = {
             "minx": southWest.lng(),
             "miny": southWest.lat(),
             "maxx": northEast.lng(),
             "maxy": northEast.lat(),
             "zoom": zoom,
             "hash": hash,
             "categories": me.options.categories
         }; 
     if (me.options.users) {
         params['u'] = me.options.users;
     }
     if (me.options.tags) {
         params['t'] = me.options.tags;   
     }


    jQuery.ajax({
        url: "/api/infopoints/nearby2.json",
        data: params,
        type: 'GET',
        dataType: 'json',
        async: false,
        cache: false,
        error: function(){
                  alert('error');
                 },
        success: function(rs,textstatus){
             //if sent hash if different than returned, do nothing
             if (rs.hash != hash) {
                 return;
             }

             me.clearOverlays();
             me.processInfopointsData(rs.infopoints);
            
             jQuery.each(rs.clusters, function(i, json) {
                 var cluster = me.createCluster(json.lat, json.lng, json.items, {id: json.cid});
                 me.clusters_.push(cluster);
                 me.map.addOverlay(cluster);           
             });
             
        }
    });
    
};


bloosee.Client.prototype.clearOverlays = function() {
    var i;
    for (i=0;i<this.infopoints_.length;i++) {
        this.map.removeOverlay(this.infopoints_[i]);
        
    }
    this.infopoints_ = [];
    for (i=0;i<this.clusters_.length;i++) {
        this.map.removeOverlay(this.clusters_[i]);
        
    }
    this.clusters_ = [];
}


bloosee.Client.prototype.createCluster = function(lat, lng, items, opt_opts) {
    var cluster = new ClusterCircle(new google.maps.LatLng(lat,lng),1,"#6666ff",items, opt_opts);
    return cluster;
};


bloosee.Client.prototype.processInfopointsData = function(rs) {
    var me = this;
    var infopoint;
    
    jQuery.each(rs, function(i, json) {  
            infopoint = me.createInfoPointFromJson(json);
            me.map.addOverlay(infopoint);
            me.infopoints_.push(infopoint);
    });
};

bloosee.Client.prototype.createInfoPointFromJson = function(json) {
 /*   
       {"hash": "iaz", 
        "like": 0, 
        "type_id": 201, 
        "cat_id": 2, 
        "title": "Major light", 
        "lon": "3.822743", 
        "cat_slug": "nav-aids", 
        "routes": 0, 
        "type_slug": "majorlight", 
        "been": 0, 
        "paradise": 0, 
        "lat": "39.996208", 
        "type": "Major light", 
        "icon-path": "/static/icons/map/major-light.png"}
  */  
    var params = {
        hash: json.hash,
        title: json.title,
        cat_id: json.cat_id,
        icon: json['icon-path']
    };
    
    return this.createInfoPoint(new google.maps.LatLng(json.lat, json.lon), params);  
};



bloosee.Client.prototype.createInfoPoint = function(latlng, params) {
    var me = this;
    params = params || {};
    var hash = params.hash;
    var cat_id = params.cat_id;
    
    function setZindex_(marker) {
        return google.maps.Overlay.getZIndex(marker.getPoint().lat()) + ((20 - bloosee.defines.category_zindex[cat_id] ) * 100000);      
    }
    
    var markericon = new google.maps.Icon(google.maps.G_DEFAULT_ICON);
    markericon.image = params.icon;
    markericon.iconSize = new google.maps.Size(32, 32);
    markericon.iconAnchor = new google.maps.Point(14, 31);
    markericon.infoWindowAnchor = new google.maps.Point(15,0);

    var marker = new google.maps.Marker(latlng, { icon:markericon, zIndexProcess:setZindex_, hash:params.hash });

    google.maps.Event.addListener(marker,"click", function() {
        var path = '/infopoints/ajax/smallinfowindow/' + hash + '/';
        var  html_loading = '<div class="map-dialog-box" ><div class="content"><div class="title"><h3>LOADING....</h3></div></div></div>';
          marker.openExtInfoWindow(
                me.map, 
                'map-dialog-box', 
                html_loading, 
                { 
                    ajaxUrl: path,
                    beakOffset: 2
                }
        );
      });    
    
    return marker;
};



/**
 * map setter
 */
bloosee.Client.prototype.setMap = function(map) {
    this.map = map;
    return this;
};



