//Objekt Category (-Pulldown)
function em_category(id,xml){
    this.id = id;
    this.xml = xml;
    this.xmldoc = null;
    this.category = null;
    this.http_request = null;
    this.loadfirst = 0;
}

//Kategorien auslesen
em_category.prototype._get_categories = function(action,part)
{
    //XMLHttpRequest-Objekt erzeugen
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        this.http_request = new XMLHttpRequest();
        if (this.http_request.overrideMimeType) {
            this.http_request.overrideMimeType("text/xml");
        }
    } else if(window.ActiveXObject) { // IE
        try {
            this.http_request = new ActiveXObject("Msxml2.DOMDocument");
        } catch(e) {
            try {
                this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                }
            }
        }
    }

    /*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
    this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
    @end @*/

    //Wenn kein Objekt vorhanden brich ab
    if (!this.http_request) {
        alert("Ende: Kann keine XMLHTTP-Instanz erzeugen");
        return false;
    }

    eval("this.http_request.onreadystatechange = function(){em_obj_" + this.id + "._initialize_categories();}");

    //XML-Datei oeffnen
    this.http_request.open("GET", this.xml, true);

    //Request senden
    this.http_request.send(null);
}

em_category.prototype._initialize_categories = function(){
    if(this.http_request.readyState == 4){
        if(this.http_request.status == 200){
            if (document.implementation && document.implementation.createDocument){
                this.xmldoc = this.http_request.responseXML;
            } else if (window.ActiveXObject){
                if (navigator.appVersion.match(/MSIE [0-6]\.\d/ )) {
/*
                    var testandoAppend = document.createElement('xml');
                    testandoAppend.setAttribute('innerHTML',this.http_request.responseText);
                    testandoAppend.setAttribute('id','_formjAjaxRetornoXML');
                    document.body.appendChild(testandoAppend);
                    document.getElementById('_formjAjaxRetornoXML').innerHTML = this.http_request.responseText;
                    this.xmldoc = document.getElementById('_formjAjaxRetornoXML');
*/
                    this.xmldoc = new ActiveXObject("Microsoft.XMLDOM");
                    this.xmldoc.async = "false";
                    this.xmldoc.loadXML(this.http_request.responseText);
                } else {
                    this.xmldoc = this.http_request.responseXML;
                }
            }

            //Elemente laden
            this.category = this.xmldoc.getElementsByTagName("category");

            //Kategorien ausgeben
            if(document.getElementById("choose_category")){
                var o = document.getElementById("choose_category");
                var og = null;
                var ot = null;
                for(var i=0; i<this.category.length; i++){
                    var item = new Object();
                    var c = i;
                    for(var j=0; j<=this.category[c].childNodes.length; j++){
                        if (this.category[c].childNodes[j]) {
                            for(var k=0; k<=this.category[c].childNodes[j].childNodes.length; k++){
                                if (this.category[c].childNodes[j].childNodes[k]
                                && (this.category[c].childNodes[j].childNodes[k].nodeValue.replace(/^\s*|\s*$/,"")) != ""
                                ){
                                    item[this.category[c].childNodes[j].nodeName] = this.category[c].childNodes[j].childNodes[k].nodeValue;
                                }
                            }
                        }
                    }
                    if(item["name"]){
                        if (!parseInt(item["belong"])) {
                            if (og != null && og.firstChild) {
                                o.appendChild(og);
                            }
                            og = null;
                        }
                        if (!item["path"]) {
                            og = document.createElement('optgroup');
                            og.label = item["name"];
                        } else {
                            ot = document.createElement('option');
                            ot.value = item["path"];
                            ot.appendChild(document.createTextNode(item["name"]));
                            if (item["name"].match(/^\t/)) {
                                ot.style.paddingLeft = "20px";
                            }
                            ot.title = item["name"];
                            if (cur_category_bar_path
                            &&  cur_category_bar_path == item["path"]
                            ) {
                                //ot.selected = true;
                                ot.className = "selOption";
                            }
                            if (og != null) {
                                og.appendChild(ot);
                            } else {
                                o.appendChild(ot);
                            }
                        }
                    }
                }
                if (og != null && og.firstChild) {
                    o.appendChild(og);
                }
            }
        }
    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//Objekt Category_Bar
function em_category_bar(id,xml){
    this.id = id;
    this.xml = xml;
    this.xmldoc = null;
    this.category = null;
    this.gallery = null;
    this.gallery_free = new Array();
    this.gallery_count = 6;
    this.gallery_pos = 0;
    this.cur_gallery_id = 0;
    this.http_request = null;
    this.steps = 1;
}

//Galerien auslesen
em_category_bar.prototype._request = function(action,part){
    //XMLHttpRequest-Objekt erzeugen
    if(window.XMLHttpRequest){ // Mozilla, Safari,...
        this.http_request = new XMLHttpRequest();
        if(this.http_request.overrideMimeType){
            this.http_request.overrideMimeType("text/xml");
        }
    }else if(window.ActiveXObject){ // IE
        try {
            this.http_request = new ActiveXObject("Msxml2.DOMDocument");
        } catch(e) {
            try {
                this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                }
            }
        }
    }

    /*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
    this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
    @end @*/

    //Wenn kein Objekt vorhanden brich ab
    if(!this.http_request){
        alert("Ende: Kann keine XMLHTTP-Instanz erzeugen");
        return false;
    }

    if(action == "init"){
        eval("this.http_request.onreadystatechange = function(){em_obj_"+this.id+"._initialize();}");
    }else if(action == "next" || action == "back"){
        eval("this.http_request.onreadystatechange = function(){em_obj_"+this.id+"._blaetter(action,part);}");
    }else{
        return false;
    }

    //XML-Datei oeffnen
    this.http_request.open("GET", this.xml, true);

    //Request senden
    this.http_request.send(null);
}

em_category_bar.prototype._initialize = function(){
    if(this.http_request.readyState == 4){
        if(this.http_request.status == 200){
            if (document.implementation && document.implementation.createDocument){
                this.xmldoc = this.http_request.responseXML;
            } else if (window.ActiveXObject){
                var testandoAppend = document.createElement('xml');
                testandoAppend.setAttribute('innerHTML',this.http_request.responseText);
                testandoAppend.setAttribute('id','_formjAjaxRetornoXML');
                document.body.appendChild(testandoAppend);
                document.getElementById('_formjAjaxRetornoXML').innerHTML = this.http_request.responseText;
                this.xmldoc = document.getElementById('_formjAjaxRetornoXML');
            }

            //Elemente laden
            this.gallery = this.xmldoc.getElementsByTagName("gallery");

            //Pr�fe ob Galerie-Templates belegt
            for(var i=0; i<6; i++){
              var next = i + 1;
                var mItem = document.getElementById("gallery_teaser_"+ next);
                if( mItem){
                    id = this.get_id(mItem);
                    if( id == "" || id.match(/item/)){
                        this.gallery_free.push(i);
                    }else{
                        this.count--;
                    }
                }
            }

            //Teaser-Navigation
            if( this.gallery.length > 0){
                for(var i=0; i<6; i++){
                      this._item(0,i);
                }
                if( this.gallery.length > this.gallery_count){
                    if( document.getElementById("gallery_teaser_next")){
                        document.getElementById("gallery_teaser_next").style.display="inline";
                    }
                    if( document.getElementById("gallery_teaser_back")){
                        document.getElementById("gallery_teaser_back").style.display="inline";
                    }
                }
            }
        }
    }
}

//Das eigentliche umblaettern, Daten aus dem XML-Objekt ziehen
em_category_bar.prototype._blaetter = function(richtung){
  if (!this.gallery
  || this.gallery.length <= 6){
    return;
  }

  var lpart = "";
  lpart = "gallery_teaser";

    if(this.http_request.readyState == 4){
        if(this.http_request.status == 200){

            this.xmldoc = this.http_request.responseXML;

            var n = Number.NaN;
            var mItem = document.getElementById(lpart+"_1");
            if( mItem && this.get_id(mItem)){
                    id = this.get_id(mItem);
                    if( id.match(/item/)){
                        n = parseInt(id.replace(/\D/g,""));
                    }
            }
            for(var i=0; i<6; i++){
              var next = i + 1;
                var mItem = document.getElementById(lpart+"_"+ next);
                if(mItem && this.get_id(mItem)){

                    id = this.get_id(mItem);
                    if( id.match(/item/)){
                        for(var i=mItem.childNodes.length; i>=0; i--){
                            cItem = mItem.childNodes[i];
                            if(cItem){
                                if( cItem.id && isNaN(n)){
                                    n = parseInt(cItem.id.replace(/\D/g,""));
                                }
                                mItem.removeChild(cItem);
                            }
                        }
                    }
                }
            }

            if( richtung == "back"){
                    n-= this.steps;
            }else{
                    n+= this.steps;
            }


      if( richtung == "back"){
                    this.gallery_pos -= this.steps;
                    if(this.gallery_pos<0){
                      this.gallery_pos = this.gallery.length-1;
                    }
            }else{
                    this.gallery_pos += this.steps;
                    if(this.gallery_pos > this.gallery.length-1){
                      this.gallery_pos = 0;
                    }
            }

            //Elemente laden
          this.gallery = this.xmldoc.getElementsByTagName("gallery");

            for(var i=0; i<6; i++){
                this._item(n,i);
            }
        }
    }
}

em_category_bar.prototype.get_id = function(o){
    var id = 0;
    for(var j=0; j<=o.childNodes.length; j++){
        if( o.childNodes[j]
        && o.childNodes[j].id
        ){
            return o.childNodes[j].id;
        }else if( o.childNodes[j]){
            for(var k=0; k<=o.childNodes[j].childNodes.length; k++){
                if( o.childNodes[j].childNodes[k]
                && o.childNodes[j].childNodes[k].id
                ){
                    return o.childNodes[j].childNodes[k].id;
                }
            }
        }
    }
    return "";
}


em_category_bar.prototype._show = function(path,c){
  var image_host;
  if(typeof em_obj_gallery != "undefined"){
    image_host = em_obj_gallery.image_host;
    em_obj_gallery = null;
  }

  em_obj_gallery = new em_gallery("gallery", path, '');
  em_obj_gallery.image_host = image_host;
  em_obj_gallery._request("init");
}


em_category_bar.prototype._item = function(n,i){
  if( i < this.gallery_count
      && typeof this.gallery_free[i] != "undefined"
    ){
    c = n + i;
        //Position fixen
        if(this.gallery.length > 6) {
          if( c >= this.gallery.length){
                  c = c - this.gallery.length;
          }else if( c < 0){
                  c = c + this.gallery.length;
          }
      }

        //alert("n:" + n + " i:" + i + " c:" + c );
        if( this.gallery[c]){
            var item = new Object();
            for(var j=0; j<=this.gallery[c].childNodes.length; j++){
                if( this.gallery[c].childNodes[j]){
                    for(var k=0; k<=this.gallery[c].childNodes[j].childNodes.length; k++){
                        if( this.gallery[c].childNodes[j].childNodes[k]
                        && (this.gallery[c].childNodes[j].childNodes[k].nodeValue.replace(/^\s*|\s*$/,"")) != ""
                        ){
                            item[this.gallery[c].childNodes[j].nodeName] = this.gallery[c].childNodes[j].childNodes[k].nodeValue;
                        }
                    }
                }
            }

          if(this.loadfirst && i == 0) {
              em_obj_category_bar._show(item["path"],c);
          }

          if( item["path_thumb"]){
            var next = parseInt(this.gallery_free[i])+1;
                var mItem = document.getElementById("gallery_teaser_"+next);
                if(mItem){
                    var cItem = document.createElement("div");
                            cItem.id = "item_cat_"+c;
                            if(cur_gallery_path){
                              if(item["path"] == cur_gallery_path){
                  //mItem.style.backgroundColor = "#00943F";
                              }else{
                  //mItem.style.backgroundColor = "";
                              }
                          }

                          if(item['description'] == undefined) {
                               item['description'] = '';
                          }
                            cItem.innerHTML =  "<div style=\"cursor:pointer; margin:5px 5px 0px 0px; text-align:center; background-color:black; float:left; width:70px; height:62px;\"><img onClick=\"em_obj_category_bar._show('" + item["path"] + "','" + c + "');\" src=\"" + item["path_thumb"] + "\" ></div>";
                            cItem.innerHTML += "<div style=\"margin-top:3px; float:left; width:150px; overflow:hidden;\"><b>" + item['title'] + "<br />" + item['date'] + "</b><br />" + item['description'] + "</div>";
                    mItem.innerHTML = "";
                    mItem.appendChild(cItem);
                }
            }
        }
    }
}
