var MAP;
var MARKER = [];
var MARKERCOUNT = 0;
var DIRECTIONS;
var d = document;

function swapIm(hrefL,titleL)
{
	d.getElementById("swapImage").src = hrefL;
	d.getElementById("swapImage").alt = titleL;
	d.getElementById("swapImage").title = titleL;
	d.getElementById("swapTitle").innerHTML = titleL;
}

function getElementsByClassName(classname) {
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = document.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	{
		if(re.test(els[i].className))a.push(els[i]);
	}
	return a;
}

/*function addE(marker,e)
{
	GEvent.addListener(marker, "click", function() 
	{
			marker.openInfoWindowHtml(marker.html);
	});
	return marker;
}*/
function addE(marker,e,html){
	GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
	});
	return marker;
}

function addDirections(marker,html)
{
	// The info window version with the "to here" form open
	marker.to_htmls = html + '<div style="width:250px;">Directions: <b>To the Castle</b> - <a href="javascript:fromD(' + MARKERCOUNT + ')">From the Castle</a>' +
		 '<form action="javascript:getDirections(0)">Start Address: ' +
		 '<input type="text" SIZE=10 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
		 '<input value="Get Directions" TYPE="SUBMIT">' +
		 '<input type="hidden" id="daddr" value="@'+ marker.getPoint().lat() + ',' + marker.getPoint().lng() + 
		 '"/></form></div>';
	// The info window version with the "to here" form open
	marker.from_htmls = html + '<div style="width:250px;">Directions: <a href="javascript:toD(' + MARKERCOUNT + ')">To the Castle</a> - <b>From the Castle</b>' +
		 '<form action="javascript:getDirections(1)">End Address: ' +
		 '<input type="text" SIZE=10 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
		 '<input value="Get Directions" TYPE="SUBMIT">' +
		 '<input type="hidden" id="saddr" value="@'+ marker.getPoint().lat() + ',' + marker.getPoint().lng() +
		 '"/></form></div>';
		
	// The inactive version of the direction info
	marker.html = html + '<br>Directions: <a href="javascript:toD('+MARKERCOUNT+')">To the Castle</a> - <a href="javascript:fromD('+MARKERCOUNT+');">From the Castle</a>';
	
	MARKER[MARKERCOUNT] =marker;
	MARKERCOUNT++;
	d.getElementById("directions").innerHTML= "";
	
	DIRECTIONS =new GDirections(MAP, d.getElementById("directions"));
	
	// === Array for decoding the failure codes ===
	var reasons=[];
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	
	// === catch Directions errors ===
	GEvent.addListener(DIRECTIONS, "error", function() {
		var code = DIRECTIONS.getStatus().code;
		var reason="Code "+code;
		if (reasons[code]) {
			reason = reasons[code]
		} 
		alert("Failed to obtain directions, "+reason);
	});
	return marker;
}

function getDirections(isFrom) 
{
	if(isFrom)
	{
		from = d.getElementById("daddr").value; // postcode
		to = d.getElementById("saddr").value; //ll castle
	}
	else{
		from = d.getElementById("saddr").value;// postcode
		to = d.getElementById("daddr").value;//ll castle
	}
	doLookup(from,to,isFrom)
}


function doLookup(from,to,isFrom) 
{
	var lookup = new GClientGeocoder();
	lookup.setBaseCountryCode('uk');    
	
	lookup.getLatLng(from, function(point){
		if(isFrom)
		{
			//alert("from: "+ to + " to: @"+point.lat() + ',' + point.lng())
			DIRECTIONS.load("from: "+ to + " to: @"+point.lat() + ',' + point.lng(), {getPolyline:true});
		}
		else
		{
			//alert("from: @"+point.lat() + ',' + point.lng() + " to: "+ to) 
			DIRECTIONS.load("from: @"+point.lat() + ',' + point.lng() + " to: "+ to, {getPolyline:true});
		}
	})
}

function toD(ref){
	MARKER[ref].openInfoWindowHtml(MARKER[ref].to_htmls);
}
function fromD(ref){
	MARKER[ref].openInfoWindowHtml(MARKER[ref].from_htmls);
}
			
function addPlaces()
{
	places = [["Langley Castle Hotel", 54.963129,-2.253742,"media/base_media.jpg","relativepath"]];
		
	var bounds = new GLatLngBounds();
	for(i=0;i<places.length;i++){
	
		var marker = new GMarker(new GLatLng(places[i][1],places[i][2]));	
				
		msg= "<div style='width:250px;'><h2>"+places[i][0]+"</h2><img src='"+places[i][3]+"' align=right><p>Langley-on-Tyne, Hexham, Northumberland, NE47 5LU<br>+44 (0)1434 688 888</p></div>"
	
		//marker = addDirections(marker,msg);	
		//marker = addE(marker,"click");
		marker = addE(marker,"click", msg);
		
		bounds.extend(marker.getPoint())
		MAP.addOverlay(marker);
	}
	
	MAP.setCenter(bounds.getCenter(),9);	
}
function hideClass(classN)
{
	allN = getElementsByClassName(classN);
	if(typeof(allN) && allN.length)
	{
		for(i=0;i<allN.length;i++)
		{
			allN[i].style.display = "none";
		}
	}
}

