/*2009-09-04*/
/*global $, document, window, alert, navigator */
var accessKeyModifier;





function readCookie(name)
{
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0) === ' ')
		{
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) === 0)
		{
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}





function setCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = '; expires=' + date.toGMTString();
	}
	else
	{
		expires = '';
	}
	document.cookie = name + '=' + value + expires + '; path=/' + '; domain=' + (document.domain.substr(0, document.domain.lastIndexOf('.')).indexOf('.') > 0 ? document.domain : '.' + document.domain);
}





function setCSS(title)
{
	var ss = document.getElementsByTagName('link');

	for (var i = 0; i < ss.length; i++)
	{
		if (ss[i].rel.indexOf('style') > -1 && ss[i].title)
		{
			ss[i].disabled = true;
			if (ss[i].title === title)
			{
				ss[i].disabled = false;

				var cookieExpire = (ss[i].rel === 'stylesheet' ? 0 : 365);

				setCookie('style', title, cookieExpire);
			}
		}
	}
}

if (readCookie('style') !== null)
{
	setCSS(readCookie('style'));
}





/*Internet Explorer sucks*/
function addEvent(thisElement, thisEvent, thisAction, bubble, allowDefault)
{
	if (navigator.appName !== 'Microsoft Internet Explorer')
	{
		thisElement.addEventListener(thisEvent, function (e)
		{
			if(allowDefault === false)
			{
				e.preventDefault();
			}

			thisAction();

		}, bubble);
	}
	else
	{
		thisElement.attachEvent('on' + thisEvent, function (e)
		{
			thisAction();

			if(allowDefault === false)
			{
				e.returnValue = false;
			}
		});
	}
}





// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = (window.location.href.slice(window.location.href.indexOf('?') + 1) + '&sf=all').split('&');
 
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}





function addSiftBox(thisParent, resetValue, submitValue)
{
	if($('siftbox') === undefined)
	{
		var siftDiv = document.createElement('div');
		siftDiv.id = 'sift';

		var siftbox = document.createElement('input');
		siftbox.id = 'siftbox';
		siftbox.type = 'text';
		siftbox.size = '128';

		var siftLabel = document.createElement('label');
		siftLabel.appendChild(document.createTextNode('Filter: '));
		siftLabel.appendChild(siftbox);
		siftLabel.appendChild(document.createTextNode(' '));
		siftDiv.appendChild(siftLabel);

		var searchTerms = getUrlVars(), searchTerm;

		for(var j = 0; j < searchTerms.length; j++)
		{
			if(!searchTerms[j].match(window.location.href) && !searchTerms[j].match('view'))
			{
				searchTerm = document.createElement('input');
				searchTerm.type = 'hidden';
				if(searchTerms[j] === 'f')
				{
					searchTerm.name = 'fid[]';	
				}
				else
				{
					searchTerm.name = searchTerms[j];
				}
				searchTerm.value = searchTerms[searchTerms[j]];
				siftDiv.appendChild(searchTerm);
			}
		}

		if(resetValue)
		{
			var siftreset = document.createElement('input');
			siftreset.id = 'siftreset';
			siftreset.type = 'reset';
			siftreset.value = resetValue;
			siftDiv.appendChild(siftreset);
			siftDiv.appendChild(document.createTextNode(' '));
		}

		if(submitValue)
		{
			var siftgo = document.createElement('input');
			siftgo.id = 'siftgo';
			siftgo.type = 'submit';
			siftgo.value = submitValue;
			siftDiv.appendChild(siftgo);
		}
		var siftform = document.createElement('form');
		siftform.id = 'siftform';
		siftform.method = 'post';
		siftform.appendChild(siftDiv);

		if(thisParent.childNodes[0] !== undefined)
		{
			thisParent.insertBefore(siftform, thisParent.childNodes[0]);
		}
		else
		{
			thisParent.appendChild(siftform);
		}
	}
}





function addClassName(item, thisName)
{
	if (!item.className.match(thisName))
	{
		item.className = (item.className + ' ').replace('  ', ' ') + thisName;
	}
}





