

var aktThumb = "";
var gallery = Array();
var galleryTitle = Array();
var gallerySize = Array();
var galArray = Array();
var galTArray = Array();
var galSArray = Array();
var counter = 0;
var slideTimer = null;
var galCounter = 0;
var outhandler = null;
var ready4 = false
//
var right_over = new Image();
right_over.src = "/styles/images/over_gal_next.png";
var left_over = new Image();
left_over.src = "/styles/images/over_gal_back.png";

var right = new Image();
right.src = "/styles/images/next_gallery.png";
var left = new Image();
left.src = "/styles/images/back_gallery.png";
///
$(document).ready(function(){

	

	$('#arrow-right').css({'position':'absolute','margin-top':253});
	$('#arrow-left').css({'position':'absolute','margin-top':253});
	
	
	
	
	
	$('#year-text').live('click',function(){
		
		var year = $(this).text();
		var target = $(this).position();
		$("#year-selector").animate({
		
			
			left: target.left
		
		
		},100,function(){
		
		
			$.ajax({
				
				url: "viewer/get_by_year/"+year,
				success:function(msg)
				{
					$('#gallery-view').html(msg);
				}
			
			});
		
		})
	});


	
	/*$('#arrow-left').live('hover',function(){
	
		$(this).html('<img src="/styles/images/over_gal_back.png"/>');
	
	},function(){	
		$(this).html('<img src="/styles/images/back_gallery.png"/>');
		});
	
	$('#arrow-right').live('mouseover',function(){
	
		$(this).html('<img src="/styles/images/over_gal_next.png"/>');
	
	})
	
	$('#arrow-right').live('mouseout',function(){
	
		$(this).html('<img src="/styles/images/next_gallery.png"/>');
	
	});
	*/
	
	$('#arrow-right').live('mouseenter',function()
	{
		$(this).html(right_over);
	});
	
	$('#arrow-right').live('mouseleave',function()
	{
		$(this).html(right);
	});
	
	
	$('#arrow-left').live('mouseenter',function()
	{
		$(this).html(left_over);
	});
	
	$('#arrow-left').live('mouseleave',function()
	{
		$(this).html(left);
	});
	
	$('#arrow-right').live('click',function()
	{
	if(galCounter == galArray.length-1)
	{
		galCounter = -1;
	}
	galCounter++;
	counter = galCounter;
	var galImage = new Image();
	
	$('#image img').animate({'opacity':0},300,function(){
	
		if($.browser.msie)
		{
			galImage.src = galArray[galCounter]+"?"+Math.random()*Math.random();
		}
		else
		{
			galImage.src = galArray[galCounter]
		}
	
	});
	$(galImage).load(function(){
		
		
		galImage.height = 336/galImage.width * galImage.height;
		galImage.width = 336;
		
		$('#title').html(galTArray[galCounter]);
		$('#size').html(galSArray[galCounter]);
		$(galImage).css({'opacity':0});
		$('#image').animate({
		
			width: galImage.width,
			height:galImage.height
		
		},300,function(){
		
			$('#image').html(galImage);
			$(galImage).animate({'opacity':1});
		});
	
	})
		
		
	})
	
	$('#arrow-left').live('click',function()
	{
	if(galCounter == 0)
	{
		galCounter = galArray.length;
	}
	galCounter--;
	counter = galCounter;
	var galImage = new Image();
	
	$('#image img').animate({'opacity':0},300,function(){
	
		if($.browser.msie)
		{
			galImage.src = galArray[galCounter]+"?"+Math.random()*Math.random();
		}
		else
		{
			galImage.src = galArray[galCounter]
		}
		
	
	});
	$(galImage).load(function(){
		
		
		galImage.height = 336/galImage.width * galImage.height;
		galImage.width = 336;
		
		
		
		$(galImage).css({'opacity':0});
		
		$('#image').animate({
		
			width: galImage.width,
			height:galImage.height
		
		},300,function(){
		
			$('#image').html(galImage);
			$(galImage).animate({'opacity':1});
		})
	})
	
	})

	$('.thumbnail').live('click',function(){
		
		galCounter = $(this).attr('counter');
		counter = galCounter;
		$(aktThumb).css({'border':'1px grey solid'});
		$(this).css({'border':'1px #AC509E solid'});
		aktThumb = this;
		$('#image img').css({'opacity':0});
		var new_link =$(this).attr('rel');
		var new_title = $(this).attr('rel2');
		var new_size = $(this).attr('rel3');
		var new_image = new Image();
		if($.browser.msie)
		{
			new_image.src = new_link+'?'+Math.random()*Math.random();
		}
		else
		{
			new_image.src = new_link;	
		}
		//
		
		
		$(new_image).css({'opacity':0});
		
		//
		$('#image_loader').css({'top':$('#image').height()/2});
		$('#image_loader').css({'left':$('#image').width()/2});
		$('#image_loader').css({'display':'block'});
		
		$(new_image).load(function(){
			
			
			
			new_image.height = 336/new_image.width * new_image.height;
			new_image.width = 336;
			
			$('#image_loader').css({'display':'none'});
			$('#image').html(new_image);
			$('#title').html(new_title);
			$('#size').html(new_size);
			
			$('#arrow-left,#arrow-right').animate({
			
				'margin-top': new_image.height/2-20
			
			})
			
			$('#image').animate({
			
				width: new_image.width,
				height: new_image.height
			
			},600,function(){
			$(new_image).animate({
			
				opacity: 1
			
			});
			
			});
		
		})
		
	
	});

});

