var curDiv = null;

function debugmsg(msg)
{
	return;
	//debugger
	var text = msg + '<br>';
	if (window == parent) 
	{
		text += document.getElementById('debugwindow').innerHTML;
		document.getElementById('debugwindow').innerHTML = text;
	}
	else
	{
		text += parent.document.getElementById('debugwindow').innerHTML;
		parent.document.getElementById('debugwindow').innerHTML = text;
	}
	
	
}

var ie4 = document.all;

var ns4 = document.layers;

var ns6 = document.getElementById && !document.all; 


function doResize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
	if(myWidth > 770)
		document.getElementById('maindiv').style.left = (myWidth-770)/2 + "px";
			
	if(myHeight > 560)
		document.getElementById('maindiv').style.top = (myHeight-598)/2 + "px";
		
	if(myWidth > 770 && myHeight > 560)
		document.body.style.overflow = 'hidden';
	else
		document.body.style.overflow = '';
			
}

window.onresize = doResize;

window.onload = function () {

	doResize();
	
	debugmsg('hash: ' + document.location.hash);
	
	switch (document.location.hash)
	{
		case '#about':
			mnuAbout.doclick('about.html', (document.getElementById? document.getElementById('iAbout'): document.all? document.all['iAbout']: null));
			setCur(document.getElementById('linkAbout'));
			break;
		case '#services':case '#servicesdata':
			mnuServices.doclick('services.html', (document.getElementById? document.getElementById('iServices'): document.all? document.all['iServices']: null));
			setCur(document.getElementById('linkServices'));
			break;
		case '#gallery':
			mnuGallery.doclick('gallery.html', (document.getElementById? document.getElementById('iGallery'): document.all? document.all['iGallery']: null));
			setCur(document.getElementById('linkGallery'));
			break;
		case '#tips':
			mnuTips.doclick('tips.html', (document.getElementById? document.getElementById('iTips'): document.all? document.all['iTips']: null));
			setCur(document.getElementById('linkTips'));
			break;
		case '#contact':
			mnuContact.doclick('contact.html', (document.getElementById? document.getElementById('iContact'): document.all? document.all['iContact']: null));
			setCur(document.getElementById('linkContact'));
			break;
		default:
			mnuHome.doclick('home.html', (document.getElementById? document.getElementById('iHome'): document.all? document.all['iHome']: null));
			setCur(document.getElementById('linkHome'));
			break;
	}
	
	var so = new SWFObject("nav.swf", "bucknav", "770", "90", "7", "#C9D79A");
	so.write("flashcontent");
	
	checkSWFLoaded();

	
};

function checkSWFLoaded()
{

	if(document.getElementById('bucknav') == null || document.getElementById('bucknav')=='undefined')
		return displayContent();
	
	try
	{
		var nopercentloaded = document.getElementById('bucknav').PercentLoaded();
	}
	catch (nopercentloaded)
	{
		document.getElementById('flashcontent').innerHTML = "<img src='images/noflash" +ranimage()+ ".gif' width='770px' height='90px'>";
		return displayContent();
	}
	
	document.getElementById('loadertext').innerHTML = '<img src="images/logowht.jpg"><br>Loading, please wait...<br><br>'
													+ document.getElementById('bucknav').PercentLoaded() + '%';
	
	if(document.getElementById('bucknav').PercentLoaded() == 100)
	{
		
		displayContent();
	}
	else
		setTimeout('checkSWFLoaded()', 10);
}

function displayContent()
{
	document.getElementById('flashcontent').style.visibility = 'visible';
	document.getElementById('loader').style.visibility = 'hidden';
	
	if(curDiv)
	{
		curDiv.style.visibility = 'visible';
		if(top.frames[curDiv.id].doTransition && top.frames[curDiv.id].doTransition != 'undefined')
			top.frames[curDiv.id].doTransition();
			
		if(document.title.indexOf('#') != -1)
			document.title = document.title.substring(0, document.title.indexOf('#'));
		
	}
}