function removeClassName(item, thisName)
{
	if (item.className.match(thisName))
	{
		item.className = item.className.replace(thisName, '').replace('  ', ' ');
	}
}





function closeButton()
{
	if (window.opener !== null)
	{
		var closeX = document.createElement('a');
		closeX.appendChild(document.createTextNode('X'));
		addClassName(closeX, 'closebutton');
		addEvent(closeX, 'click', function ()
		{
			window.close();
		}, true);
		$('close').insertBefore(closeX, $('close').childNodes[0]);
	}
}





function pmAlertMSG(newMSGs, inboxGo, later, pmLink)
{
	var pmlinkimg = document.createElement('img');
	pmlinkimg.src = '/styles/macstack/imageset/messagesnew.png';

	var pma = document.createElement('a');
	pma.href = pmLink;
	pma.appendChild(pmlinkimg);

	var pmalert = document.createElement('div');
	addClassName(pmalert, 'alert');
	pmalert.appendChild(document.createTextNode(newMSGs));
	
	var pma2 = document.createElement('a');
	pma2.href = pmLink;
	pma2.appendChild(document.createTextNode(inboxGo));

	var pma3 = document.createElement('a');
	pma3.id = 'later';
	pma3.appendChild(document.createTextNode(later));

	var pmdiv2 = document.createElement('div');
	pmdiv2.appendChild(pmalert);
	pmdiv2.appendChild(document.createElement('br'));
	pmdiv2.appendChild(pma2);
	pmdiv2.appendChild(document.createElement('br'));
	pmdiv2.appendChild(pma3);

	var pmannouncement = document.createElement('div');
	pmannouncement.id = 'pmannouncement';
	pmannouncement.appendChild(pma);
	pmannouncement.appendChild(pmdiv2);

	var pmdiv = document.createElement('div');
	pmdiv.id = 'pmdiv';
	pmdiv.appendChild(pmannouncement);
	document.body.insertBefore(pmdiv, document.body.childNodes[0]);

	var pmBG = document.createElement('div');
	pmBG.id = 'pmbackground';
	pmBG.appendChild(document.createTextNode(String.fromCharCode('160')));
	document.body.insertBefore(pmBG, document.body.childNodes[0]);

	addEvent($('later'), 'click', function ()
	{
		document.body.removeChild($('pmbackground'));
		document.body.removeChild($('pmdiv'));
	}, false, false);
}

/*like innerHTML, but proper XML*/
function getContents(item)
{
	var contents = '';
	var children = item.childNodes;
	for (var i = 0; i < children.length; i++)
	{
		if (children[i].childNodes.length !== 0)
		{
			contents += getContents(children[i]);
		}

		if (children[i].tagName !== undefined && children[i].tagName.toLowerCase() === 'br')
		{
			contents += '\n';
		}
		if (children[i].nodeValue !== null && children[i].nodeValue !== undefined && children[i].nodeValue !== '')
		{
			contents += children[i].nodeValue.replace(/(\t)/g, '');
		}
	}

	return contents;
}





function setAccessKeyModifier()
{
	if(navigator.userAgent.match("Opera"))
		{
			accessKeyModifier = 'shift esc, then';
		}
	else if(navigator.userAgent.match("Macintosh"))
	{
		if(navigator.userAgent.match("Safari"))
		{
			accessKeyModifier = 'ctrl option';
		}
		else
		{
			accessKeyModifier = 'ctrl';
		}
	}
	else if(navigator.userAgent.match("Firefox"))
	{
		accessKeyModifier = 'alt shift';
	}
	else
	{
		accessKeyModifier = 'alt';
	}
}

setAccessKeyModifier();





function addAccessKeyHint(thisElement)
{
	if(thisElement.accessKey !== undefined)
	{
		if(thisElement.title === '')
		{
			thisElement.title = accessKeyModifier + ' ' + thisElement.accessKey;
		}
		else
		{
			thisElement.title += ' (' + accessKeyModifier + ' ' + thisElement.accessKey + ')';
		}
	}
}