function createVideo(videoSource) {
	var flashvars = {
		file: videoSource,
		screencolor: "999999",
		skin: "skin.swf",
		controlbar: "over",
		autostart: "true",
		bufferlength: "3"
	};

	var params = {
		allowfullscreen: "true", 
		allowscriptaccess: "always",
		bgcolor: "#999"
	};

	var attributes = {
		id:"player",  
		name:"player"
	};
	swfobject.embedSWF("player.swf", "video", "100%", "100%", "9.0.115", false, flashvars, params, attributes);
}
function loadingAnimate() {
	if (loadingFrame > 2) {
		loadingFrame = 0;
	}
	$('.projects li .loading').css("backgroundPosition","0 "+ (loadingFrame * -30) + "px");
	loadingFrame++;
	if (loadingEnabled == true) {
		loadingTimer = setTimeout("loadingAnimate();", 250);
	}
}
function loadProject(hash) {
	if (hash === "") {
		// Close Project
		active = $('.projects li a.active');
		if (active.length > 0) {
			$('.activeProject #overlay, #close').hide();
			thisOffset = $(active).parent().offset();
			parentOffset = $(active).parent().parent().offset();
			$('.activeProject').animate({'width':$(active).parent().width(), 'height': $(active).parent().height(), 'top': thisOffset.top - parentOffset.top, 'left': thisOffset.left - parentOffset.left, 'opacity': 0}, function() {
				$(this).hide();
			});
			$(active).removeClass('active');
		}
	} else {
		hash = hash.split("/");
		project = hash[0];
		image = hash[1];
		if (typeof(image) != "undefined") {
			// Load Image
			active = $('.projects li a#' + project);
			$(active).addClass('active');
			overlayTime = 15;
			if (typeof(overlayTimer) != "undefined") {clearTimeout(overlayTimer);}	
			overlayTimer = setTimeout("hideOverlay();", 200);
			if (typeof(overlayHover) == "undefined") {
				overlayHover = 0;
			}
			$('.activeProject').show().css({'width': '100%', 'height': '100%', 'top': 0, 'left': 0, 'opacity': 1});
			if ($('.activeProject #overlay').length == 0) {
				//Load project and image
				$(".activeProject").load("project.php?p=" + project + "&i=" + image, {}, function() {
					//Preload Images
					$('#overlay .imageicon:not(.active)').each(function() {
						$.cacheImage('../projects/' + project + '/' + $(this).attr('id') + '.jpg');
					});
					if ($('#video').length > 0) {
						//Create Video
						videoSource = $('#video').attr('class');
						createVideo(videoSource);
					}
					$('#overlay .images .imageicon img').stop().fadeTo('normal',0.5);
					$('#overlay .images .imageicon.active').removeClass('active');
					$('#overlay .images #' + image).addClass('active');
					$('#overlay .images .imageicon.active img').stop().fadeTo('normal',1);
				});
			} else {
				//Just load image
				$('#overlay .images .imageicon img').stop().fadeTo('normal',0.5);
				$('#overlay .images .imageicon.active').removeClass('active');
				$('#overlay .images #' + image).addClass('active');
				$('#overlay .images .imageicon.active img').stop().fadeTo('normal',1);
				$(".bigimage").load("bigimage.php?p=" + project + "&i=" + image, {}, function() {
					if ($('#video').length > 0) {
						videoSource = $('#video').attr('class');
						createVideo(videoSource);
					}
				});
			}
		} else {
		// Load Project
			if ($('.projects li .loading').length == 0) {
			active = $('.projects li a#' + project);
			$(active).addClass('active');
			$('.projects li .active').parent().append('<span class="loading"></span>');
			jQuery.each(jQuery.browser, function(i) {
  				if($.browser.msie) {
					$('.projects li .loading').show();
				} else {
					$('.projects li .loading').fadeIn();
				}
			});
			loadingFrame = 0;
			loadingEnabled = true;
			loadingAnimate();
			thisOffset = $(active).parent().offset();
			parentOffset = $(active).parent().parent().offset();
			$(".activeProject").load("project.php?p=" + project, {}, function() {
				$('.activeProject').css({'width':$(active).parent().width(), 'height': $(active).parent().height(), 'top': thisOffset.top - parentOffset.top, 'left': thisOffset.left - parentOffset.left, 'opacity': 0});
				$('.activeProject #overlay, #close').hide();
				$('#overlay .imageicon:first').addClass('active');
				$('#overlay .imageicon:not(:first) img').css("opacity", 0.5);
				//Preload Images
				$('#overlay .imageicon:not(.active)').each(function() {
					$.cacheImage('../projects/' + project + '/' + $(this).attr('id') + '.jpg');
				});
				if ($('#video').length == 0) {
					//Wait for image to load
  					$(".bigimage a img").load(function () {
						overlayTime = 15;
						if (typeof(overlayTimer) != "undefined") {clearTimeout(overlayTimer);}	
  						overlayTimer = setTimeout("hideOverlay();", 200);
						$('.activeProject').animate({'width': '100%', 'height': '100%', 'top': 0, 'left': 0, 'opacity': 1},"normal", function() {
							jQuery.each(jQuery.browser, function(i) {
  								if($.browser.msie) {
									$('#overlay, #close').show();
								} else {
									$('#overlay, #close').fadeIn();
								}
							});
							overlayHover = 0;
							loadingEnabled = false;
							$('.projects li .loading').remove();
							$('.projects li').css('opacity',0.75);
						});
					});
				} else {
					//Creat video
					videoSource = $('#video').attr('class');
					createVideo(videoSource);
					overlayTime = 15;
					if (typeof(overlayTimer) != "undefined") {clearTimeout(overlayTimer);}	
					overlayTimer = setTimeout("hideOverlay();", 200);
					$('.activeProject').animate({'width': '100%', 'height': '100%', 'top': 0, 'left': 0, 'opacity': 1}, function() {
						jQuery.each(jQuery.browser, function(i) {
  							if($.browser.msie) {
								$('#overlay, #close').show();
							} else {
								$('#overlay, #close').fadeIn();
							}
						});
						overlayHover = 0;
						loadingEnabled = false;
						$('.projects li .loading').remove();
						$('.projects li').css('opacity',0.75);
					});
				}
			});
			}
		}
	}
}
function sizeAdjust() {
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	if (windowWidth < 1000) {
		windowWidth = 1000;
	}
	if (windowHeight < 550) {
		windowHeight = 550;
	}
	browserWidth = windowWidth - 245;
	browserHeight = windowHeight - 30;
	if (browserWidth / browserHeight > 1.5) {
		contentHeight = browserHeight;
		contentWidth = Math.floor(contentHeight * 1.5);
		thumbSize = Math.floor((browserHeight - 12) / 4);
	} else {
		contentWidth = browserWidth;
		contentHeight = Math.floor(contentWidth / 1.5);
		thumbSize = Math.floor((browserWidth - 20) / 6);
	}
	$('.projects li').width(thumbSize);
	$('.projects li').height(thumbSize);
	$('.content').width(thumbSize * 6 + 20);
	$('.content').height(thumbSize * 4 + 12);
}
function hideOverlay() {
	//Count down
	overlayTime--;
	//If counter reached zero and mouse isn't hovering
	if (overlayTime == 0 && overlayHover != 1) {
		jQuery.each(jQuery.browser, function(i) {
  			if($.browser.msie) {
				$('#overlay, #close').hide();
			} else {
				$('#overlay, #close').fadeOut(1000);
			}
		});
	}
	overlayTimer = setTimeout("hideOverlay();", 200);	
}
function interact() {
	//$(document).pngFix(); 
	//Initialise history
	$.historyInit(loadProject);
	jQuery.each(jQuery.browser, function(i) {
  		if($.browser.msie) {
			$('.projects li').css('opacity',0.75).hover(function() {
					$(this).stop().fadeTo('normal',1);
				}, function() {
					$(this).stop().fadeTo('normal',0.75);
			});
		} else {
			$('.projects li').css("opacity","0");
			$('.projects li a img').each(function() {
				$(this).attr('src','').load(function() {
					$(this).parent().parent().fadeTo('normal',0.75).hover(function() {
						$(this).stop().fadeTo('normal',1);
					}, function() {
						$(this).stop().fadeTo('normal',0.75);
					});
				}).attr('src','../projects/' + $(this).parent().attr('id') + '/thumbnail.jpg');
			});
  		}
	});
	var overlayTimer;
	sizeAdjust();
	$(window).resize(function(){
		sizeAdjust();
    });
	$('.projects li a').click(function(){
		hash = $(this).attr('id');
		$.historyLoad(hash);
		return false;
	});
	$('.activeProject .bigimage a, #close').live("click", function() {
		$.historyLoad("");
		return false;
	});
	$('#overlay .imageicon a').live("click", function() {
  		this.blur();
		hash = $('.projects li a.active').attr('id') + "/" + $(this).parent().attr('id');
		$.historyLoad(hash);
		return false;
	});
	$('#overlay .previous').live("click", function() {
		this.blur();
		previous = $('#overlay .imageicon.active').prev();
		if (previous.length == 0) {
			previous = $('#overlay .imageicon:last');
		}
		hash = $('.projects li a.active').attr('id') + "/" + $(previous).attr('id');
		$.historyLoad(hash);
		return false;
	});
	$('#overlay .next').live("click", function() {
  		this.blur();
		next = $('#overlay .imageicon.active').next();
		if (next.length == 0) {
			next = $('#overlay .imageicon:first');
		}								 
		hash = $('.projects li a.active').attr('id') + "/" + $(next).attr('id');
		$.historyLoad(hash);
		return false;
	});
	$('#overlay .imageicon').live("mouseover", function() {
		$('img', this).stop().fadeTo('normal',1);
	});
	$('#overlay .imageicon').live("mouseout", function() {
		if (!$(this).hasClass('active')) {
			$('img', this).stop().fadeTo('normal',0.5);
		}
	});
	$('#overlay, #close').live("mouseover", function () {
     	overlayHover = 1;
	});
	$('#overlay, #close').live("mouseout", function () {
    	overlayHover = 0;
    });
	$('.activeProject').mousemove(function(){
		overlayTime = 10;
		jQuery.each(jQuery.browser, function(i) {
  			if($.browser.msie) {
				$('#overlay, #close').show();
			} else {
				$('#overlay, #close').fadeIn();
			}
		});
	});
}
$(document).ready(function() {interact();});