// Global Config
var pathPHP = '/smartreserve/loadDestinations.php';
var loadingIcon = 'Loading....';
var cacheSeleted = '';
var cacheSubSeleted = '';
var destinationContainer = 'all-destinations';
var destinationPrefix = 'destinationContainer';

// Scroll to view
function myScrollTo(container, element)
{
	container = $(container); 
	element = $(element); 
	var x = element.x ? element.x : element.offsetLeft;
	var y = element.y ? element.y : element.offsetTop;
        y = y - 5; // add buffer so that item does not overlap corners
	container.scrollLeft = x - (document.all ? 0 : container.offsetLeft);
	container.scrollTop = y - (document.all ? 0 : container.offsetTop);
	return element;
}

// Destination Box Script
function changeClass(Elem, myClass, eID)
{
	var elem;
	
	if($)
	{
		var elem = $(Elem);
	} 
	else if (document.all)
	{
		var elem = document.all[Elem];
	}
	
	if (myClass != '')
	{
		elem.className = myClass;
	}
	else
	{
		elem.className = $(eID).value;
	}
}

// Update Default style
function updateDestinationDefault(objTarget)
{
	var elem = $(objTarget);
	
	if (elem.value == "arrow2")
	{
		elem.value = "arrow1";
	}
	else if (elem.value == "arrow1")
	{
		elem.value = "arrow2";
	}
	else
	if (elem.value == "selected")
	{
		elem.value = "";
	}
	else
	{
		elem.value = "selected";
	}
}


// This Class Require prototypeJS Libraly
// *******************************************************************************************

function loadDestination(destinationTarget, destinationValue, destinationType)
{
	var doRequest = 1;
	var scrollTarget = destinationTarget;
	
	if (destinationTarget == "") { destinationTarget = destinationPrefix +'_'+ destinationValue; }
	if (destinationType == "child") 
	{
		updateDestinationDefault('style_'+ destinationTarget);
		
		destinationParentTarget = destinationTarget;
		destinationTarget = 'child_'+ destinationTarget;
		
		if (cacheSeleted == "")
		{
			cacheSeleted = destinationValue;
		}
		else if (cacheSeleted == destinationValue)
		{
			cacheSeleted = "";
		}
		else if (cacheSeleted != destinationValue)
		{
			var tmpTarget = 'destinationContainer_'+ cacheSeleted;
			$('child_'+ tmpTarget).style.display = "none";
			updateDestinationDefault('style_'+ tmpTarget);
			changeClass(tmpTarget, '', 'style_'+ tmpTarget);
			cacheSeleted = destinationValue;
		}
		
		// Handle sub
		var handleObject = $(destinationTarget);
		if (handleObject.style.display == "none")
		{
			handleObject.style.display = "";
		}
		else
		{
			doRequest = 0;
			handleObject.style.display = "none";
		}
		
		// Skip Loading When Data Exist
		if (handleObject.innerHTML != '')
		{
			doRequest = 0;
		}
		else
		{
			// $(destinationTarget).innerHTML = loadingIcon;
		}
		
		myScrollTo(destinationContainer, scrollTarget);
		// scrollGoTo(scrollTarget);
	}
	else
	{
		$(destinationTarget).innerHTML = loadingIcon;
	}
	
	if (doRequest == 1)
	{
		// Call Items
		var url = pathPHP;
		var pars = 	'codeSection=loadDestination';
			pars += '&destinationPrefix='	+ escape(destinationPrefix);
			pars += '&destinationTarget='	+ escape(destinationTarget);
			pars += '&destinationValue='	+ escape(destinationValue);
			pars += '&destinationType='		+ escape(destinationType);
		var myAjax = new Ajax.Updater(destinationTarget, url, {method: 'get', parameters: pars});
	}
	else
	{
		if ($(destinationTarget).innerHTML == loadingIcon)
		{
			$(destinationTarget).innerHTML = "";
		}
	}
	
	// Load Description
	loadDestinationDetail(destinationValue);
        document.fire("destinationBox:loadDestination", destinationValue);
}

function loadSubDestination(destinationTarget, DestinationID)
{
	var workItem = destinationTarget +"_"+ DestinationID;
	
	if (cacheSubSeleted == "")
	{
		cacheSubSeleted = workItem;
		updateDestinationDefault('style_'+ destinationTarget +'_'+ DestinationID);
		loadDestinationDetail(DestinationID);
	}
	else if (cacheSubSeleted == workItem)
	{
		// updateDestinationDefault(cacheSubSeleted);
		// cacheSubSeleted = "";
	}
	else
	{
		updateDestinationDefault('style_'+ cacheSubSeleted);
		changeClass(cacheSubSeleted, '', 'style_'+ cacheSubSeleted);
		cacheSubSeleted = workItem;
		updateDestinationDefault('style_'+ destinationTarget +'_'+ DestinationID);
		loadDestinationDetail(DestinationID);
	}
        document.fire("destinationBox:loadDestination", DestinationID);
}

function loadDestinationDetail(destinationValue)
{
	$('countries-guide-container').style.display = "none";
	$('destinationShowDetail').innerHTML = loadingIcon;
	
	var url = pathPHP;
	var pars = 	'codeSection=loadDestinationDetail';
		pars += '&destinationValue='	+ escape(destinationValue);
	var myAjax = new Ajax.Updater('destinationShowDetail', url, {method: 'get', parameters: pars});
	
	//document.fire("destinationBox:loadDestination", destinationValue);
	
	new Effect.BlindDown('countries-guide-container', { duration: 0.3 });
}

/********* Map Integration *********/

box_selectedDest = null;
function box_event_loadDestination(e) {
    if (!window['hotelMap']) {
        window.setTimeout(function() { box_event_loadDestination(e); }, 50);
        return;
    }
    var destId = e.memo;
    if ($('destLevel_' + destId) == null) return;
    var destLevel  = $('destLevel_' + destId).value;
    var destParent = $('destParent_' + destId).value;

    if (destLevel == 1) {
        hotelMap.loadCountry(destId);
    } else {
        box_selectedDest = destId;
        hotelMap.loadChildDestPositions(destParent);
    }
}

document.observe("map:refreshComplete", function() {
    if (box_selectedDest != null) {
        hotelMap.selectDestination(box_selectedDest);
        box_selectedDest = null;
    }
});

document.observe("destinationBox:loadDestination", box_event_loadDestination);

document.observe("map:destinationSelected", function(e) {
    var id = 'destinationContainer_' + e.memo;
    var dest = $(id);
    if (dest) {
        dest.onclick(e);
        changeClass(id, '', 'style_destinationContainer_' + e.memo);
    }
});

