//small fix to older versions of javascript (older than 1.2)
if (!Array.prototype.push) Array.prototype.push=new Function ("elem","for(var i=0;i<arguments.length;i++) this[this.length]=arguments[i];");
if (!Array.prototype.pop) Array.prototype.pop=new Function ("elem","var x = this[this.length-1]; this.length--; return x;");

// Selection object.
// methods:
//	Add(artid,artisteIdsArray)
//  Remove(artid)
//  Clear()
//  toString()
//  IsIncluded()
function Selection(name,id,date,visible,db_id)
{
	this.arts = new Array;
	this.authors = new Object;
	this.artCount = 0;
	this.authorCount = 0;
	this.name = name.toString();
	this.id = id.toString();
	this.selected=false;
	this.visible = visible;
	if (db_id)
	{
		this.db_id = db_id;
		this.stored = true;
	}
	else
	{
        this.stored = false;
		this.db_id = null;
	}
	this.modified = false;
	if (!date) this.date=new Date();
	else this.date = new Date(date.valueOf())
}

// Add artwork with specified id into selection
function SelectionAdd(artid,artists)
{
	artid= "id"+artid.toString()
	if (this.arts[artid]) return;

	this.modified = true;
    this.arts[artid]=new Object;
	this.arts[artid].authors = new Array;
	this.arts[artid].id = artid;
	this.arts[artid].num = this.artCount;
	this.arts[this.artCount] = this.arts[artid];
	this.artCount++;
	if ((   typeof (artists) == "number" 
		 || typeof (artists) == "string") 
		&& arguments.length > 2)
	{
		var a = new Array();
		for(var i=1;i<arguments.length;i++)
			a[i-1]=arguments[i].toString();
		artists = a;
	}
	if (artists == null ) return;
	if (typeof (artists) == "number" || typeof (artists) == "string")
	{
		var authid = artists.toString();
		this.arts[artid].authors[0] = authid;
		if (this.authors[authid])
			this.authors[authid]++ ;
		else
		{
			this.authors[authid]=1;
			this.authorCount++;
		}
	}
	else
	{
		for (var i=0;i<artists.length;i++)
		{
			authid = artists[i].toString();
			this.arts[artid].authors[i] = authid;
			if (this.authors[authid])
				this.authors[authid]++ ;
			else
			{
				this.authors[authid]=1;
				this.authorCount++;
			}
		}
	}
}

// removes worart with specified id	from selection
function SelectionRemove(artid)
{
	artid = "id"+artid.toString();
	if (!this.arts[artid]) return;
	this.modified = true;
    for (var i=0;i<this.arts[artid].authors.length;i++)
	{
		authid = this.arts[artid].authors[i];
		if (this.authors[authid])
		{
			this.authors[authid]-- ;
			if (!this.authors[authid]) this.authorCount--;
		}
		//else alert("Runtime error! : 0x348c4a");
	};
	var n = this.arts[artid].num;
	this.arts[artid]=null;
	this.arts[n] = this.arts[this.artCount-1];
	this.arts[n].num = n;
	this.arts[this.artCount-1]=null;
	this.artCount--;
}

// removes all artworks from selection
function SelectionClear()
{
	this.modified = true;
    this.arts = new Object;
    this.authors = new Object;
	this.artCount = 0;
	this.authorCount = 0;
}

//returns coma separated string with artwork IDs
function SelectionToString()
{
	var s="";
	if (this.artCount>0)
	{
		s = this.arts[0].id.substr(2);
		for (var i =1; i < this.artCount; i++)
			s+= ","+this.arts[i].id.substr(2);
	}
	return s;
}

//check if artwork is included in selection
function SelectionIsIncluded(artid)
{
	artid = "id"+artid.toString();
	return this.arts[artid];
}

Selection.prototype.Add=SelectionAdd;
Selection.prototype.Remove=SelectionRemove;
Selection.prototype.Clear=SelectionClear;
Selection.prototype.toString=SelectionToString;
Selection.prototype.IsIncluded=SelectionIsIncluded;

var currentSelection=null;
var currentSelectionIdx = -1;
var selections = new Array;
var nextSelectionId = 1 ;
var selWin = window;

// chages current selection to specified by id (if exists)
function SetCurrentSelection(id)
{
	if (selWin.currentSelection)
		selWin.currentSelection.selected=false;
	for (var i=0;i<selWin.selections.length;i++)
    {
		if (selWin.selections[i].id == id)
		{
			selWin.currentSelection = selWin.selections[i];
			selWin.currentSelectionIdx = i;
			selWin.currentSelection.selected = true;
			break;
		}
	}
}

// returns selection to specified by id if exists 
// or returns null()
function GetSelectionById(id)
{
	for (var i=0;i<selWin.selections.length;i++)
		if (selWin.selections[i].id == id)
			return selWin.selections[i];
	return null;
}

function GetSelectionByDbId(id)
{
	for (var i=0;i<selWin.selections.length;i++)
		if (selWin.selections[i].db_id == id)
			return selWin.selections[i];
	return null;
}

function GetSelectionIdxById(id)
{
	for (var i=0;i<selWin.selections.length;i++)
		if (selWin.selections[i].id == id)
			return i;
	return -1;
}

function AddNewSelection(name,date,visible,db_id)
{
	var sel = new Selection(name,selWin.nextSelectionId++,date,visible,db_id);
	selWin.selections.push(sel);
	if (selWin.selections.length==1)
	{
		selWin.currentSelection = selWin.selections[0];
		selWin.currentSelectionIdx = 0;
		selWin.currentSelection.selected = true;
	}
	return sel;
}

function RemoveSelection(id,force)
{
	if (!force && selWin.selections.length==1)
	{
		alert ("You can not remove last selection.");
		return;
	}

	var sel = GetSelectionIdxById(id);
	if (sel>=0)
	{
		for (var i = sel;i<selWin.selections.length-1;i++)
			selWin.selections[i]=selWin.selections[i+1];
		selWin.selections.pop();

		if (selWin.currentSelectionIdx == sel)
		{
			if (sel>0) currentSelectionIdx--;
			if (currentSelectionIdx>=0)
			{
				selWin.currentSelection = selWin.selections[currentSelectionIdx];
				selWin.currentSelection.selected=true;
			}
			else
			{
				selWin.currentSelection = null;
			}
		}
	}
}

// returns array of selections sorted by 
// specified property
function  GetSortedSelections(param,invisible)
{
	var sel = selWin.selections;
	var out = new Array();
	var i;
	for ( i =0; i< sel.length; i++)
		if (invisible || sel[i].visible)
			out[i] = sel[i];
	if (param.charAt(0)=="-")
	{
		param = param.substring(1);
		for ( i = 0; i < out.length-1; i++)
		{
			var min = i;
			for (var j =i+1; j< out.length; j++)
				if ( out[j][param].valueOf() > out[min][param].valueOf() )
					min = j;
			var tmp = out[i];
			out[i] = out[min];
			out[min] = tmp;
		}
	}
	else
		for ( i = 0; i < out.length-1; i++)
		{
			var min = i;
			for (var j =i+1; j< out.length; j++)
				if ( out[j][param].valueOf() < out[min][param].valueOf() )
					min = j;
			var tmp = out[i];
			out[i] = out[min];
			out[min] = tmp;
		}
	return out;
}

function  GetFirstVisibleSelectionId()
{
	 for (var i=0;i<selWin.selections.length;i++)
		if (selWin.selections[i].visible)
			return selWin.selections[i].id;
	return -1;
}
