function initLinks()
{
	if(!document.getElementsByTagName)
		return false;

	var links = document.getElementsByTagName('A');
	for(var i = 0, node; node = links[i]; i++)
		if(node.getAttribute('rel') == 'external')
			node.target = '_blank';
}

function showBasicSearch()
{
	if(!document.getElementById)
		return true;

	var basicSearchLink = document.getElementById('basic-search-link');
	if(!basicSearchLink)
		return true;
	
	var basicSearchInput = document.getElementById('basic-search-input');
	if(!basicSearchInput)
	{
		basicSearchInput = document.createElement('INPUT');
		basicSearchInput.id = 'basic-search-input';
		basicSearchInput.type = 'text';
		basicSearchInput.style.display = 'none';
		basicSearchInput.onkeyup = function(event)
		{
			if(!event)
				var event = window.event;
		
			// Return
			if(event.keyCode == 13)
			{
				window.location = 'search.php?q='+this.value;
			}
			// Escape
			else if(event.keyCode == 27)
			{
				var basicSearchLink = document.getElementById('basic-search-link');
				basicSearchLink.style.display = 'block';
				this.style.display = 'none';
			}
		}
		
		basicSearchLink.parentNode.appendChild(basicSearchInput);
	}
	
	basicSearchLink.style.display = 'none';
	basicSearchInput.style.display = 'block';
	basicSearchInput.focus();
	return false;
}

function showAdvancedSearch()
{
	if(!document.getElementById)
		return true;

	var advSearchLink = document.getElementById('advanced-search-link');
	var advSearchForm = document.getElementById('advanced-search');
	var advSearchMask = document.getElementById('advanced-search-mask');
	
	if(!advSearchLink || !advSearchForm || !advSearchMask)
		return true;

	if(advSearchForm.style.display == 'block')
	{
		advSearchForm.style.display = 'none';
		advSearchMask.style.display = 'none';
		advSearchLink.parentNode.className = '';
		
		// Hacky fix for the infamous IE Select Z-Index bug
		if(document.all && document.getElementById('ss-form'))
		{
			var selects = document.getElementById('ss-form').getElementsByTagName('SELECT');
			for(var i = 0, s; s = selects[i]; i++)
				s.style.visibility = 'visible';
		}

		// Hacky fix for Flash in FF/win		
		var video = document.getElementById('key-video');
		if(video)
			video.style.visibility = 'visible';
	}
	else
	{
		advSearchForm.style.display = 'block';
		advSearchMask.style.display = 'block';
		advSearchLink.parentNode.className = 'selected';
		
		//if(!ssLoadSearchesComplete)
			ssLoadSearches();
			
		// Hacky fix for the infamous IE Select Z-Index bug
		if(document.all && document.getElementById('ss-form'))
		{
			var selects = document.getElementById('ss-form').getElementsByTagName('SELECT');
			for(var i = 0, s; s = selects[i]; i++)
				s.style.visibility = 'hidden';
		}
		
		// Hacky fix for Flash in FF/win
		var video = document.getElementById('key-video');
		if(video)
			video.style.visibility = 'hidden';
	}

	return false;
}

function showHideThumbnails()
{
	if(!document.getElementById)
		return true;
		
	var showHideLink = document.getElementById('show-hide-thumbs-link');
	var thumbnails = document.getElementById('diary-thumbnails');
	var editorPick = document.getElementById('diary-pick');
	var entry = document.getElementById('diary-entry');	

	if(!showHideLink || !thumbnails || !editorPick || !entry)
		return true;
	
	var linkText;
	if(thumbnails.style.display == 'none')
	{
		thumbnails.style.display = 'block';
		editorPick.style.display = 'block';
		linkText = 'Hide Thumbnails';
		entry.className = '';
		updateSession('showthumbs', 'true');
	}
	else
	{
		thumbnails.style.display = 'none';
		editorPick.style.display = 'none';
		linkText = 'Show Thumbnails';
		entry.className = 'no-thumbs';
		updateSession('showthumbs', 'false');
	}
	
	pregoReplaceChildren(showHideLink, linkText);
	
	return false;
}

function initImageMenus()
{
	var divs = document.getElementsByTagName('DIV');
	for(var i = 0, div; div = divs[i]; i++)
	{
		if(div.className == 'image-menu' || div.className == 'caption-image-menu')
		{
			var imgID = div.id.replace('-menu', '');
			var img = document.getElementById(imgID);
			var caption = document.getElementById(imgID+'-caption');
			if(img)
			{
				div.style.display = 'none';
				img.onmouseover = function()
				{
					var menu = document.getElementById(this.id + '-menu');
					// Moving the caption deprecated in favour of a new layout
					//var caption = document.getElementById(this.id + '-caption');
					
					if(menu)
					{
						menu.style.display = 'block';
						//if(caption)
						//	menu.style.bottom = caption.offsetHeight + 'px';
					}
				}
				img.onmouseout = function()
				{
					var menu = document.getElementById(this.id + '-menu');
					
					if(menu)
						menu.style.display = 'none';
				}
				div.onmouseover = function()
				{
					this.style.display = 'block';
				}
				div.onmouseout = function()
				{
					this.style.display = 'none';
				}
				
				if(caption)
				{		
					caption.onmouseover = function()
					{
						var menu = document.getElementById(this.id.replace('-caption', '-menu'));
						menu.style.display = 'block';
					}
					caption.onmouseout = function()
					{
						var menu = document.getElementById(this.id.replace('-caption', '-menu'));
						menu.style.display = 'none';
					}
				}
			}
		}
	}
}

