function CLink( id, row, url, name, window )
{
	this.id = id;
	this.row = row;
	this.url = url;
	this.name = name;

	this.window = window;

	this.moving = 0;

	//* methods
	this.print = CLink_print;
	this.go = CLink_go;
	
	this.printcursor = CLink_printcursor;
	this.up = CLink_up;
	this.down = CLink_down;
	this.changewindow = CLink_changewindow;
	this.endmove = CLink_endmove;

	this.edit = CLink_edit;
	this.editsubmit = CLink_editsubmit;
	this.del = CLink_delete;
	this.getindex = CLink_getindex;
}

function CLink_print( doc )
{
	if( this.window.shelf.state == "normal" )
	{
		doc.write( "<a target=\"_blank\" href=\""+this.url+"\"><font size=-2>"+this.name+"</a><br>" );
	}
	else
	{
		doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.window.shelf.getindex()+"].window["+this.window.getindex()+"].link["+this.getindex()+"].go();\" " );
		doc.write( "onmouseover=\"window.status='"+this.url+"'; return true\" onmouseout=\"window.status='';return true\">" );
		
		if( this.moving )
			doc.write( "<font color=red>" );
		doc.write( this.name+"</font></a><br>" );
	}
}

function CLink_go()
{
	if( this.window.shelf.state == "normal" )
	{
		window.open( this.url );
		this.window.shelf.select();
	}
	else if( this.window.shelf.state == "edit" )
	{
		this.window.shelf.state = "normal";
		this.edit();
	}
	else if( this.window.shelf.state == "delete" )
	{	
		this.window.shelf.state = "normal";
		this.del();
	}
	else if( this.window.shelf.state == "move" )
	{
		if( this.window.shelf.moveobj ) 
			this.window.shelf.moveobj.endmove();

		this.window.shelf.setmoveobj( this );
		this.window.orderchanged = 0;
	}
	else
		alert( "not implemented yet" );
}

function CLink_printcursor( doc )
{
	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.window.shelf.getindex()+"].window["+this.window.getindex()+"].link["+this.getindex()+"].up();\" " );
	doc.write( "onmouseover=\"window.status='Move up: "+this.url+"';return true\" onmouseout=\"window.status='';return true\">Up</a> " );
	
	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.window.shelf.getindex()+"].window["+this.window.getindex()+"].link["+this.getindex()+"].down();\" " );
	doc.write( "onmouseover=\"window.status='Move down: "+this.url+"';return true\" onmouseout=\"window.status='';return true\">Down</a> " );

	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.window.shelf.getindex()+"].window["+this.window.getindex()+"].link["+this.getindex()+"].changewindow();\" " );
	doc.write( "onmouseover=\"window.status='Change window: "+this.url+"';return true\" onmouseout=\"window.status='';return true\">Window</a> " );

	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.window.shelf.getindex()+"].window["+this.window.getindex()+"].link["+this.getindex()+"].endmove();\" " );
	doc.write( "onmouseover=\"window.status='Ok';return true\" onmouseout=\"window.status='';return true\">Done</a> " );
}

function CLink_up()
{
	this.window.linkup( this );
}

function CLink_down()
{
	this.window.linkdown( this );
}

function CLink_changewindow()
{
	this.window.changewindow( this );
}

function CLink_endmove()
{
	this.moving = 0;
	this.window.shelf.setmoveobj( 0 );
	this.window.storeorder();
}

function CLink_edit()
{
	var doc = work.document;
	var f = new CForm( "editlink", string["editlink"] );

	var window = this.window;
	var shelf = this.window.shelf;
	
	doc.write( "<body bgcolor="+bgcolor+"><center><table height=100%><tr><td valign=middle>" );

	f.add( "url", "URL: ", this.url, "text", 60 );
	f.add( "name", string["formdescription"], this.name, "text", 60 );
	f.addfunction( string["preview"], "javascript:self.parent.previewurl(document.editlink.url.value);" );
	f.addfunction( string["cancel"], "javascript:self.parent.account.shelf["+shelf.getindex()+"].select();" );
	f.setsubmit( string["edit"], "javascript:self.parent.account.shelf["+shelf.getindex()+"].window["+window.getindex()+"].link["+this.getindex()+"].editsubmit();//" );
	f.print( doc );

	doc.write( "</td></tr></table></body>" );
	doc.close();

	work.document.editlink.url.focus();
}

function CLink_editsubmit()
{
	var url = work.document.editlink.url.value;
	var name = work.document.editlink.name.value;

	if( url && url.length && name && name.length )
	{
		if( url.indexOf( "go.php3" ) == -1 && url.indexOf( "://" ) == -1 ) 
			url = "http://" + url;

		var location = "wait.php3?command=editlink";
		location += "&id=" + this.id;
		location += "&linkindex=" + this.getindex();
		location += "&windowindex=" + this.window.getindex();
		location += "&shelfindex=" + this.window.shelf.getindex();
		location += "&url=" + escape( url );
		location += "&name=" + escape( name );

		con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
		con.focus();
	}
	else
	{
		alert( string["enterurl"] );
	}

}

function CLink_delete()
{
	if( confirm( string["confirmremovelink"] ) )
	{
		var location = "wait.php3?command=deletelink";
		location += "&linkid=" + this.id;
		location += "&linkindex=" + this.getindex();
		location += "&windowid=" + this.window.id;
		location += "&windowindex=" + this.window.getindex();
		location += "&shelfindex=" + this.window.shelf.getindex();

		this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
		this.con.focus();
	}
	else
		this.window.shelf.select();
}

function CLink_getindex()
{	
	return this.window.getlinkindex( this );
}

