/**
 * This file contains JavaScript functions for use on the Resort Guide Landing Pages
 */

/*
| Function to get the URL parameter
*/
function getParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

/*
| This function moves the 'selector' arrow in the side navigation
*/
function toggleSnav(id, level)
{
	elem = $(id);
	
	/* 
	| Hide all arrows except selected one
	*/
	var arrowImages = document.getElementsByName('snav-arrows');
	
	for (var ctr = 0; ctr < arrowImages.length; ++ctr)
	{
		arrowID = arrowImages[ctr].id;
		
		if (arrowID != id && arrowID.src != '/images/theme3/snav-arrows-spacer.gif')
		{
			$(arrowID).src = '/images/theme3/snav-arrows-spacer.gif';
		}
	}
	
	if (elem.src != '/images/theme3/snav-arrows-selected-' + level + '.gif')
	{
		elem.src = '/images/theme3/snav-arrows-selected-' + level + '.gif';
	}
	
	return true;
}

/* 
| Slide any element up
*/
function elementUp(idname)
{
	if ($(idname) && $(idname).style.display != 'none')
	{
		Effect.SlideUp(idname, {duration: .5});
		return true;
	}
	else
	{
		return false;
	}
}

/* Slide any element down
|
*/
function elementDown(idname)
{
	if ($(idname) && $(idname).style.display == 'none')
	{
		Effect.SlideDown(idname, {duration: .5});
		return true;
	}
	else
	{
		return false;
	}
}

/*
| Toggle an elements display
*/
function toggleElement(element)
{
	if ($(element).style.display == 'none')
	{
		elementDown(element);
		return true;
	}
	else
	{
		elementUp(element);
		return false;
	}
}

/*
| Hide / Show the flash element
| Send true to show and false to hide
*/
function hideShowFlash(method)
{
	var flashElement = $('landing-body-flash');
	
	if (flashElement && method === true)
	{
		flashElement.style.display = '';
	}
	else if (flashElement)
	{
		flashElement.style.display = 'none';
	}
	
	return method;
}

/*
| Hide / Show elements on side nav
*/
function hideshow(menuID, childID)
{
	var flashBody		= $('landing-body-flash');
	var contentElement	= menuID + "-content";
	var childElement	= childID + "-content";
	
	/*
	| Recurse through and hide any elements except the selected one
	*/
	var contentElements = $('landing-page-text').childElements();
	
	for (var ctr =0; ctr < contentElements.length; ++ctr)
	{
		elementID = contentElements[ctr].id;
		
		if (elementID != contentElement && elementID != childElement)
		{
			$(elementID).style.display = 'none';
		}
	}
	
	if (menuID.match("types"))
	{
		elementDown('types');
		elementUp('accommodation');
		elementUp('groups');
	}
	else if (menuID.match("accommodation"))
	{
		elementUp('types');
		elementDown('accommodation');
		elementUp('groups');
	}
	else if (menuID.match("groups"))
	{
		elementUp('types');
		elementUp('accommodation');
		elementDown('groups');
	}
	else
	{
		elementUp('types');
		elementUp('accommodation');
		elementUp('groups');
	}
	
	/*
	| Check first for a content element and display it.
	| If content element doesn't exist, display the child element.
	| If neither exist, show the overview content.
	*/
	if ( $(contentElement) )
	{
		$(contentElement).style.display = '';
	}
	else if ( $(childElement) )
	{
		$(childElement).style.display = '';
	}
	else
	{
		$('overview-content').style.display = '';
	}
		
	return true;
}

function sendFlashInfo(imageBase, imageNum, imageNames, videoBase, videoNum, videoNames, iCount)
{
	var flash;
	
	flash = swfobject.getObjectById('landingflash');
		
	var didFlash = false;
	if (flash)
	{
		if (flash.switchMedia)
		{
			flash.switchMedia(imageBase, imageNum, imageNames, videoBase, videoNum, videoNames);
			didFlash = true;
		}
	}

	if ( iCount < 100 && !didFlash)
	{
		// Call the  flash player again after a few seconds
		// So it can have time to reopen the player
		iCount++;
		var returnID = setTimeout('sendFlashInfo("' + imageBase + '", ' + imageNum + ', "' + imageNames + '", "' + videoBase + '", ' + videoNum + ', "' + videoNames + '", ' + iCount + ')', 100);
	}

}

/*
| Highlight an element
*/
function highlightElement(element, color)
{
	element.style.backgroundColor = color;
}

/*
| Unhighlight the element
*/
function highlightElement(element, color)
{
	element.style.backgroundColor = color;
}

/*
| Change the text from dark blue to yellow
*/
function changeTextColors(column, on, isSide)
{
	if (on == true)
	{	
		if (isSide == true)
		{
			$('ota-title-small-' + column).addClassName('hover-color');
			$('ota-text-small-' + column).addClassName('hover-color');	
		}
		else
		{
			$('ota-title-' + column).addClassName('hover-color');
			$('ota-text-' + column).addClassName('hover-color');	
			$('ota-check-availability-' + column).addClassName('hover-color');
		}
		
		if (isSide == true)
		{
			$('ota-title-small-' + column).removeClassName('link-color');
			$('ota-text-small-' + column).removeClassName('link-color');	
		}
		else
		{
			$('ota-title-' + column).removeClassName('link-color');
			$('ota-text-' + column).removeClassName('link-color');
			$('ota-check-availability-' + column).removeClassName('link-color');
		}
	}
	else
	{	
		if (isSide == true)
		{
			$('ota-title-small-' + column).addClassName('link-color');
			$('ota-text-small-' + column).addClassName('link-color');	
		}
		else
		{
			$('ota-title-' + column).addClassName('link-color');
			$('ota-text-' + column).addClassName('link-color');
			$('ota-check-availability-' + column).addClassName('link-color');
		}
		
		if (isSide == true)
		{
			$('ota-title-small-' + column).removeClassName('hover-color');
			$('ota-text-small-' + column).removeClassName('hover-color');	
		}
		else
		{
			$('ota-title-' + column).removeClassName('hover-color');
			$('ota-text-' + column).removeClassName('hover-color');
			$('ota-check-availability-' + column).removeClassName('hover-color');
		}
	}

}

/*
| Functions for reviews links
*/
function helpful(id, answer)
{
	var url = '/util/ajax/markHelpful.php';
	
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			review: id,
			answer: answer
		},
		onCreate: function() 
		{
			$('helpful-' + id).innerHTML = '<em>Processing...</em>'
		},
		onFailure: function (transport)
		{
			alert ('failure');
		},
		onComplete: function (transport)
		{
			$('helpful-' + id).innerHTML = transport.responseText;
		}
	});
}

function inappropriate(id)
{
	var url = '/util/ajax/markReviewInappropriate.php';
	
	new Ajax.Request(url, {
		method: 'post',
		asynchronous: false,
		parameters: {
			listing: id
		},
		onCreate: function() 
		{
			$('inappropriate-' + id).innerHTML = '<em>Processing...</em>'
		},
		onFailure: function (transport)
		{
			alert ('failure');
		},
		onComplete: function (transport)
		{
			$('inappropriate-' + id).innerHTML = transport.responseText;
		}
	});
}