$.fn.slideShow = function(options)
{
	options = $.extend({
		time:500,
		opacity:0.8,
		stopButton:'/styles/images/pause.png',
		playButton:'/styles/images/play.png',
		galleryDiv:''
	
	}, options); 
	
	
	var overlay = document.createElement('div');
	
	//
	$(overlay).css('width',$(window).width());
	$(overlay).css('height',$(document).height());
	$(overlay).css('opacity',0);
	$(overlay).css('background','#000');
	$(overlay).css('position','absolute');
	$(overlay).css('z-index','10000');
	$(overlay).css('top','0');
	$(overlay).css({'display':'none'})
	$('body').append(overlay);
	
	$(options.galleryDiv).css('display','none');
	//
	$(window).resize(function(){
	
		$(overlay).css('width',$(window).width());
		$(overlay).css('height',$(document).height());
	
	});
	
	//
	//open slideshow
	this.click(function(){
		
		$(overlay).css('display','block');
		$(overlay).animate({
		
		opacity:options.opacity
		
		},options.time,function(){
		
			
			ready4 = true;
			$('body').css({'overflow-y':'visible'});
			init(options);
		
		})
	
	});
	//close slideshow
	$(overlay).click(function(){
		if(ready4 == true)
		{
		clearTimeout(slideTimer);
		$('#picture-frame').animate({
		
			opacity:0
		
		},options.time,function(){
		
			$(this).remove();
		});
		
		$(this).animate({
		
			opacity:0
		
		},options.time,function(){
			
			$(this).css({'display':'none'});
			$('body').css({'overflow-y':'auto'});
			$('#pause-button').remove();
			$('#slide-title').remove();
			$('#slide-size').remove();
		})
		ready4 = false;
		}
	});
	//
	

	
};


	var init = function(opt){
		
		gallery.length = 0;
		galleryTitle.length = 0;
		gallerySize.length = 0;
		//
		var picture = document.createElement('div');
		var pause = document.createElement('div');
		var title = document.createElement('span');
		var size = document.createElement('span');
		pause.id= "pause-button";
		picture.id = "picture-frame";
		title.id="slide-title";
		$(title).addClass("slides-info");
		size.id = 'slide-size';
		$(size).addClass("slides-info");
		$(picture).css({'position':'absolute'});
		$(picture).css({'width':'400px'});
		$(picture).css({'height':'600px'})
		$(picture).css({'background':'white'});
		$(picture).css({'left':'50%'});
		$(picture).css({'top':$(window).height()/2});
		$(picture).css({'margin-left':'-200px'});
		$(picture).css({'margin-top':'-300px'});
		$(picture).css({'z-index':'10001'});
		$(picture).css({'z-index':'10001'});
		//
		var pause_pic = new Image();
		pause_pic.src = opt.stopButton;
		var play_pic = new Image();
		play_pic.src = opt.playButton;
		
		$(pause_pic).load(function(){
		
			//alert(opt.stopButton);
			$(pause).css({'width':pause_pic.width,'height':pause_pic.height,'background':'url("'+opt.stopButton+'")'});
			$(pause).css({'position':'absolute'});
			$(pause).css({'left':'50%'});
			$(pause).css({'margin-left':0});
			$(pause).css({'margin-top':$(picture).height()/2});
			$(pause).css({'z-index':'10002'});
			$(pause).css({'display':'none'});
			//
			$(title).css({'position':'absolute','z-index':'10002','color':'white','font-family':'Verdana','font-size':'12px'});
			$(size).css({'position':'absolute','z-index':'10002','color':'white','font-family':'Verdana','font-size':'12px'});
			$('.slides-info').css({'left':'50%','display':'block','width':'120px'});
			$('.slides-info').css({'margin-left':$(picture).width()/2 + 20});
			$(title).css({'margin-top':$(window).height()/2});
			$(size).css({'margin-top':$(window).height()/2+14});
			
		})
		$('body').append(picture);
		$('body').before(pause);
		$('body').before(title);
		$('body').before(size);
		//
		$(pause).mouseover(function(){
		
			$(this).css({'display':'block'});
			//$('#picture-frame').unbind('mouseout',outhandler);
		
		})
		$(pause).click(function(){
			
			if(slideTimer != null)
			{
			clearTimeout(slideTimer);
			$(this).css({'background':'url("'+opt.playButton+'")'});
			slideTimer = null;
			}
			else
			{
				$(this).css({'background':'url("'+opt.stopButton+'")'});
				giveNext();
			}
		})
		gallery = Array();
		galleryTitle = Array();
		gallerySize = Array();
		$(".thumbnail").each(function(){
		
			gallery.push($(this).attr('rel'));
			galleryTitle.push($(this).attr('rel2'));
			gallerySize.push($(this).attr('rel3'));
			
		
		});
		
		var image = new Image();
		counter = 0;
		if($.browser.msie)
		{
			image.src = gallery[counter]+'?'+Math.random()*Math.random();
		}
		else
		{
			image.src = gallery[counter];
		}
		var wHeight = $(window).height();
		
		//
		$(image).load(function(){
			$('#slide-title').html(galleryTitle[counter]);
			$('#slide-size').html(gallerySize[counter]);
			if(wHeight < image.height)
			{
				image.width = (wHeight-100)/image.height * image.width;
				image.height = wHeight-100;
				
				
			}
			
			
				
				$(pause).animate({
			
					'margin-left':0,
					'margin-top':$(window).height()/2
				
				},600)
				$(title).animate({
			
					'margin-left':image.width/2+20,
					'margin-top':$(window).height()/2
				},600)
				$(size).animate({
			
					'margin-left':image.width/2 +20,
					'margin-top':$(window).height()/2+14
				},600)
			
			$(picture).animate({
				
				'width': image.width,
				'height': image.height,
				'margin-left': -image.width/2,
				'margin-top': - image.height/2,
				
			},600,function(){
			
				$(picture).html(image);
				$(image).css({'position':'absolute'});
				counter ++;	
				giveNext();
			});
			
		
		})
	
}