function initMyPreview()
{
	if(!document.getElementById)
		return;

	var options = document.getElementById('mypreview-options');
	if(options)
		options.parentNode.removeChild(options);
}

function initDiaryLinks()
{
	var diaryContent = document.getElementById('diary-description');
	if(!diaryContent)
		return;
	
	var links = diaryContent.getElementsByTagName('A');
	for(var i = 0, link; link = links[i]; i++)
	{
		link.target = '_blank';
	}
}

function printView(items)
{
	// Convert single values to arrays
	if(!(typeof(items) == 'object' && items.length))
		items = [items];
	
	var url = 'print.php?data=' + items.join(',');
	window.open(url,'previewPrint','status=0,toolbar=0,width=775,height=581,scrollbars=1');
	
	return false;
}

function download(items)
{
	// Convert single values to arrays
	if(!(typeof(items) == 'object' && items.length))
		items = [items];
	
	window.location = 'download.php?data=' + items.join(',');
	
	return false;
}


// Thanks to Simon Willison <http://simonwillison.net/2004/May/26/addLoadEvent/>
function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


addLoadEvent(initLinks);
addLoadEvent(initImageMenus);
addLoadEvent(initMyPreview);
addLoadEvent(initDiaryLinks);




function setAjaxLoadingStatus(node, message)
{
	var msgNode = pregoCreateElement('SPAN', [
		pregoCreateImage('images/loading.gif', ''),
		' ',
		message
	]);
	setAjaxStatus(node, msgNode);
}

function setAjaxStatus(node, message)
{
	clearAjaxStatus(node);
	
	var msgNode = pregoCreateElement('P', message);
	msgNode.className = 'ajax-status';
	
	node.insertBefore(msgNode, node.firstChild);
}

function clearAjaxStatus(node)
{
	if(node.firstChild && node.firstChild.className == 'ajax-status')
		node.removeChild(node.firstChild);
}

function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	}
	else{
		return document[movieName];
	}
}

function changeVideo(event, filename, id)
{
	if(!event)
		event = window.event;
	
	// Get the node that was clicked
	var node;
	if(event.target)
		node = event.target;
	else if(event.srcElement)
		node = event.srcElement;
		
	if(node.nodeType == 3)
		node = node.parentNode;
	
	// Set all other items in the list to not selected
	var items = node.parentNode.parentNode.getElementsByTagName('LI');
	for(var i = 0, item; item = items[i]; i++)
		item.className = '';
	
	// Set the clicked item to selected
	node.parentNode.className = 'selected';

	thisMovie("video-player").loadFile("keytrends/"+filename);
	videoID = id;
	return false;
}

var videoTime = 0;
var videoID = 0;

function getUpdate(typ,pr1,pr2) {
	if(typ == "time") {
	videoTime = Math.floor(pr1);
	}
}

function videoFunction(typ){
 	if (videoTime > 0 && videoID > 0){
		if (typ == "download")
			window.location = "download.php?data=video."+videoID+"."+videoTime;
		else if (typ == "mypreview")
		{
			var player = document.getElementById('video-player');
			
			var node = player;
			var x = 5;
			var y = player.offsetHeight + 25;
			do
			{
				x += node.offsetLeft;
				y += node.offsetTop;
			}
			while(node = node.offsetParent);
			
			//alert(x+', '+y);
			
			//var x = player.offsetParent.offsetLeft + player.offsetLeft + (player.offsetWidth / 2) - 20;
			//var y = player.offsetParent.offsetTop + player.offsetTop + player.offsetHeight - 10;
			var event = {'pageX' : x, 'pageY' : y};
			
			displayMyPreviewInterface(event, 'Key Trends Video Image', videoID, 'video',videoTime);
		}
		else if (typ == "print")
			window.open("print.php?data=video."+videoID+"."+videoTime);
	}
}

function updateSession(varn, val)
{
	pregoAjaxGetRequest('action.session.php?var='+varn+'&val='+val, function() {});
}

var imagePopupWindow;
function imagePopup(section, img, id, cat)
{
	if(imagePopupWindow)
		imagePopupWindow.close();

	var url = 'image_popup.php?section='+section+'&image='+img+'&id='+id+'&category='+cat;
	imagePopupWindow = window.open(url,'previewImage','status=0,toolbar=0,width=775,height=581,scrollbars=1');
	
	return false;
}

var userGuidePopupWindow;
function userGuidePopup(section)
{
	if(userGuidePopupWindow)
		userGuidePopupWindow.close();

	var url = 'userguide.php?section='+section;
	userGuidePopupWindow = window.open(url, 'previewHelp', 'status=0,toolbar=0,width=775,height=581,scrollbars=1');
	
	return false;
}

function showHide(id)
{
	var node = document.getElementById(id);
	if(!node)
		return;
		
	node.style.display = (node.style.display == 'none' ? 'block' : 'none');
}