function preparePage() {	
	//// setup animations
	//if($('#animation_wrapper').length > 0) {
	//	$('#logo').css('width', 0);
	//	$('#animation_wrapper').css('visibility', 'hidden');
	//	$('#header').children('img').css('display', 'none');
	//	$('#animation_wrapper').css('height', 0);
	//	$('#animation_wrapper').css('visibility', 'visible');
	//}
	height();
	Cufon.replace('.cufon');
}

function ajaxRefresh($data) {
		
	var i = $data.length;
	
	while(--i > -1) {
		prepareAjaxLinks($data[i]);
		prepareAjaxForms($data[i]);
		prepareSpamProtection($data[i]);
		preparePortfolio($data[i]);
		setupFlashContent($data[i]);
		//preparePreloads();	
	}
	
	navBarMain();
	height();
	Cufon.refresh();
}

function height() {	
	var $leftColumn = $('#leftColumn');
	var $rightColumn = $('#rightColumn');
	var $newsframe = $('#newsframe');
	var $navtarget = $('#navtarget');
	var $image = $('#dynamicContent');
	
	$leftColumn.height('auto');
	$newsframe.height('auto');
	$navtarget.height('auto');
			
	var rightheight;
		
	if($image.length>0) {
		rightheight = ($navtarget.outerHeight() <= $image.outerHeight()) ? $rightColumn.outerHeight() : ($rightColumn.outerHeight() - $image.outerHeight() + $navtarget.outerHeight() - 7);
	}
	else {
		rightheight = $rightColumn.outerHeight();
	}
		
	// make sure it's not way too short
	rightheight = (rightheight < 420) ? 420 : rightheight
	var leftheight = rightheight - 201; //height of cursorwrapper + hr
		
	$leftColumn.height(rightheight);
	$newsframe.height(leftheight);
	
	if($newsframe.length > 0) {
		if($newsframe[0].scrollUpdate) {
			$newsframe[0].scrollUpdate();
		}
		else {
			CSBfleXcroll('newsframe');
		}
	}	
}

function doMorph() {
	var height = $('#container').outerHeight(true) + $('#footer').outerHeight(true);
	var $logo;
			
	$('#header').children('img').delay(375).fadeIn(0).delay(100).fadeOut(0).delay(100).fadeIn(0).delay(100, function () {
		$logo = $('#logo');
		$logo.find('img').css('display','block');
		$logo.animate({width:434}, 1000, "easeInOutQuart", function () {
			$('#animation_wrapper').animate({height:height}, 1000, "easeInOutQuart", function () {
				$(this).height('auto');
			});
		});
	});
}

function navBarMain() {
	$('.cursor').mouseover(function () {
		var index = $('.cursor').index(this);
		var image = $('.cursorImg').eq(index);
		if(image.attr('id') != 'active') {
			$('#active').removeClass('js_active');
			image.addClass('js_active');
		}
	});
	
	$('.cursor').mouseout(function () {
		var index = $('.cursor').index(this);
		var image = $('.cursorImg').eq(index);
		image.removeClass('js_active');
		$('#active').addClass('js_active');
	});
		
	$('.cursorImg').mouseover(function () {
		if($(this).attr('id') != 'active') {
			$('#active').removeClass('js_active');
			$(this).addClass('js_active');
		}
		var index = $('.cursorImg').index(this);
		$('.cursor').eq(index).addClass('js_active');
	});
	
	$('.cursorImg').mouseout(function () {
		$(this).removeClass('js_active');
		$('#active').addClass('js_active');
		var index = $('.cursorImg').index(this);
		$('.cursor').eq(index).removeClass('js_active');
	});
}

