var marginBetweenPics = 5;
var minMarginHoriz = 10;
var minMarginVerti = 10;
var imageRatioWeek = 0;
var imageRatioPhoto = 0;
var urlArray;

jQuery(document).ready(function()
{
	url = location.pathname;
	urlArray = url.split("/");
	//alert(urlArray[1]);
	if(urlArray[1] == "weekfoto")
	{
		setImageRatioWeek();
		weekNR = urlArray[(urlArray.length - 1)];
		yearNR = urlArray[(urlArray.length - 2)];
		theLI = 'LI#week-' + yearNR + '-' + weekNR;
		jQuery("#scrollContainer").scrollTo(theLI);
	}
	else if(urlArray[1] == 'collectie')
	{
		setImageRatioGallery();
	}
});

function resizeContent()
{
	resizeBasic();
	//resizePhoto();
	resizeHome();
	if(urlArray[1] == "weekfoto")
	{
		resizePictureByDate();
	}
	else if(urlArray[1] == 'collectie')
	{
		resizePhotoGallery();
	}
}

function resizeBasic()
{
	
	windowWidth = jQuery(window).width();
	windowHeight = jQuery(window).height();
	//alert(windowWidth + 'x' + windowHeight);
	//Setting container width & height
	jQuery('.container').width(windowWidth);
	jQuery('.container').height(windowHeight);
	
	//Setting Content width & height
	contentWidth = windowWidth - 370;
	console.log('windowWidth: ' + windowWidth);
	jQuery('.content').width(contentWidth);
	jQuery('.content').height(windowHeight);
	
	//Setting Menu height;
	jQuery('.sidebar1').height(windowHeight);
}

function resizeHome()
{
	contentWidth = jQuery('.content').width();
	contentHeight = jQuery('.content').height();
	console.log('contentHeight: ' + contentHeight + ' | contentWidth: ' + contentWidth);
	jQuery('.photoList .photo').css('margin',marginBetweenPics);
	
	imageRatio = jQuery('.photoList .photo A IMG.original').width()/jQuery('.photoList .photo A IMG.original').height();
	
	if(contentHeight>800 && contentWidth > 655)
	{
		imageWidth = 320;
		imageHeight = imageWidth/imageRatio;
		
		jQuery('.photoList .photo A IMG.original').width(imageWidth);
		jQuery('.photoList .photo A IMG.original').height(imageHeight);
		
		pageMarginLeft = (contentWidth-2*imageWidth-marginBetweenPics)/2;
		jQuery('UL.pageList').css('margin-left',pageMarginLeft);
		
		pageMarginTop = (contentHeight-3*imageHeight-2*marginBetweenPics)/2;
		jQuery('UL.pageList').css('margin-top',pageMarginTop);
	}
	else
	{
		imageHeight = (contentHeight - 2*minMarginVerti - 2*marginBetweenPics)/3;
		imageWidthFromHeight = imageRatio*imageHeight;
		
		imageWidth = (contentWidth - 2*minMarginHoriz - 6*marginBetweenPics)/2;
		imageHeightFromWidth = imageWidth/imageRatio;
		
		if(imageWidthFromHeight <= imageWidth)
		{
			imageWidth = imageWidthFromHeight;	
		}
		else
		{
			imageHeight = imageHeightFromWidth;	
		}
		
		pageListWidth = 2*imageWidth + 6*marginBetweenPics;
		jQuery('UL.pageList LI.page').width(pageListWidth);
		jQuery('UL.photoList').width(pageListWidth);
		jQuery('.photoList .photo A IMG.original').width(imageWidth);
		jQuery('.photoList .photo A IMG.original').height(imageHeight);
		
		pageMarginLeft = (contentWidth-pageListWidth)/2;
		pageMarginTop = (contentHeight-3*imageHeight - 6*marginBetweenPics)/2;
		
		jQuery('UL.pageList').css('margin-left',pageMarginLeft);
		jQuery('UL.pageList').css('margin-top',pageMarginTop);
	}
}

