var map, UI_PROJ, OL_PROJ, central_point;
var point_layer, all_points, default_category, current_popup;

function load_json() {
  OpenLayers.loadURL(CONTEXT_ABSOLUTE_URL+"/feed.json", "", null, onJsonLoad);
}

function onJsonLoad(req) {
  parseDataJson(req);
  prepare_features(point_layer);
  fill_layer_category(default_category);
}

function parseDataJson(req) {
  g =  new OpenLayers.Format.JSON();
    
  json_data = g.read(req.responseText);
  features = json_data.features;
  default_category = json_data.default_category;
  all_points = new Array();

  for(var feat in features) {
      var ll = new OpenLayers.LonLat(features[feat].geometry.x, features[feat].geometry.y);
      ll.transform(UI_PROJ, OL_PROJ);
      features[feat].ll = ll;
      features[feat].icon = new OpenLayers.Icon(features[feat].marker_url,
                                 new OpenLayers.Size(features[feat].marker_width, features[feat].marker_height),
                                 new OpenLayers.Pixel(-features[feat].marker_hotspotx, -features[feat].marker_hotspoty));
      all_points.push(features[feat]);
  }
}

function hide_current_popup(feature) {
  if (current_popup != undefined) {
    current_popup.hide();
  }
}

function toggle_popup(feature) {
  if (feature.popup === null) {
    feature.popup = feature.createPopup(feature.closeBox);
    map.addPopup(feature.popup);
    feature.popup.show();
  } else {
    feature.popup.toggle();
  }
  current_popup = feature.popup;
}


function prepare_features(lr) {
  var AutoSizeFramedCloud, padding, minsize;

  AutoSizeFramedCloud = new OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
      'autoSize': true, 'minSize': new OpenLayers.Size(280,200), 'padding': new OpenLayers.Bounds(10, 10, 10, 10)});

  for(var feat in features) {
    var feature = new OpenLayers.Feature(lr, all_points[feat].ll); 
    feature.closeBox = true;
    feature.popupClass = AutoSizeFramedCloud;
    feature.data.popupContentHTML = all_points[feat].popup_html;
    feature.data.overflow = "hidden";
    feature.data.icon = all_points[feat].icon;

    var marker = feature.createMarker();

    var markerClick = function (evt) {
      // this is a OpenLayers.Feature
      hide_current_popup();
      toggle_popup(this);
      // this brings the marker to front
      point_layer.removeMarker(this.marker);
      point_layer.addMarker(this.marker);
      OpenLayers.Event.stop(evt);
    };
    marker.events.register("mousedown", feature, markerClick);

    all_points[feat].feature = feature;
    all_points[feat].marker = feature.marker;
  }
}

function dct(a) {
  var o = {};
  for(var i=0;i<a.length;i++) {o[a[i]]='';}
  return o;
}

function fill_layer_category(category) {
  var cnt;
  hide_current_popup();
  point_layer.clearMarkers();
  scroll_to_top('imageitemscontainer');

  var container_el = jQuery('div#imageitems');
  container_el.empty();

  cnt = 0;
  for (var feat=0; feat<all_points.length; feat++) {
    if (category in dct(all_points[feat].subject)) {
      point_layer.addMarker(all_points[feat].marker);
      container_el.append(all_points[feat].item_html);
      cnt += 1;
    }
  }

  if (cnt > 14) {
    jQuery('#item_scroll_button_up').attr('style', '');
    jQuery('#item_scroll_button_down').attr('style', '');
  } else {
    jQuery('#item_scroll_button_up').attr('style', 'display:none;');
    jQuery('#item_scroll_button_down').attr('style', 'display:none;');
  }

  jQuery("div.olLayerDiv > div > img").addClass("map_marker");
  jQuery("#gallerycategories button").removeClass("selected");
  jQuery("#pointcategory" + category).addClass("selected");
}


function highlight_marker(marker_id) {
  var mrk = all_points[marker_id].marker;
  hide_current_popup();
  point_layer.removeMarker(mrk);
  map.setCenter(mrk.lonlat);
  point_layer.addMarker(mrk);
  toggle_popup(all_points[marker_id].feature);
}

function map_init() {

  central_point = new OpenLayers.LonLat(CENTRAL_X, CENTRAL_Y);

  vendor_map_init();

  central_point.transform(UI_PROJ, OL_PROJ);

  if (BASE_LAYER && base_layers[BASE_LAYER]) {
    map.setBaseLayer(base_layers[BASE_LAYER]);
  }

  point_layer = new OpenLayers.Layer.Markers("Markers");

  load_json();

  map.addLayer(point_layer);

  navigation = new OpenLayers.Control.Navigation();
  map.addControl(navigation);
  navigation.disableZoomWheel();
  map.addControl(new OpenLayers.Control.MousePosition());
  panzoombar = new OpenLayers.Control.PanZoomBar();
  panzoombar.zoomStopWidth = 18;
  panzoombar.zoomStopHeight = 5;
  map.addControl(panzoombar);

  map.addControl(new OpenLayers.Control.LayerSwitcher());

  map.setCenter(central_point.clone(), map.getZoomForResolution(RESOLUTION));
}



/* Scroller */
var scroll_step=1, interval_up="", interval_down="", large_scroll_step=76*1;

function scroll_up_once(id) {document.getElementById(id).scrollTop += large_scroll_step;}
function scroll_down_once(id) {document.getElementById(id).scrollTop -= large_scroll_step;}

function start_scroll_up(id) {interval_up = setInterval("scroll_up('"+id+"')", 20);}
function start_scroll_down(id) {interval_down = setInterval("scroll_down('"+id+"')", 20);}
function scroll_up(id) {document.getElementById(id).scrollTop += scroll_step; if (scroll_step < 15) {scroll_step+=1;}}
function scroll_down(id) {document.getElementById(id).scrollTop -= scroll_step; if (scroll_step < 15) {scroll_step+=1;}}
function scroll_to_bottom(id) {document.getElementById(id).scrollTop = document.getElementById(id).scrollHeight;}
function scroll_to_top(id) {document.getElementById(id).scrollTop=0;}
function stop_scrolling() {clearInterval(interval_down);clearInterval(interval_up);scroll_step=2;}