var AjaxPage = {
	doTransition: true,
	inTransitionFunc: null,
	updateFunc: null,
	outTransitionFunc: null,
	currentPage: null,
	linkTarget: null,
	arguments: null,
	refresh: 0,
		
	processRequest: function (hash) {
		this.linkTarget = hash;		
		if(this.currentPage == this.linkTarget) return;
		
		// if we are loading a sub-page then we just want a fragment
		var basepage = (this.currentPage) ? this.currentPage.split('?')[0] : null;
		this.arguments = (this.linkTarget.indexOf(basepage) > -1) ? {action: 'ajaxFragment'} : {action: 'ajax'};
		this.getArguments();
		this.currentPage = this.linkTarget;
		this.inTransition();
	},
	
	getArguments: function () {
		var split = this.linkTarget.split('?');
		if(split[1]) {
			split = split[1].split('&');
			var len = split.length;
			var name;
			var value;
				
			while(--len > -1) {
				name = split[len].split('=');
				value = name[1];
				name = name[0];
				this.arguments[name] = value;
			}	
		}
	},
	
	inTransition: function () {
		if(this.doTransition) {
			if(this.inTransitionFunc) {
				this.inTransitionFunc();
			}
			else {
				this.ajaxFunc();
			}
		}
		else {
			this.ajaxFunc();
		}
	},
	
	ajaxFunc: function () {
		var self = this;
		$.post(self.linkTarget, self.arguments, function(data) {
			self.updateFunc(data);
			self.outTransition();	
		});
	},
		
	outTransition: function () {
		if(this.doTransition) {
			if(this.outTransitionFunc)
				this.outTransitionFunc();
		}
		else {
			this.doTransition = true;
			doMorph();
		}
	}
}

function hashCheck() {
	var url = window.location.href;
	var page = basename(url);
	var base_url = url.substring(0, -page.length);
		
	if(page.length > 0 && page.indexOf('app') < 0) {
		window.location.href = base_url + 'app#' + page;
	}
}

function hashChangeHandler(page_load) {
	if($('#animation_wrapper').length > 0) {
		var willAnimate = (page_load == true) ? false : true;
		
		var hash = (window.location.hash.length > 1) ? window.location.hash.substring(1) : 'home';
				
		if(hash.indexOf('portfolio') > -1 && AjaxPage.currentPage && AjaxPage.currentPage.indexOf('portfolio') > -1) {
			AjaxPage.inTransitionFunc = portfolioInTransition;
			AjaxPage.updateFunc = portfolioUpdateFunc;
			AjaxPage.outTransitionFunc = null;
		}
		else if(hash.indexOf('home') > -1 && AjaxPage.currentPage && AjaxPage.currentPage.indexOf('home') > -1) {
			AjaxPage.inTransitionFunc = blogInTransition;
			AjaxPage.updateFunc = blogUpdateFunc;
			AjaxPage.outTransitionFunc = blogOutTransition;
		}
		else {
			AjaxPage.inTransitionFunc = defaultInTransition;
			AjaxPage.updateFunc = defaultUpdateFunc;
			AjaxPage.outTransitionFunc = defaultOutTransition;
		}
		
		AjaxPage.doTransition = willAnimate;
		AjaxPage.processRequest(hash);	
	}
}

function defaultInTransition() {
	var $nav = $('#cursorWrapper');
	var width = $nav.outerWidth();
	var height = $nav.outerHeight() + 30;
	// by setting the callback in this way we ensure that 'this' points to the correct object in ajaxFunc
	$('#animation_wrapper').animate({'height':height, 'width':width}, 500, "easeInOutQuart", function () {AjaxPage.ajaxFunc.call(AjaxPage)});
}

function defaultUpdateFunc(data) {
	$('#container').html(data);
	ajaxRefresh(new Array($('#container')));
	setupNav();
}

function defaultOutTransition() {
	var width = $('#container').outerWidth();
	var height = $('#container').outerHeight(true) + $('#footer').outerHeight(true);
	$('#animation_wrapper').animate({'height':height, 'width':width}, 500, "easeInOutQuart", function () {
		$(this).height('auto');
	});
}

function portfolioInTransition() {
	$('#loadingDiv').css({visibility:'visible',opacity:0}).animate({opacity:0.75}, 500);
	$('#dynamicContent').animate({opacity:0}, 500);
	$('#navtarget').animate({opacity:0}, 500);
	$('#newsframe').animate({opacity:0}, 500, function () {
		$('#navtarget img').css('opacity', 0);
		AjaxPage.ajaxFunc.call(AjaxPage);
	});
}

function portfolioUpdateFunc(data) {
	var $data = $(data);
	setupNav($data.find('#navtarget'));
	setupMedia($data.find('#dynamicContent'));
	setupText($data.find('#newsframe'));
	
	var id = AjaxPage.arguments.id;
	var $target = (id) ? getPorfolioLinkByPageId(AjaxPage.arguments.id) : $('#portfoliobar a').eq(0);
	boxSwap('./img/'+ $target.children('img').attr('title')+'/square.jpg');
	$('#active_portfolio').removeAttr('id');
	$target.attr('id', 'active_portfolio');
}

