/**
 * DB OBT OBT JavaScript
 * Copyright (c) 2009, DB Vertrieb GmbH. All rights reserved.
 */

/** verhindern von Konflikten mit dem '$' anderer JS-Bibliotheken wie z. B. Prototype */
jQuery.noConflict();

var OBT = {
	key_tab: 9,
	key_return: 13,
	key_space: 32,
	key_backspace: 8,

	key_pageup: 33,
	key_pagedown: 34,

	key_left: 37,
	key_up: 38,
	key_right: 39,
	key_down: 40,

	key_minus: 86,
	key_plus: 93,
	key_add: 132,
	key_substract: 140,

	// Initialize
	init: function() {
		// Init Scramblecode
		OBT.initScramblecode();
	
		// Init Teaserimages
		OBT.initTeaserimages();
	},

	/*******************************************************************/
	/***** Initialise Scramblecode *****/
	/*******************************************************************/

	// Initialize
	initScramblecode: function() {
		OBT.initScramblecodeRCP();
		OBT.initScramblecodeRCPSND();
	},
	// Check recipient
	initScramblecodeRCP: function() {
		if (jQuery('form.validate-rcp input.rcp')) {
			jQuery('form.validate-rcp input.rcp').change(function() {
				var url = '/pbin/scramble.pl?m=a;X=R:' + encodeURI(jQuery('form.validate-rcp input.rcp').val());
				jQuery.getJSON(url,
					function(data){
						jQuery("form.validate-rcp img.scramble").attr({
							src: data.img,
							width: data.width,
							heigth: data.height
				        });
						jQuery('form.validate-rcp input.scramble').val(data.scramble);
						jQuery('form.validate-rcp div.jsscramble').removeClass('jsscramble');
				})
			});
		}
	},
	// Check recipient and sender
	initScramblecodeRCPSND: function() {
		if (jQuery('form.validate-rcp-snd input.rcp') && jQuery('form.validate-rcp-snd input.snd')) {
			jQuery('form.validate-rcp-snd input.rcp').change(function() {
				if(jQuery('form.validate-rcp-snd input.rcp').val().length == 0 || jQuery('form.validate-rcp-snd input.snd').val().length == 0) return;

				var url = '/pbin/scramble.pl?m=a;X=R:' + encodeURI(jQuery('form.validate-rcp-snd input.rcp').val()) +
						  ';X=S:' + encodeURI(jQuery('form.validate-rcp-snd input.snd').val());
				jQuery.getJSON(url,
					function(data){
						jQuery("form.validate-rcp-snd img.scramble").attr({
							src: data.img,
							width: data.width,
							heigth: data.height
				        });
						jQuery('form.validate-rcp-snd input.scramble').val(data.scramble);
						jQuery('form.validate-rcp-snd div.jsscramble').removeClass('jsscramble');
				})
			});
			jQuery('form.validate-rcp-snd input.snd').change(function() {
				if(jQuery('form.validate-rcp-snd input.rcp').val().length == 0 || jQuery('form.validate-rcp-snd input.snd').val().length == 0) return;

				var url = '/pbin/scramble.pl?m=a;X=R:' + encodeURI(jQuery('form.validate-rcp-snd input.rcp').val()) +
						  ';X=S:' + encodeURI(jQuery('form.validate-rcp-snd input.snd').val());
				jQuery.getJSON(url,
					function(data){
						jQuery("form.validate-rcp-snd img.scramble").attr({
							src: data.img,
							width: data.width,
							heigth: data.height
				        });
						jQuery('form.validate-rcp-snd input.scramble').val(data.scramble);
						jQuery('form.validate-rcp-snd div.jsscramble').removeClass('jsscramble');
				})
			});
		}
	},
	
	/*******************************************************************/
	/***** Initialise Teaserimages *****/
	/*******************************************************************/

	// Initialize
	initTeaserimages: function() {
        this.setTeaserChangeTimer();
	},
	// Set Teaser Change Timer dependent of the attribute specified at the current item
	setTeaserChangeTimer: function() {
		if (jQuery('#teaserimages div.teaserimage.toplevel')) {
            var timeToShow = jQuery('#teaserimages div.teaserimage.toplevel').attr('tts');
            if (!timeToShow || timeToShow < 1000) {
                timeToShow = 6000;
            }
            this.teaserChangeTimeout = window.setTimeout("OBT.teaserChanger()", timeToShow);
		}
	},
	// change Teaser-Images with animate-function
	teaserChanger: function() {
		// image-settings
		var $alt = jQuery('#teaserimages div.teaserimage.toplevel');
	    var $neu = $alt.next('div.teaserimage').length ? $alt.next('div.teaserimage') : jQuery('#teaserimages div.teaserimage:first');
	    $alt.removeClass('toplevel');
	    $neu.addClass('toplevel');

        // image-animation
        $alt.fadeOut(2000);
        $neu.fadeIn(2000, function() {
            jQuery('h2', $alt).hide();
            jQuery('h2', $neu).fadeIn(1000);
        });

        // Reset Teaser Change Timer
        this.setTeaserChangeTimer();
	}
};

jQuery(document).ready(function() {
	OBT.init();
});

