/****
 *
 * (c) Avinatal media solutions
 *
 * Aviantal.de - main.js
 * based on $fx animation library
 *
 * */

function $rand(min, max)
{
    return(min + parseInt(Math.random() * ( max-min+1 )));
}




$(document).ready(function()
{
    var isOpened = false;

    /**** needed functions ****/
    function openPage(page)
    {
        $('#vertical-align-fix').stop().animate({minHeight: 299, marginTop: -299});
        $('div.main').stop().animate({height: 402}, 800, 'swing');
        $('#page > div.bottom, #page > div.top').css('border', 0);
        loadPage(page);
    }
    function closePage()
    {
        $('#vertical-align-fix').stop().animate({minHeight: 99, marginTop: -99}, 800, 'swing');
        $('div.main').stop().animate({height: 0});
        $('#page > div.bottom').css('border-top', '1px solid #cccccc');
        $('#page > div.top').css('border-bottom', '1px solid #cccccc');
    }
    function loadPage(page)
    {
        if(page)
        {
            $('#content * ').stop().fadeOut(400);

            $.getJSON(page+'/json', function(data){

                /**** cnt loaded ****/
                if(data.title)
                {
                    var title = document.title;
                    document.title = data.title+' '+title.substr(title.indexOf('::'));
                }

                for(var id in data)
                {
                    $('#'+id).html(data[id]);
                }

                var mr = /(.*?)home(.*?)/i;
                if(page.match(mr) != null)
                    __initScroller();
                else
                    __destroyScroller();

               /**** fade content in ****/
                $('#content > *:not(.info) > *:not(.info)').stop().css('opacity', 0).each(function(i)
                {
                    $(this).fadeTo(256*(i+1), 1);
                });

                __initUls();
            });
        }
    }

    var __$prevLi;
    function __initUls()
    {
        $("ul span.info").css('visibility', 'hidden');
        var __show = function()
        {
            $('span.info', this).stop().css({
                                        visibility: 'visible',
                                        opacity: 0
                                     }).fadeTo(400, 1);
        };
        var __hide = function()
        {
            $('span.info', this).stop().fadeTo(256, 0, function(){
                $(this).css('visibility', 'hidden');
            });
        }

        /**** show info panes ****/
        $('#content ul li').hover(__show, __hide);

        $('#content ul li').click(function()
        {
            $(this).addClass('active');
            if(this == __$prevLi)
            {
                $(this).removeClass('active');
                $('#content ul li').hover(__show, __hide);
                __$prevLi = null;
            }
            else
            {
                $(__$prevLi).removeClass('active');
                __hide.call(__$prevLi);
                __$prevLi = this;
                $('#content ul li').unbind('mouseenter mouseleave');
                __show.call(this);
            }
        });
    }

    function __resetNav0()
    {
        $('#nav0 li.active a').stop().animate({paddingTop: 0}, 128);

        $('#nav0 li').removeClass('active');
        $('#nav0 a').unbind('mouseenter mouseleave');

        $('#nav0 a').hover(function(){
            $(this).stop().animate({paddingTop: 4}, 256, 'swing');
        }, function(){
            $(this).stop().animate({paddingTop: 0}, 128);
        });
    }
    function __makeActive(elem)
    {
        if(elem.parentNode.id == 'subnav')
        {
            $('#nav0 a.'+$(elem).attr('class')).parent().addClass('active');
            $('#nav0 a.'+$(elem).attr('class')).stop().animate({paddingTop: 4},
                                                               256, 'swing');
        }
        else
        {
            $(elem).unbind('mouseenter mouseleave');
            $(elem.parentNode).addClass('active');
        }
    }


    /****
     *
     * set event handlers
     *
     * */

    $('a.logo').click(function(event)
    {
        if(!isOpened)
        {
            isOpened = true;
            openPage();
        }
        else
        {
            isOpened = false;
            closePage();
        }

        return false;
    });

    $('.bottom a, #nav0 a').click(function(){

        __resetNav0();
        __makeActive(this);

        if(!isOpened)
        {
            isOpened = true;
            openPage(this.href);
        }
        else
        {
            loadPage(this.href);
        }

        return false;
    });

    $('#nav0 a').css({paddingTop: 0});
    var __$ = $('#nav0 li.active a');
    __resetNav0();
    window.setTimeout(function(){__$.css({paddingTop: '4px'}).parent().addClass('active');}, 400);


    /**** home scroller ****/
    var _$numSteps = 0;
    var _$currentStep = 0;
    var _$scrollTimer = null;

    function __initScroller()
    {
        __destroyScroller();
        $("#scroller0 a[rel^='prettyPhoto']").prettyPhoto();
        _$numSteps = $("#scroller0 > div.items > div.item").size()-1;
        _$currentStep = 0;
        _$scrollTimer = window.setInterval(function(){$("#scroller0-arrow-next").click()}, 4096);

        $("#scroller0-arrow-next").click(function(){

            if(_$currentStep < _$numSteps)
                _$currentStep++;
            else
                _$currentStep = 0;

            $("#scroller0 > div.items").stop().animate({

                top: (-1*_$currentStep*148)+'px'
            }, {queue:false, duration:500});
        });
        $("#scroller0-arrow-prev").click(function(){

            if(_$currentStep > 0)
                _$currentStep--;
            else
                _$currentStep = _$numSteps;

            $("#scroller0 > div.items").stop().animate({

                top: (-1*_$currentStep*148)+'px'
            }, {queue:false, duration:500});
        });
    }

    function __destroyScroller()
    {
        $('.pp_overlay, .ppt, .pp_pic_holder').remove();
        window.clearInterval(_$scrollTimer);
    }
    __initScroller();
    __initUls();


    /****
     *
     * sparks engine
     *
     * - create sparks and animate them
     *
     * */

    var basePath = tplURL+'images/sparks/';
    var sparks = ['01.png', '02.png', '03.png', '04.png', '05.png', '06.png', '07.png'];
    var numSparks = 28;

    var imgs = [];

    var doc = window.document;
    var cont = doc.getElementById('main');

    var c = 0;

    for(var i=numSparks; i--;)
    {
        imgs[imgs.length] = doc.createElement('img');
        imgs[imgs.length-1].src = basePath+sparks[c];
        imgs[imgs.length-1].id = 'spark-'+i;
        imgs[imgs.length-1].className = "spark";
        imgs[imgs.length-1].style.left = $rand(0, 980)+'px';
        imgs[imgs.length-1].style.top = $rand(0, 400)+'px';
        cont.appendChild(imgs[imgs.length-1]);

        c++;
        if(c>=sparks.length)
            c = 0;
    }

    for(var i=numSparks; i--;)
    {
        _reset.call(imgs[i]);
    }

    function _reset()
    {
        var rF, rT, rS, fS, rD;

        var cL = parseInt(this.style.left);
        var cT = parseInt(this.style.top);

        rF = cT;
        rT = $rand(0, 400);
        rS = rF < rT ? 1 : -1;

        fS = $rand(1, 6);
        rD = ((980/fS)*31)/(rF-rT)*rS*(-1);

        var fT = cL < 480 ? 980 : 0;
        fS = fT > cL ? fS : -1*fS;


        $fx(this).fxAdd({
            type: 'left',
            from: cL,
            to: fT,
            step: fS,
            delay: 31
        }).fxAdd({
            type: 'top',
            from: cT,
            to: rT,
            step: rS,
            delay: parseInt(rD)
        }).fxRun(_reset);
    }

});