function portfolioOutTransition() {
	if(++AjaxPage.refresh == 2) {
		AjaxPage.refresh = 0;
		ajaxRefresh(new Array($('#dynamicContent'), $('#newsframe')));
		$('#loadingDiv').animate({opacity:0}, 500, function () {
			$(this).css('visibility','hidden');
		});
		$('#dynamicContent').animate({opacity:1}, 500);
		$('#newsframe').animate({opacity:1}, 500, function() {
			if(this.style.removeAttribute) {
				this.style.removeAttribute('filter');
			}
		});
		$('#navtarget').animate({opacity:1}, 500);
	}
}

function blogInTransition() {
	$('#rightColumn').animate({opacity:0}, 500, function () {
		AjaxPage.ajaxFunc.call(AjaxPage);
	});
}

function blogUpdateFunc(data) {
	$('#rightColumn').html(data);
}

function blogOutTransition() {
	$('#rightColumn').animate({opacity:1}, 500, "linear", function () {
		if(this.style.removeAttribute) {
			this.style.removeAttribute('filter');
		}
	});
	ajaxRefresh(new Array($('#rightColumn')));
}

function NavImage() {
	this.nav = null;
	this.image = null;
}

NavImage.prototype.load = function (src, href, file, type, width, height, dir) {
	this.image = $('<img></img').css('opacity', 0).attr({width: 67, height:67}).load(this.fadeIn);
	this.nav = $('<a></a>').attr('class','js_nav').append(this.image).click(this.clicked);
	this.update(src, href, file, type, width, height, dir);
}

NavImage.prototype.convert = function ($nav) {
	this.nav = $nav;
	this.image = this.nav.find('img').css('opacity', 0);
		
	if(this.nav.attr('class') != 'js_nav') {
		this.nav.attr('class','js_nav').click(this.clicked);
		this.image.load(this.fadeIn);
	}
}

NavImage.prototype.update = function (src, href, file, type, width, height, dir) {
	this.nav.attr({href: href, 'data-file': file, 'data-type': type, 'data-width': width, 'data-height': height, 'data-dir': dir});
	this.image.attr('src', src);
}

NavImage.prototype.setID = function (id) {
	if(id) {
		this.image.attr('id', id);
	}
	else {
		this.image.removeAttr('id');
	}
}

NavImage.prototype.clicked = function (e) {
	e.preventDefault();
	AjaxPage.refresh++;
	var self = $(this);
	$('#nav_active').removeAttr('id');
	self.find('img').attr('id', 'nav_active');
	var dir = self.attr('data-dir'); var file = self.attr('data-file'); var type = self.attr('data-type'); var w = self.attr('data-width');	var h = self.attr('data-height');
	var source = file+"."+type;
	if(type == 'swf') {
		setSWF(source, w, h, "dynamicContent", $('#content'));
	}
	else {
		setImage(source, w, h, "dynamicContent", $('#content'));
	}
}

NavImage.prototype.getObj = function () {
	return this.nav;
}

NavImage.prototype.fadeIn = function (e) {
	$(e.target).animate({opacity:1}, 500);
}

NavImage.prototype.enable = function (bool) {
	if(bool) {
		this.nav.css('display','block');
	}
	else {
		this.nav.css('display','none');
	}
}

function setupNav(nav) {
	
	var ajaxNav = (nav) ? nav.find('a') : null;
	var ajaxLength = (nav) ? ajaxNav.length : 0;
	var $ajaxNav;
	
	var docNav = $('#navtarget').find('a');
	var poolLength = docNav.length;
	var $docNav;
	var i = poolLength;
	
	var navImage;
	var src, href, file, type, width, height, dir, id;
	
	while(--i > -1) {
		$docNav = $(docNav[i]);
		navImage = new NavImage();
		navImage.convert($docNav);
		docNav[i] = navImage;
		if(nav && i >= ajaxLength) {
			docNav[i].enable(false);	
		}
	}
		
	for(var j=0; j<ajaxLength; ++j) {
		$ajaxNav = $(ajaxNav[j]);
		src = $ajaxNav.find('img').attr('src');
		id = $ajaxNav.find('img').attr('id')
		href = $ajaxNav.attr('href');
		file = $ajaxNav.attr('data-file');
		type = $ajaxNav.attr('data-type');
		width = $ajaxNav.attr('data-width');
		height = $ajaxNav.attr('data-height');
		dir = $ajaxNav.attr('data-dir');
			
		if(j < poolLength) {
			docNav[j].enable(true);
			docNav[j].setID(id);
			docNav[j].update(src, href, file, type, width, height, dir);
		}
		else {
			navImage = new NavImage();
			navImage.load(src, href, file, type, width, height, dir);
			navImage.setID(id);
			$('#navtarget').append(navImage.getObj());
		}
	}
}

