/**
* Menu ver 1.0
* (c) 2009 Gawsoft
* www: http://www.gawsoft.pl
*/


function Menu(p)
{
	
	var active = false;
	var timeoutActive = false;
	var obj = new Object();
	
	this.parentElement = p;
	this.activeMenuObj = null;
	this.activeMenuIndex = null;

	this.deleteAllActiveStatus = true;
	this.onMouseOverElement = null;
	this.onMouseOverIndex = null;
		
	//gdy zmienia sie obiekt
	this.onChange = function(){};
	//po zmianie obiektu
	this.onAfterUnChange = function(){};
	//gdy traci focus z glownym elementem a ustawil juz aktywny domyslny element
	this.onParentMouseOutChange = function(){};
	//gdy traci focus
	this.onUnChange = function(){};
	//gdy myszka traci focus z parentElement
	this.onParentMouseOut = function(){};
	this.li = new Array();
        this.defaultClassNameElement = "";

        this.set_elements = function()
        {
            elements = this.parentElement.childNodes;
        
            for(i=0;i<elements.length;i++)
            {
                 if(elements[i].nodeType==1)
                 {
                     var thisObjMenu = this;
                     elements[i].onclick = function(){thisObjMenu.changeMenu(this)};
                    // elements[i].onmouseout = function(){thisObjMenu.unsetMenu(this)};
                     elements[i].onmouseover = function(){thisObjMenu.setMenu(this)};
                     this.li[this.li.length] = elements[i];
                 }


            }
        }

        this.isEmpty = function(s)
        {
            return ((s == null) || (s.length == 0))
        }

	this.setActiveMenu = function(obj,index)
	{
            if(obj){
                this.activeMenuObj = obj;
            }

            if(!this.isEmpty(index)){
                    this.activeMenuIndex = index;

                if(obj)
                    return true;
            }

            for(i=0;i<this.li.length;i++){
                if(this.li[i]===obj){
                    this.activeMenuIndex = i;
                    return true;
                }
            }

           throw "Nie znaleziono indexu dla aktywnego obiektu";
		
	}
	
	this.changeMenu = function(obj)
	{
            this.activeMenuObj = obj;
            this.setMenu(obj);
	}

    
	this.raiseEvent = function (name) {
            var args = [];
            for (var i = 1; i < arguments.length; i++)
                args.push(arguments[i]);

            if (typeof this["on" + name] == "function")
                this["on" + name].apply(null, args);

            if (name == "FatalError")
                alert("errror");

        }
	
	this.searchActive = function()
	{	
            for(i=0;i<this.li.length;i++){
                if(this.li[i].className=="active"){
                    this.activeMenuObj = li[i];
                    this.activeMenuIndex = i;
                    return true;
                }
            }

            return false;
	}
	
	this.setMenu = function(obj)
	{          
            this.onMouseOverElement = obj;
            for(k=0;k<this.li.length;k++)
                if(this.onMouseOverElement===this.li[k])
                     this.onMouseOverIndex = k;
        
            if(!this.deleteAllActive())
                throw "Nie znaleziono elementów li dla menu";
 
            obj.className = "active";
           
            this.raiseEvent("Change",this);

            this.active = true;
            this.timeoutActive = false;
     
	}
	
	this.deleteAllActive = function()
	{
            if(!this.parentElement)
                throw "Nie znaleziono głównego elementu menu";
            if(!this.onMouseOverElement)
                return;
            
            if(this.li.length>0)
            {
                for(i=0;i<this.li.length;i++)
                {
                    if(trim(this.li[i].innerHTML)==trim(this.onMouseOverElement.innerHTML))
                        this.onMouseOverIndex = i;

                    this.li[i].className = this.defaultClassNameElement;
                }

                this.deleteAllActiveStatus =true;
                return true;
            }


            return false;
	}
	
	this.ac = function()
	{
          
            if(this.timeoutActive && !this.active)
            {
                li = this.li[this.activeMenuIndex];
            
                if(li)
                {
                    if(li.className!="active" && trim(li.innerHTML)===trim(this.activeMenuObj.innerHTML)){
                        li.className = "active";
                       
                        this.raiseEvent("ParentMouseOutChange",this);
                    }
                    this.raiseEvent("ParentMouseOut",this);
                }

                this.deleteAllActiveStatus =true;
            }
	}
	
	this.unsetMenu = function(obj)
	{
        
            if(this.activeMenuObj!==this.onMouseOverElement)
                obj.className=this.defaultClassNameElement;

            this.active =false;
            this.timeoutActive = true;
            thisObjMenu = this;
            this.raiseEvent("AfterUnChange",thisObjMenu);
            setTimeout (function (){thisObjMenu.ac()},150);
	}


}

