function CShelf( id, account, order, name, sharename, sharepass )
{
	this.id = id;
	this.account = account;
	this.order = order;
	this.name = name;
	this.sharename = sharename;
	this.sharepass = sharepass;

	this.con = 0;
	this.readonly = 0;
	this.window = new Array();
	this.windowselected = -1;
	this.state = "normal";		//* "edit", "delete", "move"
	this.moveobj = 0;

	//* methods
	this.select = CShelf_select;
	this.printwindows = CShelf_printwindows;
	this.sortwindows = CShelf_sortwindows;
	this.printcolumn = CShelf_printcolumn;
	this.getcolumncount = CShelf_getcolumncount;
	this.getindex = CShelf_getindex;

	this.addwindow = CShelf_addwindow;
	this.addwindowsubmit = CShelf_addwindowsubmit;
	this.insertwindow = CShelf_insertwindow;					//* insert with params
	this.insertwindowdirect = CShelf_insertwindowdirect;		//* insert with pointer to already created window
	this.removewindow = CShelf_removewindow;
	this.selectwindow = CShelf_selectwindow;
	this.renamewindow = CShelf_renamewindow;
	this.renamewindowsubmit = CShelf_renamewindowsubmit;
	this.deletewindow = CShelf_deletewindow;
	this.deletewindowcallback = CShelf_deletewindowcallback;
	this.getwindowindex = CShelf_getwindowindex;

	this.changelinkwindow = CShelf_changelinkwindow;
	this.changelinkwindowsubmit = CShelf_changelinkwindowsubmit;

	this.addlink = CShelf_addlink;
	this.addlinksubmit = CShelf_addlinksubmit;

	this.windowup = CShelf_windowup;
	this.windowdown = CShelf_windowdown;
	this.windowleft = CShelf_windowleft;
	this.windowright = CShelf_windowright;
	this.changeshelf = CShelf_changeshelf;
	this.storeorder = CShelf_storeorder;

	this.setstate = CShelf_setstate;
	this.setmoveobj = CShelf_setmoveobj;
}

function CShelf_select()
{
	var doc = work.document;

	doc.clear();
	doc.write( "<body bgcolor="+self.parent.bgcolor+">" );
	printstylesheet( doc );
	doc.write( "<table width=100% height=100% border=0 cellpadding=0 cellspacing=0><tr><td valign=top>" );

		doc.write( "<table width=100% cellpadding=0 cellspacing=0 border=0 style="+self.parent.tablestyle+"><tr><td align=left>&nbsp" );

			doc.write( "<table width=100% style="+self.parent.tablestyle+"><tr><td align=right>&nbsp" );
	
			if( !this.readonly && this.state != "normal" )
			{
				if( this.moveobj )
				{
					this.moveobj.printcursor( doc );
				}
				else
				{
					doc.write( "<font color=red size=-2>" + string["Selectlinkorwindow"] + " " + string[this.state] + " " + string["or"] + "</font>" );
					doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.getindex()+"].setstate('normal');\"> " + string["clicktocancel"] + "</a> " );
				}

			}
			doc.write( "</td></tr></table>" );

			doc.write( "</td><td align=right>" );

			doc.write( "<table style="+self.parent.tablestyle+"><tr>" );

				if( !this.readonly && this.state == "normal" )
				{
					if( this.window.length )
					{
						doc.write( "<td align=right><font size=-2>" + string["action"] + ": " );
						doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.getindex()+"].setstate('edit');\">" + string["edit"] + "</a> " );
						doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.getindex()+"].setstate('move');\">" + string["move"] + "</a> " );
						doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.getindex()+"].setstate('delete');\">" + string["delete"] + "</a>" );
						doc.write( "</td>" );
					}

					doc.write( "<td align=right><font size=-2>Window: " );
						doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.getindex()+"].addwindow();\">" + string["add"] + "</a> " );
					doc.write( "</td><td align=right><font size=-2>Link: " );
						doc.write( "<a href=\"javascript:self.parent.account.shelf["+this.getindex()+"].addlink();\">" + string["add"] + "</a> " );

				}

		doc.write( "</td></tr></table>" );

	doc.write( "</td></tr></table>" );

	this.printwindows( doc );
	doc.write( "</td></tr>" );
	doc.write( "<tr><td valign=bottom align=right>" );
//	doc.write( "<a href=\"http://nl.nedstatbasic.net/cgi-bin/viewstat?name=linkshelf\"><img " );
//	doc.write( "src=\"http://nl.nedstatbasic.net/cgi-bin/nedstat.gif?name=linkshelf\" " );
//	doc.write( "border=0 alt=\"\" nosave width=12 height=12></a>" );
	doc.write( "</td></tr>" );
	doc.write( "</table>" );

	doc.write( "</body>" );
	doc.close();

	resetcursor( doc );
}

