/**
 * @author Jeremy Manoto
 */

		

	$(document).ready(function() {
		
		Promo_Init();
		
		NavMore_Init();
	});
	
	/**
	 * 
	 */
	function Promo_Init() {
		
		$(".promo.p1").PromoHover();
		$(".promo.p2").PromoHover();
	}
	
	function NavMore_Init() {
		
		var navMore = $(".nav-more");
		navMore.defaultHeight = navMore.height();
		navMore.defaultPadding = navMore.css("paddingBottom");
		navMore.hide();
		//navMore.height(0).css({paddingBottom: 0});
		$("#header .nav li.more").hover(function() {
			//navMore.stop(true, false).animate({height: navMore.defaultHeight, paddingBottom: navMore.defaultPadding});
			var anchor = $(this).find("a.last");
			anchor.addClass("active");
			navMore.show();
		}, function() {
			var anchor = $(this).find("a.last");
			anchor.removeClass("active");
			navMore.hide();
			//navMore.stop(true, false).animate({height: 0, paddingBottom: 0});
		});
		
	}
	
	$.fn.PromoHover = function(options) {
		var defaults = {
			moreSelector: '.copy-more',
			linkSelector: '.more'
		};
		
		var opts = $.extend({}, defaults, options);
		
		return this.each(function() {
			
			var container = $(this);
			
			var copyMore = container.find(opts.moreSelector);
			
			copyMore.defaultHeight = copyMore.outerHeight({margin: "true"});
			copyMore.defaultPaddingTop = copyMore.css("paddingTop");
			copyMore.defaultPaddingBottom = copyMore.css("paddingBottom");
			
			copyMore.height(0).css({padding: 0})
			
			//Set hover event
			container.hover(function() {
				copyMore.stop(true, false).show().animate({
					height: copyMore.defaultHeight + "px",
					paddingTop: copyMore.defaultPaddingTop,
					paddingBottom: copyMore.defaultPaddingBottom
				}, "normal", "swing")
			}, function() {
				copyMore.stop(true, false).animate({
					height: 0,
					paddingTop: 0,
					paddingBottom: 0
				}, "normal", "swing", function() { $(this).hide(); })
			});
			
			
			//Set click event (if link exists)
			var link = container.find(opts.linkSelector);
			if (link.length > 0) {
				var href = link.attr("href");
				
				container.click(function() {
					window.location.href = href;
				}).addClass("button");
			}
			
		})
	}
	
	/* iHotelier function */
	$(document).ready(function(){
		// device detection, so if someone visits the site from a mobile, and clicks on Stay, 
		// then the booking link is launched in a new window, as opposed to the overlay.
		if (mobile.detect()) {
			$("#iHotelOverlay").removeAttr('rel');
			$("#iHotelOverlay").attr("target", "_blank");
		} 
		
		
		/* Homepage jQuery Overlay */
		$("a[rel^='booking']").prettyPhoto({
			animation_speed: 'fast', /* fast/slow/normal */
			slideshow: false, /* false OR interval time in ms */
			autoplay_slideshow: false, /* true/false */
			opacity: 0.70, /* Value between 0 and 1 */
			show_title: true, /* true/false */
			allow_resize: true, /* Resize the photos bigger than viewport. true/false */
			default_width: 1000,
			default_height: 700,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			horizontal_padding: 0, /* The padding on each side of the picture */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
			overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
			keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){}, /* Called when prettyPhoto is closed */
			ie6_fallback: true,
			iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
			inline_markup: '<div class="pp_inline">{content}</div>',
			custom_markup: '',
			social_tools: false /* html or false to disable */
		});
		
		$('#ihotelCheck').click(function() {
				var href = "https://booking.ihotelier.com/istay/istay.jsp?";
				href += "HotelID=11382";
				href += "&LanguageID=11";
				href += "&Rooms=" + $('#ihotelRoom').val();
				href += "&Length=" + $('#ihotelNight').val();
				href += "&Adults=" + $('#ihotelAdult').val();
				href += "&Children=" + $('#ihotelChildren').val();
				href += "&DateIn=" + $("#bSelectMonth").val() + "/" + $("#bSelectDate").val() + "/" + $("#bSelectYear").val();
				href += "&iframe=true";
				//alert($('#ihotelReserve').attr("href"));
				$('#ihotelReserve').attr("href", href);
				
				// device detection, so if someone visits the site from a mobile, and clicks on Stay, 
				// then the booking link is launched in a new window, as opposed to the overlay.
				if (mobile.detect()) {
					window.open(href);
				}
				else {
					$('#ihotelReserve').click();
				}
		});
		
		
		
		
		var today = new Date();
		var thisYear = today.getFullYear();
		var thisMonth = today.getMonth() + 1;
		var thisDate = today.getDate();
		
		var hrefStart = "https://booking.ihotelier.com/istay/istay.jsp?HotelID=11382&LanguageID=1&Rooms=1&Length=1&Adults=1&Children=0";
		hrefStart += "&DateIn=" + thisMonth + "/" + thisDate + "/" + thisYear;
		hrefStart += "&iframe=true";
		$('#ihotelReserve').attr("href", hrefStart);
		
		for (i = thisYear; i <= thisYear + 5; i++) {
			$('#bSelectYear').append(
				$('<option></option>').val(i).html(i)
			);
		}
		for (i = 1; i <= 31; i++) {
			$('#bSelectDate').append(
				$('<option></option>').val(i).html(i)
			);
		}

		$("#bSelectDate").val(thisDate);
		$("#bSelectYear").val(thisYear);
		$("#bSelectMonth").val(thisMonth);
		setDay();
	});

function setDay() {
	var year = document.getElementById('bSelectYear');
	var month = document.getElementById('bSelectMonth');
	var day = document.getElementById('bSelectDate');
	setDayDrop(year,month,day);
}
function daysInMonth(month,year) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
}
function setDayDrop(dyear, dmonth, dday) {
	var year = dyear.options[dyear.selectedIndex].value;
	var month = dmonth.options[dmonth.selectedIndex].value;
	var day = dday.options[dday.selectedIndex].value;
	var days = (year == ' ' || month == ' ')
			? 31 : daysInMonth(month,year);
	dday.options.length = 0;
	dday.options[dday.options.length] = new Option(' ',' ');
	for (var i = 1; i <= days; i++)
	dday.options[dday.options.length] = new Option(i,i);
	
	$("#bSelectDate").val(day);
	
}
/* END iHotelier function */

var mobile = function(){
		return {
			detect:function(){
				var uagent = navigator.userAgent.toLowerCase(); 
				var list = this.mobiles;
				var ismobile = false;
				for(var d=0;d<list.length;d+=1){
					if(uagent.indexOf(list[d])!=-1){
						ismobile = true;
					}
				}
				return ismobile;
			},
			mobiles:[
				"midp","240x320","blackberry","netfront","nokia","panasonic",
				"portalmmm","sharp","sie-","sonyericsson","symbian",
				"windows ce","benq","mda","mot-","opera mini",
				"philips","pocket pc","sagem","samsung","sda",
				"sgh-","vodafone","xda","palm","iphone",
				"ipod","ipad","android","windows phone" 
			]
		};
	}();
	
