function createVideo(videoSource, videoWidth, videoHeight) {
	if (browserRatio > 1.3333) {
		videoHeight = browserHeight;
		videoWidth = browserHeight * 1.3333;
	} else {
		videoWidth = browserWidth;
		videoHeight = browserWidth / 1.3333;
	}
	var flashvars = {
		file: videoSource,
		screencolor: "000000",
		skin: base+"swf/skin.swf",
		controlbar: "over",
		autostart: "true",
		bufferlength: "3"
	};
	var params = {
		allowfullscreen: "true", 
		allowscriptaccess: "always",
		bgcolor: "#000"
	};
	var attributes = {
		id:"player",  
		name:"player"
	};
	swfobject.embedSWF(base+"swf/player.swf", "video", videoWidth, videoHeight, "9.0.115", false, flashvars, params, attributes);
	$imageHolder.animate({width:videoWidth, height: videoHeight});
}
function sizeAdjust() {
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	if (windowWidth < 700) { //Min width
		windowWidth = 700;
	}
	if (windowHeight < 600) { //Min height
		windowHeight = 600;
	}
	browserWidth = windowWidth - 280; //Minus sidebar
	browserHeight = windowHeight - 175; //Minus caption
	browserRatio = browserWidth / browserHeight;
	$projectImage = $('img', $imageHolder);
	imageRatio = $projectImage.width() / $projectImage.height();
	if (browserRatio > imageRatio) {
		$projectImage.height(browserHeight);
		$projectImage.width(Math.floor(browserHeight * imageRatio));
	} else {
		$projectImage.width(browserWidth);
		$projectImage.height(Math.floor(browserWidth / imageRatio));
	}	
}
function loadImage(image) {
	$imageHolder.height($imageHolder.height());
	$imageHolder.width($imageHolder.width());
	$imageHolder.load(base+'php/getimage.php?i='+escape(image), function() {
		if ($('#video').length === 0) {
			$('img', $imageHolder).fadeIn();
			sizeAdjust();
			$imageHolder.animate({width:$projectImage.width(), height: $projectImage.height()}, function() {
				$(this).css({width:'auto', height: 'auto'});
			});
		} else {
			createVideo($('#video').attr('class'));
		}
	});
}
function portfolio() {
	$content = $('#content');
	$imageHolder = $('#imageHolder');
	//Resize Images
	sizeAdjust();
	$(window).resize(function() { sizeAdjust(); });
	//Create Video 
	if ($('#video').length > 0) {
		createVideo("../" + $('#video').attr('class'));
	}
	//Preload Images
	$('#images .bullet').each(function() {
		$.cacheImage($(this).attr('href'));
	});
	//Image Navigation
	$('#images .bullet').click(function() {
		this.blur();
		$('#images li a').removeClass('active');
		$(this).addClass('active');
		image = $(this).attr('href');
		loadImage(image);
		return false;
	});
	$('#next').click(function() {
		this.blur();
		$next = $('#images .active').parent().next().children();
		if (!$next.hasClass('bullet')) {
			$next = $('#images li .bullet:first');
		}
		$('#images li a').removeClass('active');
		$next.addClass('active');
		image = $next.attr('href');
		loadImage(image);
		return false;
	});
	$('#previous').click(function() {
		this.blur();
		$previous = $('#images .active').parent().prev().children();
		if (!$previous.hasClass('bullet')) {
			$previous = $('#images li .bullet:last');
		}
		$('#images li a').removeClass('active');
		$previous.addClass('active');
		image = $previous.attr('href');
		loadImage(image);
		return false;
	});
}
$(document).ready(function() {portfolio();});