var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;


function activateMenuForCurrentElement()
{

		jQuery.each(jQuery('#categories > li > a.current'), function() { showJSDDMCategories(); });
		jQuery.each(jQuery('#clients > li > a.current'), function() { showJSDDMClients(); });
}

var addShadowAttemptCounter = 1;
function addShadows(filter, containerId)
{

	jQuery.each(jQuery(filter), function() { 
		var origTarget = jQuery(this);

		var origPosition = origTarget.position(); // offset?

		var origWidth = origTarget.outerWidth(); // outerWidth?
		var origHeight = origTarget.outerHeight(); // outer?

		if (origWidth < 1 || origHeight < 1)
		{
			// sometimes, the super heavy  site has not had time to load the image
			// especially in safari... try a few times
			if (addShadowAttemptCounter < 8)
			{
				setTimeout("addShadows('" + filter + "', '" + containerId + "')", addShadowAttemptCounter * 250);
				addShadowAttemptCounter++;
				return;
			} else {
				addShadowAttemptCounter = 1;
				
				setTimeout("setupLightBoxen()", 200);
				return;
			}
		} else {
			
			addShadowAttemptCounter = 1;
			var targetEl = origTarget.clone();
			var containerEl = jQuery('#' + containerId).clone();
			containerEl.css({width: origWidth, height: origHeight});
			containerEl.attr('id', '');
	

			var mods = new Array( {cls: '.shadowBoxBg-n', css: {width: origWidth}},
					{cls: '.shadowBoxBg-ne', css: {left: origWidth}}, 
					{cls: '.shadowBoxBg-e', css: {height: origHeight, left: origWidth}},
					{cls: '.shadowBoxBg-se', css: {top: origHeight, left: origWidth}}, 
					{cls: '.shadowBoxBg-s', css: {width: origWidth, top:origHeight}},
					{cls: '.shadowBoxBg-sw', css: {top: origHeight}}, 
					{cls: '.shadowBoxBg-w', css: {height: origHeight}});
	
			for (i=0; i<mods.length; i++)
			{
				var m = mods[i];
				containerEl.children(m.cls).css(m.css);
			}
				var spans = containerEl.children('div').children('span').replaceWith(targetEl);
	
			if (! jQuery.support.opacity)
				containerEl.addClass('.shadowBox-ie');
	
			origTarget.replaceWith(containerEl);
			
			containerEl.show();



		}

		setTimeout("setupLightBoxen()", 500);
	});
}


/*
function setHeightForShadow(toElsFilterList, basedOnElFilter)
{
	var height = jQuery(basedOnElFilter).height();
	if (! height)
	{
		return;
	}
	for (i=0; i < toElsFilterList.length; i++)
	{
		jQuery(toElsFilterList[i]).height(height);
	}
}
*/
	
function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');


}

function jsddm_timedclose()
{
	jsddm_close();

	// oky... so we want to bring back our little submenu in cases where nothing is selected... so:
	if (jQuery('#categories').css('visibility') != 'visible' && jQuery('#clients').css('visibility') != 'visible')
	{
		activateMenuForCurrentElement();
	}
}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_timedclose, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}



function showJSDDMCategories()
{
	jsddm_canceltimer();
	jQuery('#categories').css('visibility', 'visible');
	jQuery('#clients').css('visibility', 'hidden');
}

function showJSDDMClients()
{
	
	jsddm_canceltimer();
	jQuery('#categories').css('visibility', 'hidden');
	jQuery('#clients').css('visibility', 'visible');
}