function setupText(text) {
	text.css('opacity',0);
	$('#newsframe').replaceWith(text);
	portfolioOutTransition();
}

function setupMedia(media) {
	var source, width, height, type;
	
	if(media.find('#dynamic_swf').length > 0) {
		media = media.find('#dynamic_swf');
		source = media.attr('data');
		type = 'swf';
	}
	else {
		source = media.attr('src');
		type = 'jpg';
	}
	
	width = media.attr('data-width'); height = media.attr('data-height');
	
	if(type == 'swf') {
		setSWF(source, width, height, "dynamicContent", $('#content'));
	}	
	else {
		setImage(source, width, height, "dynamicContent", $('#content'));
	}
}

// maybe this should be separated into loadImage and replaceImage as we don't really need to fire the portfolioOutTransition when just changing the picture
function setImage(source, w, h, target_id, $target) {
	var $media = $('#dynamicContent');
	
	this.showImage = function () {
		// if the current item is flash we need to remove and make an image
		if($media.find('#dynamic_swf').length > 0) {
			swfobject.removeSWF('dynamic_swf');
			$media = $('<img></img>').bind('load', portfolioOutTransition).attr({id:target_id, width:w, height:h}).addClass('js_image').css('opacity',0);
			$target.html($media);
			$media.attr('src', source);
		}
		// otherwise we can just update the src
		else {
			if($media.hasClass('js_image')) {
				$media.attr({id:target_id, width:w, height:h});
				$media.attr('src', source);
			}
			else {
				$media.bind('load', portfolioOutTransition).attr({id:target_id, width:w, height:h}).addClass('js_image');
				$media.attr('src', source);
			}
		}
	}
	
	if($media.length > 0) {
		$media.animate({opacity:0}, 500, this.showImage)
	}
	else {
		this.showImage();
	}
}

function setSWF(source, width, height, id, $target) {
	var $media = $('#dynamicContent');
	var self = this;
	
	this.showSWF = function () {
		var swf_id = 'dynamic_swf';
		var swfDiv = $('<div></div>').attr('id', swf_id);
		var swfContainer = $('<div></div>').attr('id', id).css('opacity',0);
		swfContainer.append(swfDiv);
		$target.html(swfContainer);
				
		var attributes = {
			data: source,
			width: width,
			height: height,
			wmode: 'opaque'
		};
		
		var params = {
			allowscriptaccess: "always",
			allowfullscreen: "true"
		};
					
		swfobject.createSWF(attributes,params,swf_id);
		self.swfcheck = setInterval(self.checkSWF,10);
	}
	
	this.checkSWF = function () {
		if($('#dynamic_swf').outerHeight() != 0) {
			clearInterval(self.swfcheck);
			portfolioOutTransition();
		}
	}
	
	if($media.length > 0) {
		$media.animate({opacity:0}, 500, this.showSWF)
	}
	else {
		this.showSWF();
	}
}

function prepareAjaxLinks($section) {
	if(!$section) $section = $(document);
	$section.find('a.ajax').click(function (e) {
		e.preventDefault();
		$target = $(e.currentTarget);
		var hash = '#' + basename($target.attr('href'));
		window.location.hash = hash;
	});
}

function prepareAjaxForms($section) {
	if(!$section) $section = $(document);
	$section.find('form.ajax').submit(function (e) {
		e.preventDefault();
		
		var $target = $(e.currentTarget);
		var $inputAction = $target.find('input#action');
		var $inputComment = $target.find('#comment');
		var $inputAuthor = $target.find('input#author');
		var $inputCaptcha = $target.find('input#captcha');
		var $error = $target.find('#formError');
		var $submit = $target.find('#formSubmit');
		$submit.attr('disabled', true);
		$error.text('');
		
		if($inputComment.val().length != 0 || $inputAuthor.val().length != 0 && $inputCaptcha.val().length != 0) {
			var target = $target.attr('action');			
			var action = $inputAction.val();
			$inputAction.val(action + 'Ajax');
			var args = $target.serializeArray();
			$inputAction.val(action);
			processFormHandler(target, args);
		}
		else {
			$submit.removeAttr('disabled');
			$error.text('Please fill in all fields');
		}
	});
}

