var active = null;
var browser=navigator.appName;
var currentTime;

var merchUrl = null;
var forumUrl = null;
var objectPlaying = null;
var oXMLHttp = null;
var oXMLHttpClick = null;
var oXMLHttpRelease = null;
var oXMLHttpTour = null;
var oXMLHttpFlashNav = null;

var horzBackgrounds = new Array();

var horzBackgroundCount = 16;

var monthArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");


var agent = navigator.userAgent.toLowerCase(); 
var isMac = (agent.indexOf("mac") != -1) ? true : false;
var isMSIE = (navigator.appVersion.indexOf("MSIE")!=-1) ? true : false;

var browserName = "";

var historyXML = new Object;

var ua = navigator.userAgent.toLowerCase();

if ( ua.indexOf( "opera" ) != -1 )
{
	browserName = "opera";
} else if ( ua.indexOf( "msie" ) != -1 )
{
	browserName = "msie";
} else if ( ua.indexOf( "safari" ) != -1 )
{
	browserName = "safari";
} else if ( ua.indexOf( "mozilla" ) != -1 )
{
	if ( ua.indexOf( "firefox" ) != -1 )
	{
		browserName = "firefox";
	} else {
		browserName = "mozilla";
	}
}

var paneDifference = 0;


var canSwap = "true";

function printImage(imageUrl, width, height, max) {


	imageWidth = parseInt(width * (max/width));
	imageHeight = parseInt(height * (max/width));


	leftDif = parseInt((max - imageWidth) / 2);
	topDif = parseInt((max - imageHeight) / 2);

	image = "<img src=\""+imageUrl+"\" width=\""+imageWidth+"\" height=\""+imageHeight+"\" border=\"0\" align=\"top\">"; 
	
	return image;

}

