/*
             \|/
            .-*-
           / /|\
          _L_
        ,"   ".
    (\ /  O O  \ /)
     \|    _    |/
       \  (_)  /
       _/.___,\_
     (_/ doom  \_)
         ready, the end is near
*/

    var doomready = new Object();
    
    doomready = {
        add: function(fn) {
            if (doomready.loaded) return fn();
            var observers = doomready.observers;
            if (!observers) observers = doomready.observers = [];
            observers[observers.length] = fn; // Arraypush is not supported by Mac IE 5
            if (doomready.callback) return;
            doomready.callback = function() {
                if (doomready.loaded) return;
                doomready.loaded = true;
                if (doomready.timer) {
                    clearInterval(doomready.timer);
                    doomready.timer = null;
                }
                var observers = doomready.observers;
                for (var i = 0, length = observers.length; i < length; i++) {
                    var fn = observers[i];
                    observers[i] = null;
                    fn(); // make 'this' as window
                }
                doomready.callback = doomready.observers = null;
            };
    
            var ie = !!(window.attachEvent && !window.opera);
            var webkit = navigator.userAgent.indexOf('AppleWebKit/') > -1;
            if (document.readyState && webkit) { // Apple WebKit (Safari, OmniWeb, ...)
                doomready.timer = setInterval(function() {
                    var state = document.readyState;
                    if (state == 'loaded' || state == 'complete') {
                        doomready.callback();
                    }
                }, 50);
            }
            else if (document.readyState && ie) { // Windows IE
                var src = (window.location.protocol == 'https:') ? '://0' : 'javascript:void(0)';
                document.write(
                    '<script type="text/javascript" defer="defer" src="' + src + '" ' +
                    'onreadystatechange="if (this.readyState == \'complete\') doomready.callback();"' +
                    '><\/script>');
            }
            else {
                if (window.addEventListener) { // for Mozilla browsers, Opera 9
                    document.addEventListener("DOMContentLoaded", doomready.callback, false);
                    window.addEventListener("load", doomready.callback, false); // Fail safe
                }
                else if (window.attachEvent) {
                window.attachEvent('onload', doomready.callback);
                }
                else { // Legacy browsers (e.g. Mac IE 5)
                    var fn = window.onload;
                    window.onload = function() {
                        doomready.callback();
                        if (fn) fn();
                    }
                }
            }
        }
    }

/*-- spam protection --*/

    function getAdr(prefix, postfix, text) {
        document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
    }

/*-- add/remove class --*/

    function addClass(obj, newclass) {
        if(obj.className.indexOf(newclass) == -1)
            obj.className += " " + newclass;
    }
    
    function removeClass(obj, oldclass) {
        var classes = obj.className.split(' ');
        for(i=0; i<classes.length; i++) {
            if(classes[i].indexOf(oldclass) > -1)
                classes[i] = "";
        }
        obj.className = classes.join(' ');
    }

/*-- getElementsByClassName --*/

    function getElementsByClassName(strClass, strTag, objContElm) {
        strTag = strTag || "*";
        objContElm = objContElm || document;
        var objColl = objContElm.getElementsByTagName(strTag);
        if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
        var arr = new Array();
        var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
        var arrClass = strClass.split(delim);
        for (var i = 0, j = objColl.length; i < j; i++) {
            var arrObjClass = objColl[i].className.split(' ');
            if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
            var c = 0;
            comparisonLoop:
            for (var k = 0, l = arrObjClass.length; k < l; k++) {
                for (var m = 0, n = arrClass.length; m < n; m++) {
                    if (arrClass[m] == arrObjClass[k]) c++;
                    if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
                        arr.push(objColl[i]);
                        break comparisonLoop;
                    }
                }
            }
        }
        return arr;
    }