function processFormHandler(target, args) {
	$.post(target, args, function(data) {
		$('#formSubmit').removeAttr('disabled');
		if(data.substr(0,6) == "error:") {
			$('#formError').text(data.substr(6));
		}
		else {
			var ts = new Date().getTime();
			var source = 'captcha?nocache=' + ts;
			$('#captcha_img').attr('src', source);
			$('#captcha').val('');
			$('#comment').val('');
			$('#author').val('');
			$('#new_comment').removeAttr('id');
			$('#commentsContainer').append(data);
			var $new_comment = $('#new_comment');
			var target = $new_comment.find('.comment_container').outerHeight(true);
			$('#leftColumn').addClass('js_scroll_anim');
			$('#newsframe').addClass('js_scroll_anim');
			$('#newsframe_mcontentwrapper').addClass('js_scroll_anim');
			$('#newsframe_scrollwrapper').addClass('js_scroll_anim');
			$('#newsframe_vscrollerbase').addClass('js_scroll_anim');
			$new_comment.animate({height:target},500,"easeInOutQuart");
			$('.js_scroll_anim').animate({height:'+='+target+'px'},500,"easeInOutQuart", function () {
				$(this).removeClass('js_scroll_anim');
				height();
			});
		}	
	});
}

function prepareSpamProtection($section) {
	if(!$section) $section = $(document);
	var name = 'alex';
	var server = 'baseten.co.uk';
	var email = name + '@' + server;
	
	$section.find('.email_link').attr('href', 'mailto:' + email).text(email);
}

/* function to allow easy swfobject replacement for multiple dynamic flash movies
 * replaces divs with class "flash" and desired attributes with correct object tags
 */
function setupFlashContent($section) {
	if(!$section) $section = $(document);
	var flash = $section.find('.flashLoad');
	var len = flash.length;
	
	flash.each(function (i) {
		var flashvars = {};
		var params = {};
		var replace = $(this);
		var src = replace.attr('data');
		var width = replace.attr('width');
		var height = replace.attr('height');
		var id = replace.attr('id');
				
		if(replace.attr('params')) {
			var pArray = replace.attr('params').split('&');
			for(var j=0; j<pArray.length; ++j) {
				var keyValue = pArray[j].split('=');
				params[keyValue[0]] = keyValue[1]; 
			}
		}
		if(replace.attr('flashvars')) {
			var fArray = replace.attr('flashvars').split('&');
			for(var k=0; k<fArray.length; ++k) {
				var keyValue = fArray[k].split('=');
				flashvars[keyValue[0]] = keyValue[1]; 
			}
		}
		swfobject.embedSWF(src, id, width, height, '9.0.0', 'flash/expressInstall.swf', flashvars, params);
	});
}

function getPorfolioLinkByPageId(id) {
	var $target = null;
	var vars;
	var self;
	
	$('#portfoliobar a').each(function () {
		self = $(this);
		vars = URLVariables(self.attr('href'));
		if(vars['id'] == id) {
			$target = self;
			return;
		}
	});
	
	return $target;
}

function preparePortfolio($section) {
	preparePortfolioNav($section);
	prepareAlien($section);
}

