/* Animation timing, increase to slow down all animations */
var animationDuration = 400;


/*
 * jQuery Easing v1.1.1 - http://gsgd.co.uk/sandbox/jquery.easing.php
 *
 * Uses the built in easing capabilities added in jQuery 1.1
 * to offer multiple easing options
 *
 * Copyright (c) 2007 George Smith
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

function change() { }
jQuery.extend({
    easing: {
        easeinout: function(x, t, b, c, d) {
            if (t < d/2) return 2*c*t*t/(d*d) + b;
            var ts = t - d/2;
            return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;       
        },
        linear: function(x, t, b, c, d) {
            return c*t/d + b; //linear
        }
    }
});

/*!
 * jQuery JavaScript Library v1.4.3
 * http://jquery.com/
 *
 * Copyright 2010, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Thu Oct 14 23:10:06 2010 -0400
 */

/* Using prevAll(), nextAll(), delay() from jQuery 1.4.3 */
var runtil = /Until$/,
    rparentsprev = /^(?:parents|prevUntil|prevAll)/,
    // Note: This RegExp should be improved, or likely pulled from Sizzle
    rmultiselector = /,/,
    slice = Array.prototype.slice;


jQuery.extend({    
    dir: function( elem, dir, until ) {
        var matched = [], cur = elem[dir];
        while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
            if ( cur.nodeType === 1 ) {
                matched.push( cur );
            }
            cur = cur[dir];
        }
        return matched;
    }

});

jQuery.extend( jQuery.fx, {
    speeds: {
        slow: 600,
        fast: 200,
        // Default speed
        _default: 400
    }
});


jQuery.each({
    nextAll: function( elem ) {
        return jQuery.dir( elem, "nextSibling" );
    },
    prevAll: function( elem ) {
        return jQuery.dir( elem, "previousSibling" ).reverse();
    }
}, function( name, fn ) {
    jQuery.fn[ name ] = function( until, selector ) {
        var ret = jQuery.map( this, fn, until );
        if ( !runtil.test( name ) ) { selector = until; }
        if ( selector && typeof selector === "string" ) { ret = jQuery.filter( selector, ret ); }
        ret = this.length > 1 ? jQuery.unique( ret ) : ret;
        if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) { ret = ret.reverse(); }
        return this.pushStack( ret, name, slice.call(arguments).join(",") );
    };
});

var rclass = /[\n\t]/g,
    rspaces = /\s+/,
    rreturn = /\r/g,
    rspecialurl = /^(?:href|src|style)$/,
    rtype = /^(?:button|input)$/i,
    rfocusable = /^(?:button|input|object|select|textarea)$/i,
    rclickable = /^a(?:rea)?$/i,
    rradiocheck = /^(?:radio|checkbox)$/i;


jQuery.fn.extend({
    // Based off of the plugin by Clint Helfers, with permission.
    // http://blindsignals.com/index.php/2009/07/jquery-delay/
    delay: function( time, type ) {
        time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
        type = type || "fx";

        return this.queue( type, function() {
            var elem = this;
            setTimeout(function() {
                jQuery.dequeue( elem, type );
            }, time );
        });
    },
    hasClass: function( selector ) {
        var className = " " + selector + " ";
        for ( var i = 0, l = this.length; i < l; i++ ) {
            if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
                return true;
            }
        }
        return false;
    }
});
/* end of code import */

var tabCount = 0;
var tabWidth = 0;
var overallWidth = 929;
var contentWidth = 0;
var currentElement = null;
var animationStyle = 'easeinout';

function moveElements() {
    var currentElementIndex = 0;
    var newLeft = 0;
    
    currentElement.prevAll().each(function(i, e) {
        newLeft = -929 + (tabWidth * (i + 1));
        if ($(this).css('left') != newLeft) {
            $(this).children('.accordionItemClick')
                .delay(animationDuration)
                .show()
                .children('.icon')
                    .addClass('iconRight')
                    .removeClass('iconLeft')
            $(this).children('.accordionItemContent').hide();
            $(this).animate({ left : newLeft }, animationDuration, animationStyle);
        }
        currentElementIndex = i + 1;
    });
    
    currentElement.animate({ left : -929 + contentWidth + (tabWidth * (currentElementIndex + 1)) }, animationDuration, animationStyle);
    if (currentElement.children('.accordionItemContent').hasClass('accordionItemContentRight')) {
        currentElement.children('.accordionItemContent').css({ right : 24 }, animationDuration, animationStyle);
    } else {
        currentElement.children('.accordionItemContent').css({ left : tabWidth * (tabCount - 1) + 24 }, animationDuration, animationStyle);
    }
    currentElement.children('.accordionItemContent').fadeIn(animationDuration);

    currentElementIndex += 1;

    currentElement.children('.accordionItemShadowLeft').css({'left' : ((tabCount - 1) * tabWidth)});
    currentElement.children('.accordionItemClick')
        .hide()
        .children('.icon')
            .removeClass('iconRight')
            .removeClass('iconLeft');
    
    currentElement.nextAll().each(function(i, e) {
        newLeft = -929 + contentWidth + (tabWidth * (i+currentElementIndex+1));
        if ($(this).css('left') != newLeft) {
            $(this).children('.accordionItemClick')
                .delay(animationDuration)
                .fadeIn(animationDuration/2)
                .children('.icon')
                    .addClass('iconLeft')
                    .removeClass('iconRight')
                    
            $(this).children('.accordionItemContent').hide();
            $(this).animate({ left : newLeft }, animationDuration, animationStyle);
        }
    });
}

