/* links.js
 * John Greep
 *
 * Copyright June 29, 2005
 *
 */

/* Link Object
 * 
 */
function Link (URL, Description, longDescription, Added) {

	this.url = URL;
	this.description = Description;
	this.longDescription = longDescription;
	if (Added!=""){
		this.date = new Date();
		this.date.setYear (parseInt(Added.substr(0,4)));
		this.date.setMonth(parseInt(Added.substr(5,2))-1);
		this.date.setDate(1);
	}

	this.getURL = _get_URL;
	this.getId = _get_URL;
	this.getHREF = _get_HREF;
	this.getDescription = _get_description;
	this.getLongDescription = _get_long_description;
	this.getToolTip = _get_tool_tip;
	this.getDate = _get_date;
	this.isNew = l_is_new;
	this.isNewer = l_is_newer;
	this.isNewest = l_is_newest;
	this.loadOption = _load_option;
}

/* 
 * 
 */
function _get_URL ()
{
	return (this.url);
}

/* 
 * 
 */
function _get_HREF (newclass)
{
	var newclass = '';
	if (this.isNewest()) { newclass = " newest"; } else {
		if (this.isNewer()) { newclass = " newer"; } else {
			if (this.isNew()) { newclass = " new"; } 
		}
	}
	return ("<a class=\"tip"+newclass+"\" href=\""+this.getURL()+"\" target=\"_blank\">"+this.getDescription()+this.getToolTip()+"</a>");
}

/* 
 * 
 */
function _get_description ()
{
	return (this.description);
}

/* 
 * 
 */
function _get_long_description ()
{
	return (this.longDescription);
}

/* 
 * 
 */
function _get_tool_tip ()
{
	if (this.getLongDescription()!=""){
		return ("<span>"+this.getLongDescription()+"</span");
	}else{
		return ("");
	}
}


/* 
 * 
 */
function _get_date ()
{
	return (this.date);
}

/* 
 * 
 */
function l_is_new ()
{
	if (this.date && days_between (this.date, new Date()) < 270){
		return true;
	}else{
		return false;
	}
}

/* 
 * 
 */
function l_is_newer ()
{
	if (this.date && days_between (this.date, new Date()) < 90){
		return true;
	}else{
		return false;
	}
}

/* 
 * 
 */
function l_is_newest ()
{
	if (this.date && days_between (this.date, new Date()) < 30){
		return true;
	}else{
		return false;
	}
}

/*  Load Option method
 * 
 *  Creates an option object with the Description and ID
 * 
 */
function _load_option ()
{
	return (new Option (this.getDescription(),this.getId()));
}

/*  Category Object
 * 
 */
function Category (catId,Description,dateAdded) {

	this.id = catId;
	this.description = Description;
	this.links = new LinkedList();
	if (dateAdded!=""){
		this.date = new Date();
		this.date.setYear (parseInt(dateAdded.substr(0,4)));
		this.date.setMonth(parseInt(dateAdded.substr(5,2))-1);
		this.date.setDate(1);
	}


	this.addLink = _add_link;
	this.getId = _get_id;
	this.getDescription = _get_description;
	this.getLinks = _get_links;
	this.isNew = l_is_new;
	this.isNewer = l_is_newer;
	this.isNewest = l_is_newest;
	this.loadOption = _load_option;
}

/*  Add Link method
 * 
 *  Adds a link to the list of links attached to the Category
 */
function _add_link (link)
{
	this.links.insert (link);
	return true;
}

/* Get ID method
 * 
 */
function _get_id ()
{
	return (this.id);
}

/*  Get Links method
 * 
 */
function _get_links ()
{
	return (this.links.head);
}


/* Days between Dates
 * 
 *  Taken from:
 *  http://www.mcfedries.com/JavaScript/DaysBetween.asp
 */
function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY);

    // http://www.mcfedries.com/JavaScript/DaysBetween.asp
}
