var infoContent = {
}

var dummyInfoContent = '<span class="h2">Keine Information verf&uuml;gbar</span>';

$(function(){
    initFormRadioCheckbox();
    initFormControl();

    $(".info").infoBox();
    $("[title]").toolTip();
    
    // Fix für IE6, sonst werden die Tooltips angezeigt beim Browser
    $("img").each(function() {
    	$(this).attr('onload',function() {
    		$(this).removeAttr('alt');
    	});
    });

    // mit Javascript werden Checkboxen etc. anders gestyled
    $("body").removeClass("noJS");
    
    if( isIE6() ){
        $('.over').hover(function(){
            $(this).addClass('hover');
        },
        function(){
            $(this).removeClass('hover');
        });
    }
    
    $("#blocker").hide();
    
    if( $('.validationError:first').length > 0 && $('.validationError:first').html().length > 0 ){
        var message = $('.validationError:first').html();
        $('.validationError:first').remove();
        alert( message );
    }
});

function displayMessage (text) {
    }

var box = {
    offset      : {
                    x : -21,
                    y : -11
                },

    container   : $('<div/>').addClass('infobox'),

    close       : $('<a/>')
                    .addClass('close')
                    .attr('href','javascript:void(null)'),

    content     : $('<div/>'),

    setContent  : function (c) {
                    box.content.html(c);
                    return this;
                },

    show        : function (e,tooltip,additionalOffset) {
                	
                    // bisher vorhandene IFrames löschen - bei mehrfachen Anklicken verdoppelt sich der IFrame
                    $(box.container).find('iframe').remove();
                    $(box.container).find('.borderDiv').remove();

                	// IFrame erstellen für IE6
                	if (isIE6()) {
                		var iframe_element = document.createElement('iframe');
                		iframe_element.className = 'contentIFrame';
                		box.container.append(iframe_element)
                	}
                    // Neuer Border für die Tooltips, da der vorherige Border des Containers im IE6 außerhalb lag
                    var borderDiv = document.createElement('div');
                    borderDiv.className = 'borderDiv';
                    box.container.append(borderDiv);
                    // Neuer Border für die Tooltips, da der vorherige Border des Containers im IE6 außerhalb lag
                    var closeDiv = document.createElement('div');
                    closeDiv.className = 'closeButtonDiv';
                    
                    if (!tooltip) {
                        options = {}
                        box.container.removeClass('tooltip');
                        $(closeDiv).append(box.close);
                        
                    } else {
                        options = {
                            noCloseButton : true,
                            position : 'right',
                            offsetX : 10,
                            offsetY : 10
                        }
                        box.container.addClass('tooltip');
                        box.close.remove();
                    }
                    
                    var pos = 'right';
                    if (options.position != 'right') pos = 'left';
                    if (additionalOffset == 'right') pos = 'right';
                    
                    box.container
                        .append(box.content)
                        .append(closeDiv)
                        .appendTo(document.body)
                        .css('left',e.pageX + (options.offsetX || box.offset.x) - ((pos != 'right') ? box.container.width() : 0) )
                        .css('top',e.pageY + (options.offsetY || box.offset.y))
                        .fadeIn('fast');
                    
                    if (e.pageX - box.container.width() < 5)
                        box.container.css('left',e.pageX + (options.offsetX || box.offset.x));

                    if (options.position == 'right' && e.pageX + box.container.width() > $('body').width())
                        box.container.css('left',e.pageX   - box.container.width());
                    
                    // exakte Größe des Tooltips ermitteln und auf Border und IFrame anwenden
                    var width = 0;
                    var height = 0;
                    $(box.container).each(function(){width = this.clientWidth-2});
                    $(box.container).each(function(){height = this.clientHeight-2});
                    borderDiv.style.width = width+'px';
                    borderDiv.style.height = height+'px';
                    if (isIE6()) {
                    	iframe_element.style.width = width+2+'px';
                    	iframe_element.style.height = height+2+'px';
                    }

                    box.close.click(box.hide);
                },

    hide        : function () {
                    box.container.fadeOut('fast');
                }
};

$.fn.extend({
    infoBox : function () {

        $(this)
            .each(function () {
                var field = $(this).attr('href');

                if (!field)
                    return;
                
                var offset = '';
                if ($(this).hasClass('rightSided')) {
                	offset = 'right';
                } else if($(this).hasClass('leftSided')) {
                	offset = 'left';
                }

                $(this)
                    .click(function (e) {
                        box.setContent(infoContent[field] ? infoContent[field] : dummyInfoContent).show(e,false,offset);
                        $(this).blur();
                    })
                    .attr('href','javascript:void(null)');
        });
        return $(this);
    },

    toolTip : function () {

        $(this)
            .each(function () {
                if ($(this).hasClass('info') || $(this).hasClass('nott'))
                    return;

                var delay = 100;

                var title = $(this).attr('title');
                var timeout;
                var event;

                $(this)
                    .removeAttr('title')
                    .attr('ontooltip',function(){return false;})
                    .mouseover(function () {
                        timeout = window.setTimeout(function () {

                            box.setContent(title).show(event, true);
                        },delay);
                    })
                    .mousemove(function (e) {
                        event = e
                    })

                    .mouseout(function () {
                        window.clearTimeout(timeout);
                        box.hide();
                    });
        });
        return $(this);
    }
});

function initFormRadioCheckbox () {
    $("input[type!='text'][type!='submit'][type!='hidden'][type!='image']")
        .checkBox() // init Checkboxes
        .filter("[type='radio']")
        .each( function () {
            if ($(this).parents('div').hasClass('sub'))
                return;

            $(this)
                .parents('fieldset')
                .find('div.sub')
                .css('display',$(this).is(':checked') ? 'block' : 'none');
        })
        .bind('checkBoxchange',function () {
            if ($(this).parents('div').hasClass('sub'))
                return;

            if ($(this).is(':checked')) {
                $(this)
                    .parents('fieldset')
                    .find('div.sub')
                    .slideDown('normal');
                return;
            }

            $(this) // Unselect
                .parents('fieldset')
                .find('div.sub input[type="checkbox"],div.sub input[type="radio"]')
                .checkBox('changeCheckStatus',false)
                .removeAttr('selected')
                .end()
                .find('div.sub')
                .slideUp('normal');
        });

}

function initFormControl () {

    $('div.control').each(function () {
        var prev = $(this).prev();

        $('a',this).each(function (nr) {
            $(this)
                .attr('href','javascript:void(null)')
                .click(function () {
                    $('input',prev).checkBox('changeCheckStatus', ! nr % 2);
                    $('option',prev).each(function () {
                        if (!$(this).is(':disabled'))
                            $(this).attr('selected',! nr % 2);
                    });
                    prev.trigger('change');
                });
        });
    });
}

function isIE6() {
	if (window.XMLHttpRequest) {
		// IE 7, mozilla, safari, opera 9
		return false;
	} else {
		// IE6, older browsers
		return true;
	}
}

