jQuery(function($) {
	
$('.nojsbox').remove();
	
$('.wrapper').css('opacity', 0);
$('.wrapper').animate({opacity:1}, 800, 'easeInOutQuart');


//$('.wrapper').animate({opacity: 1 }, 800, 'easeInOutQuart');
	
//corners

$('.blackbar').corners('bottom-right');


//links fade

	$("a").css("opacity", .5);

	
	$("a").mouseover(function() {
		$(this).animate({
			opacity: 1	  
		}, 300 );
	});
	
	$("a").mouseout(function() {
		$(this).animate({
			opacity: .5	  
		}, 300 );
	});

//thumbs fade
	   
	$("div.thumb").css("opacity", .6);						
							
	$("div.thumb").mouseover(function() {
		$(this).animate({
			opacity: 1	  
		}, 200, 'easeOutQuart' );
	});
	   
	$("div.thumb").mouseout(function() {
		$(this).animate({
			opacity: .6		  
		}, 200, 'easeOutQuart' );
	});
	
//thumbs click

	var tnnum;

	$("div.thumb").click(function(){
		src = this.id;
		ts = src.length;
		type = src.substr(ts-1, 1);
		src = src.replace(/tna|tni/, '');
		if(type == 'a')
			type = 'flash';
		else if(type=='i')
			type = 'image';
		loadcontent(src, type);
	});

});


function nl2br(text){
  	text = escape(text);
    if(text.indexOf('%0D%0A') > -1){
    	re_nlchar = /%0D%0A/g ;
	     }else if(text.indexOf('%0A') > -1){
	     re_nlchar = /%0A/g ;
	     }else if(text.indexOf('%0D') > -1){
	     re_nlchar = /%0D/g ;
     }
    return unescape( text.replace(re_nlchar,'<br />') );
};

 
//set up SWFobject

var flashvars = {};
	
var params = {};
params.menu = "false";
params.scale = "noscale";

var attributes = {};
 
//grow/shrink and load content-----------------

//init variables

var typex;
var filenamex;
var divh = 337;
var speed = .6;

var hidden = new Array;
hidden['animation', 'images'] = true;


function loadcontent(filename, type){
	
	typex = type;
	filenamex = filename;
	
	shrinkall();
						
};

function shrinkall(){

	if(hidden['animation'] == false){
		shrink('animation');	
	}else if(hidden['images'] == false){
		shrink('images');
	}else{
		removecontent();
	}
};

function removecontent(){
	
	swfobject.removeSWF("video");
	$('#loader').remove();
	$('#closecontent').remove();
	
	$('#animation').remove();
	$('#animgallcont').append('<div class="showvidcontent" id="animation"></div>');
	
	getcontent(filenamex, typex);	
};

function shrink(divid){
	$("#" + divid ).animate({
			height: '0px'					
	}, 800, 'easeOutQuart', function(){hidden[divid] = true; loadcontent(filenamex, typex)});
};