function setImageRatioWeek()
{
	jQuery("#weekPhotoImg").hide();
	theImage = new Image();
	theImage.src = jQuery('#weekPhotoImg').attr('src');
	
	theImage.onload = function()
	{
		jQuery("#weekPhotoImg").fadeIn();
		imageWidth = theImage.width;
		imageHeight = theImage.height;
		imageRatioWeek = imageWidth/imageHeight;
		//console.log('SET!!! Ratio: (' + imageWidth + '/' + imageHeight + ') = ' + imageRatio + '');
	}
}

function setImageRatioGallery(imageRatio)
{
	if(imageRatio == "set")
	{
		theImage = new Image();
		theImage.src = jQuery('#photoBig IMG').attr('src');
		
		theImage.onload = function()
		{
			imageWidth = theImage.width;
			imageHeight = theImage.height;
			imageRatioPhoto = imageWidth/imageHeight;
			//console.log('SET!!! Ratio: (' + imageWidth + '/' + imageHeight + ') = ' + imageRatio + '');
		}
	}
	else
	{
		imageRatioPhoto = imageRatio;
	}
}

/*
function resizePhoto()
{
	resizeScrollThumbnails();
	
	contentHeight = jQuery('.content').height();
	contentWidth = jQuery('.content').width();
	
	thumbnailWidth = jQuery('.slide').width() + 10;
	
	
	maxWidth = contentWidth - thumbnailWidth - 2 * minMarginHoriz;
	maxHeight = contentHeight - 2*minMarginVerti;
	
	
	
	imageHeight = maxHeight;
	imageWidthFromHeight = imageHeight*imageRatio;
	
	imageWidth = maxWidth;
	imageHeightFromWidth = imageWidth/imageRatio;
	
	if(imageWidthFromHeight <= imageWidth)
	{
		//alert('sit1: ' + imageHeight + ' - ' + imageWidthFromHeight);
		imageWidth = imageWidthFromHeight;
	}
	else
	{
		//alert('sit2: ' + imageWidth + ' - ' + imageHeightFromWidth + ' - RATIO:' + imageRatio);
		imageHeight = imageHeightFromWidth;	
	}
	
	jQuery('IMG#photoGallery-image-view').width(imageWidth);
	jQuery('IMG#photoGallery-image-view').height(imageHeight);
	
	marginLeft = (maxWidth - imageWidth)/2;
	marginTop = (contentHeight - imageHeight)/2;
	jQuery('DIV#photoBig').width(maxWidth);
	jQuery('DIV#photoBig').height(contentHeight);
	jQuery('IMG#photoGallery-image-view').css('margin-left',marginLeft);
	jQuery('IMG#photoGallery-image-view').css('margin-top',marginTop);
}
*/

function resizeScrollThumbnails()
{
	contentHeight = jQuery('.content').height();
	contentWidth = jQuery('.content').width();
	
	buttonHeight = jQuery('IMG.up').height();
	jQuery('.slide').height(contentHeight);
	jQuery('.scrollcontainer').height((contentHeight - 2*buttonHeight-2));
	
}

