//-------------------------------------------------------------------------------------
// Replace the blank targets
//-------------------------------------------------------------------------------------

function SetTargets()
{
var links = document.getElementsByTagName('a');
for (var i=0;i < links.length;i++)
{
  if (links[i].className == 'blanktarget')
  {
            links[i].onclick = function()
            {
                window.open(this.href);
                return false;
            };
        }
    }
}

//-------------------------------------------------------------------------------------
// Prepare the background motions with the mouse
//-------------------------------------------------------------------------------------

function Backgrounds() 
{
   for(var i=0; i<100; i++) 
   {
   	e = document.getElementById('b'+i);
	if(e)
	{
		if(e.attachEvent) // IE
		{
			e.attachEvent('onmouseover',MouseIn);
			e.attachEvent('onmousedown',MouseOut);
			e.attachEvent('onmouseup',MouseClick);
			e.attachEvent('onnmouseout',MouseOut);
			e.attachEvent('onmousemove',MouseMove);
		}
            else
		{		
			e.onmouseover = MouseIn;
			e.onmousedown = MouseOut;
			e.onmouseup = MouseClick;
			e.onmouseout = MouseOut;
			e.onmousemove = MouseMove;
		}
	}
   }
}

//-------------------------------------------------------------------------------------
// Remove the parameters, if any, from the end of the current URL
//-------------------------------------------------------------------------------------

function MyUrl()
{
    var q = location.pathname.indexOf('?');
    if(q < 0)
	{
		return location.pathname;	
	}
	return location.pathname.substring(0, q);
}

//-------------------------------------------------------------------------------------
// Dynamically create a parameter
//-------------------------------------------------------------------------------------

function NewRequest(name, value)
{
  var myInput = document.createElement("input");
  myInput.type="hidden";
  myInput.setAttribute("name", name);
  myInput.setAttribute("value", value);
  return myInput;
}

//-------------------------------------------------------------------------------------
// Post to current form with scroll
//-------------------------------------------------------------------------------------

