var photoArr = new Array();
var curKey;

function showPhoto(id, module, arr) {
	photoArr = arr;
	
	if (curKey == null) skinModal(module);
	paginate(id);
	try {
		$.modal.close();
	} catch(e) {}
	$('#modalcontainer').modal({
		close:false,
		position: ["25px",],
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			var iframe = '<iframe src ="/photo/view/' + id + '_large.jpg/' + module + '" width="735" height="530" frameborder="0" scrolling="no"></iframe>';
			dialog.data.find('.modal_content').html(iframe);
			updatePagination();
		}
	});
}

function skinModal(module) {
	$('#modal_buttons').html('<div class="no modalClose" onclick="$.modal.close();"><img src="/images/buttons/modal_close.gif" /></div>');
	$('#modal_left').html('<img src="/images/buttons/modal_prev.gif" onclick="browsePrev(\'' + module + '\');" />');
	$('#modal_right').html('<img src="/images/buttons/modal_next.gif" onclick="browseNext(\'' + module + '\');" />');
}

function paginate(id) {
	var count = 0;
	for (var i = 0; i < photoArr.length; ++i) {
		if (photoArr[i] == id) curKey = i;
		++count;
	}
}

	function updatePagination() {
		if (curKey == 0) {
			$('#modal_left').css('display', 'none');
		} else {
			$('#modal_left').css('display', 'block');
		}
		if (curKey == (photoArr.length - 1)) {
			$('#modal_right').css('display', 'none');
		} else {
			$('#modal_right').css('display', 'block');
		}		
	}

function browsePrev(module) {
	if (curKey > 0) {
		showPhoto(photoArr[curKey - 1], module, photoArr);
	}
}

function browseNext(module) {
	if (curKey < (photoArr.length - 1)) {
		showPhoto(photoArr[curKey + 1], module, photoArr);
	}	
}

function showPhotoFlickr(id, module, arr) {
	photoArr = arr;
		
	if (curKey == null) skinModalFlickr(module);
	paginate(id);

	try {
		$.modal.close();
	} catch(e) {}
	$('#modalcontainer').modal({
		close:false,
		position: ["25px",],
		overlayId:'confirmModalOverlay',
		containerId:'flickrModalContainer', 
		onShow: function (dialog) {
			var iframe = '<iframe src ="/photo/flickr/' + id + '/" width="650" height="500" frameborder="0" scrolling="no"></iframe>';
			dialog.data.find('.modal_content').html(iframe);
			updatePagination();
		}
	});
}

function resizePhoto(w, h) {		
	$('#flickrModalContainer').css({
		width: w + 10,
		height: h + 40
	});
	
	$('#flickrModalContainer iframe').css({
		width: w + 10,
		height: h + 40
	});
	
	
}

function skinModalFlickr(module) {
	$('#modal_buttons').html('<div class="no modalClose" onclick="$.modal.close();"><img src="/images/buttons/modal_close.gif" /></div>');
	$('#modal_left').html('<img src="/images/buttons/modal_prev.gif" onclick="browsePrevFlickr(\'' + module + '\');" />');
	$('#modal_right').html('<img src="/images/buttons/modal_next.gif" onclick="browseNextFlickr(\'' + module + '\');" />');
}

function browsePrevFlickr(module) {
	if (curKey > 0) {
		showPhotoFlickr(photoArr[curKey - 1], module, photoArr);
	}
}

function browseNextFlickr(module) {
	if (curKey < (photoArr.length - 1)) {
		showPhotoFlickr(photoArr[curKey + 1], module, photoArr);
	}	
}