function MenuItem(x, label, id, url)
{
	
	document.write('<div id="navbar" style="left: ' + x + 'px; \
											//font-family: sans-serif; \
											display: block; \
											font-size: 12px; \
											position: absolute; \
											color: white; \
											top: 12px; \
											height: 20px; \
											width: 110px; \
											text-align: center; \
											cursor: pointer; \
											font-weight: bold;"> \
				    <a id="link' + id + '" href="' + url + '" onmouseover="this.style.color=\'white\'" \
								onmouseout="if(curLink != this){ this.style.color=\'#C9D79A\'};" \
								onclick="setCur(this); return mnu' + id + '.doclick(this.href, \
								(document.getElementById? document.getElementById(\'i' + id + '\'): document.all? document.all[\'i' + id + '\']: null));" \
								style="text-decoration: none; color: #C9D79A;">' + label + '</a></div>');
	
	this.label = label;
	this.doclick = function doclick(url, div)
	{
		
		if(curDiv == div)
			return false;
			
		if ( div && div.src != url ) {
			document.getElementById('loading').style.visibility = 'visible';
			div.src = url;
		}
		else
		{
			document.getElementById('loading').style.visibility = 'hidden';
			div.style.visibility = 'visible'; 
			top.frames[div.id].doTransition();
		}
		
		if(curDiv)
		{
			fadeFrame(curDiv, 0, 100, 0.2);
		}
			
		document.location.hash = ('#' + div.id.substring(1).toLowerCase());
		document.title = 'Buckland Landscapes - ' + this.label;		
		
		curDiv = div;
		return false;
	}
}

function docLoaded()
{
			
	document.getElementById('loading').style.visibility = 'hidden';


	if(curDiv)
	{
		curDiv.style.visibility = 'visible';
		if(document.getElementById('bucknav') == null || document.getElementById('bucknav') == 'undefined' || document.getElementById('bucknav').PercentLoaded() == 100)
			top.frames[curDiv.id].doTransition();
		
		if(document.title.indexOf('#') != -1)
			document.title = document.title.substring(0, document.title.indexOf('#'));
	}
	
	
}

var fadeMemInt = 0;
var fadeAlpha = 100;

function fadeFrame(elem, endalpha, duration, ease)
{	
	var interval = 10;
	var steps = duration / 10;
	
	if(fadeMemInt)
		window.clearInterval(fadeMemInt);
	
	var actStep = 0;
	var startalpha = fadeAlpha;
	
	fadeMemInt = window.setInterval(
			function() {
				
				fadeAlpha = easeInOut(startalpha, endalpha, steps, actStep, ease);
				elem.style.filter = 'alpha(opacity=' + fadeAlpha + ')';
				elem.style.opacity = (fadeAlpha / 100); 
				elem.style.MozOpacity = (fadeAlpha / 100); 
				elem.style.KhtmlOpacity = (fadeAlpha / 100); 
			
			actStep++;
				
				if(actStep > steps)
				{
					window.clearInterval(fadeMemInt);
					elem.style.visibility = 'hidden'; 
					fadeAlpha = 100;
					elem.style.filter = 'alpha(opacity=' + fadeAlpha + ')';
					elem.style.opacity = (fadeAlpha / 100); 
					elem.style.MozOpacity = (fadeAlpha / 100); 
					elem.style.KhtmlOpacity = (fadeAlpha / 100); 
					top.frames[elem.id].doHide();
				}
				
				
			}, interval);
	
}


function easeInOut(minValue, maxValue, totalSteps, actualStep, ease)
{
	var delta = maxValue - minValue;
	var step = minValue+(Math.pow(((1 / totalSteps) * actualStep), ease) * delta);
	return Math.ceil(step);
	
}

var AJAXDiv = null;

function getODAXData(url, divid)
{
	AJAXDiv = divid;
	
	var elem = document.getElementById(AJAXDiv);
	if(elem != 'undefined')
		elem.innerHTML = '<p style="font-size:18px; text-align:center;"><br><br><br><br>Loading, please wait...</p>';
		
	var ajax = new AjaxObject();
	ajax.open("GET", url, true);
	ajax.setRequestComplete(updatePage);
	ajax.send(null);
}

function updatePage(responseText)
{
	var elem = document.getElementById(AJAXDiv);
	if(elem != 'undefined')
		elem.innerHTML = responseText;

	if(scroller != 'undefined')
		scroller.init();
}