﻿jQuery(function($) {
    initializeBackButton();
    initializeSingleFormWorkaround();
    initializeBreadcrumb();
    initializeColorBox();
//    initializeRotatingMenuTeaser();
});


function initializeBackButton() {
    jQuery(".backButtonContainer a").click(function() {
        window.history.go(-1);
        return false;
    });
}


function initializeSingleFormWorkaround() {
    jQuery(".formSection").each(function(index, form) {
        jQuery(this)
            .bind("keypress", function(event) {
                if (event.which == 13) {
                    event.preventDefault();
                    event.stopPropagation();
                    jQuery(form).find(".formSubmit").click();
                }
            });
    });
}


function initializeBreadcrumb() {
    var breadcrumb = jQuery(".topBreadcrumb");
    if (breadcrumb != null) {
        var children = breadcrumb.children();
        if (children.length == 0 || (children.length == 1 && children.html().length == 0)) {
            breadcrumb.css("display", "none");
        }
    }
}


function initializeColorBox() {
    jQuery("a[rel='lyteframe']")
        .removeAttr("title")
        .removeAttr("rel")
        .addClass("colorbox");

    jQuery("a.colorbox").colorbox({
        iframe: true,
        innerWidth: 980,
        innerHeight: 655,
        opacity: 0.6
    });
}


window.addEvent('domready', function() {
    initializeRotatingMenuTeaser();
});


function Common_mouseover()
{
    this.addClass('hover');
}


function Common_mouseout()
{
    this.removeClass('hover');
}


function sendAndGetResponse(url, dataObject, callback)
{
    var querystring = Hash.toQueryString(dataObject);
    var ajax = new Request(
        {
            url: url,
            onComplete: callback
        }
    );
    ajax.send(querystring);
}


function getErrorFromResponse(response)
{
    if (response.split(":")[0] == "Error")
    {
        var error = response.split(":")[1];
        return error;
    }
    return null;
}


function preventTextBoxSubmit(event)
{
    var newEvent = new Event(event);
    if (newEvent.key == 'enter')
    {
        newEvent.stop();
        return false;
    }
    return true;
}

var timerRotatingMenuTeaser = null;
function initializeRotatingMenuTeaser() {
    // Check if the RotatingMenuTeaser is in use.
    var linkContainer = $$('.otherTeaserLinkContainer');
    if (linkContainer.length > 0) 
    {
        linkContainer = linkContainer[0];
        var links = linkContainer.getElements('li');
        links.each(function(link, i) {
        link.addEvent('click', function(event) {
                showRotatingMenuTeaser(i);
                timerRotatingMenuTeaser = $clear(timerRotatingMenuTeaser); 
            });
            link.set('opacity', 0.7);

        });

        // Hide all teasers except .currentTeaser.
        var teasers = $$('.topTeasers .column').filter(function(item) { return !item.hasClass('currentTeaser') });
        teasers.each(function(teaser) {
            teaser.set('opacity', 0.0);
        });

        timerRotatingMenuTeaser = swapRotatingMenuTeaser.periodical(3000);
    }
}

function swapRotatingMenuTeaser() {
    var nextIndex = 0;
    var teasers = $$('.topTeasers .column');
    teasers.each(function(teaser, i) {
        if (teaser.hasClass('currentTeaser')) {
            nextIndex = i + 1;
        }
    });
    if (nextIndex > teasers.length-1) {
        nextIndex = 0;
    }
    showRotatingMenuTeaser(nextIndex);
}

function showRotatingMenuTeaser(index) {
    var links = $$('.otherTeaserLinkContainer')[0].getElements('li');
    var teasers = $$('.topTeasers .column');
    if (teasers == null || teasers.length == 0) return;

    // Find current teaser.
    var oldTeaser = teasers.filter(function(item) { return item.hasClass('currentTeaser') });
    if (oldTeaser.length > 0) 
    {
        oldTeaser = oldTeaser[0];
        oldTeaser.removeClass('currentTeaser');
        fadeElement(oldTeaser, 0.0);
    }

    // Find current link.
    var oldLink = links.filter(function(item) { return item.hasClass('current') });
    if (oldLink.length > 0) 
    {
        oldLink = oldLink[0];
        oldLink.removeClass('current');
    }

    // Set new current.
    teasers[index].addClass('currentTeaser');
    links[index].addClass('current');
    fadeElement(teasers[index], 1.0);
}

function fadeElement(element, toValue) {
    if (Browser.Engine.trident4) {
        element.set('opacity', toValue);
    }
    else {
        element.set('tween', { duration: 1000 });
        element.tween('opacity', toValue);
    }
}
