function displayImage(divClass) {
	// console.log(divClass);
	$('#entrance').removeClass().addClass(divClass);
}

function toggleTags(caller, tag) {
    $("#header ul.tags li a").removeClass('active');
    $(caller).addClass('active');
    
    $('#portfolio a').addClass('hidden').attr('rel', '');
    if (tag != '') {
    	$('#portfolio a.' + tag).removeClass('hidden').attr('rel', 'gallery');
    } else {
    	$('#portfolio a').removeClass('hidden').attr('rel', 'gallery');
    }
    showDimensions();
}

function getSquareSize(width, height, tile_count) {
				
    //height of rectangle
    var b = height;
    //width of rectanlge
    var a = width;

    // divide the area but the number of tiles to get the max area a tile could cover
    // this optimal size for a tile will more often than not make the tiles overlap, but
    // a tile can never be bigger than this size
    var maxSize = Math.sqrt((b * a) / tile_count);
    // find the number of whole tiles that can fit into the height
    var numberOfPossibleWholeTilesH = Math.floor(b / maxSize);
    // find the number of whole tiles that can fit into the width
    var numberOfPossibleWholeTilesW = Math.floor(a / maxSize);
    // works out how many whole tiles this configuration can hold
    var total = numberOfPossibleWholeTilesH * numberOfPossibleWholeTilesW;
	
    // if the number of number of whole tiles that the max size tile ends up with is less than the require number of 
    // tiles, make the maxSize smaller and recaluate
    while (total < tile_count){
    	maxSize = maxSize - 1;
    	numberOfPossibleWholeTilesH = Math.floor(b / maxSize);
    	numberOfPossibleWholeTilesW = Math.floor(a / maxSize);
    	total = numberOfPossibleWholeTilesH * numberOfPossibleWholeTilesW;
    }

    return maxSize;

}

var dimension_keys = new Array();
var dimension_values = new Array();

function showDimensions() {
    var relevantElements = $('#portfolio a:not(.hidden) img');
    var visible_images = relevantElements.size();
    var height = $(window).height() - $('#header').height() - 40;
    var width = $(window).width();
    
    var this_key = 'w'+width+'h'+height+'n'+visible_images;
    var imageDimensions;
    var lookup = $.inArray(this_key, dimension_keys);
    
    if (lookup != -1) {
    	imageDimensions = dimension_values[lookup];
    } else {
    	imageDimensions = Math.floor(getSquareSize(width, height, visible_images) - 16);
    	dimension_keys.push(this_key);
    	dimension_values.push(imageDimensions); 
    }
    
    // var space = (width < height) ? width * width : height * height;
    // var spacePerElement = space/visible_images - 100;
    // var imageDimensions = Math.floor(Math.sqrt(spacePerElement));
    // var imageDimensions = Math.floor(getSquareSize(width, height, visible_images) - 10);
    
    relevantElements.css({
    	width: Math.min(imageDimensions, 200),
    	height: Math.min(imageDimensions, 200)
    });
}

$(document).ready(function() {
    showDimensions();
    
    $.localScroll({
    	target: $('#page'),
    	hash: true,
    	margin: true,
    	duration: 800,
    	easing: 'swing'
    });
    
    $('a.fancybox').fancybox({
    	'title': $($(this).children()[0]).attr('alt'),
    	'titlePosition': 'over',
    	'overlayColor': '#000',
    	'overlayOpacity': 0.7,
    	'transitionIn': 'elastic',
    	'transitionOut': 'fade',
    	'speedIn': 500,
    	'speedOut': 200,
    	'changeFade': 350
    });
    
    /*
    var videopath = "http://relaunch.danielroosfotografie.de/";
	var swfplayer = videopath + "includes/functions/swf/flowplayer-3.1.5.swf";
	
	$("a.fancybox").fancybox({
		
		'title': $($(this).children()[0]).attr('alt'),
    	'titlePosition': 'over',
    	'overlayColor': '#000',
    	'overlayOpacity': 0.7,
    	'transitionIn': 'elastic',
    	'transitionOut': 'fade',
    	'speedIn': 500,
    	'speedOut': 200,
    	'changeFade': 350,
		
		'onComplete' :function(){
			
			console.log($(this).attr('href'));
			
			if ($(this).attr("name") == 'image') {				
				
				$("#fancybox-right, #fancybox-left").css({height:$("#fancybox-wrap").height(), bottom: '0'});
				
			} else {
				player = $f("fancybox-inner",{src: swfplayer, wmode: 'opaque'},{							
					play:{opacity:1},

					plugins: {
						controls:  null
					},
					clip:{
						autoPlay:true,
						autoBuffering:true,
						url:videopath + $(this).attr("name") + '',
						onStart:function(clip) {
														
							// var wrap=jQuery(this.getParent());
							var clipwidth = clip.metaData.width;
							var clipheight = clip.metaData.height;							
							var pos = [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop()];
							
							$("#fancybox-wrap").css({width:clipwidth+20,height:clipheight+20});
							$("#fancybox-inner").css({width:clipwidth,height:clipheight});
							$("#fancybox-right, #fancybox-left").css({height:clipheight+20, top: '0px'});
							$.fancybox.center();
							// $("#fancybox-inner").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth	- 36)	/ 2)));
							// $("#fancybox-inner").css('top',  ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50)	/ 2)));
							// $("#fancybox-right, #fancybox-left").css({height:clipheight-60, bottom: '70px'});
						}
					}
				});
			
				player.load();
			
			}
		}
		
	});
	*/
});

$(window).bind('resize', function() {
    showDimensions();
});