var ajaxReq = new Array();

$(document).ready(function() {
	//findLinkOut();
	
	$("#carousel").carouFredSel({
		items:1,
		direction:"left",
		pagination: "#homenav",
		auto:4000,
		
		scroll:{
			fx:"fade",
			onBefore:function(o, n, s){$("#home-carousel-items").trigger("slideTo", $("#carousel").triggerHandler("currentPosition"));}
		}
	});
	
	$("#home-carousel-items").carouFredSel({
		items:1,
		auto:false,
		scroll:{fx:"fade"}
	});
	
	$("#news_box").mouseenter(function(){
		$("#carousel").trigger("pause");
	});
	$("#news_box").mouseleave(function(){
		$("#carousel").trigger("play");
	});
	
	if ($(this).scrollTop() < 330)
		$("#backtotop").hide();
	
	// fade in #backtotop
	if (!$.browser.msie || $.browser.version != 6) {
		$(function(){
			$(window).scroll(function(){
				if ($(this).scrollTop() > 330) {
					$('#backtotop').fadeIn();
				}
				else {
					$('#backtotop').fadeOut();
				}
			});
		});
	}
});

function findLinkOut()
{
	$('.linkout').append("<span style='white-space:nowrap'>&nbsp;<img src='/images/linkout.gif' alt='' /></span>").attr({target: "_blank"});
}

function displayHomeSearchResults()
{
	if($("#home-search").val().length >= 3)
	{
		$("#home-map-search-results").show().animate({marginLeft:'230px'},500);
		searchTribes($("#home-search").val());
		$("#home-loading").html("<img src='/images/loader.gif' height='15' style='vertical-align:top;' alt='loading' />");
	}else if($("#home-search").val().length < 3)
	{
		closeHomeSearchResults();
		$("#results-title").html("&nbsp;");
		$("#results-content").html("&nbsp;");
	}
}

function closeHomeSearchResults()
{
	$("#home-map-search-results").animate({marginLeft:'-2px'},500);
}

function checkPosition()
{
//	alert($("#carousel").triggerHandler("currentPosition"));
}

function searchTribes(v)
{
	var url = "/tribeSearchAjax.cfm?view=searchtribes&search="+encodeURIComponent(v);
	submitRequest(url);
}

// ***************************

function submitRequest(url)
{
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = function updatePage(){
			if(this.readyState == 4)
			{
			  createXML(this.responseText);
			}
		};
		req.open("GET", url, true);
		ajaxReq.push(req);
		ajaxReq[ajaxReq.length -1].send(null);
	}
	
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req)
		{
			req.onreadystatechange = function updatePage(){
				if(this.readyState == 4)
				{
				  createXML(this.responseText);
				}
			};
			req.open("GET", url, true);
			ajaxReq.push(req);
			ajaxReq[ajaxReq.length -1].send();
		}
	}
}

function createXML(t)
{
	var tmptext = t;
	tmptext = tmptext.substring(tmptext.indexOf("<"),tmptext.length);
	if (window.ActiveXObject)
	{
		var xmlobject=new ActiveXObject("Microsoft.XMLDOM");
		xmlobject.async="false";
		xmlobject.loadXML(tmptext);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser=new DOMParser();
		var xmlobject=parser.parseFromString(tmptext,"text/xml");
	}
	
	root = xmlobject.getElementsByTagName("node");
	
	for(x=0;x<root.length;x++)
	{
		switch(root[x].getElementsByTagName("method")[0].firstChild.data)
		{
			case "replace":
				if(document.getElementById(root[x].getElementsByTagName("id")[0].firstChild.data))
				{
					document.getElementById(root[x].getElementsByTagName("id")[0].firstChild.data).innerHTML = root[x].getElementsByTagName("content")[0].firstChild.data;
				}
				break;
			case "donothing":
				break;
			case "error":
				alert(root[x].getElementsByTagName("content")[0].firstChild.data);
				break;
			case "execute":
				eval(""+root[x].getElementsByTagName("content")[0].firstChild.data);
				break;
		}
	}
}

