﻿/* PAGE */
var H = SSP.Helpers;
SSP.newClass('Page', {
    Implements: Options
	, options: {
	    handleStartHide: false
		, engineClass: true
		, iePng: false
		, externalLinks: true
		, menuCorners: false
		, showTips: false
		, dateTime: true
		, pageLayout: false
		, textSize: false
	}
	, initialize: function(options) {
	    this.setOptions(options);
	    if (this.options.engineClass) H.engineClass('html');
	    if (this.options.handleStartHide) H.handleStartHide();
	    if (this.options.textSize) new SSP.UI.TextSize(document.getElement('.layout'), $('page'));
	    if (this.options.showTips) H.toolTips();
	    if (this.options.iePng && Browser.Engine.trident && !Browser.Engine.trident5) H.iePngFix();
	    if (this.options.externalLinks) H.externalLinks();
	    new SSP.UI.Widgets.DateTime3D($E('#header .datetime'), { time: true, shortDate: false, is3d: false });
	    if (Browser.Engine.trident || Browser.Engine.presto) {
	        (document.getElements('.navigator')).each(function(navigator) {
	            navigator.getElements('li').addEvents({
	                'mousedown': function() { this.addClass('active'); }
                    , 'mouseup': function() { this.removeClass('active'); }
                    , 'mouseout': function() { this.removeClass('active'); }
	            });
	        });
	    }
	    var pageSpecificFunction = "contextPage." + SSP.Profile.pageId + "();";
	    var pageScriptExecuted = $try(function() { return eval(pageSpecificFunction) == undefined; }, function() { return false; });
	}
});

/* CONTEXT PAGE */

var contextPage = {

    default0: function() {
        (document.getElements('.slider')).each(function(slider) {
            new SSP.UI.Widgets.SLSlider(slider, {
                slideTimer: 8000
                , transitionTime: 3000
                , orientation: slider.hasClass('_vertical') ? 'vertical' : 'vertical'
                , fade: false
                , isPaused: false
                , useNavNumbers: true
                , useNavButtons: true
            });
        });
        new SSP.UI.Widgets.SLSlider($E('ul. li.r3p'), {
            slideTimer: 8000
            , transitionTime: Browser.Engine.trident ? 750 : 1500
            , orientation: 'none'
            , fade: true
            , isPaused: false
            , useNavNumbers: true
            , useNavButtons: true
            , centerNavControls: false
            , selectors: { itemsContainer: '.simpleslider', item: '.item', navigatorContainer: false }
        });
//        new SSP.UI.Widgets.SLSlider($E('ul.hp4 li.l'), {
//            slideTimer: 8000
//            , transitionTime: Browser.Engine.trident ? 750 : 1500
//            , orientation: 'none'
//            , fade: true
//            , isPaused: false
//            , useNavNumbers: false
//            , useNavButtons: false
//            , selectors: { itemsContainer: '.simpleslider', item: '.item', navigatorContainer: false }
//        });
        new SSP.UI.Widgets.ItemsSlider(".itemsslider", {
            itemSize: 208
            , animation: { duration: 750, amount: 4 }
            , itemPerView: 2
        });
    }

};

/* SITE FORM */

var siteform = {
    validatePost: function() {
        var values = '';
        var ids = ['aci', 'ts', 'isbn', 'oth'];
        ids.each(function(id) {
            var el = $(id);
            el.value = el.value.trim();
            values += el.value;
        });
        if (values == '') {
            alert(RES.alertFormDataRequired);
            return false;
        }
        return true;
    }
    , validatePostFast: function() {
        var fvstarter = $('fvstarter');
        fvstarter.value = fvstarter.value.trim();
        if (fvstarter.value == '') {
            alert(RES.alertFormDataRequired);
            return false;
        }
        return true;
    }
};

/* PRINT TEMPLATE AREA */

var printTemplateArea = {
    init: function(target) {
        var target = $(target);
        var btn = new Element('div', { 'class': 'box print' })
		.adopt(
			new Element('div', {
			    'text': 'Stampa la pagina'
				, events: {
				    mousedown: function() { printTemplateArea.print(); }
				}
			})).inject(target);
    }
	, print: function() {
	    var code = new SSP.StringBuilder('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');
	    code.append('<html xml:lang="it" xmlns="http://www.w3.org/1999/xhtml">');
	    code.append('<head>');
	    code.append(document.getElement('head').get('html'));
	    code.append('<link href="content/20090621/pkr/SSP.Styles.Print.css" type="text/css" rel="stylesheet" />');
	    code.appendFormat('<body{0}>', document.body.className != '' ? ' class="' + document.body.className + '"' : '');
	    code.appendFormat('<div id="page"{0}>', document.getElement('#page').className != '' ? ' class="' + document.getElement('#page').className + '"' : '');
	    code.append('<div id="templates">');
	    code.appendFormat('<div class="{0}">', document.getElement('#templates .aux').className);
	    code.append(document.getElement('#templates .aux').get('html'));
	    code.append('</div>');
	    code.append('</div>');
	    code.append('</div>');
	    code.append('<div class="print-buttons">');
	    code.append('<input value="stampa la pagina" type="button" onclick="self.print();" />');
	    code.append('<input value="chiudi" type="button" onclick="self.close();" />');
	    code.append('</div>');
	    code.append('</body>');
	    code.append('</html>');
	    SSP.Helpers.openPopup('', 'printPage', code.toString(), 680, 400, 'no');
	}
};