function PostMe()
{
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = MyUrl();

  myForm.appendChild(NewRequest("scrollx", (document.all)? document.body.scrollLeft:window.pageXOffset));
  myForm.appendChild(NewRequest("scrolly", (document.all)? document.body.scrollTop:window.pageYOffset));

  for(var i=0; i<arguments.length; i+=2)
  {
    myForm.appendChild(NewRequest(arguments[i],arguments[i+1]));
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

//-------------------------------------------------------------------------------------
// Returns the object that AJAX communicates client and server 
//-------------------------------------------------------------------------------------

function GetAjaxObject()
{
   if (window.ActiveXObject)
   { 
       return new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (window.XMLHttpRequest)
   {  
       return new XMLHttpRequest();
   }
   alert("Your browser does not support AJAX.");
   return null;
}

var ajaxObject;

//-------------------------------------------------------------------------------------
// Sets the contents of a div 
//-------------------------------------------------------------------------------------

function SetContents(id,contents)
{
	var elem = document.getElementById(id);
	if(elem)
	{
		var newdiv = document.createElement("div");
		newdiv.innerHTML = contents;
		while (elem.firstChild) 
		{
  			elem.removeChild(elem.firstChild);
		}
		elem.appendChild(newdiv);
	}
}

//-------------------------------------------------------------------------------------
// Makes a post with several parameters
//-------------------------------------------------------------------------------------

function PostData()
{
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = arguments[0]; // First parameter in command tail is destination
  for(var i=1; i<arguments.length; i+=2)
  {
    myForm.appendChild(NewRequest(arguments[i],arguments[i+1]));
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

//-------------------------------------------------------------------------------------
// Add parameters to the current form and submit it
//-------------------------------------------------------------------------------------

function AppendData()
{
   var form = document.getElementById('userdata');
   form.appendChild(NewRequest("scrollx", (document.all)? document.body.scrollLeft:window.pageXOffset));
   form.appendChild(NewRequest("scrolly", (document.all)? document.body.scrollTop:window.pageYOffset));

  for(var i=0; i<arguments.length; i+=2)
  {
    	form.appendChild(NewRequest(arguments[i],arguments[i+1]));
  }
  form.submit() ;
}

//-------------------------------------------------------------------------------------
// In main, post a new search when any of the search selections change
//-------------------------------------------------------------------------------------

function changeSearch()
{
   	var sel = document.getElementById('selection');
	var dept = sel.Dept.options[sel.Dept.selectedIndex].value;
	var sortby = sel.SortedBy.options[sel.SortedBy.selectedIndex].value;
	var ascdesc = sel.SortOrder.options[sel.SortOrder.selectedIndex].value;
	PostMe("Dept", dept, "SortedBy", sortby, "SortOrder", ascdesc);
}

//-------------------------------------------------------------------------------------
// Figures out the element who triggered the event 
//-------------------------------------------------------------------------------------

function CallerId(e) 
{
   if(e.srcElement)
   {
     return e.srcElement;
   }	
   if(e.target)
   {
     return e.target;  
   }
   return NULL;
}

//-------------------------------------------------------------------------------------
// Figures out if the browser is IE 
//-------------------------------------------------------------------------------------

function isIE() {
    return navigator.userAgent.indexOf("MSIE") >= 0;
}

//-------------------------------------------------------------------------------------
// Mouse position 
//-------------------------------------------------------------------------------------

function getMouseX(event) {
    var ie = isIE();
    return ie ? window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft : event.clientX + window.scrollX;
}

function getMouseY(event) {
    var ie = isIE();
    return ie ? window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop : event.clientY + window.scrollY;
}

//-------------------------------------------------------------------------------------
// Absolute position of an element 
//-------------------------------------------------------------------------------------

function PosX(p) 
{
	var x = p.offsetLeft;
	while (p.offsetParent) 
      {
	   p = p.offsetParent
	   x += p.offsetLeft;
      }
	return x;
}

function PosY(p) 
{
	var y = p.offsetTop;
	while (p.offsetParent) 
      {
	   p = p.offsetParent
	   y += p.offsetTop;
      }
	return y;
}

//-------------------------------------------------------------------------------------
// Mouse events for the backgrounds
//-------------------------------------------------------------------------------------

var  movingImgId="";
var  movingImgIcon="";

function GetIcon(p)
{
	var b = p.style.backgroundImage;
	var idx = b.lastIndexOf("/")+1;
	var n = b.lastIndexOf("_");
	return  b.substring(idx , n); 	
}

function MouseIn(e) 
{
   p = CallerId(e);
   if(p)
   {
      movingImgId = p.id;
      movingIcon = GetIcon(p);
   	p.style.background = "url(/Images/"+ movingIcon +".jpg)";
   }
}

function MouseClick(e)
{
   p = CallerId(e);
   if(p)
   {
		var icon = GetIcon(p);
		window.location = "ShowPhoto.php?photo="+icon+".jpg";
   }
}

function MouseMove(e) 
{
   p = document.getElementById(movingImgId);
   if(p)
   {
      PlaceImage(e, p);
   }
}

function MouseOut() 
{
   var p = document.getElementById(movingImgId);
   if(p)
   {
      p.style.backgroundPosition = "0px 0px";
      p.style.background = "url(/Images/"+ movingIcon+ "_small.jpg)";
   }
}

//-------------------------------------------------------------------------------------
// Places the background big image considering the mouse position 
//-------------------------------------------------------------------------------------

function PlaceImage(e, p)
{
    // x, y will be the absolute position of the mouse
    var x = e.clientX;
    var y = e.clientY;	
    if( document.body ) 
    {
        x +=  document.body.scrollLeft;
        y +=  document.body.scrollTop;
    } 
    else // IE
    {
       x += document.documentElement.scrollLeft;
       y += document.documentElement.scrollTop;
    }

    // Now relative to the upper left corner of the image 
    x -= PosX(p);
    y -= PosY(p);
   
    // The cursor movement over the box is equal to the part of the image out of the box
    var dx = 84;
    var dy = 60;
    if(p.offsetWidth)  
    {	
    	dx = p.offsetWidth;
    	dy = p.offsetHeight;
    }

    var myImg = new Image();
    myImg.src = "/Images/"+ movingIcon + ".jpg";
    x = Math.floor( (x * (myImg.width - dx))/ dx );
    y = Math.floor( (y * (myImg.height - dy))/ dy );
    
    p.style.backgroundPosition = '-'+ x + 'px -' + y + 'px';
}


