var cols = 0;
var curr_col = 0;

$(document).ready(
	function()
	{
                cols = $('#media_previews ul li').length;
                var container_width = (cols * (163 + 13)) + 20;
                $('#media_previews').css('width', container_width + 'px');
		$('#previous a').click(onPrevious);
                $('#next a').click(onNext);
                //$('#media_previews ul li a').click(onThumbClick);
                navCheck();

                $('#media_previews ul li a').fancybox({
                    'autoScale'		: false,
                    'transitionIn'	: 'easeInBack',
                    'transitionOut'	: 'easeOutBack',
                    'type'			: 'iframe',
                    'scrolling'		: 'no',
                    'overlayColor'	: '#111',
                    'overlayOpacity': 0.9
                });

		/* If the close overlay link is clicked */
		$('#close_overlay, .halfopaque_background').click(function() {

			/* Fade out the overlay and hide it all the time */
			$('.dizzy_surround').fadeOut(150);

			/* Return false to disable the default event */
			return false;

		});

	}
);

function onPrevious()
{
        if (curr_col > 0)
        {
            var right_margin = parseInt($('#media_previews ul li').css('margin-right').match(/[+-]?\d+/));
            var new_left = 163 + right_margin + 4; // img width, margin, css fix
            $('#media_previews').animate({left: '+=' + new_left + 'px'}, 222);
            $('#previous').css('display','block');
            curr_col--;
            navCheck();
        }
        return false;
}
function onNext()
{
        if (curr_col < cols - 2)
        {
            var right_margin = parseInt($('#media_previews ul li').css('margin-right').match(/[+-]?\d+/));
            var new_left = 0 - 163 - right_margin - 4; // img width, margin, css fix
            $('#media_previews').animate({left: '+=' + new_left + 'px'}, 222);
            $('#previous').css('display','block');
            curr_col++;
            navCheck();
        }
        return false;
}

function navCheck()
{
        if (curr_col == 0)
        {
            $('#previous').css('display','none');
        }
        else $('#previous').css('display','block');

        if (curr_col == cols - 2)
        {
            $('#next').css('display','none');
        }
        else $('#next').css('display','block');
}

function onThumbClick()
{
        var link = $(this).attr('href');

        return false;
}
