$(document).ready(function() {
    try{
        if($('#unterrichtsplan')[0]) {
            $.colorbox({html:$('#unterrichtsplan').html()});
        }
        if($('#unterrichtsplanlink')[0]) {
            $('#unterrichtsplanlink').click(function() {
                $.colorbox({html:$('#unterrichtsplan').html()});
            });
        }
    
        if($("a[rel='vplan']")[0]) 
            $("a[rel='vplan']").colorbox(); 
        if($("a[rel='uplan']")[0])
            $("a[rel='uplan']").colorbox(); 
        
        $('.onscreen').each(function(i,s) {
            $(s).click(function() {
                editOnscreen(this,990,600);
                return false;
            });
        });
        
    }catch(e) {
        if(console) console.log(e);
    }
    
  
});

function deletePart(obj) {
    obj = $(obj)[0];
    $.post(obj.href,{
        success: function() {
            $(obj).parent().parent().remove();
        }
    });
    return false;
}

function saveSortable(obj,url,reload) {
    obj = $(obj);
    
    params = '';
    obj.each(function(i,s) {
        params = params + 'obj[' + i + ']=' + s.id + '&';
    });
    if(typeof reload != null && typeof reload === true){
    $.post(url,
       params,function(){	        location.reload();}
    );
	} else {
    $.post(url,
       params,function(){	        location.reload();}
    );
	}
}

$(document).ready(function(){
    $(".moreinfo").each(function(){
        var t;
        var isanimated = false;
        var isvisible = false;
        var popup = $(".floatingpanel_" + this.id);
        $(this).mouseover(function(){
            if(t) clearTimeout(t);
            if(!isvisible&&!isanimated){
                isanimated = true;
                
                obj = this;
                offsetTop = -240;
                offsetLeft = -190;
                while(obj.offsetParent) {
                    offsetTop = offsetTop + obj.offsetTop;   
                    offsetLeft = offsetLeft + obj.offsetLeft;   
                    obj = obj.offsetParent;
                }
                
                popup.css({width:200,top: offsetTop, left: offsetLeft, opacity:0,display:"block",marginTop:15}).animate({marginTop:0,opacity:1},300,function(){isanimated=false,isvisible=true;});
            }else{
                return;
            }
        }).mouseout(function(){
            if(t) clearTimeout(t);
            //if(!isanimated&&isvisible){
                t = setTimeout(function(){isanimated=true;popup.animate({marginTop:-10,opacity:0},300,function(){popup.css({display:"none"});isvisible=false;isanimated=false;});},300);
            //}else{
            //    return;
            //}
        });
    });
});

function editOnscreen(obj,w,h) {
    if(w == null) w = 800;
    if(h == null) h = 500;
    $('#onscreen-frame').css({border: 0, height: h, width: w});
    $('#onscreen-frame')[0].src = obj.href;
    $.colorbox({html:$('#onscreen').html()});
}