$(document).ready(function() {
  var findM2 = document.getElementById("middle").getElementsByTagName("div");
  for (i=0,j=1;i<findM2.length;i++) {
  		if (findM2[i].className=="ModulM2 M2R" || findM2[i].className=="ModulM2 M2L") {
			if (findM2[i].className=="ModulM2 M2R") var RiLE = " accordionItemContentRight";
			else var RiLE = "";

			var dateTime = new Date().getTime();
			var findImg = findM2[i].getElementsByTagName("img");
			if ((findImg[0].src.indexOf('1497044')!==(-1)) && (findImg[0].src.indexOf('thumb_01')!==(-1))) var helpImg =  "1497036/blobBinary/img_01.jpg?ck="+dateTime;
			if ((findImg[0].src.indexOf('1497046')!==(-1)) && (findImg[0].src.indexOf('thumb_02')!==(-1))) var helpImg =  "1497038/blobBinary/img_02.jpg?ck="+dateTime;
			if ((findImg[0].src.indexOf('1497048')!==(-1)) && (findImg[0].src.indexOf('thumb_03')!==(-1))) var helpImg =  "1497040/blobBinary/img_03.jpg?ck="+dateTime;
			if ((findImg[0].src.indexOf('1497050')!==(-1)) && (findImg[0].src.indexOf('thumb_04')!==(-1))) var helpImg =  "1497042/blobBinary/img_04.jpg?ck="+dateTime;

			findM2[i].parentNode.parentNode.parentNode.className = "html5";
			findM2[i].parentNode.parentNode.className = "rahmen";
			findM2[i].parentNode.className = "accordionWrapper";

			findM2[i].className = "accordionItem-"+j+" accordionItem";
			findM2[i].style.background = "url('/dtag/cms/contentblob/ICSS/un/"+helpImg+"')";
			var sp =document.createElement("span");
			sp.className = "accordionItemShadowLeft";
			findM2[i].appendChild(sp);
			var addA =document.createElement("a");
			addA.className = "accordionItemClick accordionItemClickOpened";
			addA.href = "javascript:change();";
			addA.innerHTML = '<span class="accordionItemShadow"></span><span class="icon iconLeft"><img src="/dtag/cms/contentblob/ICSS/un/1503032/blobBinary/01_icon_right.gif" style="display:none;"></span><span class="thumbnail"><img src="'+findImg[0].src+'" /></span>';
			findM2[i].appendChild(addA);
			
			var findText = findM2[i].getElementsByTagName("div");
			for (i=0;i<findText.length;i++) {
				if (findText[i].className=="text_klein wh105x63") {					
					findText[i].parentNode.className = "accordionItemContent"+RiLE;
					findText[i].parentNode.innerHTML = findText[i].innerHTML;
					}
				}
			j++;
			}
		}

    tabCount = $('.accordionItem').length;
    tabWidth = parseInt($('.accordionItemClick').width());
    contentWidth = parseInt($('.accordionWrapper').width()) - (tabCount * tabWidth)
    currentElement = $('.accordionItem-1');
    var j = 1;
    $('.accordionItem').children().hide();

    for (var i = tabCount; i >= 1; i--) {
        $('.accordionItem-' + i)
            .delay((animationDuration * j) * 4)
            .animate({ left: -(tabWidth * (j-1))}, animationDuration, animationStyle)
        j++;
    }
    setTimeout("moveElements()", animationDuration*4*(tabCount+1));
    
    $('.accordionItemShadowLeft').fadeIn();
    $('.accordionItemClick').click(function() {
        $(this).blur();
        currentElement = $(this).parent();
        moveElements();
    });
});
