$(document).ready(function()
{
    // Initialize variables
    var interval = '';

    // MAIN MENU

    // Display current (mouseover) submenu
    $('#main li').mouseover(function()
    {
        $(this).find('ul').show();
        $(this).css('background', '#fbfbfb');
        $(this).find('li').find('a').css('width', $(this).width()-16);
    });

    // Hide current (mouseout) submenu
    $('#main li').mouseout(function()
    {
        $(this).find('ul').hide();
        $(this).css('background', 'none');
    });

    // SUBPAGE MENU

    $('#subpage .first-level ul').each(function(n)
    {
        if ($(this).parent().hasClass('active')) {
            switch(n)
            {
                case 0:  $('#subpage .first-level').css('left', '156px'); break;
                case 1:  $('#subpage .first-level').css('left', '313px'); break;
                case 2:  $('#subpage .first-level').css('left', '465px'); break;
                case 3:  $('#subpage .first-level').css('left', '601px'); break;
                case 4:  $('#subpage .first-level').css('left', '750px'); break;
                default: $('#subpage .first-level').css('left', '156px');
            }
        }
    });

    // NEWS LIST

    if ($('.news-list-wrapper-home').length > 0)
    {
        // Display current tt_news content
        $('.news-list-item').mouseover(function()
        {
            var yPos = $(this).offset().top - $('.news-list-wrapper-home').offset().top - 85;
            $(this).find('.news-list-content').css('top', yPos);
            $(this).find('.news-list-content').show();
        });

        // Hide current tt_news content
        $('.news-list-item').mouseout(function()
        {
            $(this).find('.news-list-content').hide();
        });
    }

    // MODAL DIALOG

    // Create and open recommend modal dialog
    $(function()
    {
        $('#recommend-dialog').dialog(
        {
            modal: true,
            show: 'blind',
            autoOpen: false,
            closeText: 'Schließen'
        });

        $('#recommend-link').click(function()
        {
            $( "#recommend-dialog" ).dialog('open');
            return false;
        });
    });

    // HREF WRAPPER

    // PDF download in _blank window
    $('#export-pdf').click(function()
    {
        $(this).parent().parent().attr('target', '_blank');
    });

    // RSS-Feed in _blank window
    $('#service .last a').click(function()
    {
        $(this).attr('target', '_blank');
    });

    // var href = new Array('http://www.pz-online.de/', 'http://www.obs-portal.de/', 'http://www.duon-portal.de/');
    // $('#bodyID_1074 .spalte2-8 .csc-textpic-imagerow').each(function(n)
    // {
        // $(this).find('img').wrap('<a href="'+href[n]+'" target="_blank">');
    // });

    // OVERLAYS FOR TABLES

    // Display overlay graphic
    $('tr.content').mouseover(function(e)
    {
        if (!($('#bodyID_1096').length)) {

            pos = $(this).position();
            offset = $(this).offset();

            forbidden = $('.td-image-overlay').width()
            ? $('.td-image-overlay').width()
            : -1;

            if (e.pageX-offset.left > forbidden) {
                $('#chart').css('top', pos.top+25);
                $('#chart').css('display', 'block');
            } else {
                $('#chart').css('display', 'none');
            }

            id = $(this).attr('id');
            tempSplit = id.split('_');
            curTable = tempSplit[0]+'_'+tempSplit[1];
            $('#chart .chart-content img').attr('src', 'fileadmin/pw_de/system/image/'+curTable+'/'+id+'.png');

        }

        clearInterval(interval);
        $(this).css('background-color', '#f8f5f2');
    });

    // Hide overlay graphic
    $('tr.content').mouseout(function()
    {
        interval = setInterval('hideGraph()', 250);
        $(this).css('background-color', '#ffffff');
    });

    $('#chart').mouseover(function() { clearInterval(interval); });

    // Display overlay motiv
    $('.image-overlay').mouseover(function()
    {
        var imageId = $(this).attr('id');
        imageId = str_replace('.amp.', '&', imageId);
        $('#motiv .motiv-content-center').html('');

        $('#motiv .motiv-content-center').html('<img src="fileadmin/pw_de/system/image/motive/'+imageId+'" alt="" />');
        $('#motiv').css('top', ($(this).position().top-150)+'px');
        $('#motiv').css('left', '130px');
        $('#motiv').show(100);
    });

    // Hide overlay motiv
    $('.image-overlay').mouseout(function()
    {
        $('#motiv').hide();
    });

    // GRAFIK EMPFEHLEN
    var tip_url = "http://printwirkt.de/pw-empfehlen/?tipUrl=";
    var inhalt = $("p.bodytext");
    $.each(inhalt, function(index, value) {
        if(strpos($(value).html(), '###TIP###', 0) > -1){
            var article     = $(value).closest('.csc-default');
            var img_url     = escape(document.URL+'#'+$(article).attr('id'));
            // var img_url     = escape('http://dev-pw.aberratio.de/'+$(img).find('.csc-textpic-imagewrap img').attr('src'));
            var link        = '<a href="'+tip_url+img_url+'" target="_blank" class="popup">Argument empfehlen</a>';
            var inner_html  = $(value).html().replace('###TIP###', link);
            $(value).html(inner_html);
        }
    });

    // TIP-A-FRIEND
    // Initialize TIP-A-FRIEND layer
    init_tipafriend_layer();
});

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another
    //
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);    // *     returns 1: 14
    var i = (haystack + '').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function hideGraph()
{
    $('#chart').css('display', 'none');
}

