var timebase = 850;
var numImages = 5;
var numAnimates = 12;
var now = new Date();
var seed = now.getSeconds();
var idAppend = '#image-holder-';
var animates = new Array();
var animationStartCount = 0;
var imageLoadCount = 0;
for( i=0;i<numAnimates;i++ ) {
	animates.push(new animation());
}

function loadAnimations(data, which) {
	//console.log('loading animations...'+data+' in mode '+which+' '+animates.length);
	$.each(data.image_info,function(i,item) {
		var iid=item.vid;
		var anim = animates[i];
		anim.id = iid;
		anim.current = 0;
		anim.imgs = new Array();
		anim.animate = true;
		if( which == 'all' ) {
			$.each(item.images, function(j,k) {
				var url=k;
				var img = new Image();
				$(img).bind( 'load', {index:j,a:anim,mode:which}, addImage).attr('src', url);
			});
		} else {
			var url=item.images[0];
			var img = new Image();
			$(img).bind( 'load', {index:0,a:anim,mode:which}, addImage).attr('src', url);
		}
	});
	return false;
}

function addImage( image ) {
	//imageLoadCount++;
	var a = image.data.a;
	//a.imgs[image.data.index] = this;
	//console.log('id '+a.id+' pos '+image.data.index+' imgs '+a.imgs.length+' '+a.imgs+' '+a.contains(this));
	if( ! a.contains(this) ) {
		a.imgs.push(this);
	}
	//console.log('loaded image...'+this.src+' '+image.data.index+' in mode '+image.data.mode+' image load count '+imageLoadCount+' image array length '+a.imgs.length+' animation id '+a.id);
	//if (a.imgs.length == numImages){
	//if ( image.data.mode == 'all' ){
	if (a.imgs.length == 2){
		//animationStartCount++;
		//console.log('starting animation... '+a.id+' count '+animationStartCount);
		setTimeout( function() { a.rotate(0); }, 0 );
	} else if ( image.data.mode == 'first' ) { // do replacement on one
		elem_id=idAppend+a.id;
		img = a.imgs[0];
		//console.log('showing first image...'+img.src+' '+elem_id+' in mode '+image.data.mode);
		$(elem_id).html(img);
		//console.log($(elem_id).html());
		//console.log(img);
	}
	return false;
}

function animatesOff() {
	for( i in animates ) {
		animates[i].animate = false;
	}
	return false;
}

function animatesOn() {
	for( i in animates ) {
		animates[i].animate = true;
	}
	return false;
}

function animation() {
	this.id = null;
	this.imgs = null;
	this.current = null;
	this.animate = false;
	this.timeout = timebase-(Math.ceil(Math.random(seed)*100));
	//this.timeout = timebase;
	var me = this;
	//console.log('instantiating animation '+this.id);
	this.rotate = function(c) {
		if( me.animate ) {
			elem_id=idAppend+me.id;
			image = me.imgs[c];
			me.current = c+1;
			//if( me.current >= numImages ){ me.current = 0; }
			if( me.current >= me.imgs.length ){ me.current = 0; }
			$(elem_id).html(image);
			//console.log('setting image '+image+' in '+me.id+' with timeout '+me.timeout);
		}
		setTimeout( function() { me.rotate(me.current) }, me.timeout );
		return false;
	}
	this.contains = function(img) {
		for( i in me.imgs ) {
			if (me.imgs[i].src == img.src) {
				return true;
			}
		}
		return false;
	}
}

function showThumbs(item) {
	var href = $('#'+item).attr('href');
	var match = item.match(/(.*?)-/);
	var parent = '';
	if ( match instanceof Array ) {
		parent = '#'+match[1]+'-content';
	} else {
		parent = '#'+match+'-content';
	}
	var thumbs = '#'+item.replace('portfolio', 'thumbs');
	//console.log('showing id:'+item+' href:'+href+' parent:'+parent+' thumbs:'+thumbs);
	//console.log('before:'+$(thumbs).is(':hidden'));
	rightShift();
	if( $(parent).is(':hidden') ) { $(parent).show(); }
	$('.thumbs').not(thumbs).hide();
	if( $(thumbs).is(':hidden') ) {
		//console.log('is hidden '+thumbs);
		//$('#'+thumbs.replace(/-.*?/, ''));
		$(thumbs).show('fast');
		//console.log('after:'+$(thumbs).is(':hidden'));
		$.get(href, function(str) {
			//console.log(str);
			data = eval(str);
			loadAnimations(data, 'first'); // doing this will give first images two chances to load
			loadAnimations(data, 'all');
		});
	} else {
		//console.log('is not hidden '+thumbs);
		$(thumbs).hide();
		//$.history.add('default');
		animatesOff();
	}
	return false;
}

video_loaded = false;

function showVideo(href) {
	$('div#left').addClass('shift-left');
	$('div#video-content').load(href, showVideoCallback);
	$('div#center').show('fast');
	$('body').attr('id', 'content-center');
	$('div#right').removeClass('shift-right2').addClass('shift-right1');
	return false;
}

function showVideoCallback() {
	var slug = 'video-'+$('div#video-slug').html() || 'video';
	$.history.add(slug);
	return false;
}

function showContent(item, force_show) {
	$('.thumbs').hide();
	animatesOff();
	$('.ajax-content-left').not(item).hide();
	$('.ajax-content-right').not(item).hide();
	$(item).hasClass('ajax-content-left') || item == '#home-content' ? rightShift() : leftShift();
	force_show ? $(item).show() : $(item).toggle('fast');
}

function rightShift() {
	//console.log('shifting right');
	$('.ajax-content-right').hide();
	$('body').attr('id', 'content-right');
	$('div#center').hide();
	if( $('div#left').hasClass('shift-left') ) {
		$('div#left').hide().removeClass('shift-left').show('fast');
		$('div#right').hide().removeClass('shift-right2').removeClass('shift-right1').show('fast');
	}
}

function leftShift() {
	//console.log('shifting left');
	$('.thumbs').hide();
	animatesOff();
	$('body').attr('id', 'content-left');
	$('.ajax-content-left').hide();
	$('div#center').hide();
	if( ! $('div#right').hasClass('shift-right2') ) {
		$('div#left').hide().addClass('shift-left').show();
		$('div#right').hide().removeClass('shift-right1').addClass('shift-right2').show('fast');
	}
}

function doHistory(hash) {
	//console.log('doing history '+hash);
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		return false;
	}
	if( hash.match('portfolio') ) {
		showThumbs(hash);
	} else if( hash.match('video') ) {
		href = '/public/'+hash.replace('-', '/');
		//console.log(href);
		// This triggers an inf. loop in webkit when called from the showVideo callback
		//showVideo(href);
	} else {
		showContent('#'+hash+'-content', true);
	}
}

