var console;
var d = function($var) {
	if (null == console) {
		alert($var);
	} else {
		console.log($var);
	}
};

$(function(){
	// Просмотр галереи
	var $photo = $('.photo');
	if ($photo.length > 0) {
		var openedWindows = new Array();
		$('a', $photo).click(function(){
			for(i = 0; i < openedWindows.length; i++) {
				openedWindows.pop().close();
			}
			var $this = $(this);
			var $image = $('img', $this);
			var params = 'width=' + 700 + ',height=' + 400 + 'menubar=no,status=no,resizable=no';
			
			var imgWindow = window.open($this.attr('href'), '', params);
			imgWindow.onload = function() {
				var $img = $(imgWindow.document.images[0]);
				if (imgWindow.innerHeight) {
					imgWindow.innerHeight = $img.height();
					imgWindow.innerWidth = $img.width();
				}
			}
			openedWindows.push(imgWindow);
			return false;
		});
	}
	
	// Предросмотр 3д проекции
	var $3d = $('.3d');
	if ($3d.length > 0) {
		var openedWindows = new Array();
		$3d.click(function() {
			for(i = 0; i < openedWindows.length; i++) {
				openedWindows.pop().close();
			}
			var $this = $(this);
			var $image = $('img', $this);
			var params = 'width=' + 700 + ',height=' + 400 + 'menubar=no,status=no,resizable=no';
			var imgWindow = window.open($this.attr('href'), '', params);
			
			openedWindows.push(imgWindow);
			return false;
		});
	}
	
	var $preview = $('a[class^=preview]');
	if ($preview.length > 0) {
		var openedWindows = new Array();
		$preview.click(function() {
			for(i = 0; i < openedWindows.length; i++) {
				openedWindows.pop().close();
			}
			var $this = $(this);
            var size = $this.attr('class').split('_');
            var url = '/previewimg/' 
                    + '?img=/' + $this.attr('href').replace(/(..\/)+/, '')
                    + '&title=' + $this.attr('title');

			var params = 'width=' + size[1] + ',height=' + size[2] + 'menubar=no,status=no,resizable=no';
			var imgWindow = window.open(url, '', params);
			
			openedWindows.push(imgWindow);
			return false;
		});
	}
});

function windowSize(imgWindow) {
	var w = 0;
	var h = 0;

	// IE
	if (!imgWindow.innerWidth) {
		// strict mode
		if (!(imgWindow.document.documentElement.clientWidth == 0)) {
			w = imgWindow.document.documentElement.clientWidth;
			h = imgWindow.document.documentElement.clientHeight;
		}
		// quirks mode
		else {
			w = imgWindow.document.body.clientWidth;
			h = imgWindow.document.body.clientHeight;
		}
	}
	// w3c
	else {
		w = imgWindow.innerWidth;
		h = imgWindow.innerHeight;
	}
	return {
		width :w,
		height :h
	};
}