var sliderImages, sliderText, currentText, currentImageIndex = -1;
var content_h, content_w;

function advanceSlide() {
	var prevImageIndex = currentImageIndex;
	currentImageIndex++;

	if (currentImageIndex > sliderImages.length - 1) currentImageIndex = 0;

	if (prevImageIndex > -1) {
		$(sliderImages[prevImageIndex]).fadeOut();
	}

	var thisImage = $(sliderImages[currentImageIndex]);
	var oh = thisImage.data('oh'), ow = thisImage.data('ow');
	var ratio = content_h / oh;
	var nh = content_h, nw = Math.floor(ow * ratio);
	thisImage.width(nw).height(nh);

	thisImage.fadeIn(function() {
		currentText.html(sliderText.eq(currentImageIndex).html());

		var nextImageIndex = currentImageIndex + 1;
		if (nextImageIndex > sliderImages.length - 1) nextImageIndex = 0;

		var nextImage = $(sliderImages[nextImageIndex]);
		var nextImageHref = nextImage.attr('href');

		if ((typeof nextImageHref == 'undefined') || (nextImageHref == '')) {
			setTimeout('advanceSlide()', 4000);
		} else {
			nextImage.attr('src', nextImage.attr('href'));
			nextImage.attr('href', '');
			nextImage.load(function() {
				$(this).data('ow', $(this).width());
				$(this).data('oh', $(this).height());
				setTimeout('advanceSlide()', 4000);
			});
		}
	});
}

function resizeSliderWindow() {
	content_h = GetContentHeight(), content_w = GetContentWidth();
	$('#content').css('overflow', 'hidden').height(content_h).width(content_w);
	$('.slider').height(content_h).width(content_w);

	if (currentImageIndex > -1) {
		var thisImage = $(sliderImages[currentImageIndex]);
		var oh = thisImage.data('oh'), ow = thisImage.data('ow');
		var ratio = content_h / oh;
		var nh = content_h, nw = Math.floor(ow * ratio);
		thisImage.width(nw).height(nh);
	}

	$('.black-overlay:visible, .bioblack-overlay:visible').width(content_w).height($('#content').height()).fadeIn();
}

$(function(){
	sliderImages = $('.slider img');
	currentText = $('.director .current');
	sliderText = $('.image-info p');

	sliderImages.hide();
	sliderText.hide();

	resizeSliderWindow();

	advanceSlide();

	$(window).resize(function() {
		clearTimeout('resizeSliderWindow();');
		setTimeout('resizeSliderWindow();', 50);
	});

	$('.overlay').hide();

	setTimeout(function () {
		function AdjustOverlayCloseBtn () {
			var anchor = $('.overlay-head .top-right-anchor');
			var close = $('.overlay-head .close');
			if ( $(window).width() - anchor.offset().left < 0 ) {
				var overlay_left = parseInt($('.overlay').css('left'));
				
				close.css({ 'position' : 'absolute', 'left' : $(window).width() - 202 - overlay_left, 'margin': '15px 0 0 0'});
			} else {
				close.css({ 'position' : 'static', 'margin': '15px 15px 0 0'});
			}
		};

		$('.overlay').center(null, AdjustOverlayCloseBtn);
		$('.overlay').show(1000, AdjustOverlayCloseBtn);
		var mw = 330;
		var mh = 422;
		$('.overlay img').each(function() {
			var w = $(this).width();
			var h = $(this).height()
			if ( w>mw ) {
				var r =  w/mw;
				w=mw;
				h = h/r;
			};
			if ( h>mh ) {
				var r =  h/mh;
				h=mh;
				w = w/r;
			};
			$(this).width(w).height(h);
			$(this).css({ 'padding-top' : (mh - h) / 2, 'padding-left' : (mw - w) / 2 })
		});
	}, 5000);

	$('.overlay-head .close').click(function () {
		$(this).parents('.overlay').hide();
		return false;
	});
});

jQuery.fn.center = function(loaded, on_resize) {
	var obj = this;
	body_width = parseInt($(window).width() - 172);
	body_height = parseInt($(window).height());
	block_width = parseInt(obj.width());
	block_height = parseInt(obj.height());

	left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
	if (body_width < block_width) { left_position = 0 };
	top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
	if (body_height < block_height) { top_position = 0 };

	if(!loaded) {
		obj.css({'position': 'absolute'});
		obj.css({'left': left_position, 'top': top_position});
		$(window).bind('resize', function() {
			obj.center(!loaded, on_resize);
		});
		$(window).bind('scroll', function() { 
			//obj.center(!loaded);
		});
	} else {
		obj.stop();
		obj.css({'position': 'absolute'});
		obj.animate({'left': left_position, 'top': top_position}, 200, 'linear', on_resize);
	}
}