// Removes leading whitespaces
function LTrim( value ) {
    if(!value)
        return null;

    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
    if(!value)
        return null;

    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
    if(!value)
        return null;
    
    return LTrim(RTrim(value)).replace(/ /,"");
	
}

function MenuSlider(ul_menu)
{
    /*
     * Tablica elementow
     */
	this.menuItems = new Array();
    /**
     * Akce podczas zmiany uzywac do tworzenia nowego obiektu li
     */
	this.onChange = function(){};
    /**
     * Akcje po zmianie
     */
	this.onAfterChange = function(){};
	/**
     * Ile elementow ma pokazywac
     */
    this.showItems = 6;
    /**
     * Aktualny index od ktorego rozpozyna wyswietlanie, nie mylic z aktywnym elementem
     */
	this.actualIndex = 0;
    /**
     * Nowy element jeli pusty wyrzuci wyjatek
     */
	this.newItem = null;
    /**
     * Glowny elementu menu musi byc ul
     */
	this.MenuSliderUl = ul_menu;
	//1 up | 0 -down
    this.mode = null;
    //object to remove
    this.toRemove = null;

    /**
     * Dodaje nowy element do menu
     */
	this.addItem = function(obj)
	{
		this.menuItems[this.menuItems.length] = obj;	
	}
    /**
     * Ustawia aktywny index menu
     */
    this.setActiveIndex = function(i)
    {
        this.actualIndex = i;
    }

   	this.changeDown = function(obj,active_obj)
	{
        this.mode = 0;

        newElementIndex = this.actualIndex + this.showItems;
		if(this.menuItems[newElementIndex])
		{
            this.toRemove = this.MenuSliderUl.childNodes.item(0);
            
			this.raiseEvent("Change",this,(newElementIndex));
			if(!this.newItem)
				throw "Brak nowego obiektu";

			if(!this.MenuSliderUl)
				throw "Nie ma elementu UL";

			this.MenuSliderUl.removeChild(this.toRemove);
			this.MenuSliderUl.appendChild(this.newItem);
			this.newItem = null;
			this.searchActiveMenu(active_obj);
           
			this.actualIndex++;
          
			this.raiseEvent("AfterChange",this);
            this.toRemove = null;
		}

	}

	this.changeUp = function(obj,active_obj)
	{
        this.mode = 1;

        newElementIndex = this.actualIndex - 1;
        if(this.menuItems[newElementIndex])
        {
            this.toRemove = this.MenuSliderUl.childNodes.item(this.showItems-1);
            this.raiseEvent("Change",this,(newElementIndex));
            if(!this.newItem)
                throw "Brak nowego obiektu";

            if(!this.MenuSliderUl)
                throw "Nie ma elementu UL";

            this.MenuSliderUl.removeChild(this.toRemove);
            this.MenuSliderUl.insertBefore(this.newItem,this.MenuSliderUl.childNodes.item(0));
            this.newItem = null;
            this.searchActiveMenu(active_obj);
            this.actualIndex--;
            this.raiseEvent("AfterChange",this);
            this.toRemove = null;
        }
       


	}
    /**
     * Szuka aktywnego elementu wedlug active_obj
     */
	this.searchActiveMenu = function(active_obj)
	{
	
            if(active_obj)
                for(i=0;i<this.li.length;i++)
                {
                    if(trim(this.li[i].innerHTML.replace(/[^a-zA-Z0-9_.]/g,""))==trim(active_obj.innerHTML.replace(/[^a-zA-Z0-9_.]/g,""))){
                        this.li[i].className = "active";

                    }
               }
	}

    
	this.deleteItem = function(obj,active_obj)
	{
		change = false;
		for(i=0;i<this.menuItems.length;i++)
                    if(this.menuItems[i]===obj || change)
                    {
                        change = true;
                        if(i>0)
                            this.menuItems[i] = this.menuItems[i+1];
                        else
                            this.menuItems[0] = null;
                    }
	}
	
	this.raiseEvent = function (name) {
        
		var args = [];
		for (var i = 1; i < arguments.length; i++)
			args.push(arguments[i]);

		if (typeof this["on" + name] == "function")
			this["on" + name].apply(null, args);
			
		if (name == "FatalError")
			alert("errror");
	}
}

