﻿function convertlinks()
{
    $('a').each(function (index)
    {
        var hr = this.href;
        hr = hr.replace(/(\/de|\/en|\/ko|\/es)\/Presse_und_Aktuelles\/News\/(.*)/i, "$1/Presse_und_Aktuelles/Aktuelle_Nachrichten.aspx#$1/Presse_und_Aktuelles/News/$2");
        hr = hr.replace(/(\/de|\/en|\/ko|\/es)\/Presse_und_Aktuelles\/Pressemitteilungen\/(.*)/i, "$1/Presse_und_Aktuelles/Pressemitteilungen.aspx#$1/Presse_und_Aktuelles/Pressemitteilungen/$2");
        this.href = hr;
        /*    /de/Presse_und_Aktuelles/News/Hessische_Minister_interessieren_sich_fuer_EMA_Archive_Appliance.aspx
        ==>/de/Presse_und_Aktuelles/Aktuelle_Nachrichten.aspx#/de/Presse_und_Aktuelles/News/Hessische_Minister_interessieren_sich_fuer_EMA_Archive_Appliance.aspx*/
    });
}

$(document).ready(function ()
{

    // Tabs 
    $('div.tabs').tabs({
        event: 'mouseover'
    });

    // menu accordion
    $('#menu').accordion({
        autoheight: false,
        header: "div.folder",
        animated: 'easeslide'
    });

    $('a[modal=1]').each(function (index)
    {
        var url = this.href;
        $(this).click(function ()
        {
            $.ajax(
            {
                type: "GET",
                url: this.href.indexOf("?") != -1 ? (this.href + "&ajax=1&page=" + escape(location.href)) : (this.href + "?ajax=1&page=" + escape(location.href)),
                cache: false,
                success: function (data)
                {
                    $('<div id="formreplace">' + data + "</div>").modal(
                    {
                        closeHTML: '',
                        autoResize: true,
                        onShow: function (dlg)
                        {
                            $(dlg.container).css('height', 'auto');
                            $(window).trigger('resize.simplemodal');
                        }
                    });
                }
            });
            return false;
        });
    });

    convertlinks();


    var oldTreeView_ProcessNodeData = window.TreeView_ProcessNodeData;
    window.TreeView_ProcessNodeData = function ()
    {
        oldTreeView_ProcessNodeData.apply(this, arguments);
        convertlinks();
    }

    if (location.pathname.match(/(\/de|\/en|\/ko|\/es)\/Presse_und_Aktuelles\/News\/(.*)/i))
        location.href = '/Presse_und_Aktuelles/Aktuelle_Nachrichten.aspx#' + location.pathname;
    if (location.pathname.match(/(\/de|\/en|\/ko|\/es)\/Presse_und_Aktuelles\/Pressemitteilungen\/(.*)/i))
        location.href = '/Presse_und_Aktuelles/Pressemitteilungen.aspx#' + location.pathname;

    $("#loading").bind("ajaxStart", function ()
    {
        var height = $(window).height();
        var width = $(window).width();

        var boxheight = $(this).height();
        var boxwidth = $(this).width();

        var x = parseInt(width / 2) - parseInt(boxwidth / 2);
        var y = parseInt(height / 2) - parseInt(boxheight / 2);

        $(this).css("top", y);
        $(this).css("left", x);

        $(this).show();
    }).bind("ajaxStop", function ()
    {
        $(this).hide();
    });

    $("#ajaxerror").ajaxError(function (event, request, settings)
    {
        var elem = $(this).append("<li>Error requesting page " + settings.url + "</li>");
        $(this).show();
        setTimeout(function ()
        {
            elem.fadeOut('slow', function () { $(this).remove(); });
        }, 5000);
    });

    $("div.bulletbox, div.contact, div.highlights, div.checkbox").each(function (i)
    {
        var height = $(this).height();
        height += 32; // add padding-top and padding-bottom

        if (height > 1000)
            height = 1000;
        height = Math.floor((height + 49) / 50) * 50;
        $(this).css("background-image", "url(/res/verlauf_d-h-" + (height > 1000 ? "" : "0") + (height > 100 ? "" : "0") + height + ".png)");


    });

    $('div.accordion div.folder').removeClass('selected');

    if ($('#news_list'))
    {
        $('<div class="folderbranch"><div class="item"><p><%= Misc.T("NEW_LOADING","Inhalt wird nachgeladen ...","Loading content...") %></p></div></div>').
            insertAfter('div#news_list div.folder');
        //$('div#downloads div.folderbranch:first').toShow.load("ajax.htm #contenttext");
        $('#news_list').accordion({
            autoheight: false,
            alwaysOpen: false,
            active: false,
            animated: 'easeslide',
            supportAjax: true,
            navigation: true
        });
        if (location.hash)
            $('#news_list').activate(":has(a[realhref=\"" + location.hash.substring(1).replace("\"", "") + "\"])");
    }

    if ($('#press_releases'))
    {
        $('<div class="folderbranch"><div class="item"><p><%= Misc.T("NEW_LOADING","Inhalt wird nachgeladen ...","Loading content...") %></p></div></div>').insertAfter('div#press_releases div.folder');
        $('#press_releases').accordion({
            autoheight: false,
            alwaysOpen: false,
            active: false,
            animated: 'easeslide',
            supportAjax: true,
            navigation: true
        });
        if (location.hash)
            $('#press_releases').activate(":has(a[realhref=\"" + location.hash.substring(1).replace("\"", "") + "\"])");
    }

    $('#press_review').accordion({
        autoheight: false,
        alwaysOpen: false,
        active: false,
        animated: 'easeslide',
        navigation: true
    });

    $('#events').accordion({
        autoheight: false,
        alwaysOpen: false,
        active: false,
        animated: 'easeslide',
        navigation: true
    });

    $('#downloads').accordion({
        alwaysOpen: false,
        active: false,
        animated: 'easeslide'/*,
        complete:change: function(event, ui) 
        {
           //highlight .highlightitem 
        }*/
    });
    //Open item or area
    var index = $('#downloads > div.folderbranch:has(.highlightitem)').index('#downloads > div.folderbranch');
    if (index >= 0)
        $('#downloads').activate(index);


    //$("table#Login1 table tr:first").css("display", "none");

    //$("table#Login1 table tr:last td").removeAttr("colspan");
    $("table#Login1 table tr:last td").removeAttr("align");

    $("table#Login1 table tr:last td input").addClass("button").css("margin-top", "2px");

    $("table#Login1 table tr:last").prepend("<td></td>");

    var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
    $(document).keydown(function (e)
    {
        kkeys.push(e.keyCode);
        if (kkeys.toString().indexOf(konami) >= 0)
        {
            $(document).unbind('keydown', arguments.callee);
            $.getScript('/js/asteroids.js');
        }
    });
});

