function noBefore(date){
	var mydate= new Date()
	mydate.setDate(mydate.getDate()-1);
	if (date < mydate){
		return [false];
	}
		return [true];
}

function noFuture(date){
	var mydate= new Date()
	mydate.setDate(mydate.getDate());
	if (date > mydate){
		return [false];
	}
		return [true];
}

$(function(){

	$('#other_contacts .required').addClass('rneed').removeClass('required');
	$(".fancybox_iframe").fancybox({width: 800, height: 600, type: 'iframe'});
	$(".fancybox_ajax").fancybox({width: 800, height: 600, type: 'ajax'});
	$(".fancybox").fancybox();
	$('#cropbox').Jcrop({ onSelect: updateCoords });
	
	setTimeout(function(){
		
	},2500);

	var root = $("#scroller").scrollable({circular: true,speed: 2000}).autoscroll({autoplay:true,interval:4000});
	var root2 = $("#scroller2nd").scrollable({circular: true,speed: 2000}).autoscroll({autoplay:true,interval:4000});

	window.api = root.data("scrollable");


	$('.slideshow').cycle({fx:'fade',speed:2000,timeout:4000,delay:4000});


	$("#datepicker3, #datepicker4").datepicker({
		dateFormat:'mm/dd/yy',
		showOn: "button",
		buttonImage: "/img/calendar.png",
		beforeShowDay: noBefore
	});
  
	var bookingdates = $("#datepicker1,#datepicker2").datepicker({
		dateFormat:'mm/dd/yy',
		showOn: "button",
		numberOfMonths: 2,
		buttonImage: "/img/calendar.png",
		beforeShow: function(){$('#ui-datepicker-div').maxZIndex();},
		beforeShowDay: noBefore,
		onSelect: function(selectedDate) {
				var option = this.id == "datepicker1" ? "minDate" : "maxDate";
				var	instance = $(this).data("datepicker");
				var date = $.datepicker.parseDate( instance.settings.dateFormat || $.datepicker._defaults.dateFormat,selectedDate, instance.settings );
				bookingdates.not(this).datepicker("option",option,date);
			}
	});

	if ($("body").hasClass("zh")) {
		var alert_message = '請選擇正確的入住日期。';
	}else if ($("body").hasClass("sc")) {
    	var alert_message = '请选择正确的入住日期。';
	}else{
	    var alert_message = 'Please select a valid date range.';
  }

  var review_bookingdates = $("#datepicker5,#datepicker6").datepicker({
		dateFormat:'mm/dd/yy',
		showOn: "button",
		numberOfMonths: 2,	
		buttonImage: "/img/calendar.png",
		beforeShow: function(){$('#ui-datepicker-div').maxZIndex();},
		beforeShowDay: noFuture,
		onSelect: function(selectedDate) {
				var option = this.id == "datepicker5" ? "minDate" : "maxDate";
				var	instance = $(this).data("datepicker");
				var date = $.datepicker.parseDate( instance.settings.dateFormat || $.datepicker._defaults.dateFormat,selectedDate, instance.settings );
				review_bookingdates.not(this).datepicker("option",option,date);
			}
	});
	
	

 
  if ($("body").hasClass("zh") || $("body").hasClass("sc")) {
    $( "#datepicker, #datepicker2, #datepicker3, #datepicker4" ).datepicker("option","dayNamesMin", ['日','一', '二', '三', '四', '五', '六'] );
    $( "#datepicker, #datepicker2, #datepicker3, #datepicker4" ).datepicker("option","monthNames", ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'] );
  }

	$(".accordion, .accordion_download").treeview({
		animated: "fast",
		collapsed: true,
		unique: false,
		persist: "location"
	});

  if ($("body").hasClass("zh")) {
    var name_previous = '上一頁';
    var name_next = '下一頁';
	
  } else if ($("body").hasClass("sc")) {
    var name_previous = '上一页';
    var name_next = '下一页';
	
	  } else {
    var name_previous = 'Previous';
    var name_next = 'Next';
	
  }


  if ($('#s2>*').length>1) {
    $('#s2')
      .after('<div style="margin:13px 0 27px 0; text-align:center"><a id="prev2" class="mRight10" href="#">'+name_previous+'</a> <span id="nav" class="tRight inline-block"></span> <a id="next2" href="#">'+name_next+'</a></div>')
      .cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 0,
        next:   '#next2',
        prev:   '#prev2',
        pager:  '#nav'
    });
  }

  $(".alert").click(function(e) { alert(this.title); return false });

  $('.timepicker').timepicker({rows: 2,
 	defaultTime:'11:00',
    hours: {
        starts: 11,               // First displayed hour
        ends: 21                  // Last displayed hour
    },
    minutes: {
        starts: 0,                // First displayed minute
        ends: 45,                 // Last displayed minute
        interval: 15              // Interval of displayed minutes
    }});
	



function tpStartOnHourShowCallback(hour) {
    var tpEndHour = $('.timepicker_end').timepicker('getHour');
    // Check if proposed hour is prior or equal to selected end time hour
    if (hour <= tpEndHour) { return true; }
    // if hour did not match, it can not be selected
    return false;
}

function tpStartOnMinuteShowCallback(hour, minute) {
    var tpEndHour = $('.timepicker_end').timepicker('getHour');
    var tpEndMinute = $('.timepicker_end').timepicker('getMinute');
    // Check if proposed hour is prior to selected end time hour
    if (hour < tpEndHour) { return true; }
    // Check if proposed hour is equal to selected end time hour and minutes is prior
    if ( (hour == tpEndHour) && (minute < tpEndMinute) ) { return true; }
    // if minute did not match, it can not be selected
    return false;
}

function tpEndOnHourShowCallback(hour) {
    var tpStartHour = $('.timepicker_start').timepicker('getHour');
    // Check if proposed hour is after or equal to selected start time hour
    if (hour >= tpStartHour) { return true; }
    // if hour did not match, it can not be selected
    return false;
}
function tpEndOnMinuteShowCallback(hour, minute) {
    var tpStartHour = $('.timepicker_start').timepicker('getHour');
    var tpStartMinute = $('.timepicker_start').timepicker('getMinute');
    // Check if proposed hour is after selected start time hour
    if (hour > tpStartHour) { return true; }
    // Check if proposed hour is equal to selected start time hour and minutes is after
    if ( (hour == tpStartHour) && (minute > tpStartMinute) ) { return true; }
    // if minute did not match, it can not be selected
    return false;
}

	$('.timepicker_start').timepicker({rows: 2,
 	defaultTime:'00:00',
    hours: {
        starts: 0,               // First displayed hour
        ends: 23                  // Last displayed hour
    },
    minutes: {
        starts: 0,                // First displayed minute
        ends: 45,                 // Last displayed minute
        interval: 15              // Interval of displayed minutes
    },onHourShow: tpStartOnHourShowCallback,
        onMinuteShow: tpStartOnMinuteShowCallback});
		$('.timepicker_end').timepicker({rows: 2,
 	defaultTime:'23:00',
    hours: {
        starts: 0,               // First displayed hour
        ends: 23                  // Last displayed hour
    },
    minutes: {
        starts: 0,                // First displayed minute
        ends: 45,                 // Last displayed minute
        interval: 15              // Interval of displayed minutes
    }, onHourShow: tpEndOnHourShowCallback,
        onMinuteShow: tpEndOnMinuteShowCallback});
   
   
   var count_other_contacts = 0;
  $("#add_other_contacts").click(function(e) {
	
	if ($('#other_contacts>div:visible').length > 2){
		
		e.preventDefault();
		return;
    }
	
	count_other_contacts++;
	if(count_other_contacts == 2){
		$(this).hide();
		count_other_contacts == 0;
	}
	
	
	
    var sub = $(".sub_ordinate:hidden:first")
    sub.show();
    $(".open", sub).val(1);
	$('#other_contacts .rneed').addClass('required').removeClass('rneed');
	
    e.preventDefault();
  });

  $("#rating_box a").click(function(e) {
    var value = this.id.substr(4);
    $("#rating_value").val(value);
    $("#rating_box").removeClass().addClass(this.id);
    e.preventDefault();
  });

  $("#other_contacts .sub_ordinate").hide();

  if ($("body").hasClass("zh")) {
    $('<div class="message bg7abc27 absolute p30 cFFF w158">資料將用於生日推廣活動。</div>').insertAfter('.mouseover_box');
    $('<div class="message bg7abc27 absolute p30 cFFF w158">此計劃只適用於香港登記註冊成立之公司。</div>').insertAfter('.mouseover_box2');
	$('<div class="message bg7abc27 absolute p30 cFFF w158">請上載証件相，相片將印於會員証上。 </div>').insertAfter('.mouseover_box3');
  } else if ($("body").hasClass("sc")) {
    $('<div class="message bg7abc27 absolute p30 cFFF w158">资料将用于生日推广活动。</div>').insertAfter('.mouseover_box');
    $('<div class="message bg7abc27 absolute p30 cFFF w158">此计划只适用于香港登记注册成立之公司。</div>').insertAfter('.mouseover_box2');
	$('<div class="message bg7abc27 absolute p30 cFFF w158">请上载证件相，相片将印於会员证上。 </div>').insertAfter('.mouseover_box3');
  } else {
    $('<div class="message bg7abc27 absolute p30 cFFF w158">Information will be used for birthday campaign</div>').insertAfter('.mouseover_box');
    $('<div class="message bg7abc27 absolute p30 cFFF w158">Only Companies registered or licensed in Hong Kong are eligible for application.</div>').insertAfter('.mouseover_box2');
	$('<div class="message bg7abc27 absolute p30 cFFF w158">Please upload the passport photo for membership card.</div>').insertAfter('.mouseover_box3');
  }

  $(".mouseover_box")
    .hover(function(e) { $(this).next(".message").css("left", e.pageX+30).css("top", e.pageY-20).show() }, function(e) { $(this).next(".message").hide() })
    .click(function(e) { e.preventDefault() })
    .next(".message").hide();

  $(".mouseover_box2")
    .hover(function(e) { $(this).next(".message").css("left", e.pageX+30).css("top", e.pageY-20).show() }, function(e) { $(this).next(".message").hide() })
    .click(function(e) { e.preventDefault() })
    .next(".message").hide();
	
	
	 $(".mouseover_box3")
    .hover(function(e) { $(this).next(".message").css("left", e.pageX+30).css("top", e.pageY-20).show() }, function(e) { $(this).next(".message").hide() })
    .click(function(e) { e.preventDefault() })
    .next(".message").hide();


  
});

