
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);


/* include dinamicamente una libreria */
function include(src){
  var node = document.createElement("script");
  node.src = '';
  node.type = 'text/javascript';
  document.getElementsByTagName("head")[0].appendChild(node);
}





/**
 * Implements JSON stringify and parse functions
 * v1.0
 *
 * By Craig Buckler, Optimalworks.net
 *
 * As featured on SitePoint.com
 * Please use as you wish at your own risk.
 *
 * Usage:
 *
 * // serialize a JavaScript object to a JSON string
 * var str = JSON.stringify(object);
 *
 * // de-serialize a JSON string to a JavaScript object
 * var obj = JSON.parse(str);
 */

var JSON = JSON || {};

// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {

  var t = typeof (obj);
  if (t != "object" || obj === null) {

    // simple data type
    if (t == "string") obj = '"'+obj+'"';
    return String(obj);

  }
  else {

    // recurse array or object
    var n, v, json = [], arr = (obj && obj.constructor == Array);

    for (n in obj) {
      v = obj[n]; t = typeof(v);

      if (t == "string") v = '"'+v+'"';
      else if (t == "object" && v !== null) v = JSON.stringify(v);

      json.push((arr ? "" : '"' + n + '":') + String(v));
    }

    return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
  }
};


// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
  if (str === "") str = '""';
  eval("var p=" + str + ";");
  return p;
};

/**
 * Implements cookie-less JavaScript session variables
 * v1.0
 *
 * By Craig Buckler, Optimalworks.net
 *
 * As featured on SitePoint.com
 * Please use as you wish at your own risk.
*
 * Usage:
 *
 * // store a session value/object
 * Session.set(name, object);
 *
 * // retreive a session value/object
 * Session.get(name);
 *
 * // clear all session data
 * Session.clear();
 *
 * // dump session data
 * Session.dump();
 */

 if (JSON && JSON.stringify && JSON.parse)
  var Session = Session || (function() {

    // window object
    var win = window.top || window;

    // session store
    var store = (win.name ? JSON.parse(win.name) : {});

    // save store on page unload
    function Save() {
      win.name = JSON.stringify(store);
    };

    // page unload event
    if (window.addEventListener) window.addEventListener("unload", Save, false);
    else if (window.attachEvent) window.attachEvent("onunload", Save);
    else window.onunload = Save;

    // public methods
    return {

      // set a session variable
      set: function(name, value) {
        store[name] = value;
      },

      // get a session value
      get: function(name) {
        return (store[name] ? store[name] : undefined);
      },

      // clear session
      clear: function() { store = {}; },

      // dump session data
      dump: function() { return JSON.stringify(store); }

    };

})();