function addslashes(str) {

	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function stripslashes(str) {
	var thisStr = "";
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

window.onload=function(){


}

function toCamelCase(s) {
	for (var exp = /-([a-z])/; exp.test(s); s=s.replace(exp, RegExp.$1.toUpperCase())) {
	}
	return s;
}

function loadFlashNav()
{

	var so = new SWFObject("main.swf", "resizer", "100%", "100%", "9");
	so.addParam("scale", "noscale");
	so.addVariable("xmlFile", "xml/links_"+ territory +".xml");
	so.write("flashNav");
	
	windowResize();
}


function GetXMLHttpObject()
{
      oXMLHttp = null;

      try
      {
            oXMLHttp = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (E)
      {
            try
            {
                  oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
              oXMLHttp = null;
            }
      }

      if ((oXMLHttp == null) && (typeof(XMLHttpRequest) != 'undefined'))
      {
            oXMLHttp = new XMLHttpRequest();
      }

      return oXMLHttp;
}


function loadDates()
{
	
	if(oXMLHttp)
	{
		oXMLHttp.abort();
	}

	if (window.XMLHttpRequest)
	{
	 	 oXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	  	oXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var url="../xml/history.xml";

	oXMLHttp.onreadystatechange = function()
	    {
		  if (oXMLHttp.readyState == 4)
		  {
			if (oXMLHttp.status==200)
			{	
				historyXML = oXMLHttp.responseXML;
				placeTimeline();
				loadDate(0);
			}
			else
			{
				alert("Problem retrieving XML data");
			}
		  }
	    }
	
	oXMLHttp.open("GET", url, true);
	
	oXMLHttp.send(null);
}



function loadDate(id)
{

	html = "<table width=\"930\" cellspacing=\"0\" cellpadding=\"0\">";
	var articles = historyXML.getElementsByTagName("Item");

	html += "<tr valign=\"top\" align=\"left\">";
	html += "<td>";

	var imageTD = 0;
	var gallery = articles[id].getElementsByTagName("Photo");;
	if(gallery.length  > 0)
	{
		imageTD = 310;
		html += "<table width=\"310\" cellspacing=\"0\" cellspadding=\"0\">";

		for(y = 0; y < gallery.length; y++)
		{
			imageUrl = "../"+ gallery[y].attributes.getNamedItem("Url").value;
			imageWidth = gallery[y].attributes.getNamedItem("Width").value;
			imageHeight = gallery[y].attributes.getNamedItem("Height").value;
			max = 300;

			html += "<tr valign=\"top\" align=\"left\">";
			html += "<td style=\"padding: 0px 0px 10px 0px\">" + printImage(imageUrl, imageWidth, imageHeight, max) +"</td>";
			html += "</tr>";
		}

		html += "</table>";
	}
	html += "</td>";
	var tempValue = articles[id].getElementsByTagName("Title")[0];
	var title = tempValue.childNodes[0].nodeValue;

	var dateArray = articles[id].attributes.getNamedItem("Date").value.split('-');

	var thisMonth = monthArray[dateArray[1] - 1];

	var thisYear  = dateArray[0];

	var thisDay = dateArray[2];

	var date = thisMonth + " " + thisDay +", "+thisYear;

	var tempValue = articles[id].getElementsByTagName("Body")[0].firstChild;
	if(tempValue != null)
	{
		var text = tempValue.data;
	} else {
		var text ="";
	}

	html += "<td><table width=\""+ (930 - imageTD) +"\" cellspacing=\"0\" cellpadding=\"0\">";
	html += "<tr valign=\"top\" align=\"left\">";
	html += "<td><span id=\"headline2\">"+ title +"</span><br /><span id=\"headline3\">"+ date +"</span></td></tr>";
	html += "<tr valign=\"top\" align=\"left\">";
	html += "<td><span id=\"content\">"+text +"</span></td></tr>";
	html += "</table></td></tr>";
	html += "<tr colspan=\"2\"><td height=\"20\"/></tr>";

	
	html += "</table>";
	
	document.getElementById('historyContent').innerHTML = html;

}


function capitalize(obj) {
        val = obj;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
        }
        return newVal;
}


function dumpDivs(thisSection)
{
	document.getElementById('content').display = "block";
	document.getElementById('secondaryDiv').display = "block";
	document.getElementById('tertiaryDiv').display = "block";
	document.getElementById('content').innerHTML = "";
	if (thisSection != "tours" || thisSection != "video")
	{
		document.getElementById('secondaryDiv').innerHTML = "";
	}
	if (thisSection != "photos")
	{
		document.getElementById('tertiaryDiv').innerHTML = "";
	}

}
//
function getStyle(el, style) {
	if (!document.getElementById) {
		return;
	}
	var value = el.style[toCamelCase(style)];
	if (!value) {
		if (document.defaultView) {
			value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
		} else if (el.currentStyle) {
			value = el.currentStyle[toCamelCase(style)];
		}
	}
	return value;
}

function getElement(id){	
	if (!document.getElementById){
	 return;
	}
	//
	if (document.getElementById){
		// this is the way the standards work
		var el = document.getElementById(id);
	}else if (document.all){
		// this is the way old msie versions work
		var  el = document.all[id];
	}else if (document.layers){
		// this is the way nn4 works
		var el = document.layers[id];
	}
	return el;
}

toggleDisplay=function(el){
	el.style.display = el.style.display ? "" : "block";
}

replaceAnchors=function(){
	if (document.getElementById) {
		var atags = document.getElementsByTagName("A");
		for (var i = 0; i<atags.length; i++) {
			var ca = atags[i];
			var index=ca.href.indexOf("#");
			var hash=ca.href.substring(index+1);
			if (index>-1 &&	hash.length>0) {
				ca.link=hash;
				ca.onclick = function() {	
					var b = getElement('content');
					b.scrollTop = getElement(this.link).offsetTop;
					return false;
				};
			}
		}
	}
}

function placeTimeline()
{
	var html = "";
	
	switch(browserName)
	{

		case "msie" :

				html = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\"950\" height=\"55\" id=\"audioPlayer\" align=\"middle\">";
				html += "<param name=\"allowScriptAccess\" value=\"always\" />";
				html += "<param name=\"allowFullScreen\" value=\"false\" />";
				html += "<param name=\"wmode\" value=\"transparent\" />";
				html += "<param name=\"movie\" value=\"../flash/history_950x55.swf\" />";
				html += "<param name=\"quality\" value=\"high\" />";
				html += "<param name=\"bgcolor\" value=\"#ffffff\" />";
				html += "<embed src=\"../flash/history_950x55.swf\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#ffffff\" width=\"950\" height=\"55\" id=\"audioPlayer\" name=\"audioPlayer\" align=\"middle\" allowScriptAccess=\"always\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
				html += "</object>";
				break;

		default :
				html = "<embed src=\"../flash/history_950x55.swf\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#ffffff\" width=\"950\" height=\"55\" id=\"audioPlayer\" name=\"audioPlayer\" align=\"middle\" allowScriptAccess=\"always\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
				break;
	}
	
	document.getElementById('timelineDiv').innerHTML = html;
}

function placeFLVPlayer(videoUrl, videoType)
{
	videoUrl = videoUrl.replace(/&/, "%26");
	var flashVars = "videoUrl="+videoUrl+"&autoplay=true";
	switch(videoType)
	{
		case "Official":
			switch(browserName)
			{

				case "msie" :

					html = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\"100%\" height=\"100%\" id=\"resizer\" align=\"middle\">";
					html += "<param name=\"allowScriptAccess\" value=\"always\" />";
					html += "<param name=\"allowFullScreen\" value=\"false\" />";
					html += "<param name=\"wmode\" value=\"transparent\" />";
					html += "<param name=\"movie\" value=\"flash/videoPlayer.swf?"+flashVars+"\" />";
					html += "<param name=\"quality\" value=\"high\" />";
					html += "<param name=\"bgcolor\" value=\"#ffffff\" />";
					html += "<embed src=\"flash/videoPlayer.swf\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#ffffff\" width=\"100%\" height=\"100%\" flashvars=\"" +flashVars+ "\"  id=\"resizer\" name=\"resizer\" align=\"middle\" allowScriptAccess=\"always\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
					html += "</object>";
					document.getElementById('flashNav').innerHTML = html;
					break;

				default:

					var so = new SWFObject("flash/videoPlayer.swf", "resizer", "100%", "100%", "9");
					so.addParam("scale", "default");
					//so.addParam("FlashVars", flashVars);
					so.addVariable("videoUrl", videoUrl);
					so.addVariable("autoplay", true);
					so.addParam("wmode", "transparent");
					so.write("flashNav");
					break;
			}
			break;
			
		case "YouTube":

			var so = new SWFObject("http://www.youtube.com/v/"+ videoUrl.substr(31)+"&autoplay=1", "resizer", "100%", "100%", "9");
			so.addParam("scale", "default");
			so.addParam("bgcolor", "#000000");
			so.addParam("wmode", "transparent");
			so.write("flashNav");
			break;
	}
	
	windowResize();	
	
	if (divDirections["up"] == "true")
	{
		closeHorz();
	}
	
	document.getElementById('flashNav').innerHTML += "<div style=\"position: absolute; left: 10px; top:10px;\"><a href=\"#\" onclick=\"loadFlashNav(); return false;\"><img src=\"images/close_button_orange.gif\" width=\"33\" height=\"33\" border=\"0\"></a></div>";

}


function placePlayer(sDivId, previewUrl, iteration)
{
      var oXMLHttp = GetXMLHttpObject();

	  var url="../php/placePreviewPlayer.php";
	  url=url+"?previewUrl="+previewUrl;
	  url=url+"&iteration="+iteration;
	  url=url+"&browser="+browser;

      oXMLHttp.open("GET", url, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {	
                        document.getElementById(sDivId).innerHTML = oXMLHttp.responseText;
                  }
            }

      oXMLHttp.send(null);
}

function placePodcastPlayer(sDivId, previewUrl, iteration)
{
      var oXMLHttp = GetXMLHttpObject();

	  var url="../php/placePodcastPlayer.php";
	  url=url+"?previewUrl="+previewUrl;
	  url=url+"&iteration="+iteration;
	  url=url+"&browser="+browser;

      oXMLHttp.open("GET", url, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {	
                        document.getElementById(sDivId).innerHTML = oXMLHttp.responseText;
                  }
            }

      oXMLHttp.send(null);
}

function placePodcastPlayerHome(sDivId, previewUrl)
{

      var oXMLHttp = GetXMLHttpObject();

	  var url="php/placePodcastPlayerHome.php";
	  url=url+"?previewUrl="+previewUrl;
	  url=url+"&browser="+browser;

      oXMLHttp.open("GET", url, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {	
                        document.getElementById(sDivId).innerHTML = oXMLHttp.responseText;
                  }
            }

      oXMLHttp.send(null);
}

function placeRadio()
{
      var oXMLHttp = GetXMLHttpObject();

	  var url="../php/placeRadio.php";
	  url=url+"?browser="+browser;

      oXMLHttp.open("GET", url, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {	
                        document.getElementById("radio").innerHTML = oXMLHttp.responseText;
                  }
            }

      oXMLHttp.send(null);
}

function setPlaying(id){

	objectPlaying = getElement(id);
	
}

function stopAudio(id)
{
	if(objectPlaying != getElement(id))
	{
		objectPlaying.stopAudio();
	}
	
	var radio = getElement("radioPlayer");
	
	radio.stopAudio();
}

function changeVal(id)
{
	val = "";
	document.getElementById(id).value = val;
}

function submitEmail(loc)
{
	var email = document.getElementById('email').value;

	if(loc == null)
	{
		thisLoc = "";
	} else {
		thisLoc = loc;
	}
	
	var url = thisLoc + "community/?email="+email;

	document.location = url;
}

function getMerch()
{
	if(merchUrl != null)
	{

		var options = "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1";

		launch(merchUrl, "download", 500, 500, options);
	}
}

function getForum()
{
	if(forumUrl != null)
	{

		var options = "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1";

		launch(forumUrl, "download", screen.availWidth, screen.availHeight, options);
	}
}

switchContent=function(id){	

	//turn off last active
	if(active){
		document.images[oldSection].src = items[oldSection].off.src;
		toggleDisplay(active);
	}
	
	switch(id)
	{
		case "news":
			
			id = "news"+territory;	
			active=getElement(id);
			toggleDisplay(active);
			section = oldSection = "news";
			document.images[section].src = items[section].on.src;
			break;

		case "contest":

			id = "contest"+territory;	
			active=getElement(id);
			toggleDisplay(active);
			section = oldSection = "contest";
			document.images[section].src = items[section].on.src;
			break;

		default:

			active=getElement(id);
			toggleDisplay(active);
			section = oldSection = id;
			document.images[section].src = items[section].on.src;
			break
	}
	
	//var img=(getElement('headerimg'))
	//img.src="images/header-"+id+".jpg"

	//trackEvent(window.location.host, "/"+id+".ajaxPage", id, '');
	
	var contentDiv = getElement('content');
	contentDiv.scrollTop = 0;
}

//
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i<vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}



function toggleAlphaNav(state){
	el = document.getElementById('footerAlphaNav');
	
	if(state == "hide")
	{
	 	el.style.display = "";
	} else {
		el.style.display = "block";
	}
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


function moveDiv() {

	pageSize = getPageSize();

	thisY = 0;
	var pageScroll;
/*
	if (self.pageYOffset) {
		pageScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		pageScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		pageScroll = document.body.scrollTop;
	}
*/
	pageScroll = document.getElementById('contentDiv').scrollTop;

	widthDif = 70;
	divDiff = document.getElementById('featureDiv').offsetHeight;
	if(isMSIE){
		otherdif = 0;
	} else {
		otherdif = 120;
	}
	
	document.getElementById('featureDiv').style.display = "block";
	document.getElementById('featureDiv').style.top = pageSize[3] + pageScroll - divDiff - otherdif +"px";
	document.getElementById('featureDiv').style.left = pageSize[0] / 2 + widthDif +"px";

}

function setBg()
{
	pageSize = getPageSize();
	randomNum = Math.floor((6-1)*Math.random()) + 1;

	if(pageSize[1] < 800)
	{
		bgDim = 800;
		difference = pageSize[1] / bgDim;
		bgWidth = 1065 * difference;
		
	} else if (pageSize[1] < 1100) {
	
		bgDim = 1100;
		difference = pageSize[1] / bgDim;
		bgWidth = 1465 * difference;
		
	} else {
		
		bgDim = 1400;
		difference = pageSize[1] / bgDim;
		bgWidth = 1864 * difference;
		
	}
	
	document.getElementById('backgroundDiv').innerHTML = "<img src=\"http://www.rockridgemusic.com/amylennard/images/bg_"+ bgDim +".jpg\" width=\""+ bgWidth+"\" height=\""+ pageSize[1] +"\" />";
	document.getElementById('backgroundDiv').style.width = bgWidth +"px";
	document.getElementById('backgroundDiv').style.left = (pageSize[0] / 2) - (bgWidth / 2) +"px";

}


function launch(url, name, width, height, options)
	{
	if (!name)
		{
		name = "window_" + Math.floor(1000 * Math.random());
		}
	
	if (!width)
		{
		width = screen.availWidth;
		}
		
	if (!height)
		{
		height = screen.availHeight;
		}
	
	if (!options)
		{
		options = "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1";
		}
	
	width	= Math.min(width, screen.availWidth);
	height	= Math.min(height, screen.availHeight);
	var x	= Math.max(0, 0.5 * (screen.availWidth - width));
	var y	= Math.max(0, 0.5 * (screen.availHeight - height));

	var config = "";
	config += "left=" + x + ",top=" + y + ",";
	config += "screenX=" + x + ",screenY=" + y + ",";
	config += "width=" + width + ",height=" + height;
	config += "," + options;

	var win = window.open(url, name, config);
	if (win && window.focus)
		{
		win.focus();
		}
	}
	
var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}


