var crownewidth;
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
  		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  		x=x.replace(/^\s+|\s+$/g,"");
  		if (x==c_name) {
  			return unescape(y);
  		}
  	}
}
$(document).ready(function() {
	$('#cycle').cycle({ 
		timeout: 6500 
	});
	if ($('#expand').length > 0) {
		var elang = "";
		var fixHeight = $("#content").height();
		fixHeight = fixHeight + "px";
		$("#content").css("height", fixHeight);
		var fixMHeight = $("#main").height();
		fixMHeight = fixMHeight + "px";
		$("#main").css("height", fixMHeight);
		var fixBHeight = $("#main .box-large").height();
		var fixEHeight = fixBHeight;
		fixEHeight = fixEHeight + 69;
		fixEHeight = fixEHeight + "px";
		fixBHeight = fixBHeight + "px";
		$("#main .box-large").css({
			height: fixBHeight,
			position: "absolute",
			bottom: 0
		});
		$('#expand').css("bottom", fixEHeight);
		$("#expand").click(function(){
			if ($(this).hasClass("expand-open")) {
				$('#main .box-large').animate({
					height: 0,
					opacity: 0
				}, 300, function() {
					$('#expand').removeClass("expand-open");
					$('#expand').addClass("expand-closed");
					$('#expand').fadeOut(600, function(){
						elang = $(this).find("img").attr("src").replace(window.tdir + "/images/expand", "").replace(".png", "");
						$(this).find("img").attr("src", window.tdir + "/images/expand2.png").parent().fadeIn(600);
					});
				});
				$('#expand').animate({
					bottom: "23px"
				}, 300, function() {});
				return false;
			} else {
				$('#main .box-large').animate({
					height: fixBHeight,
					opacity: 1
				}, 300, function() {
					$('#expand').removeClass("expand-closed");
					$('#expand').addClass("expand-open");
					$('#expand').fadeOut(600, function(){
						elang = $(this).find("img").attr("src").replace(window.tdir + "/images/expand2", "").replace(".png", "");
						$(this).find("img").attr("src", window.tdir + "/images/expand.png").parent().fadeIn(600);
					})
				});
				$('#expand').animate({
					bottom: fixEHeight
				}, 300, function() {});
				return false;
			}
		});
	}
	
	var customurl=getCookie("customurl");
	var page_variation=getCookie("variation");
	var tempDate;
	var checkInDate;
	var checkInMonthYear;
	var checkOutDate;
	var checkOutMonthYear;
	var numberOfAdults;
	var numberOfChildren;
	var numberOfRooms;
	jQuery('#checkin').datepicker();
	jQuery('#checkout').datepicker();
	jQuery('#checkin').change(function() {
		var date2 = $('#checkin').datepicker('getDate', '+1d'); 
		date2.setDate(date2.getDate() + 1); 
		$('#checkout').datepicker('setDate', date2);
	});
	$('#rates a').click(function(){
		if ((($('#checkin').val() == undefined) || ($('#checkin').val() == '')) && (($('#checkout').val() == undefined) || ($('#checkout').val() == ''))) {
			$('#checkin').css("border", "1px solid red");
			$('#checkout').css("border", "1px solid red");
		} else if (($('#checkin').val() == undefined) || ($('#checkin').val() == '')) {
			$('#checkin').css("border", "1px solid red");
			$('#checkout').css("border", "1px solid #95a7ba");
		} else if (($('#checkout').val() == undefined) || ($('#checkout').val() == '')) {
			$('#checkin').css("border", "1px solid #95a7ba");
			$('#checkout').css("border", "1px solid red");
		} else {
			$('#checkin').css("border", "1px solid #95a7ba");
			$('#checkout').css("border", "1px solid #95a7ba");
			/* Check-In Day */
			tempDate = $('#checkin').val().split('/');
			tempDate[1] = tempDate[1] + '';
			if (tempDate[1].length == 1) {
				tempDate[1] = "0" + tempDate[1];
			}
			checkInDate = tempDate[1];
			/* Check-In Month + Year */
			tempDate = $('#checkin').val().split('/');
			tempDate[0]--;
			tempDate[0] = tempDate[0] + '';
			if (tempDate[0].length == 1) {
				tempDate[0] = "0" + tempDate[0];
			}
			checkInMonthYear = tempDate[0] + tempDate[2];
			/* Check-Out Day */
			tempDate = $('#checkout').val().split('/');
			tempDate[1] = tempDate[1] + '';
			if (tempDate[1].length == 1) {
				tempDate[1] = "0" + tempDate[1];
			}
			checkOutDate = tempDate[1];
			/* Check-Out Month + Year */
			tempDate = $('#checkout').val().split('/');
			tempDate[0]--;
			tempDate[0] = tempDate[0] + '';
			if (tempDate[0].length == 1) {
				tempDate[0] = "0" + tempDate[0];
			}
			checkOutMonthYear = tempDate[0] + tempDate[2];
			/* Adults, Kids, Rooms */
			numberOfAdults = $('#adults').val();
			numberOfChildren = $('#children').val();
			numberOfRooms = $('#rooms').val();
			
			if (customurl!=null && customurl!="") {
				//var url = customurl;
				//var url = '/booking.html'
				var url = "http://www.crowneplaza.com/redirect?path=asearch&brandCode=cp&localeCode=en&regionCode=1&hotelCode=NYCMH&rateCode=6CBARC&_PMID=99502222";				
				_gaq.push(['_trackEvent', 'General', 'Click', 'RezBox']);
			} else {
				var url = "http://www.crowneplaza.com/redirect?path=asearch&brandCode=cp&localeCode=en&regionCode=1&hotelCode=NYCMH&rateCode=6CBARC&_PMID=99502222";
				url = url + '&checkInDate=' + checkInDate + '&checkInMonthYear=' + checkInMonthYear + '&checkOutDate=' + checkOutDate + '&checkOutMonthYear=' + checkOutMonthYear + '&numberOfAdults=' + numberOfAdults + '&numberOfChildren=' + numberOfChildren + '&numberOfRooms=' + numberOfRooms;
			}
			
			//window.location = url;
			window.open(url,'_blank');
		}
		return false;
	});

	$("#language").click(function(){
		if ($(this).hasClass("language-visible")) {
			$(this).find("ul").fadeOut();
			$(this).removeClass("language-visible");
		} else {
			$(this).addClass("language-visible");
			$(this).find("ul").fadeIn();
		}
	});
	$("#language a").click(function(){
		var lang = $(this).parent().attr("class");
		lang = lang.replace("language-", "");
		//console.log(crownewidth);
	});
	crownewidth = $(window).width();
	var page_v = getCookie("variation");
	//console.log(page_v);
	//console.log(crownewidth);
	if ((crownewidth <= 1150) && (page_v !== "right")) {
		$("#nav ul.nav").css("right", "20px");
		$("#nav ul.nav li.menu-item").css("right", "280px");
	} else if (crownewidth <= 1150 && (page_v == "right")) {
		$("#nav ul.nav").css("left", "20px");
		$("#nav ul.nav li.menu-item").css("right", "auto");
	} else {
		$("#nav ul.nav").css("left", "50%");
		$("#nav ul.nav li.menu-item").css("right", "50%");
	}

	$("li#menu-item-1795 a, a.slideshow-link, a[title=Slideshow]").fancybox({
		"autoDimensions": false,
		"width": 700,
		"height": 500,
		"type": "iframe",
		"scrolling": "no"
	});
	$('a#video-tour').fancybox({
		autoDimensions : false,
		margin: 0,
		padding: 0,
		scrolling: "no",
		width: 500, 
	      height: 280,
	      overlayShow: true,
	      overlayOpacity: 0.0,
	      onStart: function(){
	      	$('#video').show();
	      },
	      onComplete: function(){
	      	$('#fancybox-wrap').css({'top':'120px'});
	      	_V_("_html5_api").play();

	      },
	      onClosed: function() {
	      	$('#video').parent().hide();
	      	_V_("_html5_api").pause();

	      }
	}).click( function(){
		$('#video').show();
	});;
});

$(window).resize(function() {
	crownewidth = $(window).width();
	var page_v = getCookie("variation");
	//console.log(page_v);
	//console.log(crownewidth);
	if ((crownewidth <= 1150) && (page_v !== "right")) {
		$("#nav ul.nav").css("right", "20px");
		$("#nav ul.nav li.menu-item").css("right", "280px");
	} else if ((crownewidth <= 1150) && (page_v == "right")) {
		$("#nav ul.nav").css("left", "20px");
		$("#nav ul.nav li.menu-item").css("right", "auto");
	} else {
		$("#nav ul.nav").css("left", "50%");
		$("#nav ul.nav li.menu-item").css("right", "50%");
	}
});

//google analytics for rez widget click
var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-7044295-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