function resizePictureByDate()
{
	contentHeight = jQuery('.content').height();
	contentWidth = jQuery('.content').width();
	
	thumbnailWidth = jQuery('#weekPhotoScroll').width() + 10;
	
	buttonHeight = jQuery('IMG.up').height();
	
	//jQuery('.week_photo_appendix"').height(contentHeight);
	resizeScrollThumbnails();
	
	/*Image Resize */
	
	console.log('thumbnailWidth: ' + imageRatioWeek);
	
	maxWidth = contentWidth - thumbnailWidth - (2*minMarginHoriz);
	console.log('maxWidth = ' + contentWidth + ' - ' + thumbnailWidth + ' - ' + (2*minMarginHoriz))
	maxHeight = contentHeight - (2*minMarginVerti);
	
	console.log('maxWidth: ' + maxWidth + ' maxHeight: ' + maxHeight);
	
	imageHeight = maxHeight;
	imageWidthFromHeight = Math.round(imageHeight*imageRatioWeek);
	
	imageWidth = maxWidth;
	imageHeightFromWidth = Math.round(imageWidth/imageRatioWeek);
	
	if(imageWidthFromHeight <= imageWidth)
	{
		//alert('sit1: ' + imageHeight + ' - ' + imageWidthFromHeight);
		imageWidth = imageWidthFromHeight;
	}
	else
	{
		//alert('sit2: ' + imageWidth + ' - ' + imageHeightFromWidth + ' - RATIO:' + imageRatio);
		imageHeight = imageHeightFromWidth;	
	}
	
	marginLeft = Math.round((maxWidth - imageWidth)/2) + minMarginHoriz;
	marginTop = Math.round((contentHeight - imageHeight)/2);
	
	console.log('(' + maxWidth + ' - ' + imageWidth + ')/2');
	
	console.log('marginLeft: ' + marginLeft + ' | marginTop:  ' + marginTop);
	
	console.log('imgWidth: ' + imageWidth + ' imgHeight: ' + imageHeight);
	
	jQuery('IMG#weekPhotoImg').width(imageWidth);
	jQuery('IMG#weekPhotoImg').height(imageHeight);
	
	jQuery('IMG#weekPhotoImg').css('margin-left',marginLeft);
	jQuery('IMG#weekPhotoImg').css('margin-top',marginTop);
	
}

function resizePhotoGallery()
{
	contentHeight = jQuery('.content').height();
	contentWidth = jQuery('.content').width();
	
	thumbnailWidth = jQuery('#photoGalleryThumbnails').innerWidth() + 10;
	
	buttonHeight = jQuery('IMG.up').height();
	
	//jQuery('#photoGallery"').height(contentHeight);
	resizeScrollThumbnails();
	
	/*Image Resize */
	
	//console.log('thumbnailWidth: ' + imageRatioWeek);
	
	maxWidth = contentWidth - thumbnailWidth - (2*minMarginHoriz);
	console.log('maxWidth = ' + contentWidth + ' - ' + thumbnailWidth + ' - ' + (2*minMarginHoriz))
	maxHeight = contentHeight - (2*minMarginVerti);
	
	console.log('maxWidth: ' + maxWidth + ' maxHeight: ' + maxHeight);
	
	imageHeight = maxHeight;
	imageWidthFromHeight = Math.round(imageHeight*imageRatioPhoto);
	
	imageWidth = maxWidth;
	imageHeightFromWidth = Math.round(imageWidth/imageRatioPhoto);
	
	if(imageWidthFromHeight <= imageWidth)
	{
		//alert('sit1: ' + imageHeight + ' - ' + imageWidthFromHeight);
		imageWidth = imageWidthFromHeight;
	}
	else
	{
		//alert('sit2: ' + imageWidth + ' - ' + imageHeightFromWidth + ' - RATIO:' + imageRatio);
		imageHeight = imageHeightFromWidth;	
	}
	
	marginLeft = Math.round((maxWidth - imageWidth)/2) + minMarginHoriz;
	marginTop = Math.round((contentHeight - imageHeight)/2);
	
	console.log('(' + maxWidth + ' - ' + imageWidth + ')/2');
	
	console.log('marginLeft: ' + marginLeft + ' | marginTop:  ' + marginTop);
	
	console.log('imgWidth: ' + imageWidth + ' imgHeight: ' + imageHeight);
	
	jQuery('#photoBig IMG').width(imageWidth);
	jQuery('#photoBig IMG').height(imageHeight);
	
	jQuery('#photoBig IMG').css('margin-left',marginLeft);
	jQuery('#photoBig IMG').css('margin-top',marginTop);
	
}

jQuery(window).resize(function() { resizeContent(); });
window.onload = function () { resizeContent() };
