
var Dom = {
  get: function(el) {
    if (typeof el === 'string') {
      return document.getElementById(el);
    } else {
      return el;
    }
  },
  add: function(el, dest) {
    var el = this.get(el);
    var dest = this.get(dest);
    dest.appendChild(el);
  },
  remove: function(el) {
    var el = this.get(el);
    el.parentNode.removeChild(el);
  }
};

  
var Event = {
  add: function() {
    if (window.addEventListener) {
      return function(el, type, fn) {
        Dom.get(el).addEventListener(type, fn, false);
      };
    } else if (window.attachEvent) {
      return function(el, type, fn) {
        var f = function() {
          fn.call(Dom.get(el), window.event);
        };
        Dom.get(el).attachEvent('on' + type, f);
      };
    }
  }()
};
 

var cookieHandler = {

	getCookie : function(nm) {
		var start = document.cookie.indexOf( nm + "=" );
		var len = start + nm.length + 1;
		if ( ( !start ) && ( nm != document.cookie.substring( 0, nm.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	},
	
	
	setCookie : function(nm, value, expires) {
		if (!expires) expires = new Date();
			document.cookie=nm+"="+escape (value)+"; expires="+expires.toGMTString()+"; path=/";
	}

}

var sitemarks = {
	
	expdate : new Date(),
	olddate : new Date(),
	sm : {},
	init : function(cookieHandler) {
		this.expdate.setTime (this.expdate.getTime() + (365*24*60*60*1000));
		this.olddate.setTime (this.expdate.getTime() - 1);
	},

	add : function(nm, url) {
	
		var i = 0;
		
		var emptyEntry = 0;
		
		var emptyVal = 0;
		
		var found=false;
		
		
		
		while (cookieHandler.getCookie('nms' + i) != null) { 
			if (cookieHandler.getCookie('nms' + i) == nm) {
				found=true;
			}
			
			// check whether the cookie was empty
			if (cookieHandler.getCookie('nms' + i) == "" && emptyEntry == 0) {
				emptyEntry = 1;
				emptyVal = i;
				//alert(i);
			}
			
			i++;
			
		}    
		// if there was an empty cookie, overwrite it
		if (emptyEntry != 0) {
		i = emptyVal;
		}
		
		if (!found) {
			cookieHandler.setCookie ('nms' + i, nm, this.expdate);
			cookieHandler.setCookie ('urls' + i, url, this.expdate);
			//alert("Test message: adding " + nm + " to your studio list now");
			location.reload();
		}
		
		else {
		//alert('This page has been added to your stored documents. ');
		}
	
	},
	
	delone : function(i) {
		
		// delete one entry and move any subsequent entries back one place.
		i = i+0;
		
		while (cookieHandler.getCookie('nms' + (i+1)) != null && cookieHandler.getCookie('nms' + (i+1)) != "") {
			//alert(cookieHandler.getCookie('nms' + (i+1)));
			k = i+1;
			cookieHandler.setCookie ('nms' + i, cookieHandler.getCookie('nms' + k), this.expdate);
			cookieHandler.setCookie ('urls' + i, cookieHandler.getCookie('urls' + k), this.expdate);
			i++;
		}
		
		// set the last entry to empty
		cookieHandler.setCookie ('nms' + i, '', this.olddate);
		cookieHandler.setCookie ('urls' + i, '', this.olddate);
		
		location.reload();
			
	},
	
	delall : function() {
		// set the contents of all entries to empty and set expiry dates to a date in the past
		var i = 0;
		
		if (window.confirm('Clear studio list?  ')) {
		
			while (cookieHandler.getCookie('nms' + i) != null) {
				
				cookieHandler.setCookie ('nms' + i, '', this.olddate);
				cookieHandler.setCookie ('urls' + i, '', this.olddate);
				i++; 
			} 
			
			location.reload();
			
		}
	},
	
	
	showLinks : function(id, title) {
		
		// check whether this browser supports getElementById and createElement
		if (!document.getElementById || !document.createElement) {
			// if not, end script, display nothing.
			return;
		}
		
		// check whether browser can write and then read a cookie
		if (cookieHandler.getCookie('cookiecheck') != 1) {
			
			cookieHandler.setCookie('cookiecheck', 1, this.expdate);
			if (cookieHandler.getCookie('cookiecheck') != 1) {
				// if not, end script, display nothing.
				return;
			}
		}
		
		// the container element for the sitemarks menu and links
	        this.sm = document.getElementById(id);
	
	       // add the title
	        el = document.createElement('h3');
	        el.innerHTML = title;
	        Dom.add(el, 'sitemarks');
		
		
		
		// add the list of links and check to see if the current page is in the list
		var i = 0;
		var found = 0;
		
		while (cookieHandler.getCookie('nms' + i) != null && cookieHandler.getCookie('nms' + i) != "") { 

			if (cookieHandler.getCookie('nms' + i) == document.title) {
				found = 1;
			}
			
			if (i == 0) {
				e = document.createElement('ul');
				e.className = 'list1';
			}
			
			el = document.createElement('li');
			el2 = document.createElement('a');

			el2.setAttribute('href', cookieHandler.getCookie('urls' + i));
			el2.innerHTML = cookieHandler.getCookie('nms' + i);
			e.appendChild(el);
			el.appendChild(el2);
			
			i++; 
		} 
		
		
		if (i==0) {
			
			// there are no entries
			// show the introductory text
			
			el2 = document.createElement('p');
			el2.style.marginRight = '25px';
			el2.innerHTML = 'You can keep a list of your favorite artists by using Studio List. Click on \'Add this artist to your studio list\' to save a link to the page you are viewing. Add more artists to the list in the same way.  Your Studio List will be visible on all artist\'s web pages, and be remembered whenever you return to the SEOS website.';
			Dom.add(el2, 'sitemarks');
			
		}
		else
		{
			// there are some entries, so show the link to clear all entries
			e4 = document.createElement('div');
			
			e5 = document.createElement('a');
			e5.innerHTML = 'Clear studio list &laquo;';
			e5.className = 'sitemarksmenu';
			e5.style.cursor = 'pointer';
			
			e4.appendChild(e5);
			Dom.add(e4, 'sitemarks');
			
			Event.add(e5, 'click', function(e) {
				sitemarks.delall();
				return(false);
			});
			
			Dom.add(e, 'sitemarks');
		}
		
		e3 = document.createElement('div');

		
		if (found != 1) {
			
			// entry does not exist, show 'add' link
			
			e4 = document.createElement('a');
			e4.innerHTML = 'Add this artist to your studio list &raquo;';
			e4.className = 'sitemarksmenu';
			e4.style.cursor = 'pointer';
			
			e3.appendChild(e4);
			Dom.add(e3, 'sitemarks');
			
			Event.add(e4, 'click', function(e) {
				sitemarks.add(document.title, location.href);
				return(false);
			});
			
		}
		else {
			// entry already exists
			e3.innerHTML = 'This artist is in your studio list.';
			Dom.add(e3, 'sitemarks');
		}
		
		
		
	}

}

sitemarks.init(cookieHandler);