function getcontent(filename, type){
	
	//load naimation-----------------
	
	if(type == 'flash'){
		flashvars.flvsrc = filename + '.flv';
		
		
		var d = document.createElement('div');
		d.setAttribute('id', 'video');
		d.setAttribute('class', 'showvidcontent');
		$('#animation').append(d).append("<div class='imgbottombar'></div>");
		$('.imgbottombar').append("<div id='closecontent' align='center' class='closecontent' style='left:548px'>close</div>");
		$('#closecontent').click(function(){justshrink();});
		$('.closecontent').corners('top');
		$('.imgbottombar').mouseleave(function(){$('.imgbottombar').animate({top:0}, 500);});
		
		swfobject.embedSWF("images/animation/video/video.swf", "video", "600", "337", "9.0.0", "swfobject/expressInstall.swf", flashvars, params, attributes);

		grow('animation', divh);
		
			$.ajax({
				url : 'images/animation/info/' + filename + '.txt',
				success : function (data) {
				data = nl2br(data);
				$('.imgbottombar').append("<div id='contentinfo' align='center' class='closecontent' style='left:446px'>info</div>");
				$('.imgbottombar').css('opacity', .95);
				$('.imgbottombar').append("<div class='infobar' id='imginfobar'></div>");
				$('#contentinfo').mouseover(function(){showinfo(src);});
				$('.closecontent').corners('top');

				$('#contentinfo').css('opacity', 0);
				$('#contentinfo').animate({opacity:1}, 400);

				$("#imginfobar").append("<span id='imginfotext'>" + data + "</span>");
			
				
				}
			});
	
	}
	
	//load image--------------
	
	if(type == 'image'){
		
		src=filename;
		
		var l = document.createElement('div');
		l.setAttribute('id', 'loader');
		l.setAttribute('class', 'loading');
		$('#images').append(l);

		$('#images').animate({height:70}, 600, 'easeInOutQuart', function(){
		var img = new Image();
		$(img).load(function(){
			$('#loader').removeClass('loading').append(img).append("<div class='imgbottombar'></div>");
			$('.imgbottombar').append("<div id='closecontent' align='center' class='closecontent' style='left:" + ((img.width)-54) + "px'>close</div>");
			$('#closecontent').click(function(){justshrink();});
			$('.closecontent').corners('top');
			$('.imgbottombar').mouseleave(function(){$('.imgbottombar').animate({top:0}, 500);});
			$('#images').css('width', img.width);
			
			

			$.ajax({
				url : '/images/images/info/' + src + '.txt',
				success : function (data) {
				data = nl2br(data);
				$('.imgbottombar').append("<div id='contentinfo' align='center' class='closecontent' style='left:" + ((img.width)-158) + "px'>info</div>");
				$('.imgbottombar').css('opacity', .95);
				$('.imgbottombar').append("<div class='infobar' id='imginfobar'></div>");
				$('#contentinfo').mouseover(function(){showinfo(src);});
				$('.closecontent').corners('top');

				$('#contentinfo').css('top', 0);
				$('#contentinfo').animate({top:-21}, 400, 'easeInOutQuart');

				$("#imginfobar").append("<span id='imginfotext'>" + data + "</span>");
				}
			});
			
			grow('images', img.height + 1);
			$(img).css('opacity', 0);
			$(img).animate({opacity: 1}, 1500, 'easeInOutQuart');

		}).attr('src', 'images/images/large/' + src + '.jpg');
		});
		
	}
	
};

function grow(divid, amount){
	$("#" + divid ).animate({
			height: amount + 'px'				
	}, 800, 'easeInOutQuart', function(){
		if(typex == 'flash'){
			$.scrollTo($('#animation'), 800, 'easeInOutQuart')
		}else if(typex == 'image'){
			$.scrollTo($('#pageend'), 800, 'easeInOutQuart')}
		 }    );
	hidden[divid] = false;
	
};

function justshrink(){

	if(hidden['animation'] == false){
		$("#animation" ).animate({
			height: '0px'					
		}, 800, 'easeOutQuart', function(){$('#animation').remove(); $('#animgallcont').append('<div class="showvidcontent" id="animation"></div>')});
	}else if(hidden['images'] == false){
		$("#images" ).animate({
			height: '0px'					
	}, 800, 'easeOutQuart', function(){$('#loader').remove();});
	}
	
};


function showinfo(src){
	var infosize = $('#imginfotext').height();
	$('.imgbottombar').animate({top:0 - (infosize+10)}, 500, 'easeOutQuint');
	
};


//preload images---------------------

var tnarray = new Array();

function loadthumbs(src, type){
	
	var tnl = document.createElement('div');
	tnl.setAttribute('id', src + 'loader');
	tnl.setAttribute('class', 'tnloading');
	$('#' + src + 'tna').append(tnl);
	$('#' + src + 'tni').append(tnl);
	
	var imgtn = new Image();
	$(imgtn).load(function(){
		$('#' + src + 'loader').removeClass('tnloading').append(imgtn);
		$(imgtn).css('opacity', 0);
		$(imgtn).animate({opacity: 1}, 400);
	}).attr('src', 'images/' + type + '/thumbnails/' + src + '.jpg');
	
};

//cv and showreel links

function getshowreel(){
	window.location = "http://www.tom-hunt.co.uk/showreel.mov";
};
function getcv(){
	window.location = "http://www.tom-hunt.co.uk/cv.doc";
};

