﻿/**
title: 
		WFX Context Menu

pack:
		CtxMenu
		
version:
		0.0.1
		
date:
		30.06.2007

author: 
		Zhirnov Dmitriy aka N0RtAn

org:
		Mental Studio

depends: 
		wfxclient/WFX.js 	v 1.1.0
**/

CtxMenuNode = Derive(null,
{
	title:null,
	url:null,
	
	constructor: function(title, url)
	{
		this.title = title;
		this.url = url || null;
	}
	
});


CtxMenuSubItem = Derive(CtxMenuNode,
{
	constructor: function(title, url)
	{
		this.constructor.prototype.constructor.call(this, title, url);
	},
	
	create: function()
	{
		var tpl;
		tpl = subitem_template.replace(/#title#/, this.title);
		tpl = tpl.replace(/#url#/, this.url);

		return "<div>"+tpl+"</div>";
	}
});

CtxMenuItem = Derive(CtxMenuNode,
{
	constructor: function(title, url)
	{
		this.constructor.prototype.constructor.call(this, title, url);
	},
	
	create: function()
	{
		var tpl;
		tpl = item_template.replace(/#title#/, this.title);
		tpl = tpl.replace(/#url#/, this.url);
		return "<div>"+tpl+"</div>";
	}
});

CtxMenuSection = Derive(CtxMenuNode,
{
	subitems:[],
	
	constructor: function(title, url)
	{
		this.title = title;
		this.url = url;
		this.subitems = new Array();
	},
	
	add: function(title, url)
	{
		this.subitems.push(new CtxMenuSubItem(title, url));
	},
	
	create: function()
	{
		var tpl;
		tpl = section_template.replace(/#title#/, this.title);
		tpl = tpl.replace(/#url#/, this.url);
		
		var out = "<div><div>"+ tpl + "</div><div>";
		
		for(var i=0; i<this.subitems.length; ++i)
		{
			out += this.subitems[i].create();
		}
		
		out += "</div></div>";

		return out;
	}
});

CtxMenuCategory = Derive(CtxMenuNode,
{
	id:null,
	items:[],
	w:150,
	
	constructor: function(title, url, id, w)
	{
		this.title = title;
		this.url = url;
		this.id = id;
		this.w = w;
		this.items = new Array();
	},
	
	addSection: function(title, url)
	{
		var i = this.items.push(new CtxMenuSection(title, url));
		
		return this.items[i-1];
	},
	
	addItem: function(title, url)
	{
		this.items.push(new CtxMenuItem(title, url));
	},
	
	create: function()
	{
		var items = '';
		
		for(var i=0; i<this.items.length; ++i)
		{
			items += this.items[i].create();
		}

		var tpl_n;
		tpl_n = category_n_template.replace(/#title#/, this.title);
		tpl_n = tpl_n.replace(/#url#/, this.url);
		
		WFX.Opacity.SetOpacity('h_'+this.id, 0.8);

		return "<td onmouseover=\"WFX.SetVisible('h_" + this.id + "'); \" onmouseout=\"WFX.SetHidden('h_" + this.id + "'); \" >" +
		    "<div>" + tpl_n + "</div>" +
		    "<div id='h_"+this.id+"' style='visibility:hidden; position:absolute; '>"+items+"</div>"+
		"</td>";
	}
});


CtxMenu = Derive(null,
{
    name: null,
    items: [],
    cat_ids: [],
    w: null,
    ox: null,
    oy: null,

    constructor: function(name, w, ox, oy) {
        this.name = name;
        this.w = w || 150;
        this.ox = ox || 212;
        this.oy = oy || 70;
        this.items = new Array();
    },

    add: function(title, url) {
        var id = this.name + "_" + (this.items.length + 1);

        var i = this.items.push(new CtxMenuCategory(title, url, id, this.w));

        this.cat_ids.push(id);

        return this.items[i - 1];
    },

    create: function() {
        var out = "<table border='0' cellpadding='0' cellspacing='0' ><tr>";

        for (var i = 0; i < this.items.length; ++i) {
            out += this.items[i].create();

            /*if (i < this.items.length - 1) 
            {
                out += '<td style="width:2px; background-color:#bd0464; font-size:2px;">&nbsp;</td>';
            }*/
        }

        out += '</tr></table>';
        document.write(out);
    }

});