function CShelf_printwindows( doc )
{
	if( this.window.length )
	{
		this.sortwindows();

		var colsmallest = this.window[0].col;
		var colbiggest = this.window[this.window.length-1].col;
		var i;

		doc.write( "<center><table><tr>" );
		
		for( i = colsmallest; i <= colbiggest; i++ )
			this.printcolumn( i, doc );

		doc.write( "</tr></table>" );
	}
}

function CShelf_sortwindows()
{
	this.window.sort( CWindowcompare );
}

function CShelf_printcolumn( col, doc )
{
	var row = 0;
	doc.write( "<td valign=top>" );
	
	for( i = 0; i < this.window.length; i++ )
		if( this.window[i].col == col ) 
		{
			this.window[i].row = row++;
			this.window[i].print( doc, i == this.windowselected ? 1 : 0 );
		}

	doc.write( "</td>" );
}

function CShelf_getcolumncount( col )
{
	var i, retval=0;

	for( i = 0; i < this.window.length; i++ )
		if( this.window[i].col == col ) retval++;

	return retval;
}

function CShelf_getindex()
{
	return this.account.getindexofshelf( this.id );	
}

function CShelf_addwindow( id, name, col, row )
{
	if( id )
	{
		this.window[this.window.length] = new CWindow( id, this, name, col, row );
	}
	else
	{
		var doc = work.document;
		var f = new CForm( "addwindow", string["addwindow"] );

		f.add( "name", string["formname"], "", "text", 60 );
		f.addfunction( string["cancel"], "javascript:self.parent.account.shelf["+this.getindex()+"].select();" );
		f.addparam( "command", "addwindow" );
		f.addparam( "shelfid", this.id );
		f.addparam( "shelfindex", this.getindex() );
		if( this.windowselected != -1 ) f.addparam( "windowselectedid", this.window[this.windowselected].id );
		f.setsubmit( string["add"], "javascript:self.parent.account.shelf["+this.getindex()+"].addwindowsubmit();//" );

		doc.write( "<body bgcolor="+bgcolor+"><center><table height=100%><tr><td valign=middle>" );
		
		f.print( doc );

		doc.write( "</td></tr></table></body>" );
		doc.close();

		work.document.addwindow.name.focus();
	}
}

function CShelf_addwindowsubmit()
{
	var name = work.document.addwindow.name.value;

	if( !name || !name.length )
	{
		alert( string["editwindowname"] );
		return;
	}
	else
	{
		var location = "wait.php3?command=addwindow";
		location += "&shelfid=" + this.id;
		location += "&shelfindex=" + this.getindex();
		if( this.windowselected != -1 ) location += "&windowselectedid=" + this.window[this.windowselected].id;
		location += "&name=" + escape( name );

		this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
		this.con.focus();
	}
}

function CShelf_insertwindow( id, col, row, name )
{
	var i;

	for( i = 0; i < this.window.length; i++ )
	{
		if( this.window[i].col == col && this.window[i].row >= row ) 
			this.window[i].row++;
	}

	this.addwindow( id, name, col, row );
	this.sortwindows();
}

function CShelf_insertwindowdirect( w )
{
	var i;

	for( i = this.window.length; i > 0; i-- )
	{
		this.window[i] = this.window[i-1];

		if( this.window[i].col == 0 )
			this.window[i].row++;
	}

	w.col = 0;
	w.row = 0;
	w.shelf = this;
	
	this.window[0] = w;
}

function CShelf_removewindow( index )
{
	var i, col = this.window[index].col, row = this.window[index].row;

	for( i = index; i < this.window.length - 1; i++ )
	{
		this.window[i] = this.window[1+i];

		if( this.window[i].col == col && this.window[i].row > row )
			this.window[i].row--;
	}

	this.window.length--;
}

function CShelf_selectwindow( i )
{
	if( this.state != "normal" )
	{
		if( this.state == "edit" )
		{
			this.state = "normal";
			this.renamewindow( i );
		}
		if( this.state == "delete" )
		{
			this.state = "normal";
			this.deletewindow( i );
		}
		if( this.state == "move" )
		{
			if( this.moveobj ) this.moveobj.endmove();

			this.windowselected = i;
			this.setmoveobj( this.window[i] );
		}
	}
	else
	{
		if( this.windowselected == i ) this.windowselected = -1;
		else this.windowselected = i;

		this.select();
	}
}

