function preloadImage(imgPath,maxWidth,minHeight,imageSpace,origHeight,origWidth,title,caption,id) {
	$('.fullSizeImage').fadeOut(400, function () {
		var img = new Image();
		var adjustedHeight = imageSpace;						
		var adjustedWidth = (adjustedHeight*origWidth)/origHeight;
		$('#asset').load(function() {
			$(this).css('margin-top','0px');			
			if (adjustedHeight>origHeight && adjustedWidth>origWidth) {
				adjustedHeight = origHeight;
				adjustedWidth = origWidth;
				//alert('1');
			} else if (adjustedWidth>maxWidth){
				adjustedWidth = maxWidth;
				adjustedHeight = (adjustedWidth*origHeight)/origWidth;
				//alert('2');
			} else if (origHeight<minHeight){
				adjustedWidth = 'auto';
				adjustedHeight = minHeight;
				//alert('3');
			} else if (origHeight>adjustedHeight){
				adjustedWidth = 'auto';
				//alert('4');
			} else if ((origHeight*850)/origWidth > adjustedHeight){
				adjustedWidth = 850;
				adjustedHeight = (adjustedWidth*origHeight)/origWidth;
				//alert('5');
			} else {
				adjustedWidth = 'auto';
				//alert('6');
			}
			$(this).css('margin-top', (imageSpace-adjustedHeight)/2+'px' );
			
			tempImg = $(this);
			if (adjustedWidth == 'auto'){
				$('#asset').css({height: adjustedHeight+'px', width: 'auto'});
			} else if (adjustedHeight == 'auto') {
				$('#asset').css({height: 'auto', width: adjustedWidth+'px'});
			} else {
				$('#asset').css({height: adjustedHeight+'px', width: adjustedWidth+'px'});
			}
			$(tempImg).fadeIn();
			$('#asset-title').html(title.substr(0,90));
			$('#asset-caption').html(caption.substr(0,120));
		}).error(function() {
			// alert('failed');
		}).attr('src', imgPath).addClass('fullSizeImage');
	});
	$('#like-button').load('/assets/'+id+'/votes');
	$('#fb-share-button').attr('href',$('#fb-share-button').attr('href').replace(/%2F\d+&t/i,'%2F'+id+'&t'));
	$('.flag').attr("href","/assets/"+id);
}

