/***********************************************************************************************************************************

	SHOW HOVER CLIP ON MOUSE OVER
************************************************************************************************************************************/
function showHover(clipTop,clipRight,clipBottom,clipLeft){	
	/*document.getElementById('hoverOverlay').style.display = 'block';
	
	document.getElementById('hoverOverlay').style.clip = 'rect('+clipTop+'px,'+clipRight+'px,'+clipBottom+'px,'+clipLeft+'px)';*/
}

/***********************************************************************************************************************************

	CREATE AJAX OBJECT
************************************************************************************************************************************/function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();	
	} else if (window.ActiveXObject) { // IE
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { // catch Mac IE 5 here
				xhr = false;	
			}
		}
	}
	return xhr;
}

/***********************************************************************************************************************************

	POPUP - Call page, fade in, close
************************************************************************************************************************************///
/* fetch a PRODUCT - start with page fade: */
function getPage(reqPage) {
	// check for existence! (ie related products!)
	if ($('popupContainer')) {
		fadePage(reqPage); // fade the page and pass through the requested page
	} else {
		// if we are on 'passthru' the fader div will already exist, and we need to leave it alone. Else create and fade in:
		if ($('backgroundFade')) {
			// if no fader needed, we'll call the product straight away:
			ajaxCall(reqPage);
		} else {
			// create the fader div
			var fader = document.createElement('div');
			fader.setAttribute('id','backgroundFade');
			// get the div we're appending to:
			var el = $('content');
			el.appendChild(fader);
			// set up the fade action
			var fader = new Fx.Style('backgroundFade', 'opacity', {
				duration: 500, 
				transition: Fx.Transitions.Quad.easeInOut,
				onComplete:function()
				{
					// once the fade is complete we are gonna begin the AJAX bit
					ajaxCall(reqPage);
				}
			});
			fader.set(0);
			fader.start(0,0.8);
		}
	}
}
/* call a PRODUCT via ajax */
function ajaxCall(reqPage) {
	// start the ajax object
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			// on the return, parse what comes back:
			insertPage(request);
		};
		request.open("GET", reqPage, true);
		request.send(null);
		return true;
	} else {
		return false;
	}	
}
/* insert a PRODUCT via ajax */
function insertPage(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var newEl = document.createElement('div');
			newEl.setAttribute('id','popupContainer');
			// add the 'js' classname to the panel
			newEl.className = 'js';
			newEl.innerHTML = request.responseText;
			// get the div we're appending to:
			var el = $('content');
			el.appendChild(newEl);
			// set up the fade action
			var fader2 = new Fx.Style('popupContainer', 'opacity', {
				duration: 500, 
				transition: Fx.Transitions.Quad.easeInOut,
				onComplete:function()
				{
					// once the fade is complete  -- this is the caller to Zoom.js
					//prepZoom();
				}
			});
			// set the fade to transparent
			fader2.set(0);
			// now rip through the div and do what's needed:
			
			// change close button
			el = $('closeLink');
			el.className = 'btnClose';
			el.firstChild.childNodes[0].nodeValue = "Close";
			el.onclick = function() {
				fadePage();
				return false;
			}
			
			// replace links for next and previous hole
			setLinks('pagination', 'a');
			// animate the fade in
			fader2.start(0,1);
		} else { // else die
			return false;
		}
	}
}
/* REMOVE a PRODUCT panel */
function fadePage(passThru) {
	// set up the fade action
	var fadeout = new Fx.Style('popupContainer', 'opacity', {
		duration: 250, 
		transition: Fx.Transitions.Quad.easeInOut,
		onComplete:function()
		{
			// on the completion of fade, KILL the two divs
			var d = $("content"); 
			var d_nested = $("popupContainer"); 
			var throwawayNode = d.removeChild(d_nested);
			// if this is a 'passthru' (ie a related item within the product panel) then pass back to getPage
			if (passThru != undefined) {
				getPage(passThru);
			} else {
				// if not, delete the fade layer too
				d_nested = $("backgroundFade"); 
				throwawayNode = d.removeChild(d_nested);
			}
		}
	});
	fadeout.start(1,0);
}


/***********************************************************************************************************************************

	HIJACK ALL LINKS WITHIN A SPECIFIED CONTAINER
************************************************************************************************************************************/

function gup(name, string)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( string );
  if( results == null )
    return "";
  else
    return results[1];
}

function setLinks(containerId, tagName) {
	// tests for support - stop it right now if the browsers not up to it...
	if (!document.getElementById) { return false }
	if (!document.getElementsByTagName) { return false }
	// get all the <a> tags inside prodList
	var container = $(containerId);
	var allLinks = container.getElementsByTagName(tagName);
	// a count var to plug into IDs:
	var count = 0;
	for (var i=0; i<allLinks.length; i++) {
		// search allLinks for rel='detail' attributes: this indicates a product link
		//if (allLinks[i].getAttribute('rel') == 'detail') { // -- how can we test that it's a product link??????????
			//alert('rel hit!');
			// if it's true hijack it
			var element = allLinks[i];
			element.setAttribute('id', count);
			element.onclick = function() {
				// on click, get the href attribute and pass it to the ajax function. return false to stop link working normally				
				var myLink = this.getAttribute('href');
				var hole = gup('hole', myLink);
				getPage('course-planner-detail.php?hole=' + hole);
				return false;
			}
			// increment the count var:
			count++;
		//}
	}
}