function CShelf_renamewindow( index )
{
	var doc = work.document;
	var f = new CForm( "renamewindow", string["editwindow"] + " \"" + this.window[index].name + "\"" );

	doc.write( "<body bgcolor="+bgcolor+"><center><table height=100%><tr><td valign=middle>" );

	f.add( "name", string["formname"], this.window[index].name, "text", 60 );
	f.addfunction( string["cancel"], "javascript:self.parent.account.shelf["+this.getindex()+"].select();" );
	f.setsubmit( string["apply"], "javascript:self.parent.account.shelf["+this.getindex()+"].renamewindowsubmit("+index+");//" );
	f.print( doc );

	doc.write( "</td></tr></table></body>" );
	doc.close();

	work.document.renamewindow.name.focus();
}

function CShelf_renamewindowsubmit( index )
{
	var name = work.document.renamewindow.name.value;

	if( !name || !name.length )
	{
		alert( string["enterwindowname"] );
		return;
	}

	var location = "wait.php3?command=renamewindow";
	location += "&name=" + escape( name ); 
	location += "&windowid=" + this.window[index].id;
	location += "&shelfid=" + this.id;
	
	this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
	this.con.focus();
}

function CShelf_deletewindow( index )
{
	if( confirm( string["confirmremovewindow"] ) )
	{
		var location = "wait.php3?command=deletewindow";
		location += "&windowid=" + this.window[index].id;
		location += "&windowindex=" + index;
		location += "&shelfid=" + this.id;
		location += "&shelfindex=" + this.getindex();

		this.windowselected = -1;

		this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
		this.con.focus();
	}
	else
		this.select();
}

function CShelf_deletewindowcallback( index )
{
	var i;
	var col = this.window[index].col;
	var row = this.window[index].row;

	for( i = index; i < this.window.length - 1; i++ )
	{
		this.window[i] = this.window[1+i];
		if( this.window[i].col == col )
			this.window[i].row--;
	}

	this.window.length--;
}

function CShelf_getwindowindex( id )
{
	var i;

	for( i = 0; i < this.window.length; i++ )
		if( this.window[i].id == id ) return i;

	return -1;
}

function CShelf_changelinkwindow( window, link )
{
	var f = new CForm( "changewindow", string["movelinktowindow"] );
	var winlist = new Array();
	var doc = work.document;

	for( i = 0; i < this.window.length; i++ )
		winlist[i] = this.window[i].name;

	doc.write( "<body bgcolor="+bgcolor+"><center><table height=100%><tr><td valign=middle>" );
	
	f.add( "win", "Window: ", winlist, "select", 200, 10, window.getindex() );
	f.addfunction( string["cancel"], "javascript:self.parent.account.shelf["+this.getindex()+"].select();" );
	f.setsubmit( string["move"], "javascript:self.parent.account.shelf["+this.getindex()+"].changelinkwindowsubmit("+window.getindex()+","+link.getindex()+");//" );
	f.print( doc );

	doc.write( "</td></tr></table></body>" );
	doc.close();
}

function CShelf_changelinkwindowsubmit( orgwindowindex, orglinkindex )
{
	var newwindowindex = work.document.changewindow.win.selectedIndex;

	if( newwindowindex != orgwindowindex )
	{
		var location = "wait.php3?command=changelinkwindow";
		location += "&orgwindowindex=" + orgwindowindex;
		location += "&orglinkindex=" + orglinkindex;
		location += "&newwindowindex=" + newwindowindex;
		location += "&shelfindex=" + this.getindex();
		location += "&orgwindowid=" + this.window[orgwindowindex].id;
		location += "&linkid=" + this.window[orgwindowindex].link[orglinkindex].id;
		location += "&newwindowid=" + this.window[newwindowindex].id;

		this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=1,height=100,width=200" );
			this.con.focus();
	}
	else
	{
		this.select();
	}
}

function CShelf_addlink()
{
	var doc = work.document;
	var f = new CForm( "addlink", string["addlink"] );
	var winlist = new Array();

	for( i = 1; i <= this.window.length; i++ )
		winlist[i] = this.window[i-1].name;
	winlist[0] = string["createinnewwindow"];

	doc.write( "<body bgcolor="+bgcolor+"><center><table height=100%><tr><td valign=middle>" );

	f.add( "url", "URL: ", "", "text", 60 );
	f.add( "name", string["formdescription"], "", "text", 60 );
	f.add( "win", "Window: ", winlist, "select", 200, 1, this.windowselected + 1 );		//* winlist[0] is insterted
	f.addfunction( string["preview"], "javascript:self.parent.previewurl(document.addlink.url.value);" );
	f.addfunction( string["cancel"], "javascript:self.parent.account.shelf["+this.getindex()+"].select();" );
	f.setsubmit( string["add"], "javascript:self.parent.account.shelf["+this.getindex()+"].addlinksubmit();//" );
	f.print( doc );

	doc.write( "</td></tr></table></body>" );
	doc.close();

	work.document.addlink.url.focus();
}

