//_____[ ITEM, brief view ]_____\\
function addLoadEvent(f)
{
	var o = window.onload;
	if(typeof window.onload != "function") { window.onload = f; }
	else { window.onload = function() { o(); f(); }; }
}


var record =
{
	title_long: null,
	title_short: null,
	callnumber: [],
	location: [],
	status: [],
	set: false,
	index: [],
	
	encode: function(str)
	{
		if(str)
		{
			var s;
			str = str.replace(/:$|\/$/g,'');
			str = str.replace(/^\s+|\s+$/g,'');
			s = str.split(/\s/);
			str = s.join('+');
			return str;
		}
		return '';
	},
	
	setFieldValue: function(field,value,td,id)
	{
		switch(field)
		{
			case 'Call Number':
				td.setAttribute('id',id + record.callnumber.length);
				record.callnumber[record.callnumber.length] = value;
				break;
			case 'Location':
				td.setAttribute('id',id + record.callnumber.length);
				record.location[record.callnumber.length] = value;
				break;
			case 'Status':
				record.status[record.callnumber.length - 1] = value;
				break;
		}
	},
	
	getFieldValue: function(th,field)
	{
		var value = false;
		var id = null;
		if(th.innerHTML.indexOf(field) != -1)
		{
			var td = th.nextSibling;
			while(td.nodeType != 1) { td = td.nextSibling; }
			id = field.toLowerCase();
			id = id.replace(/\s/,'_');
			
			var a = td.getElementsByTagName('a');
			if(a.length >= 1)
			{
				for(var i = 0; i < a.length; i++)
				{
					value = a[i].getAttribute('href').split('&SA=')[1];
				}
			}
			else
			{
				//value = td.firstChild.nodeValue;
				value = td.innerHTML;
				value = value.replace(/\<strong\>\<i\>/g,''); // take out the strong and italic html tags
				value = value.replace(/\<\/i\>\<\/strong\>/g,''); // take out the strong and italic html tags
				value = value.replace(/\<STRONG\>\<I\>/g,''); // take out the strong and italic html tags
				value = value.replace(/\<\/I\>\<\/STRONG\>/g,''); // take out the strong and italic html tags
			}
			record.setFieldValue(field,value,td,id);
		}
		return value;
	},
	
	setLocationField: function(i)
	{
		var this_location = document.getElementById('location'+i);
		var item_location = stacks.whichFloor(record.callnumber[i],record.location[i]);
		if(item_location != record.location[i])
		{
			this_location.appendChild(document.createTextNode(', '+item_location));
			record.location[i] = item_location;
		}
	},
	
	checkedIn: function(i)
	{
		return (record.status[i] && record.status[i].indexOf('Checked In') != -1) ? true : false;
	},
	
	locationsDiffer: function(i)
	{
		j = (i != 0) ? i - 1 : record.callnumber.length - 1;
		return (record.location[i] != record.location[j]) ? true : false;
	},
	
	setItemInfo: function()
	{
		for(var i = 0; i < record.callnumber.length; i++)
		{
			record.setLocationField(i);
		}
		for(var i = 0; i < record.callnumber.length; i++)
		{
			if(record.checkedIn(i))
			{
				if(record.locationsDiffer(i))
				{
					record.index.push(i);
					record.set = true;
				}
				else
				{
					if(!record.set)
					{
						record.index.push(i);
						record.set = true;
					}
				}
			}
			if(!record.checkedIn(i))
			{
				if(record.locationsDiffer(i))
				{
					record.index.push(i);
				}
			}
		}
	},
	
	getItemInfo: function()
	{
		if(document.title.indexOf('Brief View') == -1 && document.title.indexOf('Full View') == -1) return false;
		var th = document.getElementsByTagName('th');
		for(var i = 0; i < th.length; i++)
		{
			record.title_long = (!record.title_long) ? record.getFieldValue(th[i],'Title') : record.title_long;
			record.title_short = (!record.title_short) ? record.getFieldValue(th[i],'Title') : record.title_short;

			record.getFieldValue(th[i],'Call Number');
			record.getFieldValue(th[i],'Location');
			record.getFieldValue(th[i],'Status');
		}
		record.setItemInfo();
	},
	
	init: function()
	{
		record.getItemInfo();
	}
}
addLoadEvent(record.init);
