$.gcsRewriteCSS(true);

$.Tooltip.tooltip = '<div class="tooltip">' +
                    '<div class="left-bottom"/><div class="left-top"/><div class="right-bottom"/><div class="right-top"/>' +
                    '<div class="content"/></div>';
$.Tooltip.insert = function() {
    this.tooltip.appendTo($("body"));
};
$.Tooltip.defaultOptions.html = true;
$.Tooltip.defaultOptions.showDelay = 0;
$.Tooltip.defaultOptions.hideDelay = 0;
$.Tooltip.defaultOptions.left = 8;

$.History.emptyHash = "Home";


if (typeof bm_root_path == 'undefined')  bm_root_path = '/index.html';
if (typeof bm_root_path_topic == 'undefined')  bm_root_path_topic = '/content.html';
if (typeof plc_img_path == 'undefined')  plc_img_path = '/_binary/';



$.extend(true, {
    log: function(output) {
        if (window.console) {
            var now = new Date();
            console.log(now.toLocaleTimeString() + ":" + leadingZeros(now.getMilliseconds(),3) +" - %o",output);
        }
    },
    animationSpeed: 600,
    IE6Options: {off: true},
    BorderRadius: {off: true},
    //fx: {off: true},
    AjaxNavigate: {
        hashchangeComplete: function(){
            $.fx.off = false;
            $.BorderRadius.off = false;
            $.IE6Options.off = false;
            $("body").ie6().borderRadius();
        },
        loadingClass: "jbm-loading",
		error: function(status, textStatus, data) {
			var lb = $('#lightbox');
			lb.html('').fadeIn($.animationSpeed).append('<div class="lb_main bm-error"><div class="lb_content"><h2 title="HTTP-Status: '+ status +'">Es ist ein Fehler aufgetreten.</h2><div class="lb_onecolumn">'+ data +'</div><p><a href="#" class="bm-button bm-error" rel="close-lightbox">OK</a></p></div></div>');
			lb.enhance();
			return false;
		}
    }
});

$.fn.extend(true,{
    disableSelection: function() {
		return this
			.attr('unselectable', 'on')
			.css('MozUserSelect', 'none')
			.bind('selectstart.ui', function() {return false;});
	}
});

