
document.observe("dom:loaded", function() {
	// Check if the check in and out buttons are available, then init calendar
	if($('btnCheckIn') && $('btnCheckOut')) {
		Calendar.setup({
			dateField				: 'dateCheckIn',
			triggerElement	: 'btnCheckIn',
			dateFormat			: '%m/%d/%y',
			closeHandler 		: onCheckInSelected
		});
		
		Calendar.setup({
			dateField				: 'dateCheckOut',
			triggerElement	: 'btnCheckOut',
			dateFormat			: '%m/%d/%y'
		});
	}
	
	var modal = new Control.Modal($('specialWindow'),{  
	    overlayOpacity: 0.75,  
	    className: 'modal',  
	    fade: true,
			fadeDuration: 0.25,
			width: 500,
			height: 300,
			iframe: true
	});

});

// Called once the check in date for the calendar component has been checked
function onCheckInSelected(e) {
	e.hide();	// call the calendars own close method
	setCheckOutForward($('dateCheckIn').value, 3);
}

// Sets the check out date forward
function setCheckOutForward(checkInDate, daysForward) {
	var dtCheckIn = new Date(checkInDate);
	var dtCheckOut = new Date(checkInDate);
	dtCheckOut.setDate(dtCheckIn.getDate()+daysForward);
	
	var sCheckOut = formatDate(dtCheckOut, "MM/dd/yy");
	$('dateCheckOut').value = sCheckOut;
}

function createToolTipWrapper(width, height, content) {
	if(!width) width = "200";
	var wrapStyle = "width:" + width + "px;";
	if(height) wrapStyle += "height:" + height + "px;";
	var wrap = new Element('div', { 'class' : 'tooltip', 
																	'style' : wrapStyle}).update(content);
	return wrap;
}

// Refreshes the web cam image for the weather conditions page
function refreshSkiCam() {
	var url = "http://telrtp1.tellurideskiresort.com/website/webcam/netcam.jpg";
	var append = new Date();
	append = "?" + append.getTime();
	$('snowCamImage').src = url + append;
}

// Returns a query time stamp which may be appended to urls, generates a string like "?1234858858"
function queryTimeStamp() {
	var strdate = new Date();
	strdate = "?" + strdate;
	return strdate;
}


// BEGIN admin photo selector logic
// These methods are used to load and select photos for the Specials model. They allow simple
// click-based selection and modify the hidden field 'special_photo_id' in order to set an id

/**
* Selects the photo in the ajax window
* @param	photoId 		the ID of the photo recorded in the special_photos table
*/
function selectSpecialPhoto(photoId) {
	deselectAllSpecialPhotos();
	imgDomId = "special_photo_" + photoId;	
	setPhotoStroke(imgDomId);
	updateSelectedPhotoId(photoId);
	$('selectedPhoto').src = $(imgDomId).src;
}

// Applies a stroke to the image specified
function setPhotoStroke(imgId) {
	$(imgId).removeClassName("deselectedSpecialPhoto");
	$(imgId).addClassName("selectedSpecialPhoto");
}

// Sets images in the photo_picker table to the off-selected state
function deselectAllSpecialPhotos() {
	$$('#photo_picker img').each(function(img) {
		$(img).addClassName("deselectedSpecialPhoto");
	});
}

/**
* Updates the hidden variable on the form page effectively setting the new photo once form is submitted
* @param		photoId			the id of the 'special_photo' which will be linked to the Special model
*/
function updateSelectedPhotoId(photoId) {
	$('special_special_photo_id').value = photoId;
}
// END admin photo selector logic

/**
* RSVP form page logic
* used in /rsvps/new
*/
function toggleRsvpRadio(radControlId) {
	$(radControlId).checked = true;
}

// end RSVP logic

/**
* BEGIN logic for admin ajax actions
*/

