// - - - - DOCUMENT READY FOR RESORT PAGES - - - - //
// - - - - - - - - - - - - - - - - - - - - - - - - //
$(document).ready(function() {
		redirect(); 
		});
// - - - - - - - - - - - - - - - - - - - - - - - - //
// - - - - DOCUMENT READY FOR RESORT PAGES - - - - //
	jQuery(function($) {
		
		$('ul.gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#bImg', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}

			
		});
		$('span.caption').css({display:'none'});
		//startRotation();
	});



var stopFlag = 'N';
var xmlDoc=null;
var xXmlNode;
//To hold the season dates
var xSeasonMulti = new Array();

//xml file locations (Previous, Current, Next)
var xCurrentXML= "";
var xNextXML = "/includes/xml/season/2010seasoncalendar.xml";

//Loads the XML into browser
function loadXMLDoc(xml)
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
xmlDoc.async=false;
xmlDoc.load(xml);
}

//This get season information
function getDates(year, url, season)
{
	var xDates="";
	if(year == "c" && xCurrentXML != "")
		loadXMLDoc(xCurrentXML);
		else 
		if(year == "n" && xNextXML != "")
		loadXMLDoc(xNextXML);
		else {
		 xDate="NA";
		 return xDates;
		}
		
	var x=xmlDoc.getElementsByTagName("ns1:Site");
	
	for (var i=0;i<x.length;i++)
	{ 
		if( x[i].getElementsByTagName("ns1:URL")[0].childNodes[0].nodeValue == url.toLowerCase()
			&& x[i].getElementsByTagName("ns1:Type")[0].childNodes[0].nodeValue == season.toUpperCase())
			xDates += x[i].getElementsByTagName("ns1:Range")[0].childNodes[0].nodeValue + '<br>';
	}
	if (xDates == "")
	return "N/A";
	else
	return xDates;
}





//get weather from app
function getWeather(code){
	var ran_unrounded=Math.random()*999999;
	var ran_number=Math.floor(ran_unrounded);
	
	$.ajax({
    type: "GET",
    url: "/owners/weatherDetails.do?locationCode="+code+ '&id='+ ran_number,
    dataType: "xml",
    success: parseXml
  });
}


function parseXml(xml)
{
	var test= undefined;
	test = $(xml).find("current").attr("temperature");
	if(xml != null && test != undefined){
	  //find current temp and display
	  $(xml).find("current").each(function()
	  {
		  if($(this).attr("temperature")!= null)
			document.getElementById("5daylink").style.display = "block";
		  
		  //Append current temperature
		$("#weather").append('<strong class="DefaultTextLtBlueBold">Current Weather: </strong><img alt="'
							 + $(this).attr("skytext")
							 + '" src="/images/weather/small_icons/'
							 + $(this).attr("skycode") + '.gif" style="vertical-align:middle;"/>&nbsp;'
							 + $(this).attr("temperature") + '°F <img alt="" src="/images/dividers.gif"  style="vertical-align:middle"/>');
		
		$("#weatherExt").append('<div id="weather-current" align="center"><h5><strong>CURRENT WEATHER</strong> - Last Updated: '
							 + convertTime($(this).attr("observationtime"))
							 + ' Local Time</h5><table><tr><td><img alt="'
							 + $(this).attr("skytext")
							 + '" src="/images/weather/large_icons/'
							 + $(this).attr("skycode")
							 + '.gif"/></td><td><strong style="color:#0074BE;font-size:1.9em;">'
							 + $(this).attr("temperature")
							 + '°F</strong></td><td width="20"><span/></td><td>Humidity: '
							 + $(this).attr("humidity")
							 + '%<br/>Wind: '
							 + $(this).attr("winddisplay")
							 + '</td></tr></table></div><br>');
		});
	  //find extended weather and display
	  $(xml).find("weather").each(function()
		{
		  $(this).find("forecast").each(function()
		  {
			$("#weatherExt").append('<div class="dayOftheWeek" align="center"><h2>'
									+ $(this).attr("day") 
									+ '</h2><br/>'
									+ getMonthTxt($(this).attr("date")) + " " + getTodayDay($(this).attr("date"))
									+ '<br/><br/><img alt="'
									+ $(this).attr("skytextday") 
									+ '" src="/images/weather/large_icons/'
									+ $(this).attr("skycodeday") 
									+ '.gif"/><br/><br/>'
									+ checkLength($(this).attr("skytextday"))
									+ '<br/><strong>Hi: '
									+ $(this).attr("high") 
									+ '°F</strong><br/><strong>Lo: '
									+ $(this).attr("low") 
									+ '°F</strong></div>');
		  });
		  
		});
	  //wrap forecast in div
	  $(".dayOftheWeek").wrapAll('<div style="width:450px;height:220px;"></div>');
	   //add close button
	  $("#weatherExt").append('<div align="right"><button class="nyroModalClose" style="vertical-align:bottom">Close Window</button></div>');
	}else
	{
		$("#error").append('Weather temporarily unavailable. Please check back later.');
	}
}

function convertTime(time){
		curhour = time.substring(0,2);
		curmin = time.substring(3,5);

		if(curhour == 0) curhour = 12;
		time = (curhour > 12 ? curhour - 12 : curhour) + ":" + 
		curmin + (time.substring(0,2) > 11 ? " PM" : " AM");
		return time;
	}

function getTodayDay(dateIn){
	var date = parseDate(dateIn);
	var d = date.getDate();
	return d;
}

function getMonthTxt(dateIn){
	var date = parseDate(dateIn);
	var month=new Array(12);
	month[0]="Jan";
	month[1]="Feb";
	month[2]="Mar";
	month[3]="Apr";
	month[4]="May";
	month[5]="Jun";
	month[6]="Jul";
	month[7]="Aug";
	month[8]="Sep";
	month[9]="Oct";
	month[10]="Nov";
	month[11]="Dec";
	return month[date.getMonth()-1];
} 

function parseDate(x){
	var yy = x.substring(0,4);
	var mm = x.substring(5,7);
	var dd = x.substring(8,10);
	var date = new Date(yy,mm,dd);
	return date;
	}
//check to make sure length doesnt exceed box
function checkLength(x){
		if(x.indexOf("Thunderstorms") > -1)
			return x.replace(/Thunderstorms/g, "T-storms");
			else return x;
	
}
function startRotation(){
	setTimeout
	( 
		function()
		{
			if(stopFlag == 'N')
				{
					$.galleria.next();
				startRotation();
				}
		},
	5000
	); 
}
function startRot(){
	if(stopFlag == 'Y')
	{
	 stopFlag='N';
	 startRotation();
	}
}
function stopRot(){
	if(stopFlag == 'N')
	{
	 stopFlag='Y';
	}
}
