/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var HIDE = 'fold';
var SHOW = HIDE;

$(function() {
    $.datepicker.regional['de'] = {
                clearText: 'löschen', clearStatus: 'aktuelles Datum löschen',
                closeText: 'schließen', closeStatus: 'ohne Änderungen schließen',
                prevText: '&#x3c;zurück', prevStatus: 'letzten Monat zeigen',
                nextText: 'Vor&#x3e;', nextStatus: 'nächsten Monat zeigen',
                currentText: 'heute', currentStatus: '',
                monthNames: ['Januar','Februar','März','April','Mai','Juni',
                'Juli','August','September','Oktober','November','Dezember'],
                monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
                'Jul','Aug','Sep','Okt','Nov','Dez'],
                monthStatus: 'anderen Monat anzeigen', yearStatus: 'anderes Jahr anzeigen',
                weekHeader: 'Wo', weekStatus: 'Woche des Monats',
                dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
                dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
                dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
                dayStatus: 'Setze DD als ersten Wochentag', dateStatus: 'Wähle D, M d',
                dateFormat: 'dd.mm.yy', firstDay: 1,
                initStatus: 'Wähle ein Datum', isRTL: false};
    $.datepicker.setDefaults($.datepicker.regional['de']);

    $('#menu a.sub_link').hover(
        function() {
            $(this).addClass('ui-state-highlight');
        }, function() {
            $(this).removeClass('ui-state-highlight');
        });
        
    $("#menu").accordion({ header: "h3",
                           autoHeight: false,
                           animated: 'easeOutBounce',
                           active: '#main_pos' });

    $('#banner').dblclick(
        function() {
            $('#banner').effect('explode', null, 1500);
        }
    );

    $('#banner, #info_box').draggable({ containment: 'parent',
                                        axis: "x",
                                        revert: true });

    /*$('.draggable').draggable({ containment: 'parent',
                                cursor: 'crosshair',
                                scroll: 'false',
                                stack: { group: '.draggable',
                                         min: 1 } });
    $('.draggable').disableSelection();*/

    $('#info_box div.slide').hover(function() {
        $(this).addClass('ui-state-focus');
    }, function() {
        $(this).removeClass('ui-state-focus');
    });

    $('#info_box div.slide').click(function() {
        var old = $('#info_box').css('top');
        $('#info_box').animate({ top: old == "-128px" ? "8px" : "-128px" }, 500, 'easeOutQuad');
        $('span.ui-icon', this).addClass( old == "-128px" ? "ui-icon-arrowthick-1-n" : "ui-icon-arrowthick-1-s" );
        $('span.ui-icon', this).removeClass( old == "-128px" ? "ui-icon-arrowthick-1-s" : "ui-icon-arrowthick-1-n" );
    });

    main_content();

    /*$('#themecolorpicker1').farbtastic('#themecolor1');
    $('#themecolorpicker2').farbtastic('#themecolor2');*/
    
    /*$('#themecolor1').each(function() {
        $(this).removeClass('ui-widget-content');
        $(this).focus(function() {
            $('#themecolorpicker1').fadeIn();
        });
        $(this).blur(function() {
            $('#themecolorpicker1').fadeOut();
            $('#menu_container').css('background-color', $(this).val());
            $('#info_bar').css('background-color', $(this).val());
            $('#footer').css('background-color', $(this).val());
        });
    });

    /*$('#themecolor2').each(function() {
        $(this).removeClass('ui-widget-content');
        $(this).focus(function() {
            $('#themecolorpicker2').fadeIn();
        });
        $(this).blur(function() {
            $('#themecolorpicker2').fadeOut();
            $('#content').css('background-color', $(this).val());
        });
    });*/

    if($('#new_location').val()) {
        var redirecttime = $('#new_secs').val();
        if(!redirecttime) redirecttime = 3;
        $('#timeleft').text('in ' + redirecttime + ' Sekunde' + (redirecttime != 1 ? 'n' : ''));
        setInterval(function() {
            if(redirecttime-- == 0) {
                location = $('#new_location').val() ;
            }
            $('#timeleft').text(redirecttime < 1 ? 'sofort' : ('in ' + redirecttime + ' Sekunde' + (redirecttime != 1 ? 'n' : '')));
        }, 1000);
    }
});

