var vrFlash;
$(document).ready(function() {
    $('.resform .checkin').datepicker({dateFormat: 'mm/dd/yy', defaultDate: +0, showOtherMonths: true, changeFirstDay: false, prevText: '', nextText: '',
        onSelect: function(date) {
            $('.res-checkin').val(date);
        }
    });

    $('.resform .checkout').datepicker({dateFormat: 'mm/dd/yy', defaultDate: +3, showOtherMonths: true, changeFirstDay: false, prevText: '', nextText: '',
        onSelect: function(date) {
            $('.res-checkout').val(date);
        }
    });

    $('.resform .button').click(function() {
        document.getElementById("resform").submit();
        return false;
    });

	$('.amenities table').each(function() {
		$(this).find('tr').slice(2).addClass("hidden");
		$(this).find('tr').eq(1).addClass("last");
	});
	$('.amenities h4 a').click(function() {
		var text = $(this).text()=='see more' ? 'see less' : 'see more';
		if(text == 'see more') {
			$(this).parents('.amenities').children('table').find('.last').removeClass("last");
			$(this).parents('.amenities').children('table').find('tr').eq(1).addClass("last");
			$(this).parents('.amenities').children('table').find('.not-hidden').addClass("hidden");
			$(this).parents('.amenities').children('table').find('.hidden').removeClass("not-hidden");
		} else {
			$(this).parents('.amenities').children('table').find('.last').removeClass("last");
			$(this).parents('.amenities').children('table').find('tr').eq($(this).parents('.amenities').children('table').find('tr').length-1).addClass("last");
			$(this).parents('.amenities').children('table').find('.hidden').addClass("not-hidden");
			$(this).parents('.amenities').children('table').find('.hidden').removeClass("hidden");
		}
		$(this).text(text);
		return false;
	});

    $('.res .avail-rooms .room .tree-link').click(function() {
        if (!$(this).siblings('.tree').hasClass('open')) {
            $(this).siblings('.tree').addClass('open');
			
			var tree = $(this).siblings('.tree');
			setTimeout(function() {
				if(tree.find('li').length == 0) {
					tree.parents('.room').children('.load-mask').css("opacity","0");
					tree.parents('.room').children('.load-mask').css("left","1px");
					tree.parents('.room').children('.load-mask').children('.load-mask-top').children(".loading-message").css("display","block");
					tree.parents('.room').children('.load-mask').children('.load-mask-top').height(tree.parents('.room').children('.top').outerHeight() - 4);
					tree.parents('.room').children('.load-mask').animate({
		                opacity: "1"
		            }, 'fast');
				}
			}, 1000);
			
			// AFTER LOAD
			$(this).parents('.room').children('.load-mask').css("left","-9999px");
			$(this).parents('.room').children('.load-mask').children('.load-mask-top').children(".loading-message").css("display","none");
            var padding;
            if (document.all && !window.opera) {
                padding = "30px";
            } else {
                padding = "15px";
            }
			var height = $(this).siblings('.tree').children('li:first').height() * $(this).siblings('.tree').children('li').length + "px";
            $(this).siblings('.tree').animate({
                paddingBottom: padding,
                height: height,
                opacity: "1"
            }, 'normal');
            $(this).css({ backgroundImage: "url(../images/arrow_open.png)" });
            $(this).text("Hide Additional Rates for this Room");
			//END AFTER LOAD
			
        } else {
            $(this).siblings('.tree').removeClass('open');			
            $(this).siblings('.tree').animate({
                paddingBottom: "0",
                height: "0",
                opacity: "0"
            }, 'normal');
            $(this).css({ backgroundImage: "url(../images/arrow_closed.png)" });
            $(this).text("Show Additional Rates for this Room");
        }
        return false;
    });
    $('.res .avail-rooms .room .tree-link').mousedown(function() {
        $(this).css({ backgroundImage: "url(../images/arrow_mousedown.png)" });
    });



    $('.res .rate-info .grand-total span.option a').click(function() {
        if (!viewRates) {
            viewRates = true;
            var padding;
            if (document.all && !window.opera) {
                padding = 5;
            } else {
                padding = 9;
            }

            $('.res .rate-info .grand-total').animate({
                paddingLeft: "20px"
            }, 'fast', function() {
                $('.res .rate-info .rate-tab').animate({
                    height: $('.res .rate-info .rate-tab li:first').height() * $('.res .rate-info .rate-tab li').length + padding + "px",
                    marginTop: "10px",
                    opacity: "1"
                }, 'normal');
            });
            $(this).text("hide nightly rates");
        } else {
            viewRates = false;
            $('.res .rate-info .rate-tab').animate({
                height: "0",
                marginTop: "0",
                opacity: "0"
            }, 'fast', function() {
                $('.res .rate-info .grand-total').animate({
                    paddingLeft: "0"
                }, 'normal');
            });
            $(this).text("view nightly rates");
        }
        return false;
    });

	$('.pages .page:eq(0)').css("position", "static");
	$('.pages .page:eq(0)').css("left", "auto");
	$('.pages .page:eq(0)').css("top", "auto");
	$('.tabs li:not(.print)').click(function() {
		$('.tabs li.active').removeClass("active");
		$(this).addClass("active");
		var tabs = $('.tabs li:not(.print)');
		for(var x=0;x < tabs.length;x++) {
			if($(this).attr("class") == $(tabs[x]).attr("class")) {
				$('.pages .page').css("position", "absolute");
				$('.pages .page').css("left", "-9999px");
				$('.pages .page').css("top", "-9999px");
				$('.pages .page:eq('+x+')').css("position", "static");
				$('.pages .page:eq('+x+')').css("left", "auto");
				$('.pages .page:eq('+x+')').css("top", "auto");
			}
		}
		if($('.tabs li:not(.print)').index(this) != 2)
			$('.mna .mapperwrapper').css('display','none');
		else
			$('.mna .mapperwrapper').css('display','block');
		return false;
	});
	$('.tabs li.print').click(function() {
		return false;
	});

    if($('.prop-finder') || $('#map-prop')) {
		initialize();
	}
	
	/*$('#findaninn .submit .button').click( function() {	    
		//convert city to proper format	
		var ccCity = convertToCamelCase($("#findinn-city").val());                                  
		$("#findinn-city").val(ccCity);
	    return false; 
	});*/
	
	$('.history table').each(function() {
		$(this).find('tr').children('td:first').addClass('first');
		$(this).find('tr').children('th:first').addClass('first');
		$(this).find('tr:last').addClass('last');
	});
	
	$('.groupin input, .groupout input').focus(function() {
        if ($('#ui-datepicker-div').children('.pickerwrapper').length == 0)
            $('#ui-datepicker-div').prepend('<div class="pickerwrapper"></div>');
		if($.browser.msie)
			$('#ui-datepicker-div').css('position','fixed');
    });
	
	if($('div.inn-vr').length) {
		vrFlash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="VRXFlashPresentation"' +
					'width="571" height="668"	codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">' +
					'<param name="movie" value="http://mediaserver.vrxstudios.com/flash/app/VRXFlashPresentation.swf" />' +
					'<param name="quality" value="high" /> <param name="wmode" value="transparent" /> <param name="allowScriptAccess" value="sameDomain" />' +
					'<param name="allowFullScreen" value="true" /> <param name="FlashVars" value="id='+ vrId +
					'&template=13&package=null&locale=null">' +
					'<embed src="http://mediaserver.vrxstudios.com/flash/app/VRXFlashPresentation.swf" wmode="transparent" quality="high"' +
					'FlashVars="id='+ vrId +
					'&template=13&package=null&locale=null" width="571" height="668" name="VRXFlashPresentation" align="middle"' +
					'play="true" loop="false" quality="high" allowScriptAccess="sameDomain"	allowFullScreen="true" type="application/x-shockwave-flash"' +
					'pluginspage="http://www.adobe.com/go/getflashplayer"> </embed></object>'
		$('div.tabs li:eq(1)').click(function () {
			if(!$('div.inn-vr object').length) {
				$('div.inn-vr').append(vrFlash);
			}
		});
	}
});