function changeImage(a,maxWidth,minHeight,imageSpace) {
	var img = $(a).children();
	var imgThmb = img.attr('src');
	var origHeight = $(a).attr('rel').match(/.*\|(\d+)$/)[1];
	var origWidth = $(a).attr('rel').match(/.*\|(\d+)\|\d+$/)[1];
	var id = imgThmb.match(/\/(\d+)\//)[1];
	title = $(a).attr('rel').match(/(.*)\|.*\|\d+\|\d+$/)[1];
	caption = $(a).attr('rel').match(/.*\|(.*)\|\d+\|\d+$/)[1];
	if (origWidth > 840) {
		imgPath = imgThmb.replace(/\/small/gi, '/large');
	} else {
		imgPath = imgThmb.replace(/\/small/gi, '/original');
	}
	preloadImage(imgPath,maxWidth,minHeight,imageSpace,origHeight,origWidth,title,caption,id);
	$('.gallery-asset').css('border','1px solid #ccc');
	img.css('border','1px solid #f60');
	var idx = $('li.item').index($('.asset_'+id).parents('li'));
	myItem = $(".item", $("#sliderContent"));
	var sliderMax = (myItem.length * 125)-parseInt($(".viewer", $("#sliderContent")).css("width"));
	if (idx == 0){
		$('#slider').slider("option", "value", "0");
		if ($('#slider').children().length==1){
			$(".content-conveyor", $("#sliderContent")).css("left", "0px");
		}
	} else if ((idx+1) == myItem.length) {
		$('#slider').slider("option", "value", sliderMax);
		if ($('#slider').children().length==1 && idx > 6){
			$(".content-conveyor", $("#sliderContent")).css("left", (idx-6)*-125 + "px");
		}
	} else {
		$('#slider').slider("option", "value", (sliderMax/myItem.length)*idx);
		if ($('#slider').children().length==1 && idx > 6){
			$(".content-conveyor", $("#sliderContent")).css("left", (idx-6)*-125 + "px");
		}
	}
	if ($(a).parent().next().attr('id') == undefined){
		$('#next-link').attr('rel', $('#next-link').attr('rel').replace(/\d+$/,$(a).parent().siblings('li:first').attr('id').match(/\d+$/)));
	} else {
		$('#next-link').attr('rel', $('#next-link').attr('rel').replace(/\d+$/,$(a).parent().next().attr('id').match(/\d+$/)));
	}
	if ($(a).parent().prev().attr('id') == undefined){
		$('#prev-link').attr('rel', $('#prev-link').attr('rel').replace(/\d+$/,$(a).parent().siblings('li:last').attr('id').match(/\d+$/)));
	} else {
		$('#prev-link').attr('rel', $('#prev-link').attr('rel').replace(/\d+$/,$(a).parent().prev().attr('id').match(/\d+$/)));
	}
}

function firstImage(maxWidth,minHeight,imageSpace) {
	if($('#firstLoad').attr('id') == 'firstLoad') {
		img = $('#firstLoad').children();
		imgThmb = img.attr('src');
		var origHeight = $('#firstLoad').attr('rel').match(/.*\|(\d+)$/)[1];
		var origWidth = $('#firstLoad').attr('rel').match(/.*\|(\d+)\|\d+$/)[1];
		title = $('#firstLoad').attr('rel').match(/(.*)\|.*\|\d+\|\d+$/)[1];
		caption = $('#firstLoad').attr('rel').match(/.*\|(.*)\|\d+\|\d+$/)[1];
		var id = imgThmb.match(/\/(\d+)\//)[1];
		if (origWidth > 840) {
			neworigWidth = 850;
			origHeight = (neworigWidth*origHeight)/origWidth;
			origWidth = neworigWidth;
			imgPath = imgThmb.replace(/\/small/gi, '/large');
		} else {
			imgPath = imgThmb.replace(/\/small/gi, '/original');
		}
		preloadImage(imgPath,maxWidth,minHeight,imageSpace,origHeight,origWidth,title,caption,id);
	}
}

$(document).ready(function() {
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage;
	var maxWidth = 850;
	var minHeight = 285;
	if ( $('ul.content-conveyor').children().length > 7 ) {
		if (BrowserDetect.browser == 'Safari' || BrowserDetect.browser == 'Chrome') {
			var imageSpace = $(window).height()-340;
		} else {
			var imageSpace = $(window).height()-320;
		}
	} else {
		if (BrowserDetect.browser == 'Safari' || BrowserDetect.browser == 'Chrome') {
			var imageSpace = $(window).height()-320;
		} else {
			var imageSpace = $(window).height()-300;
		}
	}
	$(".left-nav-col").css({'padding-top' : imageSpace/2});
	$(".right-nav-col").css({'padding-top' : imageSpace/2});
	$('.nav-layer').height(imageSpace);
	$("#right-nav-arrow").show();
	$("#left-nav-arrow").show();
	$('.changeImage').click(function(){
		changeImage($(this),maxWidth,minHeight,imageSpace);
		return false;
	});

	$(".nav-layer").click(function(){
		assetId = $(this).attr('rel').match(/\d+$/);
		changeImage($('.asset_'+assetId).parent(),maxWidth,minHeight,imageSpace);
		return false;
	});
	
	$(".nav-arrow").click(function(){
		if ($(this).attr('id')=='left-nav-arrow'){
			assetId = $('#prev-link').attr('rel').match(/\d+$/);
		} else {
			assetId = $('#next-link').attr('rel').match(/\d+$/);
		}
		changeImage($('.asset_'+assetId).parent(),maxWidth,minHeight,imageSpace);
		return false;
	});
	
	firstImage(maxWidth,minHeight,imageSpace);
});
