//function will chakc the results page, and disable compare
//checkboxes in results which have no reviews
function disableCompareWithNoReview() {
	var compChkbxs = getCompareCheckboxes();
	for ( i = 0; i < compChkbxs.length; i++ ) {
		var indicator = compChkbxs[i].nextSibling.nextSibling;
		if (!indicator) {
			compChkbxs[i].style.visibility = 'hidden';
			compChkbxs[i].nextSibling.style.visibility = 'hidden';
		}
	}
}
//shows 'Blackout'
function showBlackout() {
	var overlay = document.getElementById('overlay2');
	if (overlay) {
		var arrayPageSize = getPageSize();
		overlay.style.height = ((arrayPageSize[1] + 1000) + 'px');
		overlay.style.display = 'block';
		overlay.onclick = function () {return false;}
	} else {
		//create overlay if one does not exist
		var objBody = document.getElementById('containerDiv');
		if (!objBody)
			objBody = document.getElementById('pagestart');
		var objOverlay = document.createElement('div');
		objOverlay.setAttribute('id','overlay2');
		objOverlay.onclick = function () {hideLightbox(); return false;}
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '50';
	 	objOverlay.style.width = '100%';
		objBody.insertBefore(objOverlay, objBody.firstChild);
		showBlackout();
	}
}


//
// Show the blackout for the tour operator popup.
//
function showBlackoutToop() {
	var overlay = document.getElementById('overlay2');
	if (overlay) {
		var arrayPageSize = getPageSize();
		overlay.style.height = ( $('pagestart').scrollHeight + 'px');
		overlay.style.display = 'block';
		overlay.onclick = function () {closeToopPopup();return false;}
		var selects = document.getElementsByTagName("select");
	    for (i = 0; i != selects.length; i++) {
	    	selects[i].style.visibility = "hidden";
	    }
	} else {
		//create overlay if one does not exist
		var objBody = document.getElementById('containerDiv');
		if (!objBody)
			objBody = document.getElementById('pagestart');
		var objOverlay = document.createElement('div');
		objOverlay.setAttribute('id','overlay2');
		objOverlay.onclick = function () {closeToopPopup(); return false;}
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '75';
	 	objOverlay.style.width = '100%';
		objBody.insertBefore(objOverlay, objBody.firstChild);
		showBlackoutToop();
	}
}




//hides 'Blackout'
function hideBlackout() {
	var overlay = document.getElementById('overlay2');
	if (overlay) {
		overlay.style.display = 'none';
	}
	var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
    	selects[i].style.visibility = "visible";
    }
}

//this is another workaround, it fixes additional link in the tomahawk paginator
//used on the page
function fixPaginator(id, id2) {
	if ($(id)) {
		var childNodes = $(id).childNodes;
		for (j = 0; j < 100; j++) {
			for (i = 0; i < childNodes.length; i++) {
				if (childNodes[i].id.match('scroll')) {
					childNodes[i].parentNode.removeChild(childNodes[i]);
				}
			}
		}
	}
	if ($(id2)) {
		var childNodes = $(id2).childNodes;
		for (j = 0; j < 100; j++) {
			for (i = 0; i < childNodes.length; i++) {
				if (childNodes[i].id.match('scroll')) {
					childNodes[i].parentNode.removeChild(childNodes[i]);
				}
			}
		}
	}
}
// this function fixes a bug on the direct results page
// it attaches loose content to links that lost their content in an ajax rerender
// failed to do it any other way, all page structure changes resulted in the same behaviour
function attachResultsToLinks() {
	var resultLinks = new Array();
	var allLinks = document.getElementsByTagName('a');
	var j = 0;
	for (i = 0; i < allLinks.length; i++) {
		if (allLinks[i].id.match('reRenderLink')) {
			resultLinks[j++] = allLinks[i];
		}
	}
	for (i = 0; i < resultLinks.length; i++) {
		var table = resultLinks[i].nextSibling;
		if (table != null) {
			var tableParent = table.parentNode;
			tableParent.removeChild(table);
			resultLinks[i].appendChild(table);
		}
	}
}
function toggleDrilldownItem (ddItem) {
  var ddItemHeader = $(ddItem+'_header');
  var ddItemContainerName = ddItem+'_container';

  if (ddItemHeader.className == "head") {
    ddItemHeader.className = "head selected_head";
  } else {
    ddItemHeader.className = "head";
  }

  new Effect.toggle(ddItemContainerName,'blind', { duration: 0.3 });
}
//This global variable holds ';' separated strings of refine cathegories that where open by the user
//so they can be opened again after ajax dom rebuild
var openedCathegories = '';
//This function is invoked when any checkbox on refine is clicked and when Verfijn
//button is pressed. It searches for cathegories that have a checked checkbox and opens them.
function checkWhatCathegoriesAreCheckedAndOpenThem(checkAlsoOpened) {
	var allSpans = document.getElementsByTagName('span');
	var headerSpans = new Array();
	j = 0;
	//this one gathers all the header span elements
	for (i = 0; i < allSpans.length; i++) {
		if (allSpans[i].id.match('txt')) {
			headerSpans[j++] = allSpans[i];
		}
	}
	//this one checks whether they have checked checkboxes and if so,
	//toggles them open
	for (i = 0; i < headerSpans.length; i++) {
		var parentDiv = headerSpans[i].parentNode.parentNode.parentNode;
		var checkBoxes = parentDiv.getElementsByTagName('input');
		var checked = false;
		for (j = 0; j < checkBoxes.length; j++) {
			if (checkBoxes[j].checked) {
				checked = true;
			}
		}
		//this code checks whether the cathegory was opened, it should only do that when a checkbox was clicked
		//after Verfijn button all but checked should be closed
		if (checkAlsoOpened) {
			if (openedCathegories.match(headerSpans[i].title + ";")) {
				ddItem = headerSpans[i].parentNode.parentNode;
				ddItem.className = "head selected_head";
				var parentnode = ddItem.parentNode;
				var containerdiv = parentnode.getElementsByTagName('div')[1];
				containerdiv.style.display = '';
			}
		} else {
		 	ddItem = headerSpans[i].parentNode.parentNode;
			if (ddItem.className.match('head_disabled')) {
				checked = false;
			} else {
				ddItem.className = "head";
			}
			var parentnode = ddItem.parentNode;
			var containerdiv = parentnode.getElementsByTagName('div')[1];
			if (containerdiv != null) {
				containerdiv.style.display = 'none';
			}
		}
		if (checked || headerSpans[i].title == 'Ligging' || headerSpans[i].title == 'Faciliteiten') {
			if (headerSpans[i].parentNode.parentNode.className == 'head') {
				ddItem = headerSpans[i].parentNode.parentNode;
				ddItem.className = "head selected_head";
				var parentnode = ddItem.parentNode;
				var containerdiv = parentnode.getElementsByTagName('div')[1];
				if (containerdiv != null) {
					containerdiv.style.display = '';
				}
			}
		}
	}
	if (!checkAlsoOpened) {
		$('pagestart').scrollTop = 0;
	}
}