$.Enhancer.add(function() {
    $('div.bm-box.bm-single a img').each(function(){
        $(this).css('visibility','hidden');
    });

    function getOld(rel) {
        var result = $("#" + rel);
        if (result.size()) return result;
        var _abstract = $("#" + rel + "_abstract");
        if (!_abstract.size()) return _abstract;
        result = $('<div/>');
        result.attr('id', rel);
        result.insertAfter(_abstract);
        return result;
    }

	$('a[rel][href]',this).filter(':not(.jgcs-ajax)').click(function() {
		var link = $(this);
        var rel = link.attr('rel');
        
        if(rel == 'no-follow') {
        	return true;
        }

        if (rel == "plc-back") {
            $(window).history('back');
        } else if (rel.indexOf('close-') == 0) {
            rel = rel.substring(6);
            $('#' + rel).gcsNavigate({});
        } else {
            var target = getOld(rel);
            if (!target.length) {
                return true;
            }
            target.gcsNavigateLoad(link.attr('href')/*,function() {
                // TODO: Add callback if you want
            }*/);
        }
        return false;
    }).addClass('jgcs-ajax');

    $('.bm-loginfields input:not(:submit)',this).each(function() {
        var right = "0";

        var _fix = function(){
          if (document.documentMode >= 7 || !/MSIE [56]/.exec(window.navigator.appVersion)) {
              right = "100%";
          } else {
              right = "95%";
          }
        }

        if ($(this).is(':password')) {
          _fix();
        } else {
          $(this).keyup(function(){
            if($('.bm-loginfields input:password').val().length != 0){
              _fix();
              $('.bm-loginfields input:password').css('background-position', right + ' 100%');
              right = '0';
            }
          })
        }
        $(this).bind('blur',function() {
            if ($(this).val() == "") {
                $(this).css('background-position', right + ' 0');
            } else {
                $(this).css('background-position', right + ' 100%');
            }
        }).bind('focus',function() {
            $(this).css('background-position', right + ' 100%');
        }).trigger('blur');
    });


	$('a.bm-button:not(.jplc)',this).addClass('jbm').wrapInner('<span/>');
    $('input.bm-button:not(.jplc)',this).addClass('jbm').each(function() {
        var input = $(this);
        input.wrap('<a/>');
        var wrap = input.parent();
        var span = $('<span/>');
        span.text(input.val());
        wrap.attr({
            'class': input.attr('class'),
            title: input.attr('title')
        });
        input.removeAttr('class').removeAttr('title').after(span);
        input.addClass('jbm').hide();
        wrap.addClass('bm-button jbm').bind("click", input, function(e) {
            if(input.attr('rel')){
                var form_id = input.attr('rel');
                $('form#' + form_id).submit();
            }else{
                e.data.click();                
            }
        })
    });

    $('.bm-teaser:not(.jgcs)', this).each(function() {
        $(this).hover(function() {
            $(this).find('span.bm-fading-curtain').fadeTo($.animationSpeed/2,0.3);
        },function() {
            $(this).find('span.bm-fading-curtain').fadeTo($.animationSpeed/2,0.0);
        }).append('<span class="bm-fading-curtain"> </span>').find('span:first').fadeTo(0,0.0);
    }).addClass('jgcs');

    $('div.bm-intro img.bm-people',this).appendTo($('#bm-main > div.bm-navigation'));

    $('#bm-main > div.bm-navigation').append('<span class="bm-navigation-teacher-button"/>');
    $('#bm-main > div.bm-navigation .bm-navigation-teacher-button').click(function() {
        /* .click didn'work and i don't know why */
        window.location.href=$(this).parent().find('li.bm-lc a').attr('href');
    }).hover(function() {
        $(this).parent().find('li.bm-lc a img').css({position: 'relative',top: '-46px'});
    },function() {
        $(this).parent().find('li.bm-lc a img').removeAttr('style');
    });

    $('.bm-sliderbox:not(.jgcs)',this).disableSelection().each(function() {
        var box = $(this);
        box.goto = function(i) {
            box.checkButtons(i);
            box.animate({scrollLeft: (i * (par.outerWidth()+2))},$.animationSpeed,function() {
                box.checkButtons(i);
            });
        };
        box.wrap('<div class="bm-sliderboxcontainer"></div>');
        var par = box.parents('.bm-sliderboxcontainer:first');
        var isBig = box.hasClass('bm-bigslider');

        if (!isBig) {
            var sep = $('<li class="bm-fillup" > </li>');
            while ($('>li',box).length%4 != 0) {
                box.append(sep.clone());
            }
            box.liCount = $('>li',box).length;

            $('>li',box).each(function(i) {
                if (i != 0 && i != (box.liCount - 1) && (i+1) % 4 == 0) {
                    $(this).after(sep.clone());
                }
            });
            sep.addClass('bm-lastfirst');
            box.prepend(sep.clone());
            box.append(sep.clone());
        } else {
            par.append('<p class="bm-dropshadow"/>');
            var ds = par.find('.bm-dropshadow');
            ds.click(function() {
                var ul = par.find('.bm-pagenav');
                if (ul.length) {
                    var link = $('li:eq(' + ul.find('.bm-selected').attr('rel') + ') a',box);
                    document.location = link.attr('href');
                }
                return false;

            });
            par.append('<ul class="bm-pagenav"/>');
            var ul = par.find('.bm-pagenav');
            var link = $('<li/>');
            link.click(function() {
                box.goto($(this).attr('rel'));
            });
            $('>li',box).each(function(i) {
                var li = link.clone(true);
                li.attr('rel',i);
                li.text(i);
                ul.append(li);
            });
        }
        box.checkButtons = function(i) {
            var ul = par.find('.bm-pagenav');
            if (ul.length) {
                ul.find('.bm-selected').removeClass('bm-selected');
                ul.find('li:eq(' + i + ')').addClass('bm-selected');
            }
            if (box.scrollLeft() == 0) {
                previous.addClass('bm-disabled');
                previous.fadeTo(0,0.2);
            } else {
                previous.removeClass('bm-disabled');
                previous.removeAttr('style');
            }
            var width = 0;
            $('>li',box).each(function(i) {
                width += $(this).width();
            });
            if (box.scrollLeft() + box.outerWidth() >= width) {
                next.addClass('bm-disabled');
                next.fadeTo(0,0.2);

            } else {
                next.removeClass('bm-disabled');
                next.removeAttr('style');
            }
        };
        par.append('<span class="bm-slider-previous"/><span class="bm-slider-next"/>');
        var next = $('.bm-slider-next',par);
        var previous = $('.bm-slider-previous',par);
        next.click(function() {
            if (next.hasClass('bm-disabled')) return;
            var i = Math.round(box.scrollLeft()/par.outerWidth())+1;
            box.goto(i);
            box.checkButtons(i);
        });
        previous.click(function() {if (
            previous.hasClass('bm-disabled')) return;
            var i = Math.round(box.scrollLeft()/par.outerWidth())-1;
            box.goto(i);
            box.checkButtons(i);
        });
        window.setTimeout(function() {box.checkButtons(0);},1);
        window.setTimeout(function() {box.checkButtons(0);par.borderRadius();},1000);
    }).addClass('jgcs');

    $('.bm-reg-infos > a:not(.jbm)',this).addClass('jbm').click(function() {
        $(this).parent().find('span:first').slideToggle('fast');
        return false;
    }).parent().find('span:first').hide();

    var foot = $('#bm-foot',this);
    if (!/MSIE [56]/.exec(window.navigator.appVersion)) {
        foot.css({bottom: ((foot.height()-31)*-1)});
        window.setTimeout(function() {
            foot.css({bottom: ((foot.height()-31)*-1)});
        },100);
    }

    if (/MSIE [5678]/.exec(window.navigator.appVersion)) {
        $('.bm-floating-box:first-child',this).css('margin-left',"0");
        $('* > .bm-floating-box:first',this).css('margin-left',"0");
        $('.bm-quintuple',this).css({'margin-left':"0",paddingLeft: "0",paddingRight: "0"});
    }

    this.borderRadius();
    var $$ = this;
    window.setTimeout(function(){$$.borderRadius()}, 1);
    var titles = $('.bm-tooltip',this);
    titles.tooltip();
    this.ie6();

    $(window).resize(function() {
        checkWrapperHeight();
    });
    window.setTimeout(function() {
        checkWrapperHeight();
    },100);

});


function checkWrapperHeight() {

    //var padding = 328 + $('#bm-foot').outerHeight() + parseInt($('#bm-foot').css('bottom'), 10);
    var content = $('.bm-content');
    if (document.documentMode >= 7 || !/MSIE [56]/.exec(window.navigator.appVersion)) {
        if (!content.length) return;
        content.css('height','');
        var view = $(window);
        var content_height = content.outerHeight() + 45;
        var combined_height = content_height + $('#bm-foot').outerHeight();
        if(combined_height < view.height()){
            var new_height = view.height() - combined_height + content.height();
            content.css('height',new_height);            
            content.borderRadius();
        }
        
    } else {
        content.css('overflow','visible');
    }
}


var scrollbarWidth = false;
function _scrollbarWidth() {
    if (!scrollbarWidth) {
        var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
        // Append our div, do our calculation and then remove it
        $('body').append(div);
        var w1 = $('div', div).innerWidth();
        div.css('overflow-y', 'scroll');
        var w2 = $('div', div).innerWidth();
        scrollbarWidth = (w1 - w2);
        $(div).remove();
    }
    $.log(scrollbarWidth);
    return scrollbarWidth;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
};