function ErrorPlacer(error, element)
{
    error.appendTo(element.parent("td").next("td"));
}

function CloseMe()
{
    setTimeout(function ()
    {
        $.modal.impl.o.onClose = function (dialog)
        {
            dialog.data.fadeOut('slow');
            dialog.overlay.fadeOut('slow', function ()
            {
                $.modal.close();
            });
        };
        $.modal.close();
    }, 5000);
}

function SubmitHandler(form)
{
    $(form).ajaxSubmit({
        target: "#formreplace",
        success: function (responseText, statusText, xhr, $form)
        {
            $('#simplemodal-container').css('height', 'auto');
            $('#simplemodal-container').css('width', 'auto');
            $(window).trigger('resize.simplemodal');
            //$.modal.update();
        }
    });
    $("#formreplace input[name=submit]").attr('disabled', 'disabled');
    /*success: function (responseText, statusText, xhr, $form) {
    $.modal.close();
    //There is a 10ms opera workaround to close modal, so do a 20ms workaround to show the next dialog
    setTimeout(function () {
    $(responseText).modal({
    closeHTML: ''
    });
    }, 20);
    }*/
}

function ShowErrors(errorMap, errorList)
{
    this.defaultShowErrors();
    $('#simplemodal-container').css('height', 'auto');
    $('#simplemodal-container').css('width', 'auto');
    $(window).trigger('resize.simplemodal');
}

function toggleList(thisobj)
{
    var area_id = "#" + thisobj.id + "_area";
    var showstyle = $("#listheader").css("display");

    if ($(area_id).css("display") == "none")
    {
        $("#" + thisobj.id + "_image").attr("src", "/res/expanded.gif");
        $(area_id).css("display", showstyle);
    }
    else
    {
        $("#" + thisobj.id + "_image").attr("src", "/res/collapsed.gif");
        $(area_id).css("display", "none");
    }
}

function textareafocus(thisobj)
{
    if ($("#" + thisobj.id).attr('class') == 'unfocused')
    {
    	thisobj.value = '';
        $("#" + thisobj.id).removeClass("unfocused");
        $("#" + thisobj.id).addClass("focused");
    }
}

function textareablur(thisobj, labeltext)
{
    if (thisobj.value == '')
    {
        $("#" + thisobj.id).removeClass("focused");
        $("#" + thisobj.id).addClass("unfocused");
        thisobj.value = labeltext;
    }
}

function buttonOnClick(thisobj, defaulttext)
{
	// id + "_textarea"
	var id = thisobj.id;
	var id_prefix = id.substring(0, id.indexOf("_submit"));

	var textarea = $("#" + id_prefix + "_textarea").val();
	if (textarea == defaulttext || textarea == "")
		return false;
	else
		return true;
}


