window.addEvent('domready', function() {
	if ($('nav')) { var myMenu = new MenuMatic({ id: 'nav', orientation: 'horizontal', duration: 600, opacity: 90, matchWidthMode: true });}
	if (typeof(images) != 'undefined') { var myShow = new Slideshow('headerImage', images, { height: 159, width: 474, controller: false, delay: 5000, duration: 2000, thumbnails: false, transition: Fx.Transitions.Linear, loader: false }); }
	
	init = false;
	var accordion = new Accordion($$('.toggler'),$$('.element'), {
	    alwaysHide: true,
	    display: -1,
		initialDisplayFx: false,
	    onBackground: function(toggler) {
			if (Cookie.read('AccOpen') == toggler.get('id') && init) {
				Cookie.write('AccOpen','');
			};
			toggler.removeClass('active');
	    },
	    onActive: function(toggler) {
			Cookie.write('AccOpen',toggler.get('id'));
			toggler.addClass('active');
	    }
	});
	$$('a.current.link').each(function(el) {
	   accordion.display(-1);
	   Cookie.write('AccOpen','');
	});
	var AccOpen = Cookie.read('AccOpen');
	if (AccOpen != '') {
		if (document.id(AccOpen)) {
			document.id(AccOpen).fireEvent('click');
		}
	}
	var init = true;

	calendarLinks();

	$$('#tabs-nav a, #tabs-body li').tabify({
		transition: 'sine:out'
	});

	$$('input.initClear').each(function(el) { var defaulValue = el.get('value'); el.addEvents({ 'focus': function(e) { if(this.get('value') == this.defaultValue) this.set('value', ''); } }); });
	$$('a.submit').each(function(el) {
		var formID = el.get('rel');
		el.addEvents({
			'click': function(e) {
				e.stop();
				$(formID).submit();
			}
		});
	});
	$$('.submit_annunci').each(function(el) {
		var formaction = $('annunci').get('rel');
		el.addEvents({
			'click': function(e) {
				e.stop();
				$('annunci').set('action',formaction+$('select_tipologia').value+'/'+$('select_specializzazione').value+'/'+$('select_luogo').value+'/');
				$('annunci').submit();
			}
		});
	});
});

function calendarLinks() {
	$$('a.calBoxLink').each(function(el) {
		var defaulValue = el.get('href');
		el.addEvents({
			'click': function(e) {
				e.stop();
				new Request.HTML({
					url: 'ajax.php?request=calendar',
					data: {'url':defaulValue},
					onComplete: function(response){
						$('calendarBoxAjax').empty().adopt(response);
						calendarLinks();
					}
				}).send();
			}
		});
	});
	$$('a.numero').each(function(element,index) {
		var title = element.get('title');
		var content = element.getChildren('.eventi').get('html');
		if (!content[0]) {
			content[0] = "&nbsp;";
		}
		element.store('tip:title', title);
		element.store('tip:text', content[0]);
	});
	var myTips = new Tips('.numero', {
		initialize: function() {
			this.tip.fade('hide');
		},
		onShow : function(tip){
			tip.fade('in');
		},
		onHide: function(tip) {
			tip.fade('out');
		}
	});
}

var nuTabs = new Class({
    Implements: [Events, Options],

    options: {
        // transition: $empty,
        navActiveClass: 'selected'
    },

    initialize: function(tabsNav, tabsBody, options){
        this.setOptions(options);
        this.tNav = tabsNav;
        this.tBody = tabsBody;

        this.attach();
    },

    attach: function(){
        this.tNav.each(function(tab, index){
            //The orignal line is below need to rewrite the bindwidth event for mootools 1.3
            //I rewrote it using pass and bind - I think it's correct?  Seems to work:
            tab.addEvent('click', function(e){
                this.resize.apply(this, [e, index]);
             }.bind(this));
        }, this);
    },

    alteredHeight: function(index){
        // expose to measurement
        return this.tBody[index].measure(function(){
            return this.getSize().y;
        });
    },

    resize: function(e, index){
        e.stop();

        this.tBodyCont = this.tBody.getParent();
        this.tNavCont = this.tNav.getParent();

        // set transitions if needed
        this.tBodyCont.set('tween', {
            transition: Array.pick(this.options.transition, 'sine:out')
        });

        // alter the wrappers's height
        this.tBodyCont.tween('height', this.alteredHeight(index));

        // absolutize and hide the content items
        this.tBody.set('styles', {
            'position': 'absolute',
            'top': 0,
            'opacity': 0
        }).fade('out');

        // show the active content item
        this.tBody[index].set('styles', {
            'display': 'block',
            'opacity': 0
        }).fade('in');

        // add class to the active tab
        this.tNavCont.removeClass(this.options.navActiveClass);
        this.tNavCont[index].addClass(this.options.navActiveClass);
    }
});

Elements.implement({
    tabify: function(options){
        this.tabNav = this.slice(0, this.length/2);
        this.tabBody = this.slice(this.length/2, this.length);
        new nuTabs($$(this.tabNav), $$(this.tabBody), Array.pick(options, {}));
    }
});