/*-- validform --*/

    var custom_required_text = new Object();
    function init_forms() {
        var content = document.getElementById('content');
        if(content) {
            var forms = content.getElementsByTagName('form');
            for(f=0; f<forms.length; f++) {
                init_form(forms[f]);
            }
        }
        var email_empfaenger = document.getElementById('email_empfaenger');
        if(email_empfaenger)
            email_empfaenger.value = email_empfaenger.value.replace('+at+', '@');
    }

    function init_form(form) {
        var required = getElementsByClassName('required', '*', form);
        for(i=0; i<required.length; i++) {
            required[i].onblur = function() {
                validate(this);
            }
        }
        form.onsubmit = function() {
            var doSubmit = true;
            var list = new Array();
            var required = getElementsByClassName('required', '*', this);
            for(v=0; v<required.length; v++) {
                if(! validate(required[v])) {
                    doSubmit = false;
                    list.push(required[v]);
                }
            }
            var checkboxeles = getElementsByClassName('required_checkbox', '*', this);
            for(i=0; i<checkboxeles.length; i++) {
                valid = false;
                checkboxes = checkboxeles[i].getElementsByTagName('input');
                for(c=0; c<checkboxes.length; c++) {
                    if(checkboxes[c].type == 'checkbox') {
                        checkboxeles[i].name = checkboxes[c].name; // we need the name to check for custom_required_texts
                        if(checkboxes[c].checked)
                            valid = true;
                    }
                }
    
                if(valid == false) {
                    doSubmit = false;
                    set_msg(checkboxeles[i], label_valid_checkbox);
                    list.push(checkboxeles[i]);
                } 
                else {
                    clear_msg(checkboxeles[i]);
                }
            }
            var radioeles = getElementsByClassName('required_radio', '*', this);
            for(i=0; i<radioeles.length; i++) {
                valid = false;
                radios = radioeles[i].getElementsByTagName('input');
                for(c=0; c<radios.length; c++) {
                    if(radios[c].type == 'radio') {
                        radioeles[i].name = radios[c].name; // we need the name to check for custom_required_texts
                        if(radios[c].checked)
                            valid = true;
                    }
                }
    
                if(valid == false) {
                    doSubmit = false;
                    set_msg(radioeles[i], label_valid_radio);
                    list.push(radioeles[i]);
                } 
                else {
                    clear_msg(radioeles[i]);
                }
            }

        if(doSubmit == false)
            show_error(list);
            return doSubmit;
        }
    }

    function validate(el) {
        var valid = true;
        clear_msg(el);
        switch(el.type) {
            case 'text':
            case 'textarea':
            case 'select-one':
                if(el.value != '') {
                    if(el.className.indexOf('email') > -1) {
                        var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
                        if(el.value.toUpperCase().match(regEmail)){
                            valid = true;
                        }
                        else {
                            valid = false;
                            set_msg(el, label_valid_email);
                        }
                    }
                    if(el.className.indexOf('number') > -1) {
                        if(el.value == Math.round(el.value)) {
                            valid = true;
                        }
                        else {
                            valid = false;
                            set_msg(el, label_valid_postal_code);
                        }
                    }
                }
                else {
                    valid = false;
                    set_msg(el);
                }
                break;
        }
    
        return valid;
    }

    function set_msg(el, msg) {
        if(msg == undefined) {
            msg = el.parentNode.getElementsByTagName('span')[0].innerHTML.replace(' *', '') + ' ' + label_valid_required;
        }

        if(custom_required_text[el.name])
            msg = custom_required_text[el.name];

        el.errorMessage = msg;
        if(el.parentNode.className.indexOf('error') == -1)
            el.parentNode.className += ' error';
    }

    function clear_msg(el) {
        var classes = el.parentNode.className.split(' ');
        for(i=0; i<classes.length; i++) {
            if(classes[i].indexOf('error') > -1)
                classes[i] = "";
        }
        el.parentNode.className = classes.join(' ');
        el.errorMessage = "";
    }

    function show_error(list) {
        errorlist = '<ul>';
        for(i=0; i<list.length; i++) {
            el = list[i];
            if(el.errorMessage != undefined)
                errorMessage = el.errorMessage;
            else
                var errorMessage = el.parentNode.getElementsByTagName('span')[0].innerHTML.replace(' *', '') + ' ' + label_valid_required;
    
            errorlist += '<li>' + errorMessage + '</li>';
        };
        errorlist += '</ul>';
    
        var validationMessage = document.getElementById('validationMessage');
        validationMessage.innerHTML = '<h2 title="' + label_valid_error + '">' + label_valid_error + '</h2>' + errorlist;
        document.location.href = '#validationMessage';
    }

    doomready.add(function() {
        init_forms();
    });

/*-- alternativ x-menu --*/

    var navImagesNormal = new Array();
    var navImagesHover = new Array();
    function init_nav() {
        if(document.getElementById('nav')) {
            var nav = document.getElementById('nav');
            var lis = nav.getElementsByTagName('li');
            for(var i = 0; i < lis.length; i++) {
                if(lis[i].parentNode.id == 'nav') {
                    lis[i].number = i;
                    lis[i].onmouseover = function() {
                        addClass(this, 'hover');
                        var img = this.getElementsByTagName('img');
                        if(img[0] && navImagesHover[this.number])
                            img[0].src = navImagesHover[this.number];
                    }
                    lis[i].onmouseout = function() {
                        removeClass(this, 'hover');
                        var img = this.getElementsByTagName('img');
                        if(img[0] && navImagesNormal[this.number])
                            img[0].src = navImagesNormal[this.number];
                    }
                    var img = lis[i].getElementsByTagName('img');
                    if(img[0] && img[0].onmouseover) {
                        navImagesNormal[i] = img[0].src;
                        img[0].onmouseover();
                        navImagesHover[i] = img[0].src;
                        img[0].onmouseout();
                        img[0].onmouseover = function() {};
                        img[0].onmouseout = function() {};
                    }
                }
            }
        }
    }

    doomready.add(function() {
        init_nav();
    });


