/**
* 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.setActiveMenu = function(obj)
	{
		this.activeMenuObj = obj;	
		li = this.parentElement.getElementsByTagName("li");
		for(i=0;i<li.length;i++)
			if(li[i]===obj)
				this.activeMenuIndex = i;	
				
		if(!this.activeMenuIndex && this.activeMenuIndex!=0)
			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()
	{	
		li = this.parentElement.getElementsByTagName("li");
		for(i=0;i<li.length;i++){
			if(li[i].className=="active"){
				this.activeMenuObj = li[i];
				this.activeMenuIndex = i;
				return true;
			}
		}

		return false;									
	}
	
	this.setMenu = function(obj)
	{
		this.onMouseOverElement = obj;
		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.li.length==0)
			this.li = this.parentElement.getElementsByTagName("li");

		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.deleteAllActiveStatus =true;
			return true;
		}
		

		return false;
	}
	
	this.ac = function()
	{
		if(this.timeoutActive && !this.active)
		{
			li = this.parentElement.getElementsByTagName("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.active =false;
		this.timeoutActive = true;
		thisObj = this;
		this.raiseEvent("AfterUnChange",thisObj);
		setTimeout (function (){thisObj.ac()},150); 
	}


}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	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.getElementsByTagName("li").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.getElementsByTagName("li").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.getElementsByTagName("li").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)
	{
		li = this.MenuSliderUl.getElementsByTagName("li");
        if(active_obj)
            for(i=0;i<li.length;i++)
            {
                if(trim(li[i].innerHTML.replace(/[^a-zA-Z0-9_.]/g,""))==trim(active_obj.innerHTML.replace(/[^a-zA-Z0-9_.]/g,""))){
                    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");
	}
}