function CWindow( id, shelf, name, col, row )
{
	this.id = id;
	this.name = name;
	this.shelf = shelf;

	this.col = col;
	this.row = row;
	this.moving = 0;

	this.link = new Array();
	this.orderchanged = 0;		//* set to 1 when link order changes

	//* methods
	this.print = CWindow_print;
	this.getlinkindex = CWindow_getlinkindex;
	this.getindex = CWindow_getindex;

	this.addlink = CWindow_addlink;
	this.insertlink = CWindow_insertlink;
	this.deletelink = CWindow_deletelink;

	this.linkup = CWindow_linkup;
	this.linkdown = CWindow_linkdown;
	this.changewindow = CWindow_changewindow;
	this.storeorder = CWindow_storeorder;

	this.printcursor = CWindow_printcursor;
	this.up = CWindow_up;
	this.down = CWindow_down;
	this.left = CWindow_left;
	this.right = CWindow_right;
	this.changeshelf = CWindow_changeshelf;
	this.endmove = CWindow_endmove;
}

function CWindow_print( doc, selected )
{
	var i;
	var bgcolor = selected ? windowselected : windowtitle;

	doc.write( "<table width=185 border=0 cellspacing=0 bgcolor="+windowbgcolor+" style="+tablestyle+"><tr><td bgcolor="+bgcolor+">&nbsp</td><td align=center bgcolor="+bgcolor+">" );	//* F1F1F1
	
	doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].selectwindow("+this.getindex()+");\">" );

	doc.write( "<i><font color="+windowtitletext+" size=-2>" + this.name + "</font></i></a>");

	doc.write( "</td></tr><tr><td width=5% bgcolor="+bgcolor+">&nbsp</td><td>" );

	for( i = 0; i < this.link.length; i++ )
		this.link[i].print( doc );

	doc.write( "</td></tr></table><br>" );
}

function CWindow_getlinkindex( link )
{
	var i;

	for( i = 0; i < this.link.length; i++ )
		if( this.link[i] == link ) return i;

	return -1;
}

function CWindow_getindex()
{
	return this.shelf.getwindowindex( this.id );
}

function CWindow_addlink( id, url, name )
{
	var i = this.link.length;
	this.link[i] = new CLink( id, i, url, name, this );
}

function CWindow_insertlink( pos, l )
{
	var i;

	for( i = this.link.length; i > pos; i-- )
		this.link[i] = this.link[i-1];
		
	this.link[pos] = l;
	l.window = this;
}

function CWindow_deletelink( index )
{
	var i;

	for( i = index; i < this.link.length - 1; i++ )
	{
		this.link[i] = this.link[1+i];
		this.link[i].row--;
	}

	this.link.length--;
}

function CWindow_linkup( l )
{
	var index = this.getlinkindex( l );

	if( index > 0 )
	{
		this.orderchanged = 1;

		var tmp = this.link[index];
		this.link[index] = this.link[index-1];
		this.link[index-1] = tmp;

		this.shelf.select();
	}
}

function CWindow_linkdown( l )
{
	var index = this.getlinkindex( l );

	if( index < this.link.length - 1 )
		this.linkup( this.link[1 + this.getlinkindex( l )] );
}

function CWindow_changewindow( l )
{
	this.shelf.changelinkwindow( this, l );
}

function CWindow_printcursor( doc )
{
	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].window["+this.getindex()+"].up();\" " );
	doc.write( "onmouseover=\"window.status='Move up';return true\" onmouseout=\"window.status='';return true\">Up</a> " );
	
	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].window["+this.getindex()+"].down();\" " );
	doc.write( "onmouseover=\"window.status='Move down';return true\" onmouseout=\"window.status='';return true\">Down</a> " );

	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].window["+this.getindex()+"].left();\" " );
	doc.write( "onmouseover=\"window.status='Move left';return true\" onmouseout=\"window.status='';return true\">Left</a> " );
	
	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].window["+this.getindex()+"].right();\" " );
	doc.write( "onmouseover=\"window.status='Move right';return true\" onmouseout=\"window.status='';return true\">Right</a> " );

	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].window["+this.getindex()+"].changeshelf();\" " );
	doc.write( "onmouseover=\"window.status='Change Shelf';return true\" onmouseout=\"window.status='';return true\">Shelf</a> " );

	doc.write( "<a style={text-decoration:none} href=\"javascript:self.parent.account.shelf["+this.shelf.getindex()+"].window["+this.getindex()+"].endmove();\" " );
	doc.write( "onmouseover=\"window.status='Ok';return true\" onmouseout=\"window.status='';return true\">Done</a> " );
}

function CWindow_up()
{
	this.shelf.windowup( this );
}

function CWindow_down()
{
	this.shelf.windowdown( this );
}

function CWindow_left()
{
	this.shelf.windowleft( this );
}

function CWindow_right()
{
	this.shelf.windowright( this );
}

function CWindow_changeshelf()
{
	this.shelf.changeshelf( this );
}

function CWindow_endmove()
{
	this.moving = 0;
	this.shelf.setmoveobj( 0 );
	this.shelf.storeorder();
}

function CWindow_storeorder()
{
	if( this.orderchanged )
	{
		var i;
		var location = "wait.php3?command=windowreorder";
		location += "&windowid="+this.id;
		location += "&windowindex="+this.getindex();
		location += "&linkorder=";

		for( i = 0; i < this.link.length; i++ )
		{
			if( i ) location += ","
			location += this.link[i].id;
		}

		this.orderchanged = 0;
		window.open( location, "", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
	}
}

function CWindowcompare( a, b )
{
	if( a.col < b.col ) return -1;
	if( a.col > b.col ) return 1;
	if( a.row < b.row ) return -1;
	if( a.row > b.row ) return 1;
	return 0;
}




