 if (!Kwo) var Kwo = {};
var Cfbp = {};

Cfbp.MenuSignin = {
  show: function() {
    new Effect.Morph('box-signin', {style:'height:240px;bottom:0', duration:0.5});
  },
  hide: function() {
    new Effect.Morph('box-signin', {style:'height:0px;bottom:-1px', duration:0.3});
  }
};

Cfbp.Navigate = {
  onScroll : function() {
		var scrollTop = document.viewport.getScrollOffsets().top;
		if ($('wrapper-top')) {
			var navHeight = $('wrapper-top').getHeight();
			if (scrollTop > 69) {
				$('wrapper-top').show();
				$('logo-site-img').hide();
			} else {
				$('wrapper-top').hide();
				$('logo-site-img').show();
			}
		} else {
			var navHeight = 0;
		}
		var headerHeight = $$('.wrapper-header')[0].getHeight() + $('carousel-nav-box').getHeight();
		if ($$('.breadcrumb')) headerHeight +=  26;
		var sidebar = $$('.wrapper-content .page-node .sidebar')[0];
		if (sidebar) {
			var sidebarHeight = sidebar.getHeight();
			var viewportHeight = document.viewport.getHeight();
			var content = $$('.wrapper-content .page-node .editorial-content')[0];
			var contentHeight = content.getHeight();
			if ((sidebarHeight + navHeight) <= viewportHeight && contentHeight > sidebarHeight && scrollTop > headerHeight - navHeight) {
				var limitMin = navHeight;
				var limitMax = Math.abs(contentHeight + headerHeight - sidebarHeight - navHeight);
				if (scrollTop > limitMin) {
					if (scrollTop < limitMax) {
						sidebar.setStyle({position:'fixed', top: navHeight + 'px'});
					} else {
						var scrolled = limitMax - headerHeight + navHeight;
						sidebar.setStyle({position:'relative', top: scrolled + 'px'});
					}
				} else {
					sidebar.setStyle({position:'static'});
				}
			} else {
				sidebar.setStyle({position:'static'});
			}
		}
	},
  scrollTo : function(el) {
    if (!Object.isElement(el)) {
      Event.stop(el);
      el = Event.element(el).up('a');
    }
    var el = $(el);
    if (el.hasAttribute('href') && el.readAttribute('href').length > 1) {
      target = el.readAttribute('href').substring(1);
      if (target.indexOf('#') > 0) target = target.substr(target.indexOf('#')+1);
    } else target = el;
    if ($(target)) targeted = $(target);
    else if ($$('a[name="'+target+'"]').length > 0) targeted = $$('a[name="'+target+'"]')[0];
    else {
      if (el.hasAttribute('href') && el.readAttribute('href').indexOf('#') > 0) Kwo.go(el.readAttribute('href').substr(0, el.readAttribute('href').indexOf('#')));
      return false;
    }
    Effect.ScrollTo(targeted, { duration:'1', offset:-1 * ($('wrapper-top').getHeight() + 20) } );
  },
  ifIsScrollableDoIt : function (event) {
    if (window.location.hash) {
      var hash = window.location.hash.substr(1);
      if ($$(hash).length > 0) {
        Cfbp.Navigate.scrollTo($$('#'.hash)[0]);
      } else if ($$('a[name='+hash+']').length > 0) {
        Cfbp.Navigate.scrollTo($$('a[name='+hash+']')[0]);
      }
    }
  }
};

Cfbp.Gmap = {
  map : null,
  markerCluster : null,
  initialize : function() {
    var point = new google.maps.LatLng(46.225453,2.219238); // centre france

    var myOptions = { zoom: 5,
                      center: point,
                      mapTypeId: google.maps.MapTypeId.ROADMAP,
                      scaleControl: true
                    };

    Cfbp.Gmap.map = new google.maps.Map($("gmap"), myOptions);
    Cfbp.Gmap.getMarkers();
  },
  addMarkers : function(hash) {
    if (hash.length == 0) return;
    if (Cfbp.Gmap.markerCluster) Cfbp.Gmap.markerCluster.clearMarkers();

		var markers = [];
		hash.each(function(data) { markers.push(Cfbp.Gmap.addMarker(data)); });

    var args = {averageCenter: true,
                gridSize:50,
                minimumClusterSize:10,
                styles:[{height:31, url:'/app/cfbp/pix/gmap/picto-groupe-1.png', width:41, textColor:'#ffffff', textSize:12}]
               };
		Cfbp.Gmap.markerCluster = new MarkerClusterer(Cfbp.Gmap.map, markers, args);
    Cfbp.Gmap.markerCluster.fitMapToMarkers();

  },
  addMarker : function(data) {
    var point = new google.maps.LatLng(data.lat,data.lng);
    var opt = { position: point, map: Cfbp.Gmap.map, title: data.title };
    if (data.icon) opt.icon = data.icon;
    var marker = new google.maps.Marker(opt);

    if (data.content) {
      var infowindow = new google.maps.InfoWindow({content: data.content});
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(Cfbp.Gmap.map,marker);
      });
    }
    return marker;
  },
  getMarkers : function(args) {
    Kwo.exec('/carte-station-gpl.json', args, {callback:Cfbp.Gmap.getMarkers_callback});
  },
  getMarkers_callback : function(res) {
    if (res.error != 0) Kwo.warn(res['result']['msg']);
    else {
      var hash = res['result']['hash'];
      if (hash.length == 0) {
        alert('Aucun résultat');
        return;
      } else {
        Cfbp.Gmap.addMarkers(hash);
      }
    }
  }
};

Cfbp.Documentation = {
  setFilter : function (hash) {
    if (hash['node'] != undefined) {
      $('DocumentationSearch').down('input[name="node"]').value = hash['node'];
      $('DocumentationSearch').down('input[name="offset"]').value = 0;
    } else if (hash['offset'] != undefined) $('DocumentationSearch').down('input[name="offset"]').value = hash['offset'];
  },
  search : function(hash) {
    if (hash) Cfbp.Documentation.setFilter(hash);
    $('DocumentationSearch').submit();
  },
  purchase : function (args) {
    opts = {name:'documentation-purchase', height:550, width:570};
    new Kwo.Dialog('/documentation.purchase', args, opts);
  },
  order : function(args) {
    Kwo.exec('/documentation.order', args, {callback:Cfbp.Documentation.order_callback});
  },
  order_callback : function(res) {
    if(res['error']) return Kwo.error(res);
    if(res['result']['callback_message']) Kwo.warn(res);
    Kwo.getDialog('dialog-documentation-purchase').close();
  }
};

Event.observe(window, 'scroll', Cfbp.Navigate.onScroll);

document.observe("dom:loaded", Cfbp.Navigate.ifIsScrollableDoIt);
document.observe("dom:loaded", function (){ $$('.sidebar .box:first-child .content .nav-menu a[href*="#"]').invoke('observe', 'click', Cfbp.Navigate.scrollTo ); });

