var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
var lbx_hilited = false;
var windim = new Array();
var over_the_lightbox = false;
var opa = nn6 ? 0.50 : 50;
var previews_on;
var snapStartX;
var snapStartY;
var snapEndX;
var snapEndY;
var snapTimer;
var snapCurrent;
var dnd_oid;
var finalDestination = "";
var oldX;
var oldY;
var scrolltop;
var windim=getClientDim();
var mouseDown = 0;
var myWidth = 0;
var myHeight = 0;

if (parseInt(navigator.appVersion.charAt(0)) >= 4)
{
	isSafari = (navigator.appVersion.indexOf("Safari") != -1) ? true : false;
	isStd = (navigator.appName == "Netscape") ? true : false
	isIE  = (navigator.appName.indexOf("Microsoft") != -1) ? true : false
	//alert(isIE)
	isMac = (navigator.platform.indexOf("Mac") != -1) ? true : false
	isPNG = (isIE && ! isMac) ? false : true

	//Firefox 1.0.x check - needs a refresh after certain events below (delete, add an item alrady in lb alert, etc)
	isFFOld = false;
	if(navigator.userAgent.indexOf("Firefox")!=-1)
	{
		var versionindex=navigator.userAgent.indexOf("Firefox")+8
		var versionindexSub=navigator.userAgent.indexOf("Firefox")+10
		if ( (parseInt(navigator.userAgent.charAt(versionindex))>=1) && (parseInt(navigator.userAgent.charAt(versionindexSub))==0) )
		{
			isFFOld = true;
		}
	}

}

