
se.init = function(){
////console.log("init");
    this.inserthook = null;
    this.nextbg = 1;
    
    // pseudo-ajax.
    this.target = se.create({
        nodeName : 'IFRAME',
        id : 'commontarget',
        src : '/se_content/themes/saint_elmo_s/images/saintelmos.png',
        name : 'commontarget',
        parentNode : $('body')[0]
    });
    $(this.target).unload(function() {
////console.log('Handler for .unload() called.');
    });

    
    // list image animated
    $.tools.tabs.addEffect("slide", function(i, done) {
    
        this.getPanes().slideUp(function()  {
            done.call();
        });
        this.getPanes().eq(i).slideDown(function()  {
            var theimg = $(this).find("img");
            if (theimg.length > 0) {
                var thePos = [theimg.position().left,theimg.width(),theimg.height()];
                theimg.css({'left':thePos[1]+thePos[0],'width':'1px','height':'100px','opacity':'0.1'});
                theimg.animate({
                    opacity: '1',
                    left: thePos[0],
                    width: thePos[1],
                    height: thePos[2]
                  }, 500, function() {
                    // Animation complete.
                  });
            }
            done.call();
        });
    });
    
    //adjust bg on resize
    $(window).resize(function() {
        se.whenready(function(){
            se.adjustBgSize();
        });
    });
    
    // trigger (load following pages)-link on scroll…
    $(window).scroll(function() {
        var aLink = $('.appendLink a')[0];
        if (aLink){
            if($('#content').height() - ($(window).scrollTop() + $(window).height()) < 200){
                if (se.inserthook === null){
                    se.target.src = aLink.href;
                    se.linkclick(aLink);
                }
            }
        }
    });
    
    se.initContent();
};

se.initContent = function(){
////console.log("initcontent");  

    // collapsing DLs
    se.lastTab = [];
    $('#content .collapsing dd').css("display","none");
    $("#content dl.collapsing").tabs('.collapsing dd', {
        tabs: 'dt', 
        effect: 'slide', 
        initialIndex: null,
        onClick: function(){
            var thetab = this.getCurrentPane();
            if(thetab.offset() && thetab[0] !== se.lastTab[0]){
                se.lastTab = thetab;
                var rest = 150 + (thetab.offset().top + thetab.height()) - ($(window).height() + $(window).scrollTop());
                if (rest>0){
                    $('html, body').animate({scrollTop: thetab.offset().top-100}, 1000);
                }
            }
        }
        });
    $("#content dl.collapsing").addClass('c-active');
    
    // black inline-background
    $("#content .bg-container p,#content .bg-container li").each(function(index) {
        if(this.innerHTML.length < 1){
            this.style.display = 'none';
        } 
        $(this).addClass('trans');
        this.innerHTML = '<span class="black">'+this.innerHTML + '</span>';
    });
    
    // list linked entry
    $("#content.page-category h3 a").each(function(index) {
        var listel = this.parentNode.parentNode.parentNode;
        listel.style.cursor = 'pointer';
        listel.onclick = function(){
            var d = $(this).find("h3 a")[0];
            se.target.src = d.href;
            se.linkclick(d);
        };
    });
    $("#content .scrollable").scrollable({ 
        easing: 'swing', 
        speed: 500,
        onSeek: function(event, tabIndex) {
            var flashContainer = $('.gallery #flashContainer');
            if (flashContainer){
                if(this.getIndex() !== 0){
                    flashContainer.html('<div id="flashContent"></div>');
                } else {
                    se.loadVideo();
                }
            }
        }
        });
    
    $("#content a, #sideboard a").each(function(index) {
        if (this.href.indexOf(location.host)>=0 && this.href.indexOf('admin')<0){
            this.target = 'commontarget';
            this.onclick = function(){
                se.linkclick(this);
                return true;
            };
        }
    });
    
    // subpost context
    if (se.data.currentcat !== undefined){
        var testCh = se.data.currentcat.substr(0,3);
        $("#sideboard .page_item a").each(function(i) {
            if (this.innerHTML.toLowerCase().indexOf(testCh) === 0){
                $(this).parent().addClass('current_page_item');
            }
        });
        if($('.level-up')[0] && se.recent){
            $('.level-up')[0].onclick = function(){
                se.restorePage();
                return false;
            };
        }
    };
    
    $('a img').parent().each(function(){
        if(this.href.indexOf('attachment')>0){
            this.onclick = function(){return false;};
            $(this).css('cursor', 'default');
        }
    });
    
    
    
    $("#loading").hide();
    se.loadVideo();
////console.log("initcontent done");
};

se.linkclick = function (e) {
////console.log("linkclick");
    se.inserthook = null;
    
    //loading
    $("#loading").css('width',0);
    $("#loading").show();
//console.log(111)
    $("#loading").animate({
        width: '75px'
    }, 300, function() {
        if(se_canvas){
            se.nextbg = se_canvas.bgAnimation.start();
        }
    });
    
    if(!$(e.parentNode).hasClass('appendLink')){
        
        se.recent = {
            bodyClass : $("body")[0].className,
            content : $("#content").html(),
            className : $("#content")[0].className,
            sideBoard : $("#sideboard").html(),
            wscroll : $(window).scrollTop()
        };
        
    } else {
        se.inserthook = $(e);
    }
};

