/**
 * Agenturfundus Theme and custom slideshow
 * by Philipp Nolte
 * with many thanks to blackbeltmonkey.com
 */

/**
 * @package WordPress
 * @subpackage Agenturfundus_Theme
 */

function SetupMenu() {
    var menu = document.getElementById('topmenu');
    var base = 'http://www.agenturfundus.de/';
    if(menu) {
        var buttons = menu.getElementsByTagName('li');
        var buttonslength = buttons.length;
        for(var i=0; i<buttonslength; i++) {
            buttons[i].addEvent('click', function() {
                switch(this.getProperty('class')) {
                    case 'menustart':
                        window.location = base + '';
                        break;
                    case 'agenturen':
                        window.location = base + 'agenturen';
                        break;
                    case 'kategorien':
                        window.location = base + 'kategorien';
                        break;
                    case 'share':
                        var share = document.getElementById('share');
                        var sFx = new Fx.Tween(share, {duration: 'normal', link: 'chain', transition: Fx.Transitions.Bounce.easeOut});
                        if(share.name == 'open') {
                            share.name = 'closed';
                            sFx.start('height', 0);
                        } else {
                            share.name = 'open';
                            sFx.start('height', 90);
                        }
                        break;
                    case 'submit':
                        window.location = base + 'submit';
                        break;
                }
            });
        }
    }
}

function SetupSlideshow() {
    var slideshow = document.getElementById('slideshow');
    if(slideshow) {
        var buttons = document.getElementById('slideshowbuttons').getElementsByTagName('span');
        var counterspan = document.getElementById('counter');
        var children = document.getElementById('slideshowlist').getElementsByTagName('li').length;
        var counter = 0;
        counterspan.innerHTML = '1/' + children;
        buttons[0].setProperty('style', 'color:#888;cursor:default;');
        if(children < 2) { buttons[1].setProperty('style', 'color:#888;cursor:default;'); }
        for(var i=0; i<2; i++) {
            buttons[i].addEvent('click', function() {
                var offset = -340;
                if(this.getProperty('class') == 'left') {
                    offset = -1 * offset;
                    counter--;
                } else {
                    counter++;
                }
                var margin = document.getElementById('slideshowlist').style.marginTop.replace(/px/, '');
                if(margin.length > 0) margin = parseInt(margin);
                margin += offset;
                if (counter > -1 && counter < children) {
                    var myFx = new Fx.Tween(document.getElementById('slideshowlist'), {duration: 'long', link: 'chain'});
                    myFx.start('margin-top', margin);
                } else {
                    if(this.getProperty('class') == 'left') {
                        counter++;
                    } else {
                        counter--;
                    }
                }
                counterspan.innerHTML = counter+1 + '/' + children;
                if(counter < 1) {
                    buttons[0].setProperty('style', 'color:#888;cursor:default;');
                } else {
                    buttons[0].setProperty('style', 'color:#111;cursor:pointer;');
                }
                if(counter < children -1) {
                    buttons[1].setProperty('style', 'color:#111;cursor:pointer;');
                } else {
                    buttons[1].setProperty('style', 'color:#888;cursor:default;');
                }
            });
        }
    }
}

function SetupComments() {
    var commhead = document.getElementById('kommentare');
    if(commhead) {
        var commform = document.getElementById('wpcf7-f1-p7-o1');
        var url = unescape(location.href);
        if(url.match(/wpcf/)) {
            commform.style.height = '570px';
        }
        commhead.addEvent('click', function() {
            var commform = document.getElementById('wpcf7-f1-p7-o1');
            var foldspan = document.getElementById('fold');
            var cFx = new Fx.Tween(commform, {duration: 'long', link: 'chain', transition: Fx.Transitions.Bounce.easeOut});
            if(commform.name == 'open') {
                commform.name = 'closed';
                foldspan.innerHTML = 'aufklappen';
                cFx.start('height', 0);
            } else {
                commform.name = 'open';
                foldspan.innerHTML = 'zuklappen';
                cFx.start('height', 570);
            }
        });
    }
}

window.addEvent('domready', function() {
    SetupMenu();
    SetupSlideshow();
    SetupComments();
});