$('#picture-frame').live('mouseover',function(){
	
	$('#pause-button').css({'display':'block'});
	
})

$('#picture-frame').live('mouseout',outhandler = function(){
	
	
		$('#pause-button').css({'display':'none'});
	
})

var giveNext = function()
{
	
	slideTimer = setTimeout(function(){
	
		$("#picture-frame img").animate({
		
			opacity:0
			
		},300);
		//
		var image = new Image();
		if($.browser.msie)
		{
			image.src = gallery[counter]+'?'+Math.random()*Math.random();
		}
		else
		{
			image.src = gallery[counter];
		}
		var wHeight = $(window).height();
		
		$(image).load(function(){
			
			$('#slide-title').html(galleryTitle[counter]);
			$('#slide-size').html(gallerySize[counter]);
		
			if(wHeight < image.height)
			{	
				image.width = (wHeight-100)/image.height * image.width;
				image.height = wHeight-100;
			}
			
			
			
				
				$('#pause-button').animate({
			
				'margin-left':0,
				'margin-top':$(window).height()/2
			},600)
			
			$('#slide-title').animate({
			
					'margin-left':image.width/2+20,
					'margin-top':$(window).height()/2
				},600)
				$('#slide-size').animate({
			
					'margin-left':image.width/2 +20,
					'margin-top':$(window).height()/2+14
				},600)
			
			
			
			$('#picture-frame').animate({
				
				'width': image.width,
				'height': image.height,
				'margin-left': -image.width/2,
				'margin-top': - image.height/2,
				
			},600,function(){
				
				
				$('#picture-frame').html(image);
				$(image).css({'position':'absolute'});
				$('#picture-frame img').css({'opacity':0});
				$('#picture-frame img').animate({
					opacity : 1
				},300,function(){
				counter ++;
				if(counter >= gallery.length)
				{
					counter = 0;
				}
				giveNext();
				
				})
				
			});
		
		})
	},5000)

}
