/* ---------------------------------------------------------------------------------------------------- */
/* =sbb-mobileworld.js */
/* ---------------------------------------------------------------------------------------------------- */

var SBB = {};


/* -------------------------------------------------- */
/* =popup */

SBB.popup = {
	init: function () {
		$$('.popup').each(function (anchor) {
			anchor.addEvent('click', this.launch.bindWithEvent(this, anchor));
		}, this);
	},
	launch: function (event, anchor) {
		event.stop();
		window.open(anchor.href, 'sbbpopup', 'width=665,height=840,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
	}
};
window.addEvent('domready', function () {
	SBB.popup.init();
});

SBB.popup2 = {
	init: function () {
		$$('.popup2').each(function (anchor) {
			anchor.addEvent('click', this.launch.bindWithEvent(this, anchor));
		}, this);
	},
	launch: function (event, anchor) {
		event.stop();
		window.open(anchor.href, 'sbbpopup', 'width=680,height=680,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0');
	}
};
window.addEvent('domready', function () {
	SBB.popup2.init();
});


/* -------------------------------------------------- */
/* =presenter */

SBB.presenter = {
	
	options: {
		itemWidth: 220,
		itemMargin: 20
	},
	
	init: function () {
		this.itemRange = this.options.itemWidth + this.options.itemMargin;
		this.elements = {
			container: $('presenter-container'),
			viewport: $('presenter-viewport'),
			items: $('presenter-items'),
			prev: $('presenter-prev'),
			next: $('presenter-next')
		};
		this.items = this.elements.items.getElements('li');
		this.elements.items.setStyle('width', this.items.length * this.options.itemWidth + this.items.length * this.options.itemMargin);
		this.ubound = this.items.length - 1;
		this.current = 0;
		this.elements.prev.addEvent('click', this.showPrev.bind(this));
		this.elements.next.addEvent('click', this.showNext.bind(this));
		this.fx = new Fx.Tween(this.elements.items, {
			property: 'left',
			link: 'cancel',
			transition: 'sine:out',
			onComplete: this.checkControls.bind(this)
		});
		this.checkControls();
	},
	
	showPrev: function () {
		if (this.current > 0) {
			this.current--;
			this.fx.start(-(this.current * this.itemRange));
		}
	},
	
	showNext: function () {
		if (this.current < this.ubound - 1) {
			this.current++;
			this.fx.start(-(this.current * this.itemRange));
		}
	},
	
	checkControls: function () {
		(this.current === 0) ? this.elements.prev.setStyle('display', 'none') : this.elements.prev.setStyle('display', 'block');
		(this.current === this.ubound - 1) ? this.elements.next.setStyle('display', 'none') : this.elements.next.setStyle('display', 'block');
	}
	
};
window.addEvent('domready', function () {
	if ($('presenter-container')) SBB.presenter.init();
});


/* -------------------------------------------------- */
/* =Prefill */

SBB.Prefill = new Class({
	initialize: function (element, defaultValue) {
		this.element = $(element);
		this.defaultValue = defaultValue;
		this.element.addEvents({
			'focus': this.clearValue.bind(this),
			'blur': this.prefillValue.bind(this)
		});
		
		this.type = this.element.get('type');
		this.prefillValue();
	},
	clearValue: function () {
		if (this.element.get('value') == this.defaultValue) {
			if (this.type == 'password') {
				if (Browser.Engine.trident) { // ie sucks
					var clone = new Element('input', {
						'type': 'password',
						'id': this.element.get('id'),
						'name': this.element.get('name'),
						'class': this.element.get('class')
					});
					clone.replaces(this.element);
					(function () {
						clone.focus();
						clone.cloneEvents(this.element);
						this.element = clone;
					}).delay(50, this);
				} else {
					this.element.set('type', 'password');
				}
			}
			this.element.set('value', '');
		}
	},
	prefillValue: function () {
		if (this.element.get('value').length == 0) {
			if (this.type == 'password') {
				if (Browser.Engine.trident) { // ie sucks
					var clone = new Element('input', {
						'type': 'text',
						'id': this.element.get('id'),
						'name': this.element.get('name'),
						'class': this.element.get('class')
					});
					clone.cloneEvents(this.element);
					clone.replaces(this.element);
					this.element = clone;
				} else {
					this.element.set('type', 'text');
				}
			}
			this.element.set('value', this.defaultValue);
		}
	}
});

/* -------------------------------------------------- */
/* =FlashLoader */

var FlashLoader = new Class({

    initialize: function(containers) {
        this.containers = containers;
        this.containers.forEach(function(item) {
            this.loadParams(item);
        } .bind(this));
    },

    loadParams: function(item) {
        this.container = $(item);
        this.objTag = this.container.getElement('object');

        this.params = new Hash();
        this.paramElements = this.container.getElements('param');
        this.paramElements.each(function(param) {
            this.params.include(param.get('name'), param.get('value'));
        }, this);

        this.loadSwiff();
    },

    loadSwiff: function() {
        new Swiff(this.params.get('movie'), {
            width: this.objTag.width,
            height: this.objTag.height,
            params: this.params.getClean(),
            container: this.container
        });
    }

});

window.addEvent('domready', function () {
	if (Browser.Plugins.Flash.version != 0) {
		var verbundskampagne = new Swiff('_swf/main_' + langKey + '.swf', {
			id: 'verbundskampagne',
			width: 950,
			height: 488,
			params: {
				wMode: 'opaque',
				bgcolor: '#ffffff'
			},
			container: $('teaser-swf')
		});
	} else {
		var nS2 = new noobSlide({
			box: $('box'),
			items: [0,1,2,3,4,5],
			size: 600,
			addButtons: {
				previous: $$('.prev'),
				next: $$('.next')
			}
		});
	}
});
