var ImageList = new Array();
var CurrentImage = null;

$(document).ready(function() {
	
	$('div.ImageCollection').bind("contextmenu",function(e){return false;});
	$('div.ImageCollectionBlock').bind("contextmenu",function(e){return false;});
	$('div#PopupDiv').bind("contextmenu",function(e){return false;});
	$('div#Media img').bind("contextmenu",function(e){return false;}); 
	
	$('div.Menu ul li').bind('mouseover', function() {
		$(this).children('ul').show();
	}).bind('mouseout', function() {
		$(this).children('ul').hide();
	});
	
	$('div#Media div.Medium').cycle({
		timeout : 4000,
		speed : 1500
	});
	
	$('a.iPopup').each(function() {
		var id = ImageList.push($(this));
		$(this).attr('imgID', id);
	}).bind('click', function(e) {
		var img = $(this).attr('href');
		var title = $(this).attr('title');
		var id = $(this).attr('imgID');
		PopupIMG(img, title, id);		
		return false;
	});

});

// Prints the current image to pdf
function PrintImage() {
	window.location = '?PrintImage=' + encodeURIComponent(ImageList[CurrentImage].attr('href')) + '&Name=' + encodeURIComponent(ImageList[CurrentImage].children('img').attr('alt'));
}

function PopupIMG(img, title, imgID) {

	CurrentImage = --imgID;
	
	$('#PopupDiv').width($(document).width()).height($(document).height());
	$('#PopupBackground').width($(document).width()).height($(document).height()).bind('click', ClosePopup ).attr('title', 'Click to close').css('opacity', 0.25);
	
	$(window).bind('resize', function() {
		// we zijn aan het resizen, dus opnieuw centreren!
		$('#PopupDiv').width($(window).width()).height($(window).height());
		$('#PopupBackground').width($(window).width()).height($(window).height());
	});	
	
	$('#PopupIMG').attr('src', img).attr('alt', title);
	$('#PopupHolder').css('opacity', 0).animate({
		opacity: 1
	}, 1000);
	
	$('#PopupDiv').show();
}

function ClosePopup() {
	$('#PopupHolder').animate({
		opacity: 0
	}, 750, function() {
		$('#PopupDiv').hide();
	});
	
}

function PopupNext() {
	var count = ImageList.length;
	
	if ( CurrentImage == count - 1 ) {
		var next = 0;
	} else {
		var next = CurrentImage + 1;
	}

	ShowIMG(next);
}

function PopupPrevious() {
	var count = ImageList.length;
	
	if ( CurrentImage == 0 ) {
		var next = count - 1;
	} else {
		var next = CurrentImage - 1;
	}	
	
	ShowIMG(next);
}

function ShowIMG(id) {

	//$('#PopupIMG').attr('src', ImageList[id].attr('href')).attr('alt', ImageList[id].attr('title'));
	
	$('#PopupIMG').animate({
		opacity: 0
	}, 1000, '', function() {
		$(this).attr('src', ImageList[id].attr('href')).attr('alt', ImageList[id].attr('title')).animate({
			opacity: 1
		}, 1000);
	});
	
	CurrentImage = id;
}
