// Default JS Content - Do Not Delete
$(document).ready(function() {
		$("#checkInDate").datepicker({
			minDate:0,
			showAnim: "fadeIn",
			showOn: 'both',
			dateFormat: 'dd-M-yy',
			buttonText: 'Select Check In Date',
			buttonImage: '/i/SITE_110613_14584654_MXO16/templates/btn_cal.gif',
			buttonImageOnly: true,
			onSelect:function(theDate) {
				$("#checkOutDate").datepicker('option','minDate',addDays(new Date(theDate),1))
				if(($("#checkOutDate").datepicker( 'getDate' )===null)||(trim($("#checkOutDate").datepicker( 'getDate' ))=="")||(comparedate($("#checkInDate").datepicker( 'getDate' ),$("#checkOutDate").datepicker( 'getDate' )))){
					$("#checkOutDate").datepicker('setDate' ,addDays(new Date(formatDate(theDate)),1))
				}
			}
		});

		$("#checkOutDate").datepicker({
			minDate:1,
			showAnim: "fadeIn",
			showOn: 'both',
			dateFormat: 'dd-M-yy',
			buttonText: 'Select Check Out Date',
			buttonImage: '/i/SITE_110613_14584654_MXO16/templates/btn_cal.gif',
			buttonImageOnly: true,
			defaultDate: +1,
			gotoCurrent: true

		});
							   
});

function addDays(myDate,days) {
//myDate = starting date, days = no. days to add.
    var temp_date = new Date();
    var i = 0;
    var days_to_add = 0;
    while (i < (days)){
        temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
            i+=1;
        days_to_add += 1;
    }
    return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
}

function comparedate(date1str,date2str){
    var date1 = new Date(date1str);
    var date2 = new Date(date2str);
    if (date1 > date2){
        return true;
    }else if (date1 < date2){
        return false;
    }else{
        return true;
    }
}

function trim(stringToTrim) {
    return stringToTrim.toString().replace(/^\s+|\s+$/g,"");
}

function formatDate(theString) {
	var temp = theString.split('-');
	var monthArray = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	for(var i=0;i<monthArray.length;i++){
		//console.log(monthArray[i])
		if(monthArray[i] == temp[1]){
			tempMonth = i + 1; 
		}
	}
	//console.log(tempMonth + '/' + temp[0] +'/'+ temp[2]);	
	return tempMonth + '/' + temp[0] +'/'+ temp[2]
}

