﻿$.fn.reorder = function() {

    function randOrd() { return (Math.round(Math.random()) - 0.5); }

    return ($(this).each(function() {
        var $this = $(this);
        var $children = $this.children(); //$this.children(".result:visible");
        var childCount = $children.length;

        if (childCount > 1) {
            $children.remove();

            var indices = new Array();
            for (i = 0; i < childCount; i++) { indices[indices.length] = i; }
            indices = indices.sort(randOrd);
            $.each(indices, function(j, k) { $this.append($children.eq(k)); });

        }
    }));
}

$.urlParam = function(name) {
    var results = new RegExp('[\\/]' + name + '-([^/#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}



function GetRandom(min, max) {
    if (min > max) {
        return (-1);
    }
    if (min == max) {
        return (min);
    }

    return (min + parseInt(Math.random() * (max - min + 1)));
}


$(document).ready(function() {


    /*
    * Script für die KeyVisuals
    * 
    * Dieses Script ist für die Steuerung der KeyVisuals zuständig
    *
    */

    var KVImg1 = 1;
    var KVImg2 = 0;
    var KVImg3 = 0;
    var KVImg4 = 0;

    $(".changeImg *:eq(1)").addClass("active");

    $("a.btn1").click(function() {
        if (KVImg1 == 0 && KVImg2 == 1) {
            $("img.keyVisual1").fadeIn(1000);
            $("img.keyVisual2").fadeOut(1000);
            KVImg2 = 0;
            KVImg1 = 1;
        }
        else if (KVImg1 == 0 && KVImg3 == 1) {
            $("img.keyVisual1").fadeIn(1000);
            $("img.keyVisual3").fadeOut(1000);
            KVImg3 = 0;
            KVImg1 = 1;
        }
        else if (KVImg1 == 0 && KVImg4 == 1) {
            $("img.keyVisual1").fadeIn(1000);
            $("img.keyVisual4").fadeOut(1000);
            KVImg4 = 0;
            KVImg1 = 1;
        }
        else {
        }
        if ($(".changeImg *:eq(1)").hasClass("active")) {


        }
        else {
            $(".changeImg *:eq(1)").addClass("active");
            $(".changeImg *:eq(3)").removeClass("active");
            $(".changeImg *:eq(5)").removeClass("active");
            $(".changeImg *:eq(7)").removeClass("active");

        }


    });


    $("a.btn2").click(function() {

        if (KVImg2 == 0 && KVImg1 == 1) {
            $("img.keyVisual2").fadeIn(1000);
            $("img.keyVisual1").fadeOut(1000);
            KVImg1 = 0;
            KVImg2 = 1;
        }
        else if (KVImg2 == 0 && KVImg3 == 1) {
            $("img.keyVisual2").fadeIn(1000);
            $("img.keyVisual3").fadeOut(1000);
            KVImg3 = 0;
            KVImg2 = 1;
        }
        else if (KVImg2 == 0 && KVImg4 == 1) {
            $("img.keyVisual2").fadeIn(1000);
            $("img.keyVisual4").fadeOut(1000);
            KVImg4 = 0;
            KVImg2 = 1;
        }
        else {
        }
        if ($(".changeImg *:eq(3)").hasClass("active")) {
        }
        else {
            $(".changeImg *:eq(3)").addClass("active");
            $(".changeImg *:eq(1)").removeClass("active");
            $(".changeImg *:eq(5)").removeClass("active");
            $(".changeImg *:eq(7)").removeClass("active");

        }
    });

    $("a.btn3").click(function() {

        if (KVImg3 == 0 && KVImg1 == 1) {
            $("img.keyVisual3").fadeIn(1000);
            $("img.keyVisual1").fadeOut(1000);
            KVImg1 = 0;
            KVImg3 = 1;
        }
        else if (KVImg3 == 0 && KVImg2 == 1) {
            $("img.keyVisual3").fadeIn(1000);
            $("img.keyVisual2").fadeOut(1000);
            KVImg2 = 0;
            KVImg3 = 1;
        }
        else if (KVImg3 == 0 && KVImg4 == 1) {
            $("img.keyVisual3").fadeIn(1000);
            $("img.keyVisual4").fadeOut(1000);
            KVImg4 = 0;
            KVImg3 = 1;
        }
        else {

        }

        if ($(".changeImg *:eq(5)").hasClass("active")) {



        }
        else {
            $(".changeImg *:eq(5)").addClass("active");
            $(".changeImg *:eq(1)").removeClass("active");
            $(".changeImg *:eq(3)").removeClass("active");
            $(".changeImg *:eq(7)").removeClass("active");

        }

    });
    $("a.btn4").click(function() {

        if (KVImg3 == 0 && KVImg1 == 1) {
            $("img.keyVisual3").fadeIn(1000);
            $("img.keyVisual1").fadeOut(1000);
            KVImg1 = 0;
            KVImg4 = 1;
        }
        else if (KVImg3 == 0 && KVImg2 == 1) {
            $("img.keyVisual3").fadeIn(1000);
            $("img.keyVisual2").fadeOut(1000);
            KVImg2 = 0;
            KVImg4 = 1;
        }
        else if (KVImg4 == 0 && KVImg3 == 1) {
            $("img.keyVisual4").fadeIn(1000);
            $("img.keyVisual3").fadeOut(1000);
            KVImg3 = 0;
            KVImg4 = 1;
        }
        else {

        }

        if ($(".changeImg *:eq(7)").hasClass("active")) {



        }
        else {
            $(".changeImg *:eq(7)").addClass("active");
            $(".changeImg *:eq(1)").removeClass("active");
            $(".changeImg *:eq(3)").removeClass("active");
            $(".changeImg *:eq(5)").removeClass("active");

        }

    });

    $("a.referenceNavPoints, .scrollableKomp a.result").mouseenter(function() {
        //jQuery(this.children[1]).fadeOut(1500);

        if (this.isOver == undefined || this.isOver == false) {
            this.isOver = true;

            jQuery(this.children[1]).fadeIn(800);
        }

    });

    $("a.referenceNavPoints, .scrollableKomp a.result").mouseleave(function() {
        //jQuery(this.children[1]).fadeIn(1000);
        var elm = this;
        jQuery(this.children[1]).fadeOut(1000, function() {
            elm.isOver = false; 
        });
    });

    $("a.result").mouseenter(function() {
        //jQuery(this.children[1]).fadeOut(1500);

        if (this.isOver == undefined || this.isOver == false) {
            this.isOver = true;
            jQuery(this.children[2]).fadeIn(800);
        }

    });

    $("a.result").mouseleave(function() {
        //jQuery(this.children[1]).fadeIn(1000);
        var elm = this;
        jQuery(this.children[2]).fadeOut(1000, function() {
            elm.isOver = false;
        });
    });

//    $(".mainContentInnerWc > .breadcrumbNavigation").addClass("breadcrumbPositioning");
//    
//    if($(".breadcrumbNavigatio(1)").hasClass("inactiveBC"))
//    {
//        $(".mainContentInnerWc > .breadcrumbNavigation").removeClass("breadcrumbPositioning");
//    }


    /*
    * Script für den Newsbereich
    * 
    * Dieses Script fügt eine Extra Klasse hinzu, wodurch der untere Rahmen
    * entfernt werden kann (per CSS).
    *
    */
    $(".newsObject:last").addClass("last");

    $(".subNav li:last").addClass("last");

});


var maxItems = 2;
function initResultElements() {
    var index = 0;
    var result = new Array();

    var itemid = $.urlParam("itemid");
    var customer = $.urlParam("cust");

    var insertChild;
    $.each(rElements, function(key, value) {
        var count = value.length;

        var position;
        if (itemid != 0 && customer != 0 && Url.decode(customer) == key) {
            var list = jQuery(value);

            $(value).each(function(index, value) {
                if (value.attr("id") == "result_" + itemid) {
                    position = index;
                    insertChild = value.parent();
                    insertChild.remove();
                }
            });
        }
        else {
            position = GetRandom(0, count - 1);
        }
        value[position].fadeIn(1500);
        for (var i = 0; i < count; i++) {
            if (i != position)
                value[i].parent().remove();
        }
    });
    $(".scrollableKomp .items").reorder();

    if (insertChild != undefined)
        insertChild.insertBefore($(".scrollableKomp .items div")[0]);
        
    $(".scrollableKomp").scrollable({ speed: 1000, circular: true }).autoscroll({ autoplay: true, interval: 4000, steps: 3 });
    $(".scrollableKomp .items").css("left", "-150px");
}


function initFurtherRefElements() {

    var items = $(".scrollable .items div");

    if (items.length > 3) {
        var div = document.createElement("div");
        $(".scrollable .items div").remove();
        for (var i = 0; i < items.length; i++) {

            if (i != 0 && (i % 3.0 == 0 || i == items.count - 1)) {
                $(".scrollable .items").append(div);
                div = document.createElement("div");
            }
            div.appendChild(items[i]);
        }

        $(".scrollable .items").append(div);


        var scroller = $(".scrollable").scrollable({ speed: 2000, circular: true });
        scroller.autoscroll({ autoplay: true, interval: 5000, steps: 1 });
    }
    //$(".scrollable .items").css("left", "-207px");
}

function addReferenceElement(arr, firm, id) {
    if (arr[firm] == undefined) {
        arr[firm] = [];
    }
    arr[firm].push($("#" + id));
}

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
    
    

}


/* Key Visual Slider Initialisierung */
window.open = Wait();

function Wait() {
    setTimeout(InitSlider, 10);
}

function InitSlider() {

    if (typeof $.fn.cycle == "function") {
        var visuals = $(".slideshow a");

        var elmIndex = Math.floor(Math.random() * visuals.length);

        var elements = new Array(4);

        var container = $('<div></div>').addClass("slideShowItems");

        var items = 4;
        if (visuals.length < 4)
            items = visuals.length

        for (var i = 0; i < items; i++) {
            elements[i] = elmIndex;

            jQuery(container).append(visuals[elmIndex]);

            var contains = true;
            while (contains) {
                elmIndex = Math.floor(Math.random() * 14);
                contains = false;
                for (var j = 0; j <= i; j++) {
                    if (elements[j] == elmIndex)
                        contains = true;
                }
            }
        }

        $(".slideshow").empty();
        $(".slideshow").append(container);

        $('.slideShowItems')
        .after('<div class="slideshowNavigation">')
        .cycle({
            fx: 'fade',
            speed: 1000,
            timeout: 4000,
            pager: '.slideshowNavigation'
        });


        $('.slideshowNavigation a').empty();

        for (var i = 0; i < items; i++) {
            jQuery($('.slideshowNavigation a')[i]).append("<img src=\"/portaldata/1/images/blank.gif\" />");
        }
    }
}

/* KeyVisual Link */
var stop = null;
$(document).ready(function() {

    if ($(".keyVisualBtn").length > 0) {
        setTimeout(Decrease, 3000);
    }

});

function Decrease() {
    if (stop != "stop") {
        var le = $(".keyVisualBtn img").css("left");

        le = parseInt(le);

        $(".keyVisualBtn img").animate({
            width: 29 + 'px',
            height: 28 + 'px',
            left: le + 29 + 'px'
        }, 150);
        setTimeout(Increase, 1000);
    }
}

function Increase() {
    if (stop != "stop") {
        var le = $(".keyVisualBtn img").css("left");
        le = parseInt(le);
        $(".keyVisualBtn img").animate({
            width: 58 + 'px',
            height: 55 + 'px',
            left: le - 29 + 'px'
        }, 150);
        setTimeout(Decrease, 3000);
    }
}

function OnMouseOverPointer() {
    stop = "stop";
    var w = $(".keyVisualBtn img").css("width");

    if (w != "58px") {
        var le = $(".keyVisualBtn img").css("left");
        le = parseInt(le);
        $(".keyVisualBtn img").animate({
            width: 58 + 'px',
            height: 55 + 'px',
            left: le - 29 + 'px'
        }, 150);
    }
}

function OnMouseOutPointer() {
    stop = null;
    setTimeout(Decrease, 3000);
}

