/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function menu(initVal){

    this.parentNode = initVal.parentNode;
    this.baseDiv = null;
    this.tresc = initVal.tresc;
    this.tresc.menu = this;
    this.menu_idems = Array();
    this.menu_idems_first = Array();
    this.mainXML = null;
    this.treeType = (initVal.treeType)?initVal.treeType : "ul";
    
    this.init = function(){

        var menubutton = document.getElementById("gui").appendChild(document.createElement('div'));
        menubutton.setAttribute("id", "menubutton");
        menubutton.appendChild( document.createTextNode("M"));
        menubutton.appendChild(document.createElement('br'));
        menubutton.appendChild( document.createTextNode("E"));
        menubutton.appendChild(document.createElement('br'));
        menubutton.appendChild( document.createTextNode("N"));
        menubutton.appendChild(document.createElement('br'));
        menubutton.appendChild( document.createTextNode("U"));
        menubutton.appendChild(document.createElement('br'));
          
        if($("#flagi").get(0))
            this.flagiDiv = $("#flagi").get(0)
        else
            this.flagiDiv = $("div.logo").get(0).appendChild(document.createElement('div'));
        $(this.flagiDiv).addClass("flagi");
        var img = this.flagiDiv.appendChild(document.createElement('img'));
        img.setAttribute("src", "http://89.161.149.186/landscape/art/flag_de.png");
        img.to = this;
        $(img).click(function(){
            lng = ".de";
            $(".de").show();
            $(".pl").hide();
            $(".en").hide();
            window.location. hash="!"+$(this).get(0).to.getFrist(lng);
        //   window.location.replace(/([d]+)$/,);    
        });
        
        img = this.flagiDiv.appendChild(document.createElement('img'));
        img.setAttribute("src", "http://89.161.149.186/landscape/art/flag_en.png");
        img.to = this;
        $(img).click(function(){
            lng = ".en";
            $(".de").hide();
            $(".pl").hide();
            $(".en").show();
            
            window.location. hash="!"+$(this).get(0).to.getFrist(lng);
        });
        
        img = this.flagiDiv.appendChild(document.createElement('img'));
        img.setAttribute("src", "http://89.161.149.186/landscape/art/flag_pl.png");
        img.to = this;
        $(img).click(function(){
            $(".de").hide();
            $(".pl").show();
            $(".en").hide();
            lng = ".pl";
            window.location. hash="!"+$(this).get(0).to.getFrist(lng);
        });
        
        this.baseDiv = this.parentNode.appendChild(document.createElement('div'));
        this.baseDiv.setAttribute("id", "menuCMS");
        this.baseDiv.to = this;
        this.ULmenu = this.create_menu(this.baseDiv);
    
        $(this.tresc.baseDiv).mouseover(function(){
            $('div.menu td ul').fadeOut("5000",function(){
               // $(this).css("height", $(this).outerHeight());
                        
            });
        });
    };

    this.initWin = function(){
        this.win = new filip_owl({
            parentNode:this.parentNode,
            WinName:"",
            WindowId:"menu",
            zIndex:1,
            move:1
        });
        this.parentNode = this.win.WinMain;
        this.baseDiv = this.win.WinMain;
    };
    
    this.create_menu = function(node){
        
        switch(this.treeType){
             
            case "ul":
                var menu = node.appendChild(document.createElement('ul'));
                break;
            case "table":
                if(node.div){
                   var menu = node.appendChild(document.createElement('ul'));
                break; 
                }
                var menu = node.appendChild(document.createElement('table'));
                menu = menu.appendChild(document.createElement('tbody'));menu = menu.appendChild(document.createElement('tr'));
                break;
        }
        
        
        menu.node = node;
        return menu;
    };

    this.create_menu_item = function(menu,xml){
        
        var tmp_array = $(xml.getElementsByTagName("nazwa")[0]).text().match(/\w+;/g);
        if(!tmp_array) tmp_array = $(xml.getElementsByTagName("nazwa")[0]).text();

        if(!xml.getElementsByTagName("nazwa")[0])
            return false;
        if(!menu)
            return false;
        
        switch(this.treeType){
             
            case "ul":
                var node = menu.appendChild(document.createElement('li'));
                break;
            case "table":
                 if(menu != this.ULmenu){
                  var node = menu.appendChild(document.createElement('li'));
                break; 
                }
                var node = menu.appendChild(document.createElement('td'));
                break;
        }
        
        node = node.appendChild(document.createElement('div'));
        var i = 0;
        // alert(tmp_array)
        // while(tmp_array[i])
        {
            var lang = tmp_array.replace(/;/, "").replace(/([\s\S]+_+)/,"");
            if(lang == "współpl") lang = "pl";
            var nazwa = tmp_array.replace(/;/, "").replace(/(_+[a-zA-Z]+)$/,"");
            node.div = node.appendChild(document.createElement('div'));
            if(lang != nazwa) $(node.parentNode).addClass(lang);
            node.div.a = node.div.appendChild(document.createElement('a'));
            $(node.div.a).html(nazwa);
            
           // if(this.treeType == "table")
             
             
            
                node.setAttribute("pri", this.ajax.getText(xml.getElementsByTagName("pri")[0]));
            node.div.setAttribute("idd", this.ajax.getText(xml.getElementsByTagName("idrozdzialy")[0]));
            node.div.id  = this.ajax.getText(xml.getElementsByTagName("idrozdzialy")[0]);
            node.div.a.setAttribute("href", '#!'+this.ajax.getText(xml.getElementsByTagName("idrozdzialy")[0]));
            this.setFrist({
                id: this.ajax.getText(xml.getElementsByTagName("idrozdzialy")[0]), 
                lng: "."+lang 
            })
            i++;
        }
        node.div.owner = this;
        node.div.menu = menu;
        
        this.menu_idems[node.div.id] = node;
        if(xml.getElementsByTagName("menu")[0])
            if(xml.getElementsByTagName("menu")[0].getElementsByTagName("menu_item")[0].getElementsByTagName("nazwa")[0]){
                node.div.chasMenu = true;
                menu.chasSubMenu = true;
                $(node.div).addClass("hasSubMenu");
                $(node.div.a).addClass("hasSubMenu");
                var tmpXML = xml.cloneNode(true);
                node.sxml = tmpXML.getElementsByTagName("menu")[0].cloneNode(true);
                $(xml.getElementsByTagName("menu")[0]).remove()
                node.div.smenu = this.create_menu(node);
                
                this.fill_menu(node.div.smenu,node.sxml);
                if(this.treeType == "table" && menu.parentNode != this.ULmenu)
                { 
                    $(node.div).mouseover(function(){
                        $($(this).get(0).smenu).fadeIn("5000",function(){
                            //$(this).css("height", $(this).outerHeight());
                        
                        }); 
                    //alert($($(this).get(0).smenu.parentNode).outerHeight())
                    })  
                    $(node.div.smenu).hide();
            
                    $(node.div.smenu).mouseleave(function(){
                  
                        $(this).fadeOut("slow");
                    })  
                }
            }
        return xml;
    };

    this.select_menu_idem = function(id){
        

        //if(this.prev_selectet)
        //
        //            var target = this.get_menu_idem_byID(this.prev_selectet.replace(/!/g, ""));
        
        //        if (target == undefined)
        //            return null;




        this.prev_selectet = id;
        // alert(id);
        var target = this.get_menu_idem_byID(id.replace(/!/g, ""));
        if (target == undefined)
            return null;
       
        $(target).find('div.hasSubMenu').each(function(index) {
            $($(this).get(0).smenu).slideDown("normal");;
        });
        
        
        
        try{
            this.show_menu_idem(target);
        }
        catch(e){
            alert("ten Error menu this.select_menu_idem :"+e)
        }
        

        if(target.div != undefined)
            if(target.div.smenu != undefined) $((this.treeType != "table")?target.div.smenu:target.div.smenu.parentNode).slideDown("slow");

        $(".selectedMenuItem").removeClass("selectedMenuItem");
        $(target.div.a).addClass("selectedMenuItem");
        this.tresc.ajax.spytaj("viev="+id.replace(/!/g, ""));
        this.tresc.ajax.set_hash(id);
    };

    this.get_menu_idem_byID = function(id){
        return this.menu_idems[id]
    };
    
    this.show_menu_idem = function(node){
        if(node != undefined && this.treeType != 'table')
            if(node.div != undefined)
                if(node.div.menu != undefined){
                    $(node.div.menu).show();
                    this.show_menu_idem(node.div.menu.node);
                }
    };
    
    this.fill_menu = function(menu,xml){
 
        if (xml.getElementsByTagName("menu_item"))
            if(xml.getElementsByTagName("menu_item").length){
                var i = 0;
                var iterator = xml.getElementsByTagName("menu_item")[0].parentNode.cloneNode(true).childNodes;
                while( iterator[i] ){
                    this.create_menu_item(menu,iterator[i]);
                    i++;
                }      
            }
        if(menu != this.ULmenu && this.treeType != 'table') $(menu).hide();
    };
    this.setFrist = function(ivar){
        this.menu_idems_first 
        var i = 0;
        var iterator = this.menu_idems_first;
        while( iterator[i] ){
            if(iterator[i].lng == ivar.lng) return false;
            i++;
        }
        this.menu_idems_first.push(ivar)
        return true;
    };
    this.getFrist = function(lng){
        this.menu_idems_first 
        var i = 0;
        var iterator = this.menu_idems_first;
        while( iterator[i] ){
            if(iterator[i].lng == lng) return iterator[i].id;
            i++;
        }
        return false;
    };
    
    
    this.response = function(){

        this.owner.fill_menu(this.owner.ULmenu,this.req.responseXML);
        if ( window.location.hash != "" ) {
            var current_hash = window.location.hash;
            this.owner.select_menu_idem( current_hash.substr( 1,  current_hash.length) );
        }
        else
            this.owner.select_menu_idem(this.getText(this.req.responseXML.getElementsByTagName("idrozdzialy")[0]));
    };

    this.mk_link = function(nodes){
        if(!nodes)
            nodes = this.tresc.get_links();
        var i = 0;
        while( nodes[i] ){
            nodes[i].id  = nodes[i].getAttribute("id");
            nodes[i].owner = this;

            $(nodes[i]).click(function(oEvent){
                $(this).get(0).owner.select_menu_idem($(this).get(0).id);
            });
            i++;
        }
    };

    this.cange_bg = function(node){
        if(!node)
            node = this.tresc.get_links();
        if(!this.currentbgClass && !node){
            this.currentbgClass = "none";
            $("html").addClass(this.currentbgClass);
        }

        $("html").switchClass(this.currentbgClass,node, 100000);
    };

    this.rm = function(){
        this.parentNode.parentNode.removeChild(this.parentNode);
    };

    this.initWin();
    this.ajax = new filip_ajax(this, this.response, this.win.load, this.win,initVal.dep,"ajax");
    this.ajax.spytaj("edit=init");
    this.getDiv = function(){
        return this.baseDiv;
    }
}