var buffer = false;
var loaded = false;
var thumbnails = true;
var aG = [];
function loadGallery(tn){
aG.push({
	totalW: totalW,
	totalH: totalH,
	path: path
});

if(tn =='true'){
   	thumbnails = true;
   }else{
	thumbnails = false;
   }

$(function(){
	if(loaded) return false;
	loaded = true;
	for(var p in aG){
		var gSlideshow = $('div.slideshow:eq('+p+')')
		gSlideshow.data('mP',p);
		gSlideshow.height(aG[p].totalH);
		$.ajax({
			url: aG[p].path+'myimages.xml',
			dataType: 'xml',
			context: gSlideshow,
			success: function(data){
				var slideshow = $(this);
				var mP = slideshow.data('mP');
				var dataArray = [];
				$(data).find('data').each(function(){
					dataArray.push({img:$(this).attr('img'),imgcaption:$(this).attr('imgcaption'),thumb:$(this).attr('thumb')});
				});
				slideshow.append(
					'<img src="'+aG[mP].path
					+dataArray[0].img
					+'" imagelabel="'
					+dataArray[0].imgcaption
					+'" style="position: absolute;display: none; ">'
				);
				if(thumbnails){
					var thumbs = $('<div class="thumbnails"></div>');
					slideshow.after(thumbs);
					thumbs.append(
						'<img src="'+aG[mP].path
						+dataArray[0].thumb.replace(/_c/,'_bw')
						+'">'
					);
					var nt = $('.thumbnails img');
					nt.addClass('active').attr('src',nt.attr('src').replace(/(_bw|_b|_c)\./,'_c.'));;
					thumbs.before('<div class="descriptionText"></div>');
				}
				var loaded = false;
				dataArray.shift();
				/* As soon as the first image is loaded, we may begin */
				slideshow.find('img:first').load(function(){
					if(loaded) return false;
					loaded = true;
$(window).resize();
					for(var i in dataArray){
						slideshow.append(
							'<img sr'+'c="'+aG[mP].path
							+dataArray[i].img
							+'" imagelabel="'
							+dataArray[i].imgcaption
							+'" style="position: absolute;display: none; ">'
						);
						if(thumbnails){
							thumbs.append(
								'<img src="'+aG[mP].path
								+dataArray[i].thumb.replace(/_c/,'_bw')
								+'">'
							);
						}
					}
					
					if(thumbs){
						thumbs.append('<br style="clear:both;" />');
						$('.thumbnails img').hover(function(){
							var $t = $(this);
							$t.attr('src',$t.attr('src').replace(/(_bw|_b|_c)\./,'_b.'));
						},function(){
							var $t = $(this);
							if($t.hasClass('active'))
								$t.attr('src',$t.attr('src').replace(/(_bw|_b|_c)\./,'_c.'));
							else
								$t.attr('src',$t.attr('src').replace(/(_bw|_b|_c)\./,'_bw.'));
						}).click(function(){
							var $t = $(this);
							if($t.hasClass('active'))
								return false;
							var i = $t.prevAll('img').length;
							pause();
							nextSlide('.slideshow img:eq('+i+')');
						});
					}
					
					/* Variables */
					var transitionTime = 4200;
					
					/* Show First Image / Gallery / Hide Play Button */
					var n = slideshow.find('img:first');
					$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
					n.show();
					if(buffer){
						$('.buffer').css({
							'height':n.height(),
							'width':n.width(),
							'margin-left' : (aG[mP].totalW-n.width())/2,
							'margin-top' : (aG[mP].totalH-n.height())/2
						}).fadeOut(1000,function(){
							n.css('z-index','5');
							$('.buffer').show();
						});
						n.css({
							position : 'absolute',
							'margin-left' : (aG[mP].totalW-$(this).width())/2,
							'margin-top' : (aG[mP].totalH-$(this).height())/2
						});
					}
					
					$('.play').hide();
					var nextSlide = function(next){
						var f = slideshow.find('img:visible');
						if(next){
							var n = $(next);
						}else{
							var n = slideshow.find('img:visible').next('img');
							if(n.length==0) n = slideshow.find('img:first');
						}
						/* Make sure the next image is loaded before proceeding (don't worry, the loop event will re-fire) 
						if(!n[0].complete) return false;*/
						
						if(thumbs){
							var i = n.prevAll('img').length;
							var nt = $('.thumbnails img:eq('+i+')');
							var pt = $('.thumbnails img.active');
							pt.removeClass('active').attr('src',pt.attr('src').replace(/(_bw|_b|_c)\./,'_bw.'));
							nt.addClass('active').attr('src',nt.attr('src').replace(/(_bw|_b|_c)\./,'_c.'));;
						}
						
						/* Buffered Animation */
						if(buffer){
							$('.buffer').css({
								'z-index':'4',
								'height':f.height(),
								'width':f.width(),
								'margin-left' : (aG[mP].totalW-f.width())/2,
								'margin-top' : (aG[mP].totalH-f.height())/2
							});
							f.css('z-index','5').fadeOut(600,function(){
								$('.buffer').animate({
									'height':n.height(),
									'width':n.width(),
									'margin-left' : (aG[mP].totalW-n.width())/2,
									'margin-top' : (aG[mP].totalH-n.height())/2
								},300,'swing',function(){
									n.css({
										position : 'absolute',
										'margin-left' : (aG[mP].totalW-$(this).width())/2,
										'margin-top' : (aG[mP].totalH-$(this).height())/2,
										'z-index' : 3
									}).show();
									/* Alter Text */
									$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
									
									$('.buffer').fadeOut(1000,function(){
										n.css('z-index','5');
										$('.buffer').show();
									});
								});
							});
						}else{
							/* Non Buffered Animation */
							f.css('z-index',5);
							n.css('z-index',4).show();
								f.fadeOut(800,'swing',function(){
									$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
								});
						}
					};
					var looping=true;
					var t = setTimeout(function(){beginLoop();},transitionTime);
					var beginLoop = function(){
						nextSlide();
						t = setTimeout(function(){beginLoop();},transitionTime- -800);
					};
					var play = function(){
						clearTimeout(t);
						$('.play').hide();
						$('.pause').show();
						beginLoop();
					};
					var pause = function(){
						clearTimeout(t);
						$('.play').show();
						$('.pause').hide();
						slideshow.find('img').stop(true,true);
					};
					$('.play').click(play);
					$('.pause').click(pause);
					$('.next').click(function(){nextSlide();play();});
				/* If the image is loaded from cache, the load event doesn't fire, this checks if it's already loaded and the load event missed */
				}).each(function(){
					if(this.complete) $(this).trigger("load");
				});
			}
		});
	}
});
}

