/**
 * Add a rollover effect to the specified image, by adding event
 * handlers to switch the image to the specified URL while the
 * mouse is over the image.
 *
 * If the image is specified as a string, search for an image with that
 * string as its id or name attribute.
 * 
 * This method sets the onmouseover and onmouseout event handler properties
 * of the specified image, overwriting and discarding any handlers previously
 * set on those properties.
 */
   
function addRollover(img, rolloverURL) {
    if (typeof img == "string") {  // If img is a string,
        var id = img;              // it is an id, not an image
        img = null;                // and we don't have an image yet.
        // First try looking the image up by id
        if (document.getElementById) {
           img = document.getElementById(id);
        } else if (document.all) {
           img = document.all[id];
        }

        // If not found by id, try looking the image up by name.
        if (!img) img = document.images[id];
        
        // If we couldn't find the image, do nothing and fail silently
        if (!img) return;
    }

    // If we found an element but it is not an <img> tag, we also fail
    if (img.tagName.toLowerCase() != "img") return;

    // Remember the original URL of the image
    var baseURL = img.src;

    // Preload the rollover image into the browser's cache
    (new Image()).src = rolloverURL;

    img.onmouseover = function() { img.src = rolloverURL; }
    img.onmouseout = function() { img.src = baseURL; }
}
/**
 * Find all <img> tags in the document that have a "rollover"
 * attribute on them.  Use the value of this attribute as the URL of an 
 * image to be displayed when the mouse passes over the image and set 
 * appropriate event handlers to create the rollover effect.
 */
function initRollovers() {
    var images = document.getElementsByTagName("img");
    for(var i = 0; i < images.length; i++) {
        var image = images[i];
        var rolloverURL = image.getAttribute("rollover");
        if (rolloverURL) addRollover(image, rolloverURL);
    }
}

function NDS_preloadImages() { //v1.0
   var d=document; 
   if(d.images){ 
      if(!d.NDS_p) d.NDS_p=new Array();
   }
   var i,j=d.NDS_p.length,a=NDS_preloadImages.arguments; 
   for(i=0; i<a.length; i++) {
      if (a[i].indexOf("#")!=0){ 
         d.NDS_p[j]=new Image; 
         d.NDS_p[j++].src=a[i];
      }
   }
}

//Good Script for centering a div box.

function centerDiv(id) {

   var vd = document.viewport.getDimensions();
   var so = document.viewport.getScrollOffsets(); 
   var myID = $(id).getDimensions();     
   $(id).style.position = 'absolute';
   $(id).style.left = Math.round(vd.width/2,0) - Math.round(myID.width/2,0)  + so.left + "px";
   $(id).style.top = Math.round(vd.height/2,0)- Math.round(myID.height/2,0) + so.top + "px";   
      
}

function addMask(o) {
   //Create Mask
   var m = document.createElement('div');
   m.setAttribute('class','mask');
   m.setAttribute('id','mask');

   var a = document.viewport.getDimensions();
   var b = document.viewport.getScrollOffsets();  
   var c = a.width + b.left + 'px';
   var d = a.height + b.top + 'px';
   
   m.style.width = c;
   m.style.height = d;
      
   document.body.appendChild(m);
   $('mask').setOpacity(o);

}

function frmPopup(frmURL) {
   
   // Add mask with transparency 0 = transparent, 1 = Opaque
   addMask(".50");
   
   //Create a Popup Window DIV   
   var popupWindow = document.createElement('div');
   popupWindow.setAttribute('class','popupWindow');
   popupWindow.setAttribute('id','popupWindow');
  
   var closeBox = document.createElement('div');
   closeBox.setAttribute('class','closeBox');
   closeBox.innerHTML ='<img title=\"Close\" src=\"site_images/closeX.gif\" onclick=\"$(\'mask\').remove();$(\'popupWindow\').remove();\" onmouseover=\"this.style.cursor=\'hand\'\"/>';    
   popupWindow.appendChild(closeBox);
  
   document.body.appendChild(popupWindow);
  
  
   centerDiv('popupWindow');
   showForm(frmURL);     
}

// Removes any element based on ID
function removeModal(list) {
   var a = new Array();
   a = list.split(",");
   for (var i = 0; i < a.length; i++) {
      var e = document.getElementById(a[i]);
      e.parentNode.removeChild(e);
   }
}

/*
Script for editing horses
*/
function showForm(frmURL) { 

   var frmContainer = document.createElement('div');
   frmContainer.setAttribute('id','frmContainer');         
   $('popupWindow').appendChild(frmContainer);
   $('frmContainer').innerHTML="<img src='site_images/loading_bar.gif'/>";   
            
	new Ajax.Request(frmURL, {
		method: 'get',
		parameters: 'xhr=1&',
		onSuccess: function(xhrResponse) {
			$('frmContainer').innerHTML = xhrResponse.responseText;
		},
		onFailure: function(xhrResponse) {
			$('frmContainer').innerHTML = xhrResponse.responseText;
		}
	});
	return (false);
}

/*
Script to add the Image Iframe Upload Form
*/

function frmImageIframe(hi_id) {
   var ifID = "if" + hi_id;
   var toolbarID = "toolbar" + hi_id;
   var new_iframe = document.createElement('iframe');
   new_iframe.src = 'lazyadmin/upload.php?hi_id=' + hi_id;
   new_iframe.frameBorder = '0';
   $(ifID).appendChild(new_iframe);
   $(toolbarID).style.visibility = 'hidden';

}


if (window.addEventListener) 
	window.addEventListener("load", initRollovers, false);
else if (window.attachEvent)
	window.attachEvent("onload", initRollovers);