
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;


function $() {
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    results.push( element );
  }
  return results.length < 2 ? results[0] : results;
}

function count(a){ return a.length; }


function div_hide(id){
  this.element=YAHOO.util.Dom.get(id);
  $D.setStyle(this.element,'display','none');
  $D.setStyle(this.element,'visibility','hidden');
}

function div_show(id){
  this.element=$D.get(id);
  $D.setStyle(this.element,'display','block');
  $D.setStyle(this.element,'visibility','visible');
}

function div_fade(inElm, opts){
  this.element=$D.get(inElm);
  var attributes={ opacity:{ from:1,to:0} } ;

  this.onEffectComplete=new YAHOO.util.CustomEvent('oneffectcomplete',this);
  var ease=((opts && opts.ease) ? opts.ease : YAHOO.util.Easing.easeOut );
  var secs=((opts&&opts.seconds) ? opts.seconds : 1 );
  var delay=((opts && opts.delay) ? opts.delay : false);

  this.effect=new YAHOO.util.Anim(this.element, attributes, secs, ease);
  this.effect.onComplete.subscribe(function(){
      div_hide(this.element);
      this.onEffectComplete.fire();
  } ,this, true);

  if(!delay){
    this.effect.animate();
  }
}

function div_appear(inElm, opts){
  this.element=$D.get(inElm);
  $D.setStyle(this.element,'opacity','0');
  div_show(this.element);
  var attributes={ opacity:{ from:0,to:1} };

  this.onEffectComplete=new YAHOO.util.CustomEvent('oneffectcomplete',this);
  var ease=((opts&&opts.ease)?opts.ease:YAHOO.util.Easing.easeOut);
  var secs=((opts&&opts.seconds)?opts.seconds:3);
  var delay=((opts&&opts.delay)?opts.delay:false);

  this.effect=new YAHOO.util.Anim(this.element,attributes,secs,ease);
  this.effect.onComplete.subscribe(function(){
      this.onEffectComplete.fire();
  } ,this,true);
  if(!delay){
    this.effect.animate();
  }
}

/*
function _div_hide(id){
  var anim = new YAHOO.util.Anim(id, { opacity: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
  anim.onComplete.subscribe(function(){
      $(id).style.display = 'none';
  });
  anim.animate();
  return $(id);
}

function _div_show(id, onComplete){
  //$(id).style.opacity = '.10';
  $(id).style.display = 'block';
  var anim = new YAHOO.util.Anim(id, { opacity: { to: 100 } }, 3, YAHOO.util.Easing.easeIn);
  if ( onComplete ){
    anim.onComplete.subscribe( onComplete  );
  }
  anim.animate();
  return $(id);
}
*/

function is_email_valid(s) {
   //return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
   return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(s);
}
//------------------------------------------------------------------------------
//  ajax
//------------------------------------------------------------------------------
function on_ajax_failure(o){ alert('richiesta asincrona fallita.'); }

function ajax_get(url, callback) {
  var clb = {
    success: callback,
    failure: on_ajax_failure
    //, scope: AjaxObject
  };
  return YAHOO.util.Connect.asyncRequest('GET', url, clb, null);
}

/*
data puņ essere un
- array o oggetto
- string id o name della form
- object HTML form
*/
function ajax_post(url, data, callback) {
  var postData = null;
  var clb = {
    success: callback,
    failure: on_ajax_failure
  };
  if( typeof(data) == 'string' ){
    var formObject = document.getElementById( data );
    YAHOO.util.Connect.setForm(formObject);
  } else if( typeof(data) == 'object' ){
    postData = data;
  }
  return YAHOO.util.Connect.asyncRequest('POST', url, clb, postData);
}
//------------------------------------------------------------------------------
//  scroller
//------------------------------------------------------------------------------
var CI = window.CI || {};