function convertToCamelCase(s) {
     var inputStrArr = new Array();
     var x;
     inputStrArr = s.split(' ');
     var newStr = "";
     for (x in inputStrArr) {
	  var tempWord = inputStrArr[x];
	  if (x == 0) {
	    newStr += tempWord.substring(0,1).toUpperCase() + tempWord.substring(1,tempWord.length);
	  } else {
	    newStr += " " +tempWord.substring(0,1).toUpperCase() + tempWord.substring(1,tempWord.length);
	  }					
     }
     return newStr;  
};

function initialize() {
	if(document.getElementById("prop-map") && GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("prop-map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(39, -99.5), 3);
		var icon = new GIcon();
		icon.image = "../images/rri-loc_icon.png";
		icon.iconSize = new GSize(37, 30);
		icon.iconAnchor = new GPoint(12, 26);
		icon.infoWindowAnchor = new GPoint(10, 4);
		
		/*var stData = StateCount.split("|");
		var stCode = stData[0].split(",");
		var stNum = stData[1].split(",");
		
		function drawMap() {
			for(var x=0;x < 50;x++) {
				if(typeof stateLocs[stCode[x]] == "object") {
					var lat = stateLocs[stCode[x]][0];
					var lon = stateLocs[stCode[x]][1];
					
					icon.image = "../images/rri-loc_icon-" + stNum[x] + ".png";
					var marker = new GMarker(new GLatLng(lat, lon), { icon: icon, title: stCode[x] });
					GEvent.addListener(marker, "click", function() {
						map.clearOverlays();
						map.setCenter(new GLatLng(this.getLatLng().y, this.getLatLng().x), 6);
						icon.image = "../images/rri-loc_icon.png";
						
						for(var y=0;y < rriName.length;y++) {
							if(rriSt[y] == this.getTitle()) {
								var submarker = new GMarker(new GLatLng(rriLat[y], rriLon[y]), { icon: icon, title: y });
								GEvent.addListener(submarker, "click", function() {
									var dex = parseFloat(this.getTitle());
									this.openInfoWindowHtml('<div style="height: 80px"><h2><a href="'+ rriUrl[dex] +'">'+ rriName[dex] +'</a></h2><p class="addy">'+ rriLoc[dex] +'<br />'+ rriPh[dex] +'</p><a class="url" href="'+ rriUrl[dex] +'">More about this Red Roof Inn &raquo;</a></div>');
								});
								map.addOverlay(submarker);
							}
						}
					});
					
					map.addOverlay(marker);
				}
			}
		}*/
		//drawMap();
		/*GEvent.addListener(map, "zoomend", function(oldz,newz) {
			if(newz < 5) {
				map.setCenter(new GLatLng(39.5, -95.7));
				map.clearOverlays();
				drawMap();
			}
		});*/
	}
	if(document.getElementById("map-prop") && GBrowserIsCompatible()) {
			var lat = $('.mna .mapperwrapper .rri-lat').text();
			var lon = $('.mna .mapperwrapper .rri-lon').text();
			map = new GMap2(document.getElementById("map-prop"));
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(lat, lon), 10);
			
			var icon = new GIcon();
			icon.image = "../images/rri-loc_icon.png";
			icon.iconSize = new GSize(37, 30);
			icon.iconAnchor = new GPoint(12, 26);
			icon.infoWindowAnchor = new GPoint(10, 4);
			var marker = new GMarker(new GLatLng(lat, lon), icon);
			map.addOverlay(marker);
	}
}