var overlayAPI;

function init_tipafriend_layer()
{
    $('a.popup').click(function(){
        var breite = screen.width;
        var hoehe = screen.height;
        
        url_location = $(this).attr("href");
        tip_window = window.open(url_location, "Weiterempfehlen", "location=0,status=0,scrollbars=0,width=530,height=650");
        return false;
    });
    
//     Attach a submit handler to the form
    // $('form#tipafriend').submit(function(event)
    // {
        // event.preventDefault();
        // var $form = $(this),
            // name          = $form.find( 'input[name="TIPFORM[name]"]' ).val(),
            // email         = $form.find( 'input[name="TIPFORM[email]"]' ).val(),
            // recipient     = $form.find( 'input[name="TIPFORM[recipient]"]' ).val(),
            // message       = $form.find( 'textarea[name="TIPFORM[message]"]' ).val(),
            // tipUrl        = $form.find( 'input[name="tipUrl"]' ).val(),
            // sendTip       = $form.find( 'input[name="sendTip"]' ).val(),
            // html_message  = $form.find( 'input[name="TIPFORM[html_message]"]' ).val(),
            // url           = $form.attr( 'action' );

        // $.post( url, { 'TIPFORM[name]': name, 'TIPFORM[email]': email, 'TIPFORM[recipient]': recipient, 'TIPFORM[message]': message, 'TIPFORM[tipUrl]': tipUrl, 'sendTip': sendTip, 'html_message': html_message},
            // function(data)
            // {
                // $("#layer_content").html(data);
            // }
        // );
    // });
}

$(function()
{
    var scroll_timer;
    var displayed = false;
    var $window = $(window);
    var $message = $('#message a');
    var top = $(document.body).children(0).position().top;

    $window.scroll(function()
    {
        window.clearTimeout(scroll_timer);
        scroll_timer = window.setTimeout(function()
        {
            if ($window.scrollTop() <= top) {
                displayed = false;
                $message.fadeOut(500);
            } else if (displayed == false) {
                displayed = true;
                $message.stop(true, true).show().click(function()
                {
                    $message.fadeOut(500);
                });
            }
        }, 100);
    });
});

// str_replace works like PHP
function str_replace(search, replace, string)
{
    return string.split(search).join(replace);
}