CI.Scroller = function () {
  var stepIncrement = 50;	// The number of pixels that each step moves the window.
  var stepDelay = 10;	// The number of milliseconds between steps.
  var limit = 6 * 1000;	// After 6 seconds the scroll is killed.

  var running = false;

  /* Recursive scrolling method. Steps through the complete scroll. */

  function scrollStep(to, dest, down) {

    if(!running || (down && to >= dest) || (!down && to <= dest)) {
      CI.Scroller.killScroll();
      return;
    }
    if((down && to >= (dest - (2 * stepIncrement))) ||
       (!down && to <= (dest - (2 * stepIncrement)))) {
      stepIncrement = stepIncrement * .55;
    }
    window.scrollTo(0, to);

    // Assign the returned function to a public method.

    CI.Scroller.nextStep = callNext(+to + stepIncrement, dest, down);
    window.setTimeout(CI.Scroller.nextStep, stepDelay);
  }

  /* Create a closure so that scrollStep can be accessed by window.setTimeout(). */

  function callNext(to, dest, down) {

    return function() { scrollStep(to, dest, down); };
  }
  return {

    nextStep: null,
    killTimeout: null,

    /* Sets up and calls scrollStep. */

    anchorScroll: function(e, obj) {

      var clickedLink = $E.getTarget(e);
      var anchorId = clickedLink.href.replace(/^.*#/, '');
      var target = $D.get(anchorId);


      if(target) {

        $E.stopEvent(e);
        running = true;

        var yCoord = (($D.getY(target) - 6) < 0) ? 0 : $D.getY(target) - 6;
        var currentYPosition = (document.all) ? document.body.scrollTop : window.pageYOffset;
        var down = true;
        //alert( 'scrol to ' + yCoord );
        if(currentYPosition > yCoord) {
          stepIncrement *= -1;
          down = false;
        }

        // Stop the scroll once the time limit is reached.

        CI.Scroller.killTimeout = window.setTimeout(CI.Scroller.killScroll, limit);

        scrollStep(currentYPosition + stepIncrement, yCoord, down);
      }
    },

    targetScroll: function(anchorId){
      var target = $D.get(anchorId);

      if(target) {
        //$E.stopEvent(e);
        running = true;

        var yCoord = (($D.getY(target) - 6) < 0) ? 0 : $D.getY(target) - 6;
        var currentYPosition = (document.all) ? document.body.scrollTop : window.pageYOffset;
        var down = true;
        //alert( 'scrol to ' + yCoord );
        if(currentYPosition > yCoord) {
          stepIncrement *= -1;
          down = false;
        }

        // Stop the scroll once the time limit is reached.

        CI.Scroller.killTimeout = window.setTimeout(CI.Scroller.killScroll, limit);

        scrollStep(currentYPosition + stepIncrement, yCoord, down);
       }
    },

    /* Kill the scroll after a timeout, to prevent an endless loop. */

    killScroll: function() {
      window.clearTimeout(CI.Scroller.killTimeout);
      running = false;
      stepIncrement = 50;
    },

    /* Attach the scrolling method to the links with the class 'scrolling-link'. */
    init: function() {

      var links = $D.getElementsByClassName('scrolling-link', 'a');
      $E.addListener(links, 'click', CI.Scroller.anchorScroll, CI.Scroller, true);
    }
  }
} ();

//$E.onAvailable('doc', CI.Scroller.init, CI.Scroller, true);


//------------------------------------------------------------------------------
// GMaps
//------------------------------------------------------------------------------
var gmap_map = null;
var gmap_geocoder = null;

function gmap_show_address(map_id, address, zoom_level, showMarker, html) {

  if ( GBrowserIsCompatible() ) {
    if(!gmap_map) {
      gmap_map = new GMap2($(map_id));
      gmap_map.addControl(new GSmallMapControl());
      gmap_map.addControl(new GMapTypeControl());
    }
    if(!gmap_geocoder) {
      gmap_geocoder = new GClientGeocoder();
      gmap_geocoder.setBaseCountryCode('IT');
    }
    if(!zoom_level){
      zoom_level = 11;
    }
    if(!showMarker){
      showMarker = false;
    }
    if(!html){
      html = null;
    }

    gmap_geocoder.getLatLng(address, function(point) {
        if (!point) {
          // log_err(address + " not found");

        } else {
          gmap_map.setCenter(point, zoom_level);
          if( showMarker ){
            var marker = new GMarker(point);
            gmap_map.addOverlay(marker);
            if( html ){
              GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
              });
            }
          }
        }
      }
    );
  }
}

