﻿/* 
 * openComponent
 * @author isavar, thohol
 * @copyright S2 Hamburg
 * @since 8.08.2007
 */    
 
    addEvent ( window, 'load', function (e)
    { 
        if($$('#contentArea div.mustnaBox').length>0) {
            initMustna();
        }
    }, false );
 
var mustnaTheme;
 
function initMustna() {
    var mustnaBox = $$('#contentArea div.mustnaBox');
    mustnaTheme =  $(mustnaBox[0]).getAttribute('id');
} 
function openComponent(lnk, scrollid, filterID) {
    var theMuStNaID = ($(lnk).up('.mustnaContent')).getAttribute('id');
    var step1 = $(lnk).up('.mustna1');
    var theDiv = $(lnk).next('.scrollComponents');
    var allDivs = $$('#'+theMuStNaID+" .mustna1 .scrollComponents");
    var allMainLis = $$('.mainmenu'); 
    var step2box = $('mustna2');
    //falls von schritt 2 zurueck auf 1
    if($(step1).hasClassName('inaktiv')) {
        Element.removeClassName($(step1), 'inaktiv');
        $A(allMainLis).each(function(d){
            Element.removeClassName(d, 'inaktiv');
        });
    }
    //alle boxen zuklappen
    $A(allDivs).each( function(d) {
        if(d.visible()) {
            Effect.SlideUp(d);
            Element.removeClassName(d.down('li'), 'open');
            Element.addClassName(d.up('.submenu'), 'inaktiv');
            Element.hide(step2box.down('.scrollComponents'));
            Element.hide(step2box.down('h4'));
            Element.addClassName(step2box, 'inaktiv');
        }
    });
    
    //eine box auf-/zuklappen
    var theLi = $(lnk).up('li');
    //alle links auf "nicht open" setzen
    $A(allMainLis).each( function(d){
        if(d!=theLi){
            Element.removeClassName(d, 'open');
        }
    });
    Effect.toggle2(theDiv, 'slide', { queue: 'end', scope: 'sContainer3',
        beforeStart: function() {
            $('sContent'+scrollid).setStyle({top:'0px'});
            // auf und zuklappen des menus
            if(Element.hasClassName(theLi, 'open')){
                Element.removeClassName(theLi, 'open');
            }
            else{
                Element.addClassName(theLi, 'open');
            }
        },
        afterFinish: function() {
            if($(theDiv).visible()) {
                var container = 'sContainer'+scrollid;
                var content = 'sContent'+scrollid;
                var handle = 'sHandle'+scrollid;
                var track = 'sTrack'+scrollid;
                new VerticalScroller (container, content, false, false, handle, track, 0);
            }
    }});
    $(lnk).blur();
    //xml laden
    loadXML(filterID);
    return false;
}
var xmlDoc;
var xmlArray;
var contentHTML = '';
var path = new Array();
var stepsCount = 3;
function loadXML(filterID) {
    // xml finden, random parameter dran um caching zu umgehen
    var randNr = Math.round(1000*Math.random());
    if (mustnaTheme == 'hochschulen') {
        filterCount = 4;
        prefix = 'hs';
    }    
    else if (mustnaTheme == 'fachschulen1') {
        filterCount = 3;
        prefix = 'fs1';
    }    
    else if (mustnaTheme == 'fachschulen2') {
        filterCount = 3;
        prefix = 'fs2';
    }    
    else if (mustnaTheme == 'gesundheit') {
        filterCount = 1;
        stepsCount = 2;
        prefix = 'gs';
    }    
    //hier pfad zu den xml files angeben
    var xmlDirectory = '/cps/rde/xchg/srh//xml/';
    
    filterXMLURL = new Array();
    for(j=1;j<filterCount+1;j++) {
        filterXMLURL[j] = xmlDirectory+prefix+'_'+'filter'+j+'.xml?'+randNr;
    }
    //xml daten anfordern
    xmlArray = new Array();
        
    new Ajax.Request(filterXMLURL[filterID], {
      method: 'get',
      onSuccess: function(req) {
        xmlDoc = req.responseXML;
        root = xmlDoc.documentElement;
        
        //xml daten in array xmlArray lesen
        firstLvl = root.childNodes; //studienrichtung / ort / abschluss..
        for(i=0;i<firstLvl.length;i++) {
            title = firstLvl[i].getAttribute('id');
            xmlArray[title] = new Array() ;
            
            secondLvl = firstLvl[i].childNodes;
            for(k=0;k<secondLvl.length;k++) {
                //wenn >3 ebenen
                if(stepsCount!=2) {
                    linktext = secondLvl[k].childNodes[0].firstChild;
                    linkevent = secondLvl[k].childNodes[0].getAttribute('clickevent');
                    link = '<a href="#" onclick="'+linkevent+'">'+ linktext.nodeValue +'</a>';
                    orte = secondLvl[k].childNodes[1].childNodes;
                    xmlArray[title][k]= new Array();
                    xmlArray[title][k]['link'] = link;
                    xmlArray[title][k]['orte'] = new Array();
                    for(b=0;b<orte.length;b++) {
                        var tempURL = orte[b].getAttribute('url').replace(/#RDE-REQUEST/g,"rawExpr");
                        tempURL = tempURL.replace(/rawExpr:rdePrefix\/#/g,"cps/rde");
                        tempURL = tempURL.replace(/rawExpr:rdeProjectID\/#\/rawExpr:rdeXslID\/#/g,"srh//main.xsl");
                        var tmp = '<a href="'+ tempURL +'">';
                        tmp += orte[b].firstChild.nodeValue;
                        tmp += '</a>';
                        xmlArray[title][k]['orte'][b] = tmp;
                    }
                }
                else {
                    orte = secondLvl[k].childNodes;
                    xmlArray[title][k]= new Array();
                    xmlArray[title][k]['orte'] = new Array();
                    for(b=0;b<orte.length;b++) {
                        var tempURL = orte[b].getAttribute('url').replace(/#RDE-REQUEST/g,"rawExpr");
                        tempURL = tempURL.replace(/rawExpr:rdePrefix\/#/g,"cps/rde");
                        tempURL = tempURL.replace(/rawExpr:rdeProjectID\/#\/rawExpr:rdeXslID\/#/g,"srh//main.xsl");
                        var tmp = '<a href="'+ tempURL  +'">';
                        tmp += orte[b].firstChild.nodeValue;
                        tmp += '</a>';
                        xmlArray[title][k]['orte'][b] = tmp;
                    }
                    
                }
            }
        }
      },
      onFailure: function(req) {
          //fehlermeldung?
      }
    });
}

// 2ter Schritt
function goToStep2(url, lnk, scrollid) {
    var theStepBox = $('mustna1');
    var step1box = ($(lnk).up('.mustna1'));
    var step2box = $('mustna2');
    // navigation vorwaerts
    if(Element.hasClassName('mustna3', 'inaktiv'))
    {
        path[0] = url;
        // step 1 inaktivieren
        Element.addClassName(step1box, 'inaktiv');
        Element.addClassName(step1box, 'finished');
        parentLis = $$('.mustna1>ul>li');
        $A(parentLis).each(function(d) {
            if(d!=$(lnk).up('li.mainmenu')) {
            Element.addClassName(d, 'inaktiv');
        }
        });
        
        var theLi = $(lnk).up('li');
        var otherLis = ($(lnk).up('ul')).immediateDescendants();
        $A(otherLis).each(function(d) {
            Element.removeClassName(d, 'open');
        });
        Element.addClassName(theLi, 'open');
        //Element.addClassName('submenu', 'inaktiv');
            
          // step 2 aktivieren
        Element.removeClassName(step2box, 'inaktiv');
         //ergebnisliste zusammenstellen 
        if(stepsCount==2) {
            contentHTML = '<ul>\n';
            for(m=0;m<xmlArray[path[0]][0]['orte'].length;m++) {
                contentHTML += '<li>';
                contentHTML += xmlArray[url][0]['orte'][m];;
                contentHTML += '</li>\n';
            }
            contentHTML += '</ul>\n';
        }
        else {
            contentHTML = '<ul>\n';
            for(m=0;m<xmlArray[path[0]].length;m++) {
                contentHTML += '<li>';
                contentHTML += xmlArray[path[0]][m]['link'];
                contentHTML += '</li>\n';
            }
            contentHTML += '</ul>\n';
        }
            
        var targetHeadline = step2box.down('h4');
        if(!Element.hasClassName(lnk,'backNav')){
            Element.addClassName(targetHeadline, 'filled');
              targetHeadline.innerHTML = $(lnk).innerHTML;
        }
        var targetContent = step2box.down('.scrollContent');
        targetContent.innerHTML = contentHTML;
        
        Element.show(step2box.down('h4'));
        Element.show(step2box.down('.scrollComponents'));
        // resize scrollContainer
          /* ACHTUNG HACK element height 216 & heading height 25 sind fix! */
              var hoffset = 216 - Element.getHeight(targetHeadline) + 25;
              /* end HACK */
          step2box.down('.scrollContainer').setStyle({height: hoffset+'px'});
          step2box.down('.scrollTrack').setStyle({height: hoffset+'px'});
        var container = 'sContainer'+scrollid;
        var content = 'sContent'+scrollid;
        var handle = 'sHandle'+scrollid;
        var track = 'sTrack'+scrollid;
        // 16 is padding
        new VerticalScroller (container, content, false, false, handle, track, 16);
    }
    
    // navigation rueckwaerts
    else
    {
        var step3box = $('mustna3');
        var dim = step2box.getDimensions();
        new Effect.MoveRight('mustna1', dim.width, {
            afterFinish: function(){
                Element.addClassName(step3box, 'inaktiv');
                  Element.addClassName(step3box, 'finished');
                  Element.removeClassName(step2box, 'inaktiv');
                  Element.removeClassName(step2box, 'finished');
                  Element.hide(step2box.down('.backNav'));
                  Element.removeClassName(step2box.down('span'), 'back');
                  // TODO align header left
            }
        });
    }
    return false;
}
// 3ter Schritt
function goToStep3(url, lnk, scrollid,linkID) {
    path[1] = linkID;
    var step2box = ($(lnk).up('.mustna2'));
    var dim = step2box.getDimensions();
    if(!Element.hasClassName(step2box, 'finished')){
        Effect.MoveLeft('mustna1', dim.width);
    }
    var theLi = $(lnk).up('li');
    Element.addClassName(step2box, 'inaktiv');
    Element.addClassName(step2box, 'finished');
    var allLis = $$('.mustna2 li');
    $A(allLis).each(function(d) {
        Element.removeClassName(d, 'open');
    });
    Element.addClassName(theLi, 'open');
    
    // step 3 aktivieren
    var step3box = $('mustna3');
    Element.removeClassName(step3box, 'inaktiv');
    
    
     //ergebnisliste zusammenstellen 
    contentHTML = '<ul>\n';
    for(m=0;m<xmlArray[path[0]][path[1]]['orte'].length;m++) {
        contentHTML += '<li>';
        contentHTML +=xmlArray[path[0]][path[1]]['orte'][m];
        contentHTML += '</li>\n';
    }
    contentHTML += '</ul>\n';
    
    var targetHeadline = step3box.down('h4');
    targetHeadline.innerHTML = $(lnk).innerHTML;
    var targetContent = step3box.down('.scrollContent');
    targetContent.innerHTML = contentHTML;
    
    Element.show(step3box.down('h4'));
    Element.show(step3box.down('.scrollComponents'));
    Element.show(step2box.down('.backNav'));
        Element.addClassName(step2box.down('span'), 'back');
    // resize scrollContainer
        /* ACHTUNG HACK element height 216 & heading height 25 sind fix! */
          var hoffset = 216 - Element.getHeight(targetHeadline) + 25;
          /* end HACK */
        step3box.down('.scrollContainer').setStyle({height: hoffset+'px'});
        step3box.down('.scrollTrack').setStyle({height: hoffset+'px'});
        var container = 'sContainer'+scrollid;
    var content = 'sContent'+scrollid;
    var handle = 'sHandle'+scrollid;
    var track = 'sTrack'+scrollid;
    // 16 is padding
    new VerticalScroller (container, content, false, false, handle, track, 16);
    return false;
}
/* 
 * JS-Scroller
 * @author henhey, isavar, thohol
 * @copyright S2 Hamburg
 * @since 16.04.2007
 */
// speed
var scroll_diff_verti = 20; 
var scroll_diff_hori = 50;
// to re-init all Scrollers
var allScroller = new Array();
function initAllScroller(){
    for(var i=0; i<allScroller.length; i++){
        allScroller[i].init_scroller();
    }
}
/*
 * @param container {string}      id of containing div with overflow set to hidden
 * @param scroll_div {string}    id of div with a lot of content
 * @param button_top {string}      id of the up-image or false if not needed
 * @param button_bottom {string} id of the down-image or false if not needed
 * @param button_slider {string} id of the slider handle-img
 * @param track_slider {string}  id of the slider track
 */
var drag_int = null;
function VerticalScroller (container, scroll_div, button_top, button_bottom, button_slider, track_slider, padding){
    this.container_div = $(container);
    this.scroll_div = $(scroll_div);
    this.button_top = $(button_top) ? $(button_top) : false;
    this.button_bottom = $(button_bottom) ? $(button_bottom): false;
    this.button_slider = $(button_slider);
    this.theSlider = null;
    allScroller.push(this);
    
    this.container_div.setStyle({position:'relative'});
    this.scroll_div.setStyle({position:'relative'});
    // content back to top
    this.scroll_div.setStyle({top:'0px'});
     
    this.scroll_up = function (){
        var pos_now = xTop(this.scroll_div);
        if(pos_now < 0) pos_now = 0;
        var new_pos = 0;
        if (pos_now < (0-scroll_diff_verti)) {
            new_pos = (pos_now+scroll_diff_verti);
        }
        xTop(this.scroll_div, new_pos);
        var slider_value = new_pos/this.maxScroll;
        this.theSlider.setValue(slider_value);
        return false; 
    };
    this.scroll_down = function (){
        var pos_now = xTop(this.scroll_div);
        var new_pos = this.maxScroll;
        if (pos_now > (this.maxScroll+scroll_diff_verti)) {
            new_pos = (pos_now-scroll_diff_verti);
        } 
        xTop(this.scroll_div, new_pos); 
        var slider_value = new_pos/this.maxScroll;
        this.theSlider.setValue(slider_value);
        return false; 
    };
    this.dragStart_up = function(){
        this.stopDrag();
        drag_int = window.setInterval(this.scroll_up.bind(this), 100);
    };
    this.dragStart_down = function(){
        this.stopDrag();
        drag_int = window.setInterval(this.scroll_down.bind(this), 100);
    };
    this.stopDrag = function(){
        window.clearInterval(drag_int);
    };
    this.disableSlider = function(){
        Element.hide(this.button_slider);
    };
    this.enableSlider = function(){
        Element.show(this.button_slider);
    };
        
    // init
    this.init_scroller = function(){   
        this.maxScroll = -10 - (xHeight(this.scroll_div) - xHeight(this.container_div));
        var pos_now = xTop(this.scroll_div);
        if(this.maxScroll>=-padding){
            this.disableSlider();
        }
        else{
            this.enableSlider();
            var theScoller = this;
            this.theSlider = new Control.Slider(button_slider, track_slider, {axis:'vertical',
            onChange:function(v){theScoller.setPos(v);},
            onSlide:function(v){theScoller.setPos(v);}});
        }
    };
    this.init_scroller(padding);
    
    if(this.button_top) {
           this.button_top.onclick = this.scroll_up.bind(this);
        this.button_top.onmousedown = this.dragStart_up.bind(this);
    }
    if(this.button_bottom) {
           this.button_bottom.onclick = this.scroll_down.bind(this);
        this.button_bottom.onmousedown = this.dragStart_down.bind(this);
    }
    document.onmouseup = this.stopDrag.bind(this);
    
    this.setPos = function(value){
        var startPos = 0;
        var newPos = (this.maxScroll*value);
        //$('debug2').innerHTML='slide: '+newPos;
        xTop(this.scroll_div, newPos);
    };
}