se.restorePage = function(){
////console.log("restorePage");
    $("#content").fadeOut(100,function(){
        $("body")[0].className = se.recent.bodyClass;
        $("#content").html(se.recent.content);
        $("#content")[0].className = se.recent.className;
        $("#sideboard").html(se.recent.sideBoard);
        se.initContent();
        $("#content").fadeIn(1000);
        $(window).scrollTop(se.recent.wscroll);
        se.recent = null;
    });
};

se.updatePage = function(b,c,s,bg){
////console.log("updatePage");

    if(se_canvas){
        se_canvas.bgAnimation.stop();
    }
    if (se.inserthook !== null){
        se.inserthook.remove();
        se.inserthook = null;
        $('#content').append(c.innerHTML);
        se.initContent();
    } else {
        $("#content").fadeOut(10, function(){
            $(window).scrollTop(0);
            $("body")[0].className = b.className;
            $("#content").html(c.innerHTML);
            $("#content")[0].className = c.className;
            $("#sideboard").html(s.innerHTML);
            se.initContent();
            $('#bgimg')[0].src = bg.replace(/bg_(.*).jpg/,"bg_"+se.nextbg+".jpg");
            $("#content").fadeIn(1000);
        });
    }
};

se.loadVideo = function(){
////console.log(se.data.playlist_url);
    if (se.data.playlist_url === undefined){
        return false;
    }
    
////console.log(2);
    var flashvars = {};
        flashvars.playlistURL = se.data.playlist_url;
        flashvars.autostart = true;
        flashvars.autosound = true;
        flashvars.externalInterfaceDisabled = 'false';
    var params = {};
        params.allowscriptaccess = "always";
        params.allowfullscreen = "true";
        params.bgcolor = "#000000";
    var attributes = {};
    swfobject.embedSWF("http://dl.mdn.nacamar.net/contentwidgets/flashplayer/see/se_media_player.swf", "flashContent", "100%", "100%", "9.0.0", "http://dl.mdn.nacamar.net/contentwidgets/flashplayer/see/expressInstall.swf", flashvars, params, attributes);
    return true;
};

se.pauseVideo = function(){
//does not seem to work XXX-domain
    var videoPlayer = swfobject.getObjectById("flashContent");
    if (videoPlayer && typeof videoPlayer.stopVideo != "undefined") { 
        videoPlayer.stopVideo();
    }
};

se.adjustBgSize = function(){
    $("#bgimg").width($(window).width()+25);
    $("#bgimg").height($(window).height()+25)
    if(se_canvas){
        se_canvas.redraw();
    }
};

se.create = function (a) {
    var el = null;
    if (a.nodeName) {
        el = document.createElement(a.nodeName);
        if (a.textNode) { 
            var tn = document.createTextNode(a.textNode);
            el.appendChild(tn);
        }
        if (a.id) { el.id = a.id; }
        if (a.className) { el.className = a.className; }
        if (a.title) { el.title = a.title; }
        if (a.name) { el.name = a.name; }
        if (a.src) { el.src = a.src; }
        if (a.href) { el.href = a.href; }
        if (a.style) { el.style = a.style; }
        if (a.onclick) { el.onclick = a.onclick; }
        if (a.onmouseover) { el.onmouseover = a.onmouseover; }
        if (a.onmouseout) { el.onmouseout = a.onmouseout; }
        if (a.parentNode) { a.parentNode.appendChild(el); }
        if (a.target) { el.target = a.target; }
    }
    return el;
};

se.running = 0;
se.whenready = function(f){
    se.running += 1;
    setTimeout(function(){
        se.running -= 1;
        if (se.running < 1) {
            f();
        }
    },1000);
};

se.supports_canvas = function(){
  return !!document.createElement('canvas').getContext;
}

se.canvas = {
    init : function() {
        var c_lib_src = "/script/three/build/Three.js";
        var scrList = ["/script/three/examples/js/Stats.js",
            "/script/three/examples/js/Tween.js",
            "/script/three/src/extras/primitives/Cube.js",
            "/script/three/examples/js/RequestAnimationFrame.js"];
        if(true || se.perfomanceCheck){
            var c_lib = se.create({
                nodeName: 'SCRIPT',
                src: customPath + c_lib_src,
                parentNode: $('head')[0]
            })
            $(c_lib).load(function(){
                $.each(scrList, function(index, value) { 
                    se.create({
                        nodeName: 'SCRIPT',
                        src: customPath + value,
                        parentNode: $('head')[0]
                    })
                });
                var c_ses = se.create({
                    nodeName: 'SCRIPT',
                    src: customPath + "/script/canvas.js",
                    parentNode: $('head')[0]
                })
                $(c_ses).load(function(){
                    if(THREE){
                        se_canvas.init();
                    } else {
                        // desperate attempt to load canvas again after 2 sek
                        setTimeout(function(){
                            if(THREE){
                                se_canvas.init();
                            }
                        },3000)
                    }
                });
            });
        } else {
            return false;
        }
    }
}




// main switch to handle different request situations
$(function() {
////console.log("dom loaded");
    if(top !== self){
        parent.se.data.currentcat = se.data.currentcat;
        parent.se.data.playlist_url = se.data.playlist_url;
        parent.se.updatePage($('body')[0],$('#content')[0],$('#sideboard')[0],$('#bgimg')[0].src);
        //parent.se.initContent();
    } else {
        $("html").addClass('jsavailable');
        if(navigator.userAgent.indexOf('WebKit')>0){
            $("html").addClass('render-webkit');
        }
        
        se_canvas = null;
        if(se.supports_canvas){
            se.canvas.init();
        }
        se.adjustBgSize();
        se.init();

    }
////console.log("ready");
});


////console.log(" script file loaded");