function onClosedHandler() {
  $(".thumbnails").each(function() {
    $(this).load(window.location.href + " #" + this.id);
  });
}

function updateCoords(c) {
  $('#ImageX').val(c.x);
  $('#ImageY').val(c.y);
  $('#ImageW').val(c.w);
  $('#ImageH').val(c.h);
}

function checkCoords() {
  if (parseInt($('#ImageW').val()))
    return true;
  alert('Please select a crop region then press submit.');
  return false;
}

function replacer(key, value) {
  if (typeof value === 'number' && !isFinite(value))
    return String(value);
  return value;
}


$.maxZIndex = $.fn.maxZIndex = function(opt) {   
    return $('#s2').css("z-index", 0);
}


	function fn_login_type(val){
	$('#login_type').val(val);
		switch(val){
		case 'onlinebooking':
		$('#resform').attr('action','https://reservations.ihotelier.com/crs/LoyaltyLogin.cfm?HotelID='+HotelID+'&LanguageID='+LanguageID+'&Rooms='+Rooms);
		$('.red_carpet_identifier').hide();
		$('.onlinebooking_identifier').show();
		break;	
		case 'red_carpet':
		$('.onlinebooking_identifier').hide();
		$('.red_carpet_identifier').show();
		//$('#resform').submit();
		break;	
		case 'no':
		$('#resform').attr('action','https://booking.ihotelier.com/istay/istay.jsp?HotelID='+HotelID+'&languageid='+LanguageID+'&Rooms='+Rooms);
		$('.red_carpet_identifier').hide();
		$('.onlinebooking_identifier').hide();
		break;	
		}
	}

	function fn_red_carpet_submit(){
		$('#identifier').val($('#identifier_b').val())
		$('#resform').attr('action','https://booking.ihotelier.com/istay/istay.jsp?identifier='+$('#identifier_b').val()+'&HotelID='+HotelID+'&languageid='+LanguageID+'&Rooms='+Rooms+'&DateIn='+$('#datepicker1').val()+'&DateOut='+$('#datepicker2').val()+'#CORP');
		$('#resform').submit();
	}

	function fn_panda_submit(){
		var val = $('#login_type').val();
		switch(val){
		case 'onlinebooking':
		$('#onlinebooking_email').val($('#onlinebooking_e').val());
		$('#onlinebooking_password').val($('#onlinebooking_p').val());
		$('#resform').attr('action','https://reservations.ihotelier.com/crs/LoyaltyLogin.cfm?turn=1&hotelID='+HotelID+'&languageID='+LanguageID+'&LOGIN_TYPE='+val+'&JOIN=0&DateIn='+$('#datepicker1').val()+'&DateOut='+$('#datepicker2').val());
		$('#resform').submit();
		break;	
		case 'red_carpet':
		$('.red_carpet_identifier').show();
		fn_red_carpet_submit();
		//$('#resform').submit();
		break;	
		case 'no':
		$('#resform').attr('action','https://booking.ihotelier.com/istay/istay.jsp?HotelID='+HotelID+'&languageid='+LanguageID+'&Rooms='+Rooms+'&DateIn='+$('#datepicker1').val()+'&DateOut='+$('#datepicker2').val()+'#CORP');

		$('#resform').submit();
		tb_remove();
		break;	
		}
	}
	
	function fn_ipanda_submit(){
		var val = $('#login_type').val();
		$('#onlinebooking_email').val($('#onlinebooking_e').val());
		$('#onlinebooking_password').val($('#onlinebooking_p').val());
		$('#resform').attr('action','https://reservations.ihotelier.com/crs/LoyaltyLogin.cfm?turn=1&hotelID='+HotelID+'&languageID='+LanguageID+'&LOGIN_TYPE='+val+'&JOIN=0&DateIn='+$('#datepicker1').val()+'&DateOut='+$('#datepicker2').val());
		$('#resform').submit();
	}
	
	function fn_ipanda_submit_logined(){
		var val = $('#login_type').val();
		
		$('#resform').attr('action','https://reservations.ihotelier.com/crs/LoyaltyLogin.cfm?turn=1&hotelID='+HotelID+'&languageID='+LanguageID+'&LOGIN_TYPE='+val+'&JOIN=0&DateIn='+$('#datepicker1').val()+'&DateOut='+$('#datepicker2').val());
		$('#resform').submit();
	}