SSP.UI.Widgets.newClass('SLSlider', {

    Implements: [Options]

	, numNav: new Array()                       //will store number nav elements (if used)
	, timer: null                               //periodical function variable holder
	, isSliding: 0                              //flag for animation/click prevention
	, direction: 1                              //flag for direction (forward/reverse)

	, options: {
	    useSimpleNav: true                        //Time between slides (1 second = 1000), a.k.a. the interval duration
	    , slideTimer: 8000                        //Time between slides (1 second = 1000), a.k.a. the interval duration
		, orientation: 'horizontal'             //vertical, horizontal, or none: None will create a fading in/out transition.
		, fade: false                           //if true will fade the outgoing slide - only used if orientation is != None
		, isPaused: false                       //flag for paused state
		, transitionTime: 1500                  //Transition time (1 second = 1000)
		, frames: 150                           //Transition time (1 second = 1000)
		, transitionType: 'cubic:out'           // 'linear' //Transition type
		, itemNum: 0                            //Current item number
		, useNavButtons: false                  //Whether or not the buttons navigation will be used
		, useNavNumbers: false                  //Whether or not the number navigation will be used
		, centerNavControls: true
		, selectors: { /*mainContainer: '.slider',*/itemsContainer: 'ol', item: 'li', navigatorContainer: false} // or false //all elements selectors
		, navClassNames: { navControls: 'controls', navNumbers: 'nav num', navButtons: 'nav btns', play: 'play', pause: 'pause', prev: 'prev', next: 'next'} // or false //all elements selectors
		, labels: { play: 'avvia', pause: 'ferma', prev: '&lt; prec.', next: 'succ. &gt;' }
		, titles: { play: 'avvia', pause: 'ferma', prev: 'precedente', next: 'successivo' }
	}

	, initialize: function(slider, options) {
	    this.mainContainer = $(slider);
	    this.actionButtons = false;
	    this.setOptions(options);
	    if (this.options.selectors != false) {
	        this.itemsContainer = this.mainContainer.getElement(this.options.selectors.itemsContainer);
	        this.navigatorContainer = !this.options.selectors.navigatorContainer
	            ? this.mainContainer
	            : this.mainContainer.getElement(this.options.selectors.navigatorContainer);
	        this.items = $$(this.itemsContainer.getElements(this.options.selectors.item));
	        if (this.options.useSimpleNav) {

	            this.playBtn = new Element('div', { 'class': "btn " + this.options.navClassNames.pause, title: this.options.titles.pause })
					.addEvent('mousedown', function() { this.pauseIt(); } .bind(this))
					.inject(this.navigatorContainer);
	            this.options.navControls = new Element('ul', { 'class': 'monitor' }).inject(this.navigatorContainer);
	        }
	        else {
	            if (this.options.useNavNumbers != false || this.options.useNavButtons != false) {
	                this.options.navControls = new Element('ul', { 'class': this.options.navClassNames.navControls }).inject(this.navigatorContainer);
	            }
	            if (this.options.useNavNumbers != false) {
	                this.navNumbers = new Element('ul', { 'class': this.options.navClassNames.navNumbers }).inject(this.options.navControls);
	            }
	            if (this.options.useNavButtons != false) {
	                this.navButtons = new Element('ul', { 'class': this.options.navClassNames.navButtons }).inject(this.options.navControls).adopt(
				        this.prevBtn = new Element('li', { 'class': this.options.navClassNames.prev, title: this.options.titles.prev })
					        .adopt(new Element('span', { 'html': this.options.labels.prev }))
					        .addEvent('mousedown', function() {
					            if (this.isSliding == 0) {
					                if (this.options.isPaused == false) {
					                    $clear(this.timer);
					                    this.timer = this.slideIt.periodical(this.options.slideTimer, this, null);
					                }
					                this.direction = 0;
					                this.slideIt();
					            }
					        } .bind(this))
				        , this.playBtn = new Element('li', { 'class': this.options.navClassNames.pause, title: this.options.titles.pause })
					        .adopt(new Element('span', { 'html': this.options.labels.pause }))
					        .addEvent('mousedown', function() { this.pauseIt(); } .bind(this))
				        , this.nextBtn = new Element('li', { 'class': this.options.navClassNames.next, title: this.options.titles.next })
					        .adopt(new Element('span', { 'html': this.options.labels.next }))
					        .addEvent('mousedown', function() {
					            if (this.isSliding == 0) {
					                if (this.options.isPaused == false) {
					                    $clear(this.timer);
					                    this.timer = this.slideIt.periodical(this.options.slideTimer, this, null);
					                }
					                this.direction = 1;
					                this.slideIt();
					            }
					        } .bind(this))
				        );
	                this.actionButtons = this.navButtons.getElements('li');
	            }
	        }
	    }
	    else
	        return false;
	    this.items.each(function(el, i) {
	        el.setStyle('position', "absolute");
	        var itemH = el.getSize().y;
	        var itemW = el.getSize().x;
	        if (this.options.orientation == 'vertical') {
	            el.setStyle('top', (-10 * itemH));
	            el.setStyle('left', 0);
	        } else if (this.options.orientation == 'none') {
	            el.setStyle('left', 0);
	            el.setStyle('top', 0);
	            el.set('opacity', 0);
	        } else { el.setStyle('left', (-10 * itemW)); }
	        if (this.options.useSimpleNav) {
	            var numItem = new Element('li', { id: 'num' + i, 'text': i + 1 });
	            numItem.addEvents({
	                'mousedown': function() { this.numPress(i); } .bind(this)
	            });
	            this.numNav.push(numItem);
	            this.options.navControls.adopt(numItem);
	            if (i == this.items.length - 1 && this.options.centerNavControls) {
	                this.options.navControls.setStyles({
	                    position: 'absolute'
	                    , left: '50%'
	                    , marginLeft: -(this.options.navControls.getSize().x / 2).toInt()
	                });
	            }
	        }
	        else {
	            if (this.options.useNavNumbers == true) {
	                var numItem = new Element('li', { id: 'num' + i, 'html': '<span>' + (i + 1) + '</span>' });
	                this.navNumbers.adopt(numItem);
	                this.numNav.push(numItem);
	                numItem.set('morph', { duration: 100, transition: Fx.Transitions.linear, link: 'ignore' });
	                numItem.addEvents({
	                    'mousedown': function() { this.numPress(i); } .bind(this)
	                });
	                if (i == this.options.itemNum) {
	                    var initNum = this.numNav[i];
	                    initNum.addClass('active');
	                }
	            }
	        }
	    } .bind(this));
	    /*
	    this.options.navControls.fade(.25);
	    this.mainContainer.addEvents({
	    mouseenter: function() { this.options.navControls.fade(1); } .bind(this)
	    , mouseleave: function() { this.options.navControls.fade(.25); } .bind(this)
	    });
	    */
	    this.start();
	}

	, start: function() {
	    this.slideIt(this.options.itemNum);
	    if (this.options.isPaused == false) {
	        this.timer = this.slideIt.periodical(this.options.slideTimer, this, null);
	        if (this.playBtn) {
	            if (!this.options.useSimpleNav)
	                this.playBtn.getElement('span').set('html', this.options.labels.pause);
	            this.playBtn
	                .set('title', this.options.titles.pause)
	                .swapClass(this.options.navClassNames.pause, this.options.navClassNames.play);
	        }
	    }
	    else {
	        if (this.playBtn) {
	            if (!this.options.useSimpleNav)
	                this.playBtn.getElement('span').set('html', this.options.labels.play);
	            this.playBtn
	                .set('title', this.options.titles.play)
	                .swapClass(this.options.navClassNames.play, this.options.navClassNames.pause);
	        }
	    }
	}

	, slideIt: function(passedID) {
	    var curItem = this.items[this.options.itemNum];
	    if (this.options.useNavNumbers == true) { var curNumItem = this.numNav[this.options.itemNum]; }
	    if (passedID != null) {
	        if (this.options.itemNum != passedID) {
	            if (this.options.itemNum > passedID) { this.direction = 0; }
	            else { this.direction = 1; }
	            this.options.itemNum = passedID;
	        }
	    }
	    else { this.changeIndex(); }
	    var newItem = this.items[this.options.itemNum];
	    if (!$defined(this.itemStyles)) {
	        this.containerSize = this.itemsContainer.getSize();
	        this.itemStyles = { padding: 0, width: this.containerSize.x, height: this.containerSize.y, overflow: 'auto' };
	        //	        this.itemStyles = { padding: '5px 10px', width: this.containerSize.x - 20, height: this.containerSize.y - 10, overflow: 'auto' };
	        this.newItemSize = newItem.getSize();
	        if (this.direction == 0) {
	            this.limits = {
	                curX: this.containerSize.x
	                , newX: (-1 * this.newItemSize.x)
	                , curY: this.containerSize.y
	                , newY: (-1 * this.newItemSize.y)
	            };
	        }
	        else {
	            this.limits = {
	                curX: (-1 * this.containerSize.x)
	                , newX: this.newItemSize.x
	                , curY: (-1 * this.containerSize.y)
	                , newY: this.newItemSize.y
	            };
	        }
	    }
	    newItem.setStyles(this.itemStyles);
	    if (this.options.useNavNumbers == true) {
	        var newNumItem = this.numNav[this.options.itemNum];
	        newNumItem.addClass('active');
	    }
	    var item_in = new Fx.Morph(newItem, {
	        duration: this.options.transitionTime,
	        frames: this.options.frames,
	        transition: this.options.transitionType,
	        link: 'ignore',
	        onStart: function() { this.isSliding = 1; } .bind(this),
	        onComplete: function() { this.isSliding = 0; } .bind(this)
	    });
	    if (this.options.orientation == 'vertical') {
	        if (this.options.fade == true) { item_in.start({ 'opacity': [0, 1], 'top': [this.limits.newY, 0] }); }
	        else { item_in.start({ 'top': [this.limits.newY, 0] }); }
	    } else if (this.options.orientation == 'none') {
	        item_in.start({ 'opacity': [0, 1] });
	    } else {
	        if (this.options.fade == true) { item_in.start({ 'opacity': [0, 1], 'left': [this.limits.newX, 0] }); }
	        else { item_in.start({ 'left': [this.limits.newX, 0] }); }
	    }
	    newItem.scrollTop = 0;
	    newItem.scrollLeft = 0;
	    if (curItem != newItem) {
	        var item_out = new Fx.Morph(curItem, {
	            duration: this.options.transitionTime,
	            frames: this.options.frames,
	            transition: this.options.transitionType,
	            link: 'ignore'
	        });
	        if (this.options.useNavNumbers == true) { curNumItem.removeClass('active'); }
	        if (this.options.orientation == 'vertical') {
	            if (this.options.fade == true) { item_out.start({ 'opacity': [0], 'top': [(this.limits.curY)] }); }
	            else { item_out.start({ 'top': [(this.limits.curY)] }); }
	        } else if (this.options.orientation == 'none') { item_out.start({ 'opacity': [1, 0] }); } else {
	            if (this.options.fade == true) { item_out.start({ 'opacity': [0], 'left': [(this.limits.curX)] }); }
	            else { item_out.start({ 'left': [(this.limits.curX)] }); }
	        }
	    }
	}

	, fader: function() {
	    if (!this.actionButtons)
	        return;
	    if (this.isSliding == 0)
	        this.actionButtons.fade(1);
	    else
	        this.actionButtons.fade(0);
	}

	, pauseIt: function() {
	    if (this.isSliding == 0) {
	        if (this.options.isPaused == false) {
	            this.options.isPaused = true;
	            $clear(this.timer);
	            if (!this.options.useSimpleNav)
	                this.playBtn.getElement('span').set('html', this.options.labels.play);
	            this.playBtn
					.set('title', this.options.titles.play)
					.swapClass(this.options.navClassNames.play, this.options.navClassNames.pause);
	        }
	        else {
	            this.options.isPaused = false;
	            this.slideIt();
	            this.timer = this.slideIt.periodical(this.options.slideTimer, this, null);
	            if (!this.options.useSimpleNav)
	                this.playBtn.getElement('span').set('html', this.options.labels.pause);
	            this.playBtn
					.set('title', this.options.titles.pause)
					.swapClass(this.options.navClassNames.pause, this.options.navClassNames.play);
	        }
	    }
	}

	, changeIndex: function() {
	    var numItems = this.items.length;
	    if (this.direction == 1) {
	        if (this.options.itemNum < (numItems - 1)) { this.options.itemNum++; }
	        else { this.options.itemNum = 0; }
	    }
	    else if (this.direction == 0) {
	        if (this.options.itemNum > 0) { this.options.itemNum--; }
	        else { this.options.itemNum = (numItems - 1); }
	    }
	}

	, numPress: function(theIndex) {
	    if ((this.isSliding == 0) && (this.options.itemNum != theIndex)) {
	        if (this.options.isPaused == false) {
	            $clear(this.timer);
	            this.timer = this.slideIt.periodical(this.options.slideTimer, this, null);
	        }
	        this.slideIt(theIndex);
	    }
	}

});