function getClientDim() 
{
	//var b_version=navigator.appVersion
	//var version=parseFloat(b_version)

	if( typeof( window.innerWidth ) == 'number' ) 
	{
	  //Non-IE
	  myWidth = window.innerWidth;
	  myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
	  //IE 6+ in 'standards compliant mode'
	  myWidth = document.documentElement.clientWidth;
	  myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
	  //IE 4 compatible
	  myWidth = document.body.clientWidth;
	  myHeight = document.body.clientHeight;
	}
	
	// window.alert( 'Width = ' + myWidth );
	// window.alert( 'Height = ' + myHeight );
	return [myWidth, myHeight];
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function getObject(o)
{
	if (document.getElementById)
	    return document.getElementById(o)
	else if (document.all)
	    return document.all.o
}

function mousedownHandler(e)
{
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";
  var thebutton;
  var delay;
  var target = nn6 ? e.target : event.srcElement;
  var showprev

  if(isdrag) return false;

  delay = 100;

  while (fobj.tagName != topelement && fobj.className != "dragme")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (nn6) theButton = e.which;
  else theButton = event.button;
      
  if (theButton != 1) return false;

  if (fobj.className=="dragme")
  {
	showprev = fobj.name.charAt(fobj.name.length-1)
	dnd_oid = fobj.name;
	if (showprev == 'Y') 
	{
		dnd_oid = dnd_oid.substring(0,dnd_oid.length-1)
		isdrag = true;
	}

	dobj = getObject('dragger');
	dobj.style.visibility='hidden';
	dobj.innerHTML = '<img src="' + fobj.src + '">';
	dobj.style.zIndex = 2;

	if(nn6) dobj.style.opacity = opa
	else dobj.style.filter='alpha(opacity='+opa+')';

   x = nn6 ? e.clientX : event.clientX;
   y = nn6 ? e.clientY : event.clientY;
   //alert(x)
	oldX = x;
	oldY = y;
   if (isIE) 
   {
   	shift = 30;
	}
	else
	{
	shift = 30;
	}
	
	//	if(!isSafari) y += scrolltop;

	dobj.style.top = (y - shift) + 'px';
	dobj.style.left = (x - shift) + 'px';

   tx = parseInt(dobj.style.left);
   ty = parseInt(dobj.style.top);
    
   snapStartX = x - shift;
   snapStartY = y - shift;

   document.onmousemove=mousemoveHandler;
	document.onmouseup=mouseupHandler;
   return false;
  }
}

function mouseupHandler(e){

	var mu_x = nn6 ? e.clientX : event.clientX;
	var mu_y = nn6 ? e.clientY : event.clientY;

	document.onmousemove='';
	document.onmouseup='';
	if(oldX == mu_x && oldY == mu_y){
		if(dobj != "undefined") {
		
			dobj.style.visibility='hidden';
			dobj.innerHTML='';
		}
	}else if(isdrag){
	
		isdrag=false;
		
		if(lbx_hilited) {
			if(dobj != "undefined") dobj.style.visibility='hidden';
			if(dobj != "undefined") dobj.innerHTML='';
			hilite_Lbx();
			dropInLightbox(dnd_oid);
			//parent.window.location.reload();
			
			//Set lightbox icon to highlighted on search results without a wasted page refresh
			//alert("dnd_oid: " + dnd_oid);
			setLightBoxOnParent("lbIcon" + dnd_oid);
			
			
		}else{
			snapEndX = parseInt(dobj.style.left);
			snapEndY = parseInt(dobj.style.top);
			snapCurrent = 0;
			snapTimer = setInterval("snapBack('dragger', snapStartX, snapStartY, snapEndX, snapEndY, 20)", 2);
		}
	}
	hidetrail();
   mouseDown = 0;  // added to fix showTrail once the mouse is up
	return false;
}

function mousemoveHandler(e)
{
	var bottom_frame_top = windim[1]-180;
	var mouse_x = nn6 ? e.clientX : event.clientX;
	var mouse_y = nn6 ? e.clientY : event.clientY;
	var mouse_drag_y = isSafari ? mouse_y : mouse_y;
	if (isdrag)
	{
		if(mouse_x > windim[0]-115 || mouse_x < 75 || mouse_drag_y > windim[1]-45 || mouse_drag_y < 20) return false;
			
		dobj.style.left = tx + mouse_x - x;
		dobj.style.top  = nn6 && !isSafari ? ty + mouse_y - y  :  ty + mouse_y - y;
			
		if(mouse_drag_y > bottom_frame_top) 
		{
			if(!lbx_hilited) 
			{
				hilite_Lbx();
				over_the_lightbox = false;
			}
		}
		else if(lbx_hilited) 
		{
			hilite_Lbx();
			over_the_lightbox = false;
		}
			
	}
	else if(oldX != mouse_x && oldY != mouse_y)
	{
		isdrag = true;
		//document.advsearch.dragging.value = 1;
		dobj.style.visibility='visible';
	}

	dobj.style.visibility='visible';
	gettrailobj().left=mouse_x+"px"  // added on 1/3/07 on trial and error basis
	gettrailobj().top=mouse_y+"px"	// also this
		
	return false;
}

function hilite_Lbx() 
{
	if(lbx_hilited) 
	{
		frames['lbxframe'].document.bgColor = '#E4E4E4';
		lbx_hilited = false;
	}
	else 
	{	
		frames['lbxframe'].document.bgColor = '#CCCCCC';
		lbx_hilited = true;
	}
}

function dropInLightbox(o)
{
	//alert("Adding the image " + o + " to the lightbox")
	window.frames['lbxframe'].add_to_lbx3(o);
}


function go_to(URL)
{
	document.location=escape(URL);
	return false;
}


function snapBack(layerToSnap, startX, startY, endX, endY, steps) 
{
   snapCurrent++;

   if (snapCurrent == 1)
   {
       multi = 0;
   } 
   else 
   { 
       multi = (1 - Math.pow(0.9, snapCurrent - 1)) / (1 - Math.pow(0.9, steps - 1));
   }

   snapX = endX + (startX - endX) * multi;
   snapY = endY + (startY - endY) * multi;

   getObject(layerToSnap).style.left = snapX;
   getObject(layerToSnap).style.top = snapY;

   
   if (snapCurrent == steps) 
   {
		clearInterval(snapTimer);
		if(dobj != "undefined") dobj.innerHTML='';
   }
}

function toggle(fl) {
   if (document.forms.lightbox.toggle_lightbox.value == 1) 
   {
	   parent.document.getElementById('lbxbottomFrame').style.bottom=-90;
	   parent.document.body.style.paddingBottom = 35;
	   document.forms.lightbox.toggle_lightbox.value = 0;
	   document.getElementById('cxlbxview').style.visibility = 'visible'; 
	   document.getElementById('exlbxview').style.visibility = 'hidden';
	   
   } 
   else 
   {
	   parent.document.getElementById('lbxbottomFrame').style.bottom=0;
	   parent.document.body.style.paddingBottom = 125;
	   document.forms.lightbox.toggle_lightbox.value = 1;
	   document.getElementById('exlbxview').style.visibility = 'visible';
	   document.getElementById('cxlbxview').style.visibility = 'hidden';
   }
   update_cookie();
}

function mouseStatusCheck(md,mv,imagename, showthumb, imageNumber, brandText) 
{

	if (md == 1) 
	{
		mouseDown = 1;
	}
	//alert(mouseDown)
	else if((mv == 1) && (mouseDown == 0))
	{
		showtrail(imagename, showthumb, imageNumber, brandText);
	}
	else 
	{
		hidetrail();
	}
}

function setMouseUp() 
{
	mouseDown = 0;
}

function update_cookie (n,drf) 
{
	var asf = document.forms.lightbox;
	prefs = "toggleLightbox="  + parseInt(asf.toggle_lightbox.value);
	document.cookie = prefs;
}

function get_cookie ( cookie_name )
{
	var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

	if ( results )
		return ( unescape ( results[1] ) );
	else
		return null;
}

function applyLightboxCookie() 
{
	//No need to set here, pulls from form already.  
	//document.forms.lightbox.toggle_lightbox.value = get_cookie("toggleLightbox");
	//alert("document.forms.lightbox.toggle_lightbox.value: " + document.forms.lightbox.toggle_lightbox.value)

   if (document.forms.lightbox.toggle_lightbox.value == 1) 
   {
  	   document.getElementById('cxlbxview').style.visibility = 'hidden';
	   document.getElementById('exlbxview').style.visibility = 'visible';
	   parent.document.getElementById('lbxbottomFrame').style.bottom=0;
	   parent.document.body.style.paddingBottom = 125;	
   } 
   else 
   {
	   document.getElementById('exlbxview').style.visibility = 'hidden';
	   document.getElementById('cxlbxview').style.visibility = 'visible'; 
	   parent.document.getElementById('lbxbottomFrame').style.bottom=-90;
	   parent.document.body.style.paddingBottom = 35;
	}
	
}

//Set LBicon to highlighted on main page - note: needed new function b/c it sets parent.document.
function setLightBoxOnParent(imgname) 
{
	//window.alert("setLightBoxOnParent on imgname: " + imgname);
	parent.document.getElementById(imgname).setAttribute("src","/web/images/buttons/thumbnails/IconLBon.gif");
}

//Set LBicon to NOT highlighted on main page
function setLightBoxOffParent(imgname) 
{
	//window.alert("setLightBoxOffParent on imgname: " + imgname);
	//Check that we're even on a search results page that has that thumbnail icon.  IE, searching dog but deleting a cat image.
	if (parent.document.getElementById(imgname) != null)
	{
		parent.document.getElementById(imgname).setAttribute("src","/web/images/buttons/thumbnails/IconLBoff.gif");
	}
}


function Left(str, n)
{
   if (n <= 0)     // Invalid bound, return blank string
           return "";
   else if (n > String(str).length)   // Invalid bound, return
           return str;                // entire string
   else // Valid bound, return appropriate substring
           return String(str).substring(0,n);
}

document.onmousedown=mousedownHandler;
//document.onmousemove=mousemoveHandler;
//document.onmouseup = setMouseUp();

