var slideshow = {
    parentDiv: false,
    loaded: 0,
    imagepath: '/images/header/',
    slides: ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg', '6.jpg'],
    images: false,
    current: 0,
    last: 0,
    intervalID: 0,
    init: function()
    {
        slideshow.parentDiv = $('#slides');
        $('#slideImage').remove();
        slideshow.preload();
        if(slideshow.loaded == slideshow.slides.length)
        {
            slideshow.setImages();
            slideshow.intervalID = setInterval('slideshow.animateSlides()', 5000);
        }
    },
    preload: function()
    {
        for(var i=0; i<slideshow.slides.length; i++)
        {
            var preImage = new Image();
            preImage.loaded = slideshow.setLoaded();
            preImage.src = slideshow.imagepath + slideshow.slides[i];
        }
    },
    setLoaded: function()
    {
        if(slideshow.loaded < slideshow.slides.length){slideshow.loaded++;}
    },
    animateSlides: function()
    {
        for(var i=0; i<slideshow.images.length; i++)
        {
			$(slideshow.images[i]).css('display', 'none');
		}
		$(slideshow.images[slideshow.last]).css('display', 'block').css('zIndex', '0');
		$(slideshow.images[slideshow.current]).css('zIndex', '1').fadeIn('slow');

	    if(slideshow.current < (slideshow.images.length - 1))
	    {
			slideshow.current = slideshow.current + 1;
			slideshow.last = slideshow.current - 1;
		}
		else
		{
			slideshow.current = 0;
			slideshow.last = slideshow.slides.length - 1;
		}
    },
    setImages: function()
    {
       for(var i=0; i<slideshow.slides.length; i++)
       {
           var newImage = document.createElement('img');
           newImage.setAttribute('alt', '');
           newImage.setAttribute('width', '840');
           newImage.setAttribute('height', '142');
           $(newImage).css('zIndex', slideshow.slides.length - i)
           .css('position', 'absolute')
           .css('top', '6px')
           .css('left','0')
           .css('display','none');
           newImage.setAttribute('src', slideshow.imagepath + slideshow.slides[i]);
           slideshow.parentDiv.append(newImage);
       }
       slideshow.images = slideshow.parentDiv.find('img');
       slideshow.last = slideshow.images.length - 1;
       $(slideshow.images[slideshow.last]).css('display', 'block').css('zIndex', slideshow.images.length - 1);
	   $(slideshow.images[slideshow.last]).css('zIndex', slideshow.images.length).fadeIn('slow');
    }
}


	function autoTab(original,destination)
	{		
		if (original.getAttribute&&original.value.length>=original.getAttribute("maxlength"))
		{
	         original.value = original.value.slice(0, original.getAttribute("maxlength"));
	         destination.select();
		}
	}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function changeCC(elem)
{
	//var payLater = document.getElementById('payLater');
	//var payLaterRadio = document.getElementById('payLaterRadio');
	var flights = document.getElementById('flights');
	if(elem.options[elem.selectedIndex].value == 'Credit Card')
	{
		flights.style.display = '';
		//if(payLaterRadio.checked === true)
		//{
		//    payLaterRadio.checked = false;
		//}
		//payLater.style.display = 'none';
	}
	else
	{
		flights.style.display = 'none';
		//payLater.style.display = 'inline';
	}
}

$(document).ready(function()
{
    slideshow.init();
});