jQuery(document).ready(function() 
{
    preload_index = 5; // 0,1,2,3,4,5
    images_loaded = 0;
    image_list = {};
    counter = 0;

    // If we are on the index page, display the special for 10 seconds and then transition to home page
    if ($('#image img').attr('src') == 'windows/special.jpg' && getUrlVars()['page'] != 'special') 
    {
        setTimeout("$('#image').fadeTo(1000, 0, function(){$('#image').html('<span></span><img class=\"noborder\" src=\"windows/home.jpg\" />').fadeTo(600,1);});", 10000);
    }
    
    $('#boxlinks a').click( function() 
	{
        //var thumb = $(this).children('div.image').children('img').attr('src');
        //var full = thumb.replace(/_thumb/g, '');
        var image = './windows/' + $(this).attr('id') + '.jpg';

        $('#image').fadeTo(200, 0, function() {
            $('#image').html('<span></span><img class="noborder" src="' + image + '" />').fadeTo(600,1);
        });
    });
        
    if(getUrlVars()['page'] == 'gallery')
    {
        jQuery('#mycarousel').jcarousel({
            // Configuration goes here
        });

        $('#mycarousel li').mouseout( function() 
		{
            var image = $(this).children('a').children('img').attr('src');
            var path = image.substr(0, image.lastIndexOf('/')+1);
            var filename = image.substr(image.lastIndexOf('/')+1);
            $(this).children('a').children('img').attr('src', path + 'bw/' + filename);
        }).mouseover(function() {
            $(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('id'));
        });

        $('#mycarousel li').click( function() 
		{
            var thumb = $(this).children('a').children('img').attr('src');
            var full = thumb.replace(/thumb\//g, '');

            $('#image').fadeTo(200, 0, function() 
			{
                $('#image').html('<span></span><img src="' + full + '" />').fadeTo(600,1);
            });
        });
        
        $('.jcarousel-prev').click(function() 
		{
            if (!$(this).hasClass('jcarousel-prev-disabled'))
            {
                preload_index -= 3;
            }
        });
        
        $('.jcarousel-next').click(function() 
		{
            if (!$(this).hasClass('jcarousel-next-disabled'))
            {
                if (preload_index + 3 > (counter - 1)) // There are less than 3 remaining to load
                {
                    for (var i = preload_index + 1; i < counter; i++)
                    {
                        var image = $('<img />').attr('src', image_list[i].filename);
                        image_list[i].loaded = true;
                    }
                }
                else
                {
                    for (var i = preload_index + 1; i < preload_index + 4; i++)
                    {
                        var image = $('<img />').attr('src', image_list[i].filename);
                        image_list[i].loaded = true;
                    }
                }
                
                preload_index += 3;
            }
        });
        
        $('#mycarousel li').each( function() 
		{
            var image = $(this).children('a').children('img').attr('src');
            var path = './gallery/wedding/';
            var filename = image.substr(image.lastIndexOf('/')+1);
            //image_list.push(array(path + image);
            image_list[counter] = {filename: path + filename, loaded: false};
            counter++;
        });
        
        // Load the first 6 images if on the gallery page
        if (counter > 0)
        {
            for (var i = 0; i < 6; i++)
            {
                var image = $('<img />').attr('src', image_list[i].filename);
                image_list[i].loaded = true;
            }
        }
    }

	
});

function getUrlVars() 
{
    var map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) 
    {
        map[key] = value;
    });
    
    return map;
}

