//	close the pop-up, refresh main window
function	pop_down(address)
{
	parent.location.reload(true);
	if	(address	!=	null)
	{
		parent.location.href	=	(address);
	}
	top.tb_remove();
}


var	seconds			=	2;

function	display_countdown(address)
{
	var span 		=	document.getElementById('countdown');

	if (span.hasChildNodes())
	{
		while (span.childNodes.length >= 1)
		{
			span.removeChild(span.firstChild);
		}       
	}
	
	var countdown	=	document.createElement('span');
	countdown.appendChild(document.createTextNode(seconds));
	
	--seconds;
	
	//	append your newly created element to an already existing element.
	document.getElementById('countdown').appendChild(countdown);
			

	if	(seconds	== -1)
	{
			pop_down(address);
 	} 
 	else
	{
    	if	(address	!=	null)
		{
			setTimeout('display_countdown("'+address+'");', 1000);
		}
		else
		{
			setTimeout('display_countdown();', 1000);
		}
	}
} 

function trim(string)
{
	return string.replace(/^\s+|\s+$/g, '')
}

//	replace with jquery
function text_counter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else // otherwise, update 'characters left' counter
	{
		document.getElementById('character_count').innerHTML = maxlimit - field.value.length + ' characters left';
	}
}

function	highlight_document(document_id)
{
	$('#docu_'	+	document_id).parent().parent().removeClass('odd').addClass('highlight').fadeOut('normal').fadeIn('normal').fadeOut('normal').fadeIn('normal').fadeOut('normal').fadeIn('normal').fadeOut('normal').fadeIn('normal');
}

function	highlight_folder(folder_id)
{
	$('#fold_'	+	folder_id).parent().parent().removeClass('odd').addClass('highlight').fadeOut('normal').fadeIn('normal').fadeOut('normal').fadeIn('normal').fadeOut('normal').fadeIn('normal').fadeOut('normal').fadeIn('normal');
}

//	fixPNG();
//	http://www.tigir.com/js/fixpng.js (author Tigirlas Igor)
function	fixPNG(element)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
		var src;

		if (element.tagName	==	'img')
		{
			if (/\.png$/.test(element.src))
			{
				src			=	element.src;
				element.src	=	'http://img.docuview.co.uk/px.gif';
			}
		}
		else
		{
			src				=	element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src)
			{
				src			=	src[1];
				element.runtimeStyle.backgroundImage	=	'none';
			}
		}
		
		if (src) 
		{
			element.runtimeStyle.filter			=	'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '",sizingMethod="scale")';
		}
	}
}

//	jquery - qTips/simple tooltip - eurocopter world
function simple_tooltip(target_items, name)
{
	$(target_items).each(function(i)
	{
		$('body').append('<div class="'	+	name	+	'" id="'	+	name	+	i	+	'"><p>'	+	$(this).attr("title")	+	'</p></div>');
		var my_tooltip = $('#'+name+i);
		
		var height = $('#' + name + i).height();
		var width = $('#' + name + i).width();

		var top  = $(this).offset().top - (height + 10);
		var left = $(this).offset().left + ($(this).width() / 2) + (width / 2);
		
		$(this).removeAttr('title').mouseover(function(){
				my_tooltip.css({opacity:0.9, display:'none'}).fadeIn(200).css('left', left).css('top', top);
		}).mousemove(function(kmouse){
				//	pageX - half of the width of the element
				my_tooltip.css({left:kmouse.pageX-125, top:kmouse.pageY-35});
		}).mouseout(function(){
				my_tooltip.fadeOut(200);
		});

	});
}

//filter results based on query
function	filter(selector, query)
{
	//trim white space
	query	=	$.trim(query);
	//add OR for regex
	query	=	query.replace(/ /gi, '|');

	$(selector).each(function()
	{
		($(this).text().search(new RegExp(query, 'i')) < 0) ? $(this).hide().removeClass('visible') : $(this).show().addClass('visible');
	});
}

//	initiate on document load
$(document).ready(function()
{
	//	initiate tooltips
	simple_tooltip('td.action a', 'tooltip');
	
	//default each row to visible
	$('tbody tr').addClass('visible');
	
	//overrides CSS display:none property
	//so only users w/ JS will see the
	//filter box
	$('#filter').keyup(function(event)
	{
		//if esc is pressed or nothing is entered
		if (event.keyCode == 27 || $(this).val() == '')
		{
			//if esc is pressed we want to clear the value of search box
			$(this).val('');
				
			//we want each row to be visible because if nothing
			//is entered then all rows are matched.
			$('tbody tr').removeClass('visible').show().addClass('visible');
		}
		//if there is text, lets filter
		else
		{
			filter('tbody tr', $(this).val());
		}
	});

	//	sort tables
	$('table').tablesorter({widgets: ['zebra']});
	
	//	nice timestamp
	$('abbr[class*=timeago]').timeago();

	//	calendars
	$('#date_from').datepick();
	$('#date_to').datepick();
	$('#document_expiry_date').datepick();
	
	//	dock for document actions
	var dock_options = {labels: 'tc', duration: 200, size: 48, distance: 60, coefficient: .9};
	$('#dock').jqDock(dock_options);
});

