/* Landmark JS */
/* Author: pumkin.co.uk */
/* Common functions and global vars */

var bookingLink = "https://gc.synxis.com/rez.aspx?Hotel=28827&Chain=11829&locale=en-GB";
//var reviewsXML = "xml/reviews-test.xml"; //"http://ws.reviewpro.com/publishedReviews/136/4e0dc49ed5b1071b39f43db5eb8dd0cdd6ac1ba9";
reviewsXML ="http://ws.reviewpro.com/publishedReviews/136/4e0dc49ed5b1071b39f43db5eb8dd0cdd6ac1ba9";
//*** NAVIGATION ***//

$(document).ready(function() {
						   
	// Use JS to remove css hover styles and replace with js
	$('.tier1, .tier2').removeClass('hover');
	//$('.tier1').mouseover
	
	$('#nav').find('li').mouseenter(function(){
		//
		if ($(this).children('ul').length) {
			//
			// fade in next nav item
			$(this).find('ul').first().stop();
			$(this).find('ul').first().css('opacity',0);
			$(this).find('ul').first().css('display','inline-block');
			$(this).find('ul').first().fadeTo(400,0.95);	// set transparency of menu
			//
			//$(this).find('ul.tier2').first().css('backgroundPosition', '0 '+($(this).position().top-$(this).parent('ul').position().top)+'px');
			//$(this).find('ul.tier3').first().css('backgroundPosition', '0 '+($(this).position().top)+'px');
			
			$(this).children('li>ul').first().css('backgroundPosition', '0 '+($(this).position().top-($(this).parent('ul').position().top-3))+'px');
			$(this).children('li>ul>li>ul').last().css('backgroundPosition', '0 '+($(this).position().top+8)+'px');
			//
		}
	});
	
	$('#nav').find('li').mouseleave(function(){
		//
		// fade out nav item
		$(this).find('ul').first().stop();
		$(this).find('ul').first().fadeTo(400,0,function() {
			$(this).css('display','none');											  
		});
	});
});



//*** REVIEWS XML ***//

function loadXML(file, onComplete, params, onFail) {
	//alert('loadXML:'+file);
	//
	if (!params) {
		params = new Array();
	}
	var data = 'url=' + file;
	
	$.ajax({
		type: "POST",
		url: '/uploads/xml.php',
		data: data,
		dataType: "xml",
		success: function(xml) {
			params.unshift(xml);
			onComplete.apply(this, params);
		},
		error: function() {
			//alert('load error');
			onFail.call(this);
		}
	});
}

function shuffle(array) {
    var tmp, current, top = array.length;

    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array;
}

function listReviewsBrief(xml, target, limit) {
	//	
	//alert('listReviews');
	var reviews = new Array();
	//
	$(xml).find("review").each(function() {
		//	
		if (parseInt($(this).find("overall").text()) > 4) {
			//
			reviews.push($(this));		
		}
	});
	//
	//reviews = shuffle(reviews);
	var counter = 0;
	//
	$(reviews).each(function() {
		//	
		if (counter<limit) {
			target.append('<p class="quote">&ldquo;'+$(this).find("title").text()+'&rdquo;</p>');
        	target.append('<p class="small right">by '+$(this).find('author').text()+'<br />on '+$(this).find('published_date').text()+'</p>');
			//if (counter < limit-1) {
				target.append('<div class="divider dotted light thin"></div>');	
			//}
			//
			counter++;
		}
	});
};


function listReviewsLong(xml, target, limit) {
	//	
	//alert('listReviews');
	var reviews = new Array();
	//
	$(xml).find("review").each(function() {
		//	
		if (parseInt($(this).find("overall").text()) > 4) {
			//
			reviews.push($(this));		
		}
	});
	//
	//reviews = shuffle(reviews);
	var counter = 0;
	//
	$(xml).find("review").each(function() {
		//	
		if (counter<limit) {
			target.append('<h4>&ldquo;'+$(this).find("title").text()+'&rdquo;</h4>');
			target.append('<p class="quote">&ldquo;'+$(this).find("text").text()+'&rdquo;</p>');
        	target.append('<p class="small right">by '+$(this).find('author').text()+'<br />on '+$(this).find('published_date').text()+'</p>');
			//
			// add stars
			//
			target.append('<p>');
			var stars = parseInt($(this).find("overall").text());
			for (s=1;s<=5;s++) {
				//
				var cls = s<=stars ? "dark" : "mid";
				target.append('<span class="'+cls+' star">*</span>');
			}
			target.append('<br />');
			//
			target.append('<a href="'+$(this).find("click_url").text()+'" title="Read full review">Read full review &raquo;</a></p>');
			//if (counter < limit-1) {
				target.append('<div class="divider dotted light thin"></div>');	
			//}
			//
			counter++;
		}
	});
};

$(document).ready(function() {
	//
	loadXML(reviewsXML, listReviewsBrief, new Array($('#reviews'), 2));
});


$(document).ready(function() {
	//
	//$('.fancy').fancybox();	
	//
});

//*** BOOKING TOOL ***//

function setMinDepartDate() {
	//
	// reset min date on depart field
	var arriveDate = $('#arrive').val();
	//
	// convert to correct format for JS
	arriveDateParts = arriveDate.split(".");
	minDepartDate = new Date(parseInt(arriveDateParts[2]), parseInt(arriveDateParts[1])-1, parseInt(arriveDateParts[0]));
	//
	// reset min-date on departure picker
	$('#depart').datepicker("option", "minDate", minDepartDate);
}

$(document).ready(function() {
	//
	//alert(new Date(today));
	//
	$('#booking-form input.datepicker').datepicker({
		 dateFormat: 'dd.mm.yy',
		 minDate: today,
		 showOtherMonths: true,
		 selectOtherMonths: true,
		 showButtonPanel: false,
		 beforeShow: function(input) {
			var x = 0; // add offset
			if ($(window).width()>1440) { var x = 200; }
			var y = 20; 
			field = $(input);
			left = field.position().left + x;
			bottom = $('#booking-form').height() + y;
			setTimeout(function(){
				$('#ui-datepicker-div').css({'top':'', 'bottom':bottom + 'px', 'left': left + 'px'});      
			},1);                    
		}
	});
	//
	$('#booking-form input').change(function() {
		//
		setMinDepartDate();
		//
		// Validate entry
		switch ($(this).attr("id")) {
			
			case "arrive" :
			
				break;
				
			case "depart" :
			
				break;	
				
			case "adult" :
			
				break;	
				
			case "child" :
			
				break;	
				
			case "rooms" :
			
				break;
				
			case "promo" :
			
				break;
		}
		//
		$('#search-btn').attr("href", bookingLink+"&start=availresults&"+$('#booking-form').serialize());									 
	});
	
	// if boxes are prefilled, serialize link
	if ($('#booking-form #arrive').val() != '' && $('#booking-form #depart').val() != '') {
		$('#search-btn').attr("href", bookingLink+"&start=availresults&"+$('#booking-form').serialize());	
	}
	
	// add function to next button to revel second line of options
	$('#search-btn').click(function() {
		//
		//alert($('#booking-form').serialize());
		//$(this).attr("href");
	});
});
