﻿
window.addEvent('domready', function() {

    // parte lo scroller delle news
	if ($('NewsVertical')) {
		window.setTimeout("var hor = new Ticker('TickerVertical', {speed: 500, delay: 5000, direction: 'vertical'})",5000);
	}

    /* Script per il calendario */
    var mese = new Date().getMonth() + 1;
    var anno = new Date().getFullYear();
    mostraCalendario(mese, anno, 'div_calendario');

    //****************
    //* Menu top CMS *
    //****************
    $$('div#nav_main ul li').addEvent('mouseenter', function() {
        if (this.getFirst('ul')) {
            this.getFirst('ul').setStyle('display', 'block');
        }
    }).addEvent('mouseleave', function() {
        if (this.getFirst('ul')) {
            this.getFirst('ul').setStyle('display', 'none');
        }
    })

    //**********************************
    //* Effetto su menu aree tematiche *
    //**********************************
    //$$('div#nav_add_home ul li ul').setStyle('opacity', 0.9);


    $$('div#nav_add_home ul li ul').addEvent('mouseenter', function() {
        this.tween('height', 137);
    }).addEvent('mouseleave', function() {
        this.tween('height', 0);
    });

    //***********************
    //* Menu aree tematiche *
    //***********************
    $$('a.main').addEvent('mouseenter', function() {
        if (this.getNext('ul')) {
            this.getNext('ul').tween('height', 137);
        }
    }).addEvent('mouseleave', function() {
        if (this.getNext('ul')) {
            this.getNext('ul').tween('height', 0);
        }
    });

    //***************
    //* Menu sx CMS *
    //***************
   if ($('nav_supp')) {
        if ($('nav_supp').getFirst('ul')) {

            var sottoMenu = $('nav_supp').getFirst('ul');

            sottoMenu.getChildren('li').each(function(el) {

                var myheight = 0;

                if (el.getFirst('ul')) {

                    if (!el.getFirst('ul').hasClass('thisFolder')) {

                        el.getFirst('ul').setStyle('display', 'block');
                        myheight = el.getFirst('ul').getSize().y + 5;
                        el.getFirst('ul').setStyle('height', 0);
                        el.getFirst('ul').setStyle('overflow', 'hidden');

                        el.addEvent('mouseenter', function() {
                            el.getFirst('ul').tween('height', myheight);
                        }).addEvent('mouseleave', function() {
                            el.getFirst('ul').tween('height', 0);
                        });
                    }
                    else {
                        el.getFirst('ul').setStyle('display', 'block');
                    }
                }
            })
        }
    }

});

function mostraCalendario(mese, anno, div) {
    if ($(div)) {
        var qryStr = 'm=' + mese + '&a=' + anno + '&cache=' + Math.floor(Math.random() * 32767);
        var myRequest = new Request({
            onRequest: function() { },
            onCancel: function() { },
            onSuccess: function(a, b) {
                $(div).innerHTML = a;
                $(div).getElements('td.event').each(function(el) {
                    el.getFirst('a').addEvent('mouseenter', function() {
                        el.getFirst('div').setStyle('display', 'block');
                    });
                    el.addEvent('mouseleave', function() {
                        this.getFirst('div').setStyle('display', 'none');
                    });
                });
            },
            onFailure: function(instance) { alert("Errore durante l'operazione"); },
            method: 'post',
            url: '/source/ajax_calendario.asp'
        }).send(qryStr);
    }
}

	function ValidaLoginUtente() {
		$('ErroreLogin').innerHTML = "";
		
		if (!isEmail(document.frmLogin.email.value)) {
			document.frmLogin.email.focus();
			$('ErroreLogin').innerHTML = "Inserire un indirizzo email valido";
			return false;
		}
		if (!isValidString(document.frmLogin.password.value)) {
			document.frmLogin.password.focus();
			$('ErroreLogin').innerHTML = "Inserire la password";
			return false;
		}
		return true;
	}

	var Ticker = new Class({
	    setOptions: function(options) {
	        this.options = Object.extend({
	            speed: 1500,
	            delay: 5000,
	            direction: 'vertical',
	            onComplete: Class.empty,
	            onStart: Class.empty
	        }, options || {});
	    },
	    initialize: function(el, options) {
	        this.setOptions(options);
	        this.el = $(el);
	        this.items = this.el.getElements('li');
	        var w = 0;
	        var h = 0;
	        if (this.options.direction.toLowerCase() == 'horizontal') {
	            h = this.el.getSize().y;
	            this.items.each(function(li, index) {
	                w += li.getSize().x;
	            });
	        } else {
	            w = this.el.getSize().x;
	            this.items.each(function(li, index) {
	                h += li.getSize().y;
	            });
	        }
	        this.el.setStyles({
	            position: 'absolute',
	            top: 0,
	            left: 0,
	            width: w,
	            height: h
	        });
	        this.fx = this.el.set('morph', { duration: this.options.speed, onComplete: function() {
	            var i = (this.current == 0) ? this.items.length : this.current;
	            this.items[i - 1].injectInside(this.el);
	            this.el.setStyles({
	                left: 0,
	                top: 0
	            });
	        } .bind(this)
	        });
	        this.current = 0;
	        this.next();
	    },
	
	    pause: function() {
	        $clear(mytimer);
	        mytimer = null;
	    },
	    resume: function() {
	        if (mytimer == null) {
	            this.next();
	        }
	    },
	    next: function() {
	        this.current++;
	        if (this.current >= this.items.length) this.current = 0;
	        var pos = this.items[this.current];
	        this.el.morph({
	            top: -pos.offsetTop,
	            left: -pos.offsetLeft
	        });
	        mytimer = this.next.bind(this).delay(this.options.delay + this.options.speed);
	    }
	});

function ValidaFormLoginUtente() {
    if (!isValidString($('frmLoginUtente').username.value)) {
        $('errorMessage').innerHTML = "E' necessario inserire il proprio ID personale";
        $('errorMessage').setStyle('display', 'block');
        $('frmLoginUtente').username.focus();
        return false;
    }
    if (!isValidString($('frmLoginUtente').password.value)) {
        $('errorMessage').innerHTML = "E' necessario inserire la propria password personale";
        $('errorMessage').setStyle('display', 'block');
        $('frmLoginUtente').password.focus();
        return false;
    }
}