function toggleRefine (ddItem) {
  if (ddItem.className == "head") {
    ddItem.className = "head selected_head";
    //This peice of code gathers all opened cathegories in the global 'openedCathegories' variable
    var allSpans = document.getElementsByTagName('span');
	var headerSpansOpen = new Array();
	j = 0;
	//this one gathers all the header span elements that are opened
	openedCathegories = '';
	for (i = 0; i < allSpans.length; i++) {
		if (allSpans[i].id.match('txt') && allSpans[i].parentNode.parentNode.className == 'head selected_head') {
			openedCathegories += allSpans[i].title + ';';
		}
	}
  } else {
    ddItem.className = 'head';
  }

	var parentnode = ddItem.parentNode;
	var containerdiv = parentnode.getElementsByTagName('div')[1];

  new Effect.toggle(containerdiv,'blind', { duration: 0.3 });

}

function toggleSelectionScreen (linkElement) {
  if (linkElement.innerHTML.substr(0,4) == 'Open') {
    linkElement.innerHTML = 'Sluit selectiescherm <input type="image" src="../../media/images/white-arrow.gif"/>';
  } else {
    linkElement.innerHTML = 'Open selectiescherm <input type="image" src="../../media/images/white-arrow.gif"/>';
  }

  new Effect.toggle('selection_screen_container','blind', { duration: 0.3 })
}



//
//Function that opens the tour operator overlay on the search result page.
//
function openToopPopup( e , toopContent )
{
	var marginTopBottom = 40;

	var scrollXy = getScrollXYRelativeTo( "pagestart" );

	var popupDiv = document.getElementById( 'toopPopup' );
	popupDiv.style.display = 'block';
	popupDiv.style.top = marginTopBottom + scrollXy[1] + 'px';

	/* Exception for book page popup position */
	var bookPage = document.getElementById( 'bookBorder' );

	if(bookPage == null){
		popupDiv.style.left = ( getScreenDimension()[0] / 2 ) - ( popupDiv.offsetWidth / 2 ) + 'px';
	}else{
		popupDiv.style.left = ( getScreenDimension()[0] / 2 ) - ( popupDiv.offsetWidth / 2 ) - 120 + 'px';
	}

	/*
	 * Set the toop content.
	 */
	 var popupDivOverride = document.getElementById( 'toopPopupOverride' );
	 popupDivOverride.innerHTML = toopContent;

	 /*
	 * Black-out the page.
	 */
	showBlackoutToop();

	/*
	 * Activate the carousel.
	 */
	new UI.Carousel( "horizontal_carousel" );


	/*
	 * Check if the pop-up is fully visible.
	 *
	 * If not:
	 * - Resize the info text div making it smaller, so the pop-up gets fully visible.
	 * - Add a scroll bar to the info text div, so the content gets scrollable.
	 */
	var viewPortDimensions = getScreenDimension();
	var toopInfoTextDiv    = document.getElementById( 'toopInfoText' );

	if ( popupDiv.offsetHeight + marginTopBottom > viewPortDimensions[ 1 ] )
	{
		var heightExclToopInfo = popupDiv.offsetHeight - toopInfoTextDiv.offsetHeight;

		toopInfoTextDiv.style.height    = ( viewPortDimensions[ 1 ] - heightExclToopInfo - ( 3 * marginTopBottom ) ) + 'px';
		toopInfoTextDiv.style.overflowY = "auto";
	}
}



//
//Function that closes the tour operator overlay on the search result page.
//
function closeToopPopup() {
	var popupDiv = document.getElementById('toopPopup');
	popupDiv.style.display = 'none';

	hideBlackout();
}


//
// Get the coordinates of the screen center for Y coordinate.
//
function getScreenCenterY() {
	var y = 0;

	y = getScrollOffset()+(getInnerHeight()/2);

	return(y);
}


//
//Get the coordinates of the screen center for X coordinate.
//
function getScreenCenterX() {
	return(document.body.clientWidth/2);
}



function getInnerHeight() {
	var y;
	if (self.innerHeight) // all except Explorer
	{
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		y = document.body.clientHeight;
	}
	return(y);
}

function getScrollOffset() {
	var y;
	if (self.pageYOffset) // all except Explorer
	{
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		y = document.body.scrollTop;
	}
	return(y);
}