//prepares swap img for the portfolio navbar
function preparePortfolioNav($section){
	if(!$section) $section = $(document);
	//this section prepares the main swap img for the three parts of the navbar
	$section.find('#portfoliobar a').each(function () {
		var self = $(this);
		if(self.hasClass('print') || self.hasClass('web') || self.hasClass('other') || self.hasClass('topright')) {			
			self.mouseover(function () {
				var link = $(this);
				if(link.hasClass('print')){var imageswap = 'portfoliobar5_r1_c1'; var classN = 'Print';}
				if(link.hasClass('web') || link.hasClass('topright')){var imageswap = 'portfoliobar5_r4_c1'; var classN = 'Web';}
				if(link.hasClass('other')){var imageswap = 'portfoliobar5_r7_c1'; var classN = 'Other';}
				
				var imageswapUrl = 'img/Portfoliobar/portfolio' + classN + '_f2.gif';
				var image = $(this).children('img');
				if(image.attr('title') != "") {
					boxSwap('img/upload/'+image.attr('title')+'-square.jpg');
				}
				MM_swapImage(imageswap,'',imageswapUrl,1);			
			});
			
			self.mouseout(function () {
				MM_swapImgRestore();
				if($(this).children('img').attr('title') != "") {
					boxSwapRestore();
				}
			});
			
			self.click(function (e) {
				e.preventDefault();
			})
		}
	});
	
	// this section prepares the special image swaps
	//$('#shop').click(clicked);	
	$('#shop').mouseover(function (){MM_nbGroup('over','portfoliobar5_r6_c30','img/Portfoliobar/portfolioShop_f2.gif','','portfoliobar5_r3_c33','img/Portfoliobar/portfolioOneStar_f2.gif','','portfoliobar5_r4_c32','img/Portfoliobar/portfolioTwoStar_f2.gif','','portfoliobar5_r5_c31','img/Portfoliobar/portfolioThreeStar_f2.gif','','portfoliobar5_r8_c28','img/Portfoliobar/portfolioSixStar_f2.gif','',1);});
	$('#shop').mouseout(function (){MM_nbGroup('out');});
	
	$('#blask').click(function (){MM_nbGroup('down','group1','portfoliobar5_r7_c29','',1);});	
	$('#blask').mouseover(function (){MM_nbGroup('over','portfoliobar5_r7_c29','img/Portfoliobar/portfolioBlask_f2.gif','','portfoliobar5_r3_c33','img/Portfoliobar/portfolioOneStar_f2.gif','','portfoliobar5_r4_c32','img/Portfoliobar/portfolioTwoStar_f2.gif','','portfoliobar5_r5_c31','img/Portfoliobar/portfolioThreeStar_f2.gif','','portfoliobar5_r8_c28','img/Portfoliobar/portfolioSixStar_f2.gif','',1);});
	$('#blask').mouseout(function (){MM_nbGroup('out');});
	
	$('#myspace').click(function (){MM_nbGroup('down','group1','portfoliobar5_r9_c27','',1);});	
	$('#myspace').mouseover(function (){MM_nbGroup('over','portfoliobar5_r9_c27','img/Portfoliobar/portfolioMyspace_f2.gif','','portfoliobar5_r3_c33','img/Portfoliobar/portfolioOneStar_f2.gif','','portfoliobar5_r4_c32','img/Portfoliobar/portfolioTwoStar_f2.gif','','portfoliobar5_r5_c31','img/Portfoliobar/portfolioThreeStar_f2.gif','','portfoliobar5_r8_c28','img/Portfoliobar/portfolioSixStar_f2.gif','',1);});
	$('#myspace').mouseout(function (){MM_nbGroup('out');});
}

