﻿$(document).ready(function(){
		$.ajaxSetup({
		    beforeSend: function (request) {
		      request.setRequestHeader('Accept', 'application/html+ajax');
		    }
		  })
});


jQuery.fn.resizeImage = function (boxSize) {
	boxSize = boxSize || 100;
	return this.each(function () {
		var max, width, height;
		if (this.width && this.height) {
			max = this.height > this.width  ? { dim: 'h', val: this.height } : { dim: 'w', val: this.width };
			if (max.val <= boxSize) {
				height = max.dim == 'h' ? max.val : this.height;
				width  = max.dim == 'w' ? max.val : this.width;
			} else {
				height = max.dim == 'h' ? boxSize : Math.floor(this.height * boxSize / max.val);
				width  = max.dim == 'w' ? boxSize : Math.floor(this.width * boxSize / max.val);
			}
			$(this).css({
				height: height,
				width: width
			});
		}
	});
}

$(function () {
	$('#body div.content a[rel="photo-gallery"]')
		.wrap('<div class="image-zero-wrapper"><div class="image-first-wrapper"><div class="image-second-wrapper"></div></div></div>')
		.colorbox({
			photo: true,
			scalePhotos: true,
			maxWidth: function () {
				return $(window).width() - 20;
			},
			maxHeight: function () {
				return $(window).height() - 20;
			},
			rel: 'photo-gallery',
			slideshowStart: "Начать показ слайдов",
			slideshowStop: "Остановить показ слайдов",
			current: "{current} из {total}",
			previous: "предыдущее",
			next: "следующее",
			close: "закрыть"
		});
});

$(window).load(function () {
	var gg = $('#body div.content a[rel="photo-gallery"] img')
		.resizeImage(211)
		.parents('div.image-first-wrapper')
		.each(function () {
			var $img = $('img:first', this);
			if ($img.length && $img.width() > 1) {
				$(this).css({
					marginBottom: Math.floor((211 - $img.get(0).height) / 2),
					width: $img.get(0).width
				});
			}
		});
});
// FOO function
function afterAjax(){return true;}