function doLoad()
{
	
	isSectionId = location.search;
  if(isSectionId.length)
	{
		hideClass("hideme");
	}
	//SORT TITLE OUT
		links= new Array("blank","Home","Newsletter","Special Offers","Careers","Loyalty Club","History","Views","Rooms","Dining","Weddings","Awards","Conferences","Online Shop","Location","Links","Booking","Contact")
	selectedLinks = getElementsByClassName("selected");
		if(selectedLinks[0]){
		for(l in links)
		{
			
			if(selectedLinks[0].innerHTML.indexOf(links[l])>0)
			{
				
				d.getElementById("titles").style.backgroundPosition = "0 -"+(l*60)+"px"
			}
		}
	}
	
	// PUT THE ARROWS ON
	if(d.getElementById("lhs"))
	{
		lhsDiv = d.getElementById("lhs");
		if(lhsDiv.getElementsByTagName("A"))
		{
			aLinks = lhsDiv.getElementsByTagName("A");
			
			for( i = 0; i < aLinks.length; i++ ) 
				{
					
					if(aLinks[i].childNodes[0] && aLinks[i].childNodes[1] && (aLinks[i].getAttribute('href')).indexOf('facebook') == -1 )
					{
						imgInLink = aLinks[i].childNodes[0];
						textInLink = aLinks[i].childNodes[1];
						
						arrowDiv = d.createElement("DIV");
						arrowDiv.className = "lhs_picture_arrow";
						
						adjust_left =parseInt(imgInLink.getAttribute("width"))+10;
						adjust_top = parseInt(imgInLink.getAttribute("height"))+7;
				
						arrowDiv.style.cssText = "display:block;position:absolute;left:"+adjust_left+"px;top:"+adjust_top+"px;";
						aLinks[i].insertBefore(arrowDiv,textInLink);
					}
					if(  (aLinks[i].getAttribute('href')).indexOf('facebook') != -1   )
					{
						aLinks[i].style.cssText = "background:none;paddingTop:0";
					}
			}
		}
	}

// IF NO RHS STRETCH ACROSS
	if(d.getElementById("rhs_pic") && d.getElementById("rhs_pic").getElementsByTagName('p').length == 0 && d.getElementById("rhs_pic").getElementsByTagName('img').length == 0 && d.getElementById("rhs_pic").getElementsByTagName('object') == 0)
	{
		d.getElementById("main_content").style.width = "568px"; 
	}

	if(d.getElementById("trolley"))
	{
			if(d.getElementById("trolley").childNodes.length > 2)
			{
				d.getElementById("trolley").style.width ="540px"
			}
	}
	// IF PHOTO GALLERY
	if(d.getElementById("photo_gallery"))
	{
		d.getElementById("lhs").style.display = "none"; 
		d.getElementById("rhs").style.width = "925px"; 
		d.getElementById("main_content").style.width = "334px"; 
		d.getElementById("rhs_pic").style.marginLeft = "0";
		d.getElementById("rhs_pic").style.width = "576px";
		
		aH1 = d.getElementById("rhs_pic").getElementsByTagName("H1");
		aH1[0].style.width = "376px";
	}
	else if(d.getElementById("lhs") && d.getElementById("lhs").childNodes.length <= 1)
	{// IF lhs  && !photo_gallery STRETCH ACROSS
		//d.getElementById("rhs").style.width = "908px"; 
		//d.getElementById("main_content").style.width = "476px"; 
	}
	if(d.getElementById("n"))
	{
		d.getElementById("n").setAttribute("title","")
	}
	if(d.getElementById("photo_gallery"))
	{
		photo_galleryE = d.getElementById("photo_gallery")
		imLinks = photo_galleryE.getElementsByTagName("A");
		if(imLinks[0])
		{
			if(imLinks[0].getAttribute("href"))
			{
				if(d.getElementById("swapImage"))
				{
					swapIm(imLinks[0].getAttribute("id"),imLinks[0].getAttribute("title"))
				}
			}
		}
	}

	if( d.getElementById("map") )
	{
		if(GBrowserIsCompatible())
		{
			d.getElementById("map").innerHTML ="";
			MAP = new GMap2(d.getElementById("map"));
			MAP.setCenter(new GLatLng(37.4419, -122.1419), 13);
			MAP.addControl(new GSmallMapControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT)); //GLargeMapControl
			MAP.enableDoubleClickZoom();
			addPlaces();
		}
	}

}

//facebook