$(document).ready(function(){


/*
* starts fancybox
*
* depending on jquery.js / jquery.fancybox.js / style.fancybox.css
* using jquery.easing.js / jquery.mousewheel.js
*/
$('.thumbContainer a').fancybox({
	'titleShow' :false
	});

$('a.fancybox').fancybox({
	'titleShow' :false
	});

	
/*
* Makes the imgcontainer dragable; deaktivates and reactivates
* the fancybox during dragging
*
* depending on jquery.js / jquery.ui.js
*/	
	$(function() {
		$(".thumbContainer").draggable({
			start : function(event, ui){
				$('.thumbContainer a').unbind('click.fb');
			},
			stop : function(event, ui){
				setTimeout(function(){$('.thumbContainer a').fancybox();},10)
			}
		});
	});


/*
* starts randomPos
*
* depending on jquery.js 
*/	

	randomPos();
	bringToFrontOnHover()

//end of document ready; all code after is interpreted at load and not on document loaded...

});


/*
* gives the imgcontainer a random position
*
* depending on jquery.js
* 
* p.s. position set to absolute in styles.css
*/
function randomPos() {
	var maxX = $('.galleriecontent').width()-600;
	var maxY = $('.galleriecontent').height()-350;
	var maxRot = 90; // +/- 45°
	
	if(maxX<0){maxX=0};
	if(maxY<0){maxY=0};
	
	$('.thumbContainer').each(function(){
		var posX = Math.floor( maxX * Math.random()) + 203;
		var posY = Math.floor( maxY * Math.random())+50;
		var rot = maxRot/2 - Math.floor(maxRot * Math.random());
		//alert(posX + ' - ' + posY);
		$(this).css({
			'left': posX,
			'top': posY,
			'-moz-transform': 'rotate(' + rot + 'deg)', /*mozilla firefox*/
			'-o-transform': 'rotate('+ rot + 'deg)',  /* Opera 10.5 */
			'-webkit-transform': 'rotate('+rot+'deg)'  /* Saf3.1+, Chrome */
		});
		
	});
}


/*
* brings the image you're hovering on to front
*
* depending on jquery.js
* 
* p.s. position set to absolute in styles.css
*/

function bringToFrontOnHover(){
	
	var indexMargin = 100;
	
	var topElement = $('.galleriecontent .thumbContainer').size()+indexMargin;
	
	//default z-index:
	
	$('.thumbContainer').each(function(){
		var number = $('.thumbContainer').index(this)+indexMargin;
		$(this).css({'z-index': number + 1});
	})
	
	$('.thumbContainer').hover(function(){
		var actualindex = $(this).css('z-index');
		$(this).css({'z-index': topElement+1});
		
		$('.thumbContainer').each(function(){
			var tempIndex = $(this).css('z-index');
			if(tempIndex != topElement+1 && tempIndex > actualindex){
				$(this).css({'z-index': tempIndex - 1});
			};
		});
	},function(){
		$(this).css({'z-index': topElement})
	})
};

/*
* function to activate deactivat hover function
*/