function CShelf_addlinksubmit()
{
	var winindex = work.document.addlink.win.selectedIndex-1;
	var url = work.document.addlink.url.value;
	var name = work.document.addlink.name.value;

	if( url && url.length && name && name.length )
	{
		if( url.indexOf( "://" ) == -1 ) 
			url = "http://" + url;

		var location = "wait.php3?command=addlink";
		location += "&windowid=" + ( winindex == -1 ? -1 : this.window[winindex].id );
		location += "&windowindex=" + winindex;
		location += "&shelfid=" + this.id;
		location += "&shelfindex=" + this.getindex();
		location += "&url=" + escape( url );
		location += "&name=" + escape( name );

		this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
		this.con.focus(); 
	}
	else
	{
		alert( string["enterurl"] );
	}
}

function CShelf_windowup( w )
{
	var i;

	for( i = 0; i < this.window.length; i++ )
	{
		if( this.window[i].col == w.col && this.window[i].row == w.row - 1 )
		{
			this.window[i].row++;
			w.row--;
			this.sortwindows();

			this.windowselected = w.getindex();
			this.select();

			break;
		}
	}
}

function CShelf_windowdown( w )
{
	var i;

	for( i = 0; i < this.window.length; i++ )
	{
		if( this.window[i].col == w.col && this.window[i].row == 1 + w.row )
		{
			this.window[i].row--;
			w.row++;
			this.sortwindows();

			this.windowselected = w.getindex();
			this.select();

			break;
		}
	}
}

function CShelf_windowleft( w )
{
	if( w.col > 0 )
	{
		for( i = 0; i < this.window.length; i++ )
			if( this.window[i].col == w.col && this.window[i].row > w.row )
				this.window[i].row--;

		w.col--;

		if( this.getcolumncount( w.col ) <= w.row ) 
			w.row = this.getcolumncount( w.col ) - 1;

		for( i = 0; i < this.window.length; i++ )
			if( this.window[i].id != w.id && this.window[i].col == w.col && this.window[i].row >= w.row )
				this.window[i].row++;
	}
	else
	{
		for( i = 0; i < this.window.length; i++ )
		{
			this.window[i].col++;

			if( this.window[i].col == w.col && this.window[i].row >= w.row )
				this.window[i].row--;
		}

		w.col = 0;
		w.row = 0;
	}

	this.sortwindows();
	this.windowselected = w.getindex();
	this.select();
}

function CShelf_windowright( w )
{
	var i;

	for( i = 0; i < this.window.length; i++ )
		if( this.window[i].col == w.col && this.window[i].row > w.row )
			this.window[i].row--;

	w.col++;

	if( this.getcolumncount( w.col ) <= w.row ) 
		w.row = this.getcolumncount( w.col ) - 1;

	for( i = 0; i < this.window.length; i++ )
		if( this.window[i].id != w.id && this.window[i].col == w.col && this.window[i].row >= w.row )
			this.window[i].row++;

	this.sortwindows();

	//* if there's no col 0 anymore, make sure col starts counting at 0
	if( this.window[0].col > 0 )
		for( i = 0; i < this.window.length; i++ )
			this.window[i].col--;

	this.windowselected = w.getindex();
	this.select();
}

function CShelf_changeshelf( w )
{	
	this.account.changeshelf( w );
}

function CShelf_setstate( state )
{
	if( this.windowselected != -1 )
		this.windowselected = -1;
	
	this.state = state;
	this.select();
}

function CShelf_storeorder()
{
	var string = "";
	var i;

	for( i = 0; i < this.window.length; i++ )
	{
		if( i != 0 ) string += ";";
		string += this.window[i].id + "," + this.window[i].col + "," + this.window[i].row;
	}

	var location = "wait.php3?command=storewindoworder";
	location += "&data=" + string;

	this.con = window.open( location, "con", "menubars=0,scrollbars=0,resizable=0,height=100,width=200" );
	this.con.focus();
}

function CShelf_setmoveobj( obj )
{
	if( !obj ) 
	{
		if( this.obj ) this.obj.endmove();
		
		this.windowselected = -1;
		this.state="normal";
	}
	else
	{
		this.state="move";
		obj.moving = 1;
	}

	this.moveobj = obj;
	this.select();
}

function compareshelforder( a, b )
{
	return a.order - b.order;	
}

function sortshelfs( list )
{
	list.sort( compareshelforder );	
}