/**
* Updates a publish row, change its styles based on whether the item is published or unpublished
* @param	listItemId		the id of the list item containing the pressed button
@ @param	isPublished		whether the row is published or not
*/
function updatePublishStatusRow(listItemId, isPublished) {
	//console.log("update " + listItemId + " new status, published? " + isPublished);
	var newStyle = (isPublished) ? "unpublished" : "published";
	var oldStyle = (isPublished) ? "published" : "unpublished";
	var buttonContainer = $(listItemId).down();
	buttonContainer.removeClassName(oldStyle);
	buttonContainer.addClassName(newStyle);
}

/**
* Called at the very start of a row's ajax request. This prevents subsequent ajax actions to be called until
* the server has processed the command and returned a result
* @param	recordId	The database id of the object being changed. This is used to determine the domID of the element that triggered the ajax call
*/
function onChangePublishStatusRowChanged(recordId) {
	//console.log("received change from " + recordId);
	var linkId = "record_link_" + String(recordId);
	$(linkId).onclick = $(linkId).href = "javascript:void(0);";
}

/**
 * Replaces all text area fields in a form with non-MS paste text
 */
function formStripWordHtml(formId) {
	$$(form + ' textarea').each(function(name, index) {
		console.log(name);
	});
	return false;
}

/**
 * Clean word html function, found at http://www.1stclassmedia.co.uk/developers/clean-ms-word-formatting.php
 */
function CleanWordHTML( str )
{
	str = str.replace(/<o:p>\s*<\/o:p>/g, "") ;
	str = str.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;") ;
	str = str.replace( /\s*mso-[^:]+:[^;"]+;?/gi, "" ) ;
	str = str.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "" ) ;
	str = str.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;
	str = str.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, "" ) ;
	str = str.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;
	str = str.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;
	str = str.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;
	str = str.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;
	str = str.replace( /\s*tab-stops:[^;"]*;?/gi, "" ) ;
	str = str.replace( /\s*tab-stops:[^"]*/gi, "" ) ;
	str = str.replace( /\s*face="[^"]*"/gi, "" ) ;
	str = str.replace( /\s*face=[^ >]*/gi, "" ) ;
	str = str.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, "" ) ;
	str = str.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	str = str.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;
	str = str.replace( /\s*style="\s*"/gi, '' ) ;
	str = str.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ;
	str = str.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
	str = str.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	str = str.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' ) ;
	str = str.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' ) ;
	str = str.replace(/<\\?\?xml[^>]*>/gi, "") ;
	str = str.replace(/<\/?\w+:[^>]*>/gi, "") ;
	str = str.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
	str = str.replace( /<H1([^>]*)>/gi, '' ) ;
	str = str.replace( /<H2([^>]*)>/gi, '' ) ;
	str = str.replace( /<H3([^>]*)>/gi, '' ) ;
	str = str.replace( /<H4([^>]*)>/gi, '' ) ;
	str = str.replace( /<H5([^>]*)>/gi, '' ) ;
	str = str.replace( /<H6([^>]*)>/gi, '' ) ;
	str = str.replace( /<\/H\d>/gi, '<br>' ) ; //remove this to take out breaks where Heading tags were
	str = str.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;
	str = str.replace( /<(B|b)>&nbsp;<\/\b|B>/g, '' ) ;
	str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
	str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
	str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
	//some RegEx code for the picky browsers
	var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;
	str = str.replace( re, "<div$2</div>" ) ;
	var re2 = new RegExp("(<font|<FONT)([^*>]*>.*?)(<\/FONT>|<\/font>)","gi") ;
	str = str.replace( re2, "<div$2</div>") ;
	str = str.replace( /size|SIZE = ([\d]{1})/g, '' ) ;

	return str ;
}


// END admin ajax actions

/*
*
* Below is logic for converting dates from a simple string
*
*/

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
function LZ(x) {return(x<0||x>9?"":"0")+x}
function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var E=date.getDay();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real date parts into formatted versions
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=MONTH_NAMES[M-1];
	value["NNN"]=MONTH_NAMES[M+11];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["E"]=DAY_NAMES[E+7];
	value["EE"]=DAY_NAMES[E];
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
	}