//sets up arrays for the alien img and prepares their swap img
function prepareAlien($section){
	if(!$section) $section = $(document);
	if($section.find('#portfoliobar').length == 0)return false;
	
	var swap = 'img/Portfoliobar/rollover.gif';
	var alienx1 = [1,1,2,3,4,5,4,5,6,8,3,4,5,6,7,2,3,5,6,3,4,5,6,3,4,5,6,3,4,5,6,2,3,5,6,3,4,5,6,7,4,5,6,8,2,3,4,5];
	var alieny1 = [11,17,9,9,9,9,10,10,10,10,11,11,11,11,11,12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15,16,16,16,16,17,17,17,17,17,18,18,18,18,19,19,19,19];
	var alienx2 = [1,1,5,6,7,4,5,6,3,4,5,6,7,2,3,5,6,8,3,4,5,6,8,3,4,5,6,3,4,5,6,8,2,3,5,6,8,3,4,5,6,7,4,5,6,5,6,7];
	var alieny2 = [11,17,9,9,9,10,10,10,11,11,11,11,11,12,12,12,12,12,13,13,13,13,13,14,14,14,14,15,15,15,15,15,16,16,16,16,16,17,17,17,17,17,18,18,18,19,19,19];

	var explosionx1	= [1,1,3,2,4,6,3,5,7,3,6,2,3,4,5,3,4,5,3,4,6,2,5,4,7,5];
	var explosiony1	= [17,23,15,16,16,16,17,17,17,18,18,19,19,19,19,20,20,20,21,21,21,22,22,23,23,24];
	var explosionx2	= [1,1,1,2,5,8,3,5,7,2,3,6,5,4,5,3,5,7,8,4,2,4,7,2,5,7,8];
	var explosiony2	= [16,20,25,14,14,14,15,15,15,16,17,17,18,19,19,20,20,20,20,21,22,22,22,24,24,24,25];
	var explosionx3	= [6,8,5,3,7,6,2,5,8,7,2,5,3,6,8];
	var explosiony3	= [14,14,15,16,17,19,20,20,20,22,23,24,25,25,25];
	var explosionx4	= [6,4,8,4,7,8,6,4];
	var explosiony4	= [14,15,16,20,20,24,25,26];
	
	var frames = [];
	
	//build arrays onload for every frame to reduce processing onmouseover
	for(var i=0; i<25; ++i) {
		var mouseover = ['img/Portfoliobar/rollover.gif',''];
		var num = i+1;
		
		//workout what baseframe array is needed 
		if(num < 19){
			if((num%2) !== 0){var alienx = alienx1; var alieny = alieny1;}
			else {var alienx = alienx2; var alieny = alieny2;}
		}
		else if(num == 19){var alienx = explosionx1; var alieny = explosiony1;}
		else if(num == 20){var alienx = explosionx2; var alieny = explosiony2;}
		else if(num == 21){var alienx = explosionx3; var alieny = explosiony3;}
		else if(num == 22){var alienx = explosionx4; var alieny = explosiony4;}
		
		//create the swap image array from the baseframe array
		var len = alienx.length;
		for(var j=0; j<len; ++j){
			var row = alienx[j];
			var cell = alieny[j]
			if(num < 7){cell += (num-1);}
			else if(num >= 7 && num < 12){cell += (11-num);}
			else if(num >= 12 && num < 17){cell += (num-11);}
			else if(num >= 17 && num < 19){cell += (21-num);}
			else if(num >= 19){cell+=(15-num);}
			var image = 'portfoliobar5_r' + row + '_c' + cell;
			mouseover.push(image);
			mouseover.push(swap);
			mouseover.push('');
		}	
		mouseover.push(1);
		
		//add the swap image array to the frames array
		frames.push(mouseover);
	}
	
	$.each($('.alien'), function () {
		//$(this).click(clicked);
		
		$(this).mouseover(function () {
			var name = $(this).find('img').attr('name')
			var framenum = name.substring(18) - 1;
			MM_nbGroup('over',name,frames[framenum]);
		});
		
		$(this).mouseout(function () {
			MM_nbGroup('out');
		});
	});
}

function boxSwap(image){
	$('#boxswap').attr('data-o_src', $('#boxswap').attr('src'));
	$('#boxswap').attr('src', image);
}

function boxSwapRestore(){
	$('#boxswap').attr('src', $('#boxswap').attr('data-o_src'));
	$('#boxswap').removeAttr('data-o_src');
}

//this function looks for images in 'a' tags and creates an array containing elements with the same name + f2
function preparePreloads(){
	
	var preloads = [];
	
	$.each($("a:has(img)"), function() {
		var string = $(this).children('img').attr('src');
		if(string.indexOf("spacer") != -1 || string.indexOf("full") != -1) {
			string = 'img/Portfoliobar/rollover.gif';
		}
		else {
			var file = string.split('.');
			var ext = '.' + file.pop();
			file = file.join('.');
			string = file + '_f2' + ext;
		}
		if($.inArray(string, preloads) == -1) {
			preloads.push(string);
		}
	});
	
	$.each(preloads, function () {
		if(fileExists(this)) {
			var image = new Image();
			image.src = this;
		}
	});
	
	prepareSquarePreloads();
}

function prepareSquarePreloads() {
	
	$.each($('#portfoliobar').find('a'), function () {
		if($(this).attr('href').slice(-1) != '#' && $(this).find('img').attr('title').length > 0) {
			var source = 'img/upload/' + $(this).find('img').attr('title') + '-square.jpg';
			if(fileExists(source)) {
				var image = new Image();
				image.src = source;
			}
		}
	});
}

function addDelayFunc() {
	$.fn.delay = function(time, callback) {
		return this.animate({ opacity: '+=0' }, time, callback);
	}
}

function main() {
	hashCheck();
	addDelayFunc();
	preparePage();
	setupFlashContent();
	$(window).bind('hashchange', hashChangeHandler);
	hashChangeHandler(true);
	prepareAjaxLinks();
	prepareAjaxForms();
	prepareSpamProtection();
	navBarMain();
	//preparePreloads();
	preparePortfolio();
};

$(document).ready(main);