function main_content(prefix) {
    if(!prefix) prefix = '';
    
    $(prefix + ' input[type=button], ' + prefix + ' input[type=reset], ' + prefix + ' input[type=submit]').each(
        function() {
            $(this).addClass('ui-state-default');
            $(this).hover(
                function() {
                    $(this).addClass('ui-state-hover');
                }, function() {
                    $(this).removeClass('ui-state-hover');
                    $(this).removeClass('ui-state-highlight');
                }
            );
            $(this).mousedown(
                function() {
                    $(this).addClass('ui-state-highlight');
                }
            );
            $(this).mouseup(
                function() {
                    $(this).removeClass('ui-state-highlight');
                }
            );
        }
    );
    $(prefix + ' input, ' + prefix + ' textarea, ' + prefix + ' select, '/* + prefix + ' option, ' + prefix + ' optgroup'*/).each( function() {
        $(this).addClass('ui-widget-content');
        $(this).addClass('ui-corner-all');
    });

    $(prefix + ' *[disabled]').addClass('ui-state-disabled');

    $(prefix + ' a[title], ' + prefix + ' .tooltip[title]').tipTip( { delay: 0, fadeIn: 0, fadeOut: 0, edgeOffset: 8 } );

    $(prefix + ' #delete_dialog').dialog( { autoOpen: false,
                                            modal: true } );

    $(prefix + ' .del-button').each(
        function() {
            $(this).click(function() {
                var id = $(this).attr('id').substr(4);
                $('#delete_dialog').dialog('option', 'buttons', {
                    OK: function() {
                        $('#deleteid').val(id);
                        $('#delete_dialog form').submit();
                    }, Abbrechen: function() {
                        $('#delete_dialog').dialog('close');
                        $('object, applet').show();
                    }
                });
                $('object, applet').hide();
                $('#delete_dialog').dialog('open');
            });
        }
    );

    $(prefix + ' .form_dialog').each(function() {
        var width = 512;
        if($(this).css('width') != 'auto') {
            width = parseInt($(this).css('width').substr(0, $(this).css('width').length-2));
        }
        $(this).dialog({ autoOpen: false,
                         resizable: false,
                         width: width,
                         modal: true,
                         buttons: { OK:
                             function() {
                                 var ok = true;
                                 $('.required', this).each(
                                     function() {
                                         if(!$(this).val() && !$(this).attr('disabled')) {
                                             ok = false;
                                             $('<span class="ui-state-error ui-corner-all" style="margin-left:8px; padding:0 4px;">Ungültig</span>').
                                                 appendTo('label[for='+ $(this).attr('id') +']').fadeOut(3000, function() { $(this).remove(); });
                                             $(this).addClass('ui-state-error');
                                         }
                                     });
                                 if(ok) {
                                     $(this).dialog('close');
                                     $('form', $(this)).submit();
                                 }
                             }, Abbrechen:
                                 function() {
                                     var form = $('form', $(this));
                                     if( form.length != 0 ) form[0].reset();
                                     $('.required', this).removeClass('ui-state-error');
                                     $(this).dialog('close');
                                     $('object, applet').show();
                                 }} }); });

    $(prefix + ' .button').click(function() {
        if($(this).attr('formid')) {
            $('object, applet').hide();
            $($(this).attr('formid')).dialog('open');
        }
    });

    $(prefix + ' .button, ' + prefix + ' .hoverable').hover(
        function() {
            $(this).addClass('ui-state-hover');
        }, function() {
            $(this).removeClass('ui-state-hover');
        }
    );

    $(prefix + ' .button').each(function() {
        if($(this).attr('href') && $(this).attr('href').substr(0,1) == '#') {
            $(this).attr('formid', $(this).attr('href'));
            $(this).attr('href', '#');
        }
    });

    $(prefix + " input").filter(":checkbox,:radio").checkbox();
}