jQuery(function($) {
    if ($('div.box.weather').length > 0) {
        $('div.box.weather').click(function() {
            location.href = $(this).attr('name');
        });
    }

    if (!Modernizr.input.placeholder) {
        $('input.placeholder').each(function() {
            var value = $(this).attr('placeholder');
            $(this).attr('value', value);
        });
        setPlaceholderEvent();
    }
    function setPlaceholderEvent() {
        $('input.placeholder').focus(function() {
            if ($(this).attr('value') == $(this).attr('placeholder')) {
                $(this).attr('value', '');
                $(this).attr('class', 'placeholderSet');
                setPlaceholderEvent();
            }
        });
        $('input.placeholderSet').blur(function() {
            if ($(this).attr('value') == '') {
                $(this).attr('value', $(this).attr('placeholder'));
                $(this).attr('class', 'placeholder');
                setPlaceholderEvent();
            }
        });
    }

    initSponsorImages();
    initBannerImages();

    var eyecatcherImages;
    var directory = $('div.headerPics').attr('class');
    directory = directory.split(' ');
    directory = directory[1];
    $.get('/images/content/eyecatcher/getEyecatcherImages.php', {'dir':directory}, function(images) {
        eyecatcherImages = images.split(';');
        maxIndex = eyecatcherImages.length - 2;
        var i = 0;
        $('div.headerPics').html('');
        while (i <= maxIndex) {
            addNewEyecatcherImage(eyecatcherImages[i]);
            i++;
        }
        function addNewEyecatcherImage(url) {
            $('div.headerPics').append('<img src="' + url + '" alt="headerPic">');
        }

        $('div.headerPics').cycle('fade');
    });
    if ($('div.eventCountdown').length != 0) {
        $(function() {
            var austDay = new Date();
            austDay = new Date(2013, 1 - 1, 18, 10, 30);
            $('div.eventCountdown').countdown({until:austDay});
        });
    }
    if ($('#content').find('div.box').length > 0) {
        $('#content').find('div.box').each(function() {
            $(this).click(function() {
                window.location = $(this).children('div.content').children('a.more').attr('href');
            });
        });
    }
    $('#menu_level_1').children('li').each(function() {
        if ($(this).children('#menu_level_2').length > 0) {
            $(this).children('#menu_level_2').children('li').each(function() {
                if ($(this).children('ul').length > 0) {
                    $(this).children('a').append('<div class="hasSub"></div>');
                }
            });
        }
    });

    function initSponsorImages() {
        var sponsorImages;
        var maxIndex = 0;
        var index = 0;
        var delayTime = 2400;
        var arrImages;
        $.get('/images/content/sponsoren/getSponsorImages.php', '', function(images) {
            sponsorImages = images.split(';');
            maxIndex = sponsorImages.length;
            maxIndex = maxIndex - 2;
            addHTML(sponsorImages);
            nextBanner();
        });

        function nextBanner() {
            // show next picture
            $(arrImages[index]).show();

            // hide previous picture
            if (index == 0) {
                $(arrImages[maxIndex]).hide();
            } else {
                $(arrImages[index - 1]).hide();
            }

            // set next index value
            if (index == maxIndex) {
                index = 0;
            } else {
                index++;
            }

            // set timer for next banner
            setTimeout(nextBanner, delayTime);
        }

        function addHTML(images) {
            $(images).each(function() {
                if (this != "") {
                    $('div.random').append('<img src="' + this + '" alt="">');
                }
            });
            $('div.random img').hide();
            arrImages = $('div.random img').slice();
        }
    }

    function initBannerImages() {
        var bannerImages;
        var maxIndex = 0;
        var index = 0;
        var delayTime = 3000;
        var arrImages;
        var language;

        language = $('#banner').attr('class');

        $.get('/images/content/banner/getBannerImages.php', {'language':language}, function(images) {
            bannerImages = images.split(';');
            maxIndex = bannerImages.length;
            maxIndex = maxIndex - 2;
            addHTML(bannerImages);
            nextBanner();
        });

        function nextBanner() {
            // show next picture
            $(arrImages[index]).show();

            // hide previous picture
            if (index == 0) {
                $(arrImages[maxIndex]).hide();
            } else {
                $(arrImages[index - 1]).hide();
            }

            // set next index value
            if (index == maxIndex) {
                index = 0;
            } else {
                index++;
            }

            // set timer for next banner
            setTimeout(nextBanner, delayTime);
        }

        function addHTML(images) {
            $(images).each(function() {
                if (this != "") {
                    $('#banner a').append('<img border="0" src="' + this + '" alt="">');
                }
            });
            $('#banner img').hide();
            arrImages = $('#banner img').slice();
        }
    }

    if ($('a.deleteProduct').length > 0) {
        $('a.deleteProduct').click(function() {
            $(this).parents('tr').find('input.form').attr('value', 0);
            $(this).parents('form').submit();
            return false;
        });
    }
});