/*-- cloud movement --*/

    var moved = 0;
    function move_clouds() {
        var clouds = document.getElementById('clouds');
        var cloud = clouds.getElementsByTagName('img')[0];
        var cloud2 = clouds.getElementsByTagName('img')[1];
        moved += 0.2;
        if(moved >= cloud.offsetWidth)
            moved = 0;
        cloud.style.marginLeft = (0-moved) + "px";
        cloud2.style.marginLeft = (cloud.offsetWidth - moved) + "px";
    }

    doomready.add(function() {
        move_clouds();
        setInterval('move_clouds()', 10);
    });



/* Atikon Video Playlist */

var video_properties = new Array();
var video_current_scroll = 0;
var video_show = 0;
var video_moving = false;
$(function() {
    var videos = $('.playlist_video');
    if(videos.length) {
        videos.each(function(i) {
            var el = this;
            var spans = el.getElementsByTagName('span');
            var properties = new Object();
            $(spans).each(function(o) {
                properties[this.title] = this.innerHTML;
            });
            video_properties.push(properties);
            $('.playlist_thumbnails')[0].innerHTML += '<div><a href="#"><img src="' + properties['thumbnail'] + '" alt="" /></a><span class="playlist_thumbnail_title">' + properties['thumbnail_title'] + '</span><span class="playlist_thumbnail_subtitle">' + properties['thumbnail_subtitle'] + '</span></div>';
        });

        $($('.playlist_arrow_right')[0]).click(function(event) {
            event.preventDefault();
            if(video_moving == false) {
                video_moving = true;
                video_current_scroll++;
                if(video_current_scroll > video_properties.length-2) {
                    video_current_scroll = 0;
                }
                var thumbwidth = $('.playlist_thumbnails a')[0].offsetWidth;
                $($('.playlist_thumbnails')[0]).animate({
                        marginLeft: (0-(thumbwidth * video_current_scroll)) + 'px'
                    }, 500, function() {
                        video_moving = false;
                    }
                );
            }
        });

        $($('.playlist_arrow_left')[0]).click(function(event) {
            event.preventDefault();
            if(video_moving == false) {
                video_moving = true;
                video_current_scroll--;
                if(video_current_scroll < 0) {
                    video_current_scroll = video_properties.length-2;
                }
                var thumbwidth = $('.playlist_thumbnails a')[0].offsetWidth;
                $($('.playlist_thumbnails')[0]).animate({
                        marginLeft: (0-(thumbwidth * video_current_scroll)) + 'px'
                    }, 500, function() {
                        video_moving = false;
                    }
                );
            }
        });

        $('.playlist_close').click(function(event) {
            event.preventDefault();
            $('#playlist_player').animate({
                    height: '0px'
                }, 500, function() {
                    $('#playlist_video_container').html('<div id="playlist_video_display"></div>');
                }
            );
            $('.playlist_close').animate({
                    opacity: 0
                }, 500, function() {
                }
            );
        });

        $('.playlist_thumbnails div').each(function(t) {
            var thumb = this;
            thumb.showvideo = t;
            $(thumb).click(function(event) {
                event.preventDefault();
                video_show = this.showvideo;

                $('.playlist_close').animate({
                        opacity: 1
                    }, 500, function() {
                    }
                );

                $('#playlist_player').animate({
                        height: (332) + 'px'
                    }, 500, function() {
                        var flashvars = {};
                        flashvars.minutes = video_properties[video_show]['minutes'];
                        flashvars.seconds = video_properties[video_show]['seconds'];
                        flashvars.movieurl = video_properties[video_show]['movieurl'];
                        if(video_properties[video_show]['logo'])
                            flashvars.logourl = video_properties[video_show]['logo'];
                        if(video_properties[video_show]['videotitle'])
                            flashvars.videotitle = video_properties[video_show]['videotitle'];
                        if(video_properties[video_show]['autoplay'])
                            flashvars.autoplay = video_properties[video_show]['autoplay'];
                        if(video_properties[video_show]['videolink'])
                            flashvars.videolink = video_properties[video_show]['videolink'];

                        var params = {};
                        params.allowfullscreen = "true";
                        params.wmode = "transparent";

                        var attributes = {};
                        swfobject.embedSWF($('#atikon_flash_link')[0].href, 'playlist_video_display', 535, 300, "9.0.0", '', flashvars, params, attributes);
                    }
                );
            });
        });

        $('.playlist_close').css({opacity: 0});
    }
});



