// creating the first Variable for all the tour media gallery
var pageId;
var imagePause;
var imageFade      = 500;
var imagePause     = 4000;
var photoList      = [];
var title          = [];
var subTitle       = [];
var bodyText       = [];
var priceWas       = [];
var priceNow       = [];
var priceCTA       = [];
var learnMoreUrl   = [];
var descriptionUrl = [];
var resmarkID      = [];
var daysAvailable  = [];
var playSlideShow  = true;
var imagePath;
var counter;
var clockText;
var clockStart;
var clockEnds;
var timer;
var counter;
var currentImage;
var totalImages;
var id;
var xmlData;
var dat;
var xmlReady = false;

// function for the next images
function nextImage()
{
	clearTimeout(timer);
	currentImage++
	
	if(currentImage == totalImages)
	{
		currentImage = 0;	
	}
	
	
	
	nextDesc();
	setThumbNail('clear');
	loadImage();
	setThumbNail('update');
	
}

// function for load the images
function loadImage()
{
	var imgSrc = imagePath + photoList[currentImage];
	
		
	if(counter > 3){ counter = 2; $('#containerMG img:first-child').css('z-index','1');  }
	var imgStyle = 'z-index:'+ counter +'; display:none;';
	
	$('<img / >').attr('src', imgSrc).attr('style', imgStyle).load(function(){
			$('#containerMG').prepend($(this));
			$('#containerMG img:first').stop(true,false).fadeIn(imageFade, function()
			{
				$('#containerMG img:not(:first-child, #contNowPrice img)').remove();
				counter++;
				if(playSlideShow)
				{ 
					timer = setTimeout(nextImage, imagePause, nextDesc); 
				}
			});	
	});
}

// function for the previouse imaes
function prevImage()
{
		
}

// get the number with the current images, also this number need to be clickeble, to switch to speciphic images or tours...
function loadNumbers()
{
	var numHTML = '';
	var img = 'num.png';
	
	
	for(i = 0; i < totalImages; i++)
	{
		var sum = i + 1;
		numHTML += '<a href id="'+[i]+'" class="numbi" onclick="return false">'+sum+'</a>';
		
	}	
	
	$('#numb').html(numHTML);
	$('#numb a').each(function(){
		$(this).click(function(){
			clearTimeout(timer);
			if($('#containerMG').find('#book-box').is(':visible')){$('#book-box').hide();}
			setThumbNail('clear');
			currentImage = $(this).attr('id');
			playSlideShow = false;
			setThumbNail('upDate');
			loadImage();
			nextDesc();
			//alert(currentImage)	;
		});	
	});
	
}

function setThumbNail(type)
{
	
	if(type == 'clear')
	{
		$('.current').removeClass('current')
			
	}	
	else
	{
		$('#' + currentImage).addClass('current');	
	}
}

function loadDesc()
{
	$('#tourCont').animate({left:"-=317", }, 1200);
	nextDesc();
}

function nextDesc()
{
	clearTimeout(timer);
	
	var titulo    = title[currentImage];
	var subtitulo = subTitle[currentImage];
	var bod       = bodyText[currentImage];	
	var wasPr     = priceWas[currentImage];
	var curPr     = priceNow[currentImage];
	var learnF    = learnMoreUrl[currentImage];
	var floatL    = descriptionUrl[currentImage];
	var res       = resmarkID[currentImage];
	var days      = daysAvailable[currentImage];
	 
	//adding the clock text info about the day left of the offer
	$('#hurryClock').html(clockText.toUpperCase());
	
	//alert(learnF);
	// adding the title for the island routes
	
	// this are the two link, the first one is the float box and the second one is the offerDetails
	$('#learnMoreMG').attr('href', floatL);
	$('#offerDetailsMG').attr('href', learnF);
	
	$('#titleMG').prepend('<p>' + titulo + '</p>');
	$('#titleMG p:first-child').fadeIn(imageFade, function(){
		$('#titleMG p:not(:first-child)').remove();	
	});
	
	// line divider
	$('#dividerMG').prepend("<img class='divider-imgMG' src='/mediaGallery/divider-specialTour.gif' />");
	$('#dividerMG img:first-child').fadeIn(imageFade, function(){
		$('#dividerMG p:not(:first-child)').remove();
	});
	
	// this is the subtitle of the 
	$('#subTitleMG').prepend('<p>' + subtitulo + '</p>');
	$('#subTitleMG p:first-child').fadeIn(imageFade, function(){
		$('#subTitleMG p:not(:first-child)').remove();	
	});
	
	// this is the body 
	$('#bodyTextMG').prepend('<p>'+bod+'</p>');
	$('#bodyTextMG p:first-child').fadeIn(imageFade, function(){
		$('#bodyTextMG p:not(:first-child)').remove();	
	});
	
	// put the was price to the tours
	$('#was').prepend('<span>$ '+wasPr+'.00' + '</span>');
	$('#was span:first-child').fadeIn(imageFade, function(){
		$('#was span:not(:first-child)').remove();	
	});
	
	//update the 'add to cart' button ID
	$('#contNowPrice .book-now').attr('id','gall-'+res);
	isAdded(res);
	
	$('#currentPrice').prepend('<p>' +'$' + curPr +'</p>');
	$('#currentPrice p:first-child').fadeIn(imageFade, function(){
		$('#currentPrice p:not(:first-child)').remove();	
	});
	
	$('#lstDaysAvailable').html(days);
}

function isAdded(id) {
	var resmarkID = id,
		itemsArray = $.cookie('ITEMSADDED'),
		imgElem = $('#gall-'+resmarkID),
		imgPath1 = '/images/msg_incart.gif',
		imgPath2 = '/images-specials/btn_booknow.gif';
	
	if(itemsArray != null) {
		itemsArray = itemsArray.split(',');
		result = include(itemsArray,resmarkID);
	} else {
		result = false;
	}
	
	if(result) {
		imgElem.attr('src',imgPath1);
	} else {
		imgElem.attr('src',imgPath2);
	}
}

// Find Value in an Array
function include(arr, obj) {for(var i=0; i<arr.length; i++){if(arr[i] == obj) return true;}}
