// JavaScript Document

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

function getXML(dname) 
{
	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.open("GET", dname, false);
	xhttp.send();
	
	return xhttp.responseXML;
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

var currentSlide = 0;	// which slide is currently showing

function buildRotatorFromXML(XMLpath)	// Rotator holds a maximum of 35 slides
{
	var Rotator_array = [];	// holds the HTML to be written
	
	var holdDuration = 8000;	// how long to show each slide
	var longHoldDuration = 4000;	// how long to show each slide
	var slideSpeed = 1000;	// length of slide in milliseconds
                
	//var Locations_URLvars = new urlVars();	// holds the URL params
	
	if ((!IE) && (navigator.userAgent.indexOf('Safari') == -1))	// IE & Safari say XML is undefined 
	{
		var Rotator_XMLDoc = new XML();	// holds all the XML data
	}
	
	Rotator_XMLDoc = getXML(XMLpath); // the XML

	var Rotator_XMLroot = Rotator_XMLDoc.documentElement; // another way to access the XML
	var numSlides = 0; // count the number of slides in the rotator				
	
	// write the HTML
	Rotator_array.push('<ul>');
	
	// for each slide in the XML...
	$(Rotator_XMLroot).find("slide").each(function() 
	{
		// get the slide data
		var theSlide = $(this);
		
		try
		{
			// Make sure there's an image
			if (/\S/.test($(theSlide).find('image')[0].childNodes[0].nodeValue))	
			{
				numSlides++;  // count this slide
				Rotator_array.push('<li id="rotatorSlide_' + numSlides + '"><div class="rotatorBG" ');
				try
				{
					
					Rotator_array.push('style="background:#FFF url(');
					Rotator_array.push(theSlide.find('image')[0].childNodes[0].nodeValue);
					Rotator_array.push(') top left no-repeat;"');
				}
				catch(e)
				{
					// if there's no image, don't add the style attribute
					
				}
				
				Rotator_array.push('>');
				
				try
				{
					// Make sure there's a link
					if (/\S/.test($(theSlide).find('url')[0].childNodes[0].nodeValue))	
					{
						Rotator_array.push('<a target="');
						try
						{
							Rotator_array.push($(theSlide).find('target')[0].childNodes[0].nodeValue);
						}
						catch(e)
						{
							Rotator_array.push('_self');
						}
						
						Rotator_array.push('" href="');
						Rotator_array.push($(theSlide).find('url')[0].childNodes[0].nodeValue);
						Rotator_array.push('"></a>');
					}
				}
				catch(e)
				{	
					// if there's no link, don't add the <a> tag
				}
				
				Rotator_array.push('</div></li>');
			}
		}
		catch(e)
		{
			// if there's no image, don't add the 
		}
									
	});
	
	Rotator_array.push('</ul>');
	
	$('#rotator').html(Rotator_array.join(''));
	
	//----------------------------------
	
	var RotatorButtons_array = [];	// holds the HTML to be written
	
	for (var i = 1; i <= numSlides; i++)
	{
		RotatorButtons_array.push('<div id="box_');
		RotatorButtons_array.push(i);
		RotatorButtons_array.push('" class="rotatorButton"></div>');
	}
	
	if (numSlides > 1)
	{
		$('#rotatorButtons').html(RotatorButtons_array.join(''));
	}
	else
	{
		holdDuration = 0;
	}
	
	//----------------------------------
	
	//console.log("Now showing:" + currentSlide);
	
	//----------------------------------
	
	$("#rotator").jCarouselLite({	// start up the carousel
		btnNext: ".next",
		btnPrev: ".prev",
		auto: holdDuration,
		hold:longHoldDuration,
		speed: slideSpeed,
		beforeStart: function(a) {
			//console.log("Before animation starts:" + a.length);
		},
		afterEnd: function(a) {
			//console.log("After animation ends:" + a.length);
			// Slide text box up
			//selectNextRotatorButton(currentSlide);
			currentSlide++;
			//console.log("Now showing:" + currentSlide);
		},
		visible: 1,
		btnGo: ["#box_1", "#box_2", "#box_3","#box_4", "#box_5", "#box_6", "#box_7", "#box_8", "#box_9","#box_10", "#box_11", "#box_12", "#box_13","#box_14", "#box_15", "#box_16", "#box_17", "#box_18", "#box_19","#box_20","#box_21", "#box_22", "#box_23","#box_24", "#box_25", "#box_26", "#box_27", "#box_28", "#box_29","#box_30","#box_31", "#box_32", "#box_33","#box_34", "#box_35"]
	});
}


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function selectNextRotatorButton(theButton)
{
	$('.rotatorButton').removeClass('selected');
	//console.log('the selected button is: ' + theButton);
	$('.rotatorButton:eq('+theButton+')').addClass('selected');
	
	if (currentSlide >= $('.rotatorButton').length)
	{
		currentSlide = 0;	// reset
	}
}


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function buildContactsFromXML(XMLpath)
{
	var Contacts_array = [];	// holds the HTML to be written
                
	//var Contacts_URLvars = new urlVars();	// holds the URL params
	
	if ((!IE) && (navigator.userAgent.indexOf('Safari') == -1))	// IE & Safari say XML is undefined 
	{
		var Contacts_XMLDoc = new XML();	// holds all the XML data
		var ChunkOf_XML = new XML();	// holds one chunk of XML at a time
	}
	
	Contacts_XMLDoc = getXML(XMLpath); // the XML

	var Contacts_XMLroot = Contacts_XMLDoc.documentElement; // another way to access the XML
	var numTeams = 0;	// the number of teams listed in the xml
	var numContacts = 0; // the number of contacts on a team				
	
	try
	{
		numTeams = $(Contacts_XMLDoc).find('team').length;
		
	}
	catch(e)
	{
		alert('holy moly');
	}
	
	// write the HTML
	Contacts_array.push('<ul>');
	
	// for each contact in the XML...
	$(Contacts_XMLroot).find("contact").each(function() 
	{
		// get the slide data
		var theContact = $(this);
		
		/*try
		{
			// Make sure there's an image
			//if (/\S/.test($(Rotator_XMLroot).find('image')[0].childNodes[0].nodeValue))	
			if (/\S/.test($(theSlide).find('image')[0].childNodes[0].nodeValue))	
			{
				numSlides++;  // count this slide
				Rotator_array.push('<li id="rotatorSlide_' + numSlides + '"><div class="rotatorBG" ');
				try
				{
					
					Rotator_array.push('style="background:#FFF url(');
					Rotator_array.push(theSlide.find('image')[0].childNodes[0].nodeValue);
					Rotator_array.push(') top left no-repeat;"');
				}
				catch(e)
				{
					// if there's no image, don't add the style attribute
					
				}
				
				Rotator_array.push('>');
				
				// Make sure there's a link
				if (/\S/.test($(theSlide).find('url')[0].childNodes[0].nodeValue))	
				{
					Rotator_array.push('<a target="');
					try
					{
						Rotator_array.push($(theSlide).find('target')[0].childNodes[0].nodeValue);
					}
					catch(e)
					{
						Rotator_array.push('_self');
					}
					
					Rotator_array.push('" href="');
					Rotator_array.push($(theSlide).find('url')[0].childNodes[0].nodeValue);
					Rotator_array.push('"></a>');
				}
				
				Rotator_array.push('</div></li>');
			}
		}
		catch(e)
		{
			// if there's no image, don't add the 
		}*/
									
	});
	
	Contacts_array.push('</ul>');
	
	$('#contacts').html(Contacts_array.join(''));
	
}


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

/*function ScaleRightAdColumn()
{
	$("#rightSideBannerColumn").height($("#mainContentColumn").height() -3);
}*/



//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function showRandomFunFact(url_for_XML)
{
	var FunFact_array = [];	// holds the HTML to be written
	var makeLink = true;	// whether or not to link
	// Get the data
	if (url_for_XML == null) {
        url_for_XML = 'xml/FunFacts.xml';
    }
    FunFact_XMLDoc = getXML(url_for_XML); // the XML
	var FunFact_XMLroot = FunFact_XMLDoc.documentElement; // another way to access the XML
	var theNumber = Math.floor(Math.random() * ($(FunFact_XMLroot).find("FunFact").length ) );	// which banner to pull

	var theBanner = $(FunFact_XMLroot).find("FunFact")[theNumber];	// the banner's XML													
	var theImage = $(theBanner).find("imageURL").text();	// the image URL
	try
	{
		if (/\S/.test(FunFact_XMLDoc.getElementsByTagName('linkURL')[0].childNodes[0].nodeValue))
		{
			var theLink = $(theBanner).find("linkURL").text();	// the link URL
		}
		else
		{
			throw("no link");
		}
	}
	catch(e)
	{
		makeLink = false;
		var theLink = "javascript:void(0)";
	}
	// Write the HTML
	FunFact_array.push('<div id="funFact">');
	if (makeLink)
	{
		FunFact_array.push('<a href="');
		FunFact_array.push(theLink);
		FunFact_array.push('">');
	}
	FunFact_array.push('<img src="');
	FunFact_array.push(theImage);
	FunFact_array.push('" />');
	if (makeLink)
	{
		FunFact_array.push('</a>');
	}
	FunFact_array.push('</div>');
	$('.FunFact').html(FunFact_array.join(''));
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function CallPrint(areatoprint)
{
	//alert('CallPrint(' + areatoprint + ')');

	/*var prtContent = document.getElementById(areatoprint);
	var border = "border=1";
	var actualContent = prtContent.innerHTML;
	var goodprtContent = '<body style="background:none;"><link rel="stylesheet" type="text/css" href="includes/css/Arganics.css" media="all" />
		<!--[if gt IE 6]>
			<link rel="stylesheet" type="text/css" href="includes/cssbin/mrn/ie7+_rules.css" />
		<![endif]-->
		<!--[if gt IE 7]>
			<link rel="stylesheet" type="text/css" href="includes/cssbin/mrn/ie8+_rules.css" />
		<![endif]-->
		<script type="text/javascript" src="includes/js/arganics.js"></script><!--[if gt IE 6]><link rel="stylesheet" type="text/css" href="includes/css/ie7Plus_rules.css" /><![endif]--><!--[if gt IE 7]><link rel="stylesheet" type="text/css" href="includes/css//ie8Plus_rules.css" /><![endif]--><div id="PrintFormat">' + actualContent.replace(/border=0/gi, border) + '</div></body>';
	var WinPrint = window.open('','','left=0,top=0,width=800,height=600,toolbar=1,menubar=1,scrollbars=1,status=0');
	WinPrint.document.write(goodprtContent);
	WinPrint.print();
	WinPrint.document.close();
	WinPrint.focus();*/
	
	window.print();
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////


