// When the page is ready
$(document).ready(function(){
	
	var oldMenuWidth = 0;
	var menuPadding = 0;

	var foTime = 250;
	var fiTime = 150;
	
	var thumbs = true;
	var active = 0;

	/* FADE IN CONTENT */
	
	//$('#menu li ul').hide();
	//$('#menu a.active').next('ul').show();

	//$('#image').hide();			//
	//$('#viewimage').hide();
	$('#content').hide();
	$('#content').fadeIn(fiTime);
	$('#viewer').fadeIn(fiTime);
	
	$('div.text').hide();
	url = location.href.split('#');
	if(url[1]) {
		div_id = '#'+url[1];
		$(div_id).fadeIn(fiTime);
		$('a.'+url[1]).addClass('active');
	} else {
		$('a.text:first').addClass('active');
		$('div.text:first').fadeIn(fiTime);
	}

	$('#menu > ul > li > a').hover(
		function(){
			oldMenuWidth = $('#menu > ul > li > a').css('width');
			menuPadding = $('#menu > ul > li > a').css('padding-left').substr(0,2);
			hoverMenuWidth = document.width - menuPadding;
			$(this).css('width', hoverMenuWidth+'px'); 
		},
		function(){ $(this).css('width', oldMenuWidth); }
	);
	
	$('#menu > #language > a').hover(
		function(){
			oldMenuWidth = $('#menu > ul > li > a').css('width');
			menuPadding = $('#menu > ul > li > a').css('padding-left').substr(0,2);
			hoverMenuWidth = document.width - menuPadding;
			$(this).css('width', hoverMenuWidth+'px'); 
		},
		function(){ $(this).css('width', oldMenuWidth); }
	);
	
	$('a.fade').click(function(){
		url = $(this).attr('href');
		$('#content').fadeOut(foTime, function(){
			location.href = url;
		});
		return false;
	});

	
	$('#menu a.text').click(function(){
		url = $(this).attr('href').split('#');
		div_id = '#'+url[1];
		$('a.text').removeClass('active');
		$('a.'+url[1]).addClass('active');
		$('div.text').fadeOut(foTime, function(){
			fin = function(){ $(div_id).fadeIn(fiTime);};
			setTimeout(fin, 200);
		});
		//alert("got");
		$.scrollTo( div_id, 800, {offset:-150} );
		return false;
	});
	
	$('#menu a:not(a.text)').click(function(){
		url = $(this).attr('href');
		if(url == '#') return false;
		$('#content').fadeOut(foTime, function(){
			location.href = url;
		});
		return false;
	});
	

/*	MENU OPEN/CLOSE FUNCTIONS */
/*

	$('#menu > ul > li > a').click(function(){
		if($(this).hasClass('active')) var open = true;
		$('#menu a').removeClass('active');
		$('#menu ul.sub').hide();
		
		// differentiate for submenus
		
		if(!open) {
			$(this).parent().children('ul').show();
			$(this).addClass('active');
		} else {
// 			$('#content').fadeOut(foTime, function(){
// 				window.location($('this').attr('href'));
// 			});
		}
		return false; 
	});
	
*/	

	
	/* OPENING AN IMAGE */
	
	function openImageWin(nr,width,height){
		var x = Math.round(($(window).width() - width) / 2);
		var y = Math.round(($(window).height() - height) / 2);
		var url = pagelevel + "viewer/?gal=" + curgalid + "&nr=" + nr + "&table=" + table + "&galdir=" + galdir;
		config = 'width=' + width + ',height=' + height + ', top=' + y + '. left=' + x + ', toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0';
		newWindow = window.open(url, 'myWin', config);
		newWindow.focus();
		newWindow.moveTo(x,y);
		return false;
		//alert("open image");
	}
	
	function showImage(nr) {
	
		if(nr >= images.length) nr = 0;
		else if(nr < 0) nr = images.length - 1;
	
		if(thumbs) {
			$('#image img').attr('src',images[nr]);
			active = nr;
			$('#thumbs').fadeOut(foTime, function(){
				thumbs = false;
				$('#image').fadeIn(fiTime);
			});
		} else {
			$('#image img').fadeOut(foTime, function(){
				$('#image img').attr('src','');
				active = nr;
				$('#image img').attr('src',images[nr]);
				$('#image img').fadeIn(fiTime);
			});
		}	
	}
	
	function showViewImage(nr) {
		if(nr >= images.length){
			nr = 0;
		}else if(nr < 0){
			nr = images.length - 1;
		}
		$('#viewimage img').fadeOut(foTime, function(){
			$('#viewimage img').attr('src','');
			active = nr;
			$('#viewimage img').attr('src',images[nr]);
			$('#viewimage img').fadeIn(fiTime);
		});
	}
	
	$('#thumbs a').click(function(){
		var nr = $(this).children('img').attr('alt');
		var targWidth = imageWidth;
		var targHeight = imageHeight;
		openImageWin(nr, parseInt(targWidth)+30, parseInt(targHeight)+30);
		return false;
	});

	/* NAVIGATION WITHIN IMAGES */
	
	// for our friend, the IE 6
	$('#image img').click(function(){
		showImage(parseInt(active)+1);
		return false;
	});	

	$('#next').click(function(){
		showImage(parseInt(active)+1);
		return false;
	});
	
	$('#prev').click(function(){
		showImage(parseInt(active)-1);
		return false;
	});
	
	$('#viewnext').click(function(){
		showViewImage(parseInt(active)+1);
		return false;
	});
	
	$('#viewprev').click(function(){
		showViewImage(parseInt(active)-1);
		return false;
	});
	
	$('#viewclose').click(function(){
		self.close();
		return false;
	});
	
	$('#close').click(function(){
		$('#image').fadeOut(foTime, function(){
			$('#thumbs').fadeIn(fiTime);
			thumbs = true;
		});
		return false;
	});
	
	function openCommentWin(url){
		var width = 500;
		var height = 680;
		var x = Math.round(($(window).width() - width) / 2);
		var y = Math.round(($(window).height() - height) / 2);
		//var url = pagelevel + "forms/commentform.php";
		config = 'width=' + width + ',height=' + height + ', top=' + y + '. left=' + x + ', toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0';
		newWindow = window.open(url, 'myWin', config);
		newWindow.focus();
		newWindow.moveTo(x,y);
		return false;
		//alert("open image");
	}
	
	$('a.leavecomm').click(function(){
		var href = $(this).attr('href');
		openCommentWin(href);
		//alert(href);
		return false;
	});
	
	$('#closeThis').click(function(){
		self.close();
		return false;
	});
	
	$('.revealComments').click(function(){
		var nextDiv = $(this).parent().next('div.extraComments');
		$(this).toggleClass('hideMe');
		nextDiv.toggleClass('hideMe');
		return false;
	});
	/*$('.hideComments').click(function(){
		var prevDiv = $(this).parent().prev('div.extraComments');
		var prevLink = $(this).parent().prev('li').child('a');
		$(this).toggleClass('hideMe');
		prevDiv.toggleClass('hideMe');
		prevLink.toggleClass('hideMe');
		return false;
	});*/
	
	active = cur;

});