// JavaScript Document

var ALIGN_LEFT=1;
var ALIGN_RIGHT=2;
var ALIGN_TOP=3;
var ALIGN_BOTTOM=4;
var ALIGN_CENTER=5;

// CFramework class
function CFramework()
{
	this.classes=new Array();		// Array of styles
	this.menus=new Array();
	this.addStyle=function(style){this.classes[style.name]=style;}
	this.addMenu=function(menu) {this.menus[menu.id]=menu;}
	this.exportClasses=function()
	{
		document.write('<style type=\"text/css\">');
		for (var i in this.classes) this.classes[i].exportClass();
		document.write('</style>');
	}
	this.value=function()
	{
		var html='';
		for (var i in this.classes)
		{
			html+=' .'+this.classes[i].name+'{'+this.classes[i].value()+'}<br>'
		}
		return html;
	}
	this.init=function()
	{
		this.exportClasses();
		for (var i in this.menus) this.menus[i].init();
	}
}
// Border class
function CBorder(leftwidth,leftstyle,leftcolor,rightwidth,rightstyle,rightcolor,topwidth,topstyle,topcolor,bottomwidth,bottomstyle,bottomcolor)
{
	this.leftwidth=leftwidth;
	this.leftstyle=leftstyle;
	this.leftcolor=leftcolor;
	this.rightwidth=rightwidth;
	this.rightstyle=rightstyle;
	this.rightcolor=rightcolor;
	this.topwidth=topwidth;
	this.topstyle=topstyle;
	this.topcolor=topcolor;
	this.bottomwidth=bottomwidth;
	this.bottomstyle=bottomstyle;
	this.bottomcolor=bottomcolor;
	this.value=function()
	{
		var html='';
		if (this.leftwidth > '')
			html+=' border-left:'+this.leftwidth+' '+this.leftstyle+' '+this.leftcolor+';';
		if (this.rightwidth > '')
			html+=' border-right:'+this.rightwidth+' '+this.rightstyle+' '+this.rightcolor+';';
		if (this.topwidth > '')
			html+=' border-top:'+this.topwidth+' '+this.topstyle+' '+this.topcolor+';';
		if (this.bottomwidth > '')
			html+=' border-bottom:'+this.bottomwidth+' '+this.bottomstyle+' '+this.bottomcolor+';';
		return html;
	}
}
// Padding class
function CPadding(left,right,top,bottom)
{
	this.left=left;
	this.right=right;
	this.top=top;
	this.bottom=bottom;
	this.value=function()
	{
		var html='';
		if (this.left) html+=' padding-left:'+this.left+';';
		if (this.right) html+=' padding-right:'+this.right+';';
		if (this.top) html+=' padding-top:'+this.top+';';
		if (this.bottom) html+=' padding-bottom:'+this.bottom+';';
		return html;	
	}
}
// Font class
function CFont(family,size,style,weight)
{
	this.family=family;
	this.size=size;
	this.style=style;
	this.weight=weight;
	this.value=function()
	{
		var html='';
		if (this.family) html+=' font-family:'+this.family+';';
		if (this.size) html+=' font-size:'+this.size+';';
		if (this.style) html+=' font-style:'+this.style+';';
		if (this.weight) html+=' font-weight:'+this.weight+';';
		return html;
	}
}
// Position class
function CPosition(xalign,xoffset,yalign,yoffset)
{
	this.xalign=xalign;
	this.xoffset=xoffset;
	this.yalign=yalign;
	this.yoffset=yoffset;
}
// Image Class
function CImage(url,width,height,align)
{
	this.url=url;
	this.width=width;
	this.height=height;
	this.align=align;
	this.value=function()
	{
		var html='';
		html='<img src=\''+this.url+'\'';
		if (this.width) html+=' width=\''+this.width+'\'';
		if (this.height) html+=' height=\''+this.height+'\'';
		if (this.align) html+=' align=\''+this.align+'\'';
		html+='>';
		return html;
	}
}
// Background Image class
function CBGImage(url,repeatx,repeaty)
{
	this.url=url;
	this.repeatx=repeatx;
	this.repeaty=repeaty;
	this.value=function()
	{
		var html='';
		if (this.url)
		{
			html+=' background-image:url(\''+this.url+'\');';
			if (this.repeatx && this.repeaty)
				html+=' background-repeat:repeat;';
			else if (this.repeatx)
				html+=' background-repeat:repeat-x;';
			else if (this.repeaty)
				html+=' background-repeat:repeat-y;';
			else
				html+=' background-repeat:no-repeat;';
		}
		return html;
	}
}
// Style Class
function CStyle(name,width,height,position,bgcolor,bgimage,fgcolor,font,border,padding,text,isVisible,zindex,extra)
{
	this.name=name;
	this.position=position;
	this.width=width;
	this.height=height;
	this.bgcolor=bgcolor;
	this.bgimage=bgimage;
	this.fgcolor=fgcolor;
	this.border=border;
	this.padding=padding;
	this.text=text;
	this.font=font;
	this.isVisible=isVisible;
	this.zindex=zindex;
	this.extra=extra;
	this.value=function()
	{
		var html='';
		if (this.position) html+='position:'+this.position+';';
		if (!this.isVisible) html+='visibility:hidden;';
		if (this.width) html+='width:'+this.width+';';
		if (this.height) html+='height:'+this.height+';';
		if (this.border) html+=this.border.value();
		if (this.padding) html+=this.padding.value();
		if (this.text) html+=this.text.value();
		if (this.bgcolor) html+='background-color:'+this.bgcolor+';';
		if (this.bgimage) html+=this.bgimage.value();
		if (this.fgcolor) html+='color:'+this.fgcolor+';';
		if (this.font) html+=this.font.value();
		if (this.zindex) html+='z-index:'+this.zindex+';';
		if (this.extra) html+=extra;
		return html;
	}
	this.exportClass=function() 
	{
		document.write(' .'+this.name+'{'+this.value()+'}');
	}
}
// Menu class
function CMenu(id,className,position,style,arrow)
{
	this.id=id;
	this.className=className;
	this.parent=null;
	this.style=style;
	if (position)
		this.position=position;
	else
		this.position=new CPosition(ALIGN_LEFT,0,ALIGN_BOTTOM,0);
	this.arrow=arrow;
	this.hideTimeout=0;
	this.isMouseOver=false;
	this.value=function()
	{
		var html='<div id=\''+this.id+'\'';
		if (this.style)	html+= ' style=\''+this.style.value()+'\'';
		if (this.className) html+=' class=\''+this.className+'\'';
		html+=' onMouseOver=\'framework.menus["'+this.id+'"].mouseOver()\' onMouseOut=\'framework.menus["'+this.id+'"].hide()\'';
		html+='>';
		// Write Menu items here
		for (var i=0;i<this.items.length;i++)
			html+=this.items[i].value();	
		html+='</div>';
		return html;
	}
	this.items=new Array();
	this.addItem=function(text,url,onClass,overClass,menu)
	{
		var menuItem=new CMenuItem(text,url,onClass,overClass,menu);
		menuItem.parent=this;
		this.items[this.items.length]=menuItem;
	}
	this.init=function()
	{
		for (var i=0;i<this.items.length;i++)
			if (this.items[i].menu) framework.menus[this.items[i].menu].parent=this;

		document.write(this.value());
		this.object = new CObject(this.id);
	}
	this.show=function(el)
	{
		this.isMouseOver=true;
		var x=0;
		var y=0;
		if (this.hideTimeout > 0) this.resetTimeout();
		
		var obj=new CObject(el);
		switch (this.position.xalign)
		{
			case 1:
				x=obj.left();
				break;
			case 2:
				x=obj.left()+obj.width();
				break;
			case 5:
				x=obj.left()+(Math.round(obj.width()/2));
				break;
		}
		x+=this.position.xoffset;
		switch (this.position.yalign)
		{
			case 3:
				y=obj.top();
				break;
			case 4:
				y=obj.top()+obj.height();
				break;
			case 5:
				y=obj.top()+(Math.round(obj.height()/2));
				break;
		}
		y+=this.position.yoffset;
		this.object.move(x,y);
		this.object.show();
	}
	this.mouseOver=function()
	{
		this.isMouseOver=true;
		this.resetTimeout();
	}
	this.resetTimeout=function()
	{
		if (this.hideTimeout > 0)
		{
			clearTimeout(this.hideTimeout);
			this.hideTimeout=0;
			if (this.parent)
				this.parent.resetTimeout();
		}
	}
	this.hide=function(ms)
	{
		this.isMouseOver=false;
		var obj=this;
		var timeoutFunc=function() {obj.onHideTimeout(true)};
		this.hideTimeout=setTimeout(timeoutFunc,200);
	}
	this.onHideTimeout=function(children)
	{
		if (children)
		{
			for (var i=0;i<this.items.length;i++)
				if (this.items[i].menu)
					framework.menus[this.items[i].menu].object.hide();	
		}
		this.object.hide();
		if (this.parent && this.parent.isMouseOver == false) this.parent.onHideTimeout(false);
	}

	this.move=function(x,y)	{this.object.move(x,y);}
	this.left=function() {this.object.left();}
	this.top=function() {this.object.top();}
	this.width=function() {this.object.width();}
	this.height=function() {this.object.height();}
}
// Text class
function CText(align,height,decoration,indent,transform)
{
	this.align=align;
	this.height=height;
	this.decoration=decoration;
	this.indent=indent;
	this.transform=transform;
	
	this.value=function()
	{
		var html='';
		if (this.align) html+='text-align:'+this.align+';';
		if (this.height) html+='line-height:'+this.height+';';
		if (this.decoration) html+='text-decoration:'+this.decoration+';';
		if (this.indent) html+='text-indent:'+this.indent+';';
		if (this.transform) html+='text-transform:'+this.transform+';';
		return html;
	}
}
// Menu Item class
function CMenuItem(text,url,onClass,overClass,menu)
{
	this.text=text;
	this.url=url;
	this.onClass=onClass;
	this.overClass=overClass;
	this.menu=menu;
	this.parent=null;
	this.value=function()
	{
		var html='';
		html+='<table border=\'0\' cellspacing=\'0\' cellpadding=\'0\'';
		if (this.onClass) html+=' class=\''+this.onClass+'\'';
		html+=' style=\'cursor:pointer;cursor:hand;\'';
		if (this.url)
		{
			html+=' onClick=\'';
			if (typeof this.url == 'string')
				html+='javascript:location=\"'+this.url+'\"';
			else
				html+=this.url.value();
			html+='\'';
		}

		if (this.overClass)
		{
			html+=' onMouseOver=\'this.className=\"'+this.overClass+'\";framework.menus["'+this.parent.id+'"].mouseOver();';
			if (this.menu) html+='framework.menus[\"'+this.menu+'\"].show(this);';
			html+='\'';
			html+=' onMouseOut=\'this.className=\"'+this.onClass+'\";';
			if (this.menu) html+='framework.menus[\"'+this.menu+'\"].hide();';
			html+=' \'';
		}
		html+='><tr><td>'+this.text+'</td>';
		if (this.menu && framework.menus[this.menu].arrow)
			html+='<td align=\'right\'>'+framework.menus[this.menu].arrow.value()+'</td>';
		html+='</tr></table>';	
		return html;
	}
}
// CLink class
function CLink(url,win)
{
	this.url=url;
	this.win=win;
	this.value=function()
	{
		var html='';
		if (this.win) 
			html='javascript:'+this.win.value(this.url);
		else
			html=url;
		return html;
	}
}
function CWindow(name,width,height,resizable,scrollbars,toolbar,location,status,menubar)
{
	this.name=name;
	this.width=width;
	this.height=height;
	this.resiable=resizable;
	this.scrollbars=scrollbars;
	this.toolbar=toolbar;
	this.location=location;
	this.status=status;
	this.menubar=menubar;
	this.value=function(url)
	{
		var html='openWindow(\"'+url+'\",\"'+this.name+'\",\"';
		if (this.width) html+='width='+this.width+',';
		if (this.height) html+='height='+this.height+',';
		if (this.toolbar) html+='toolbar='+this.toolbar+',';
		if (this.location) html+='location='+this.location+',';
		if (this.status) html+='status='+this.status+',';
		if (this.menubar) html+='menubar='+this.menubar+',';
		if (this.scrollbars) html+='scrollbars='+this.scrollbars+',';
		if (this.resizable) html+='resizable='+this.resizable+',';
		html+='\");';
		return html;
	}
}
var framework=new CFramework();
