var WindowProperties = function(){};
WindowProperties.prototype = {
	GetWindowScrollTop: function(){
		var WindowScrollTop = $(window).scrollTop();
		return WindowScrollTop;
	},
	GetWindowClientHeight: function(){
		var WindowHeight = $(window).height();
		return WindowHeight;
	},
	GetWindowClientWidth: function(){
		var WindowWidht = $(window).width();;
		return WindowWidht;
	}
};

var PopUp = function(options) {
	this._window = new WindowProperties();
	this.SetOptions(options);

	this.Marks = $(this.options.Marks);
	this.Wrapper = $("#" + this.options.Wrapper);

	this.Pointer = $("#" + this.options.Pointer);

	this._Pointer_width = this.Pointer.outerWidth();
	this._Marks_height = this.Marks.height();
	this._Marks_width = this.Marks.outerWidth();
	this._Wrapper_height = this.Wrapper.outerHeight();
	this._Wrapper_width = this.Wrapper.outerWidth();

	this._img = new Image();
	this.BigImage = $("#" + this.options.BigImage);
	this.ImgSrc = [];
	this.ImgWidth = this.options.ImgWidth;
	this.ImgHeight = this.options.ImgHeight;
	this.LoadingSrc = this.options.LoadingSrc;
	this.loadingImage = $("<img alt=\"загрузка...\" title=\"грузится картинка...\" src=\"" + this.LoadingSrc + "\" />");

	this.Link = $(this.options.Link);
	this.ImageLink = $("#" + this.options.ImageLink);
};

PopUp.prototype = {
	SetOptions: function(options) {
		this.options = {
			Marks: ".load_list",
			Link: ".pro_link",
			Wrapper: "pop",
			Pointer: "pointer",
			BigImage: "BigImage",
			ImageLink: "ImgLink",
			ImgWidth: 263,
			ImgHeight: 263,
			LoadingSrc: templatesUrl + "Styles/images/2009/search/images/loading.gif"
		};
		$.extend(this.options, options || {});
	},
	/*=====================================================================*/
	SetPosition: function() {
		var obj_this = this;
		for (var i = 0; i < this.Marks.length; i++) {
			(function() {
				var p = i;
				$(obj_this.Marks[p]).mouseover(function() {
					var _WindowScrollTop = obj_this._window.GetWindowScrollTop();
					var _WindowHeight = obj_this._window.GetWindowClientHeight();
					var _WindowWidth = obj_this._window.GetWindowClientWidth();

					var Marks_offset_top = $(obj_this.Marks[p]).offset().top;
					var Marks_offset_left = $(obj_this.Marks[p]).offset().left;
					
					
					var zz = _WindowHeight - (Marks_offset_top - _WindowScrollTop); //pointer to screen
					var dd = Marks_offset_top - (obj_this._Wrapper_height - zz);
					var ee = _WindowWidth - Marks_offset_left;

					obj_this.Pointer.css({ top: parseInt(Marks_offset_top + obj_this._Marks_height) + "px" });

					if (zz > obj_this._Wrapper_height) {
						obj_this.Wrapper.css({ top: parseInt(Marks_offset_top - obj_this._Wrapper_height / 4) + "px" });
					} else {
						obj_this.Wrapper.css({ top: parseInt(dd) + "px" });
					}

					if (ee > obj_this._Wrapper_width) {
						obj_this.Pointer.removeClass("ri");
						obj_this.Wrapper.removeClass("ri");
						obj_this.Pointer.css({ left: parseInt(Marks_offset_left + 10) + "px" });
						obj_this.Wrapper.css({ left: parseInt(Marks_offset_left + 10) + "px" });
					} else {
						obj_this.Pointer.addClass("ri");
						obj_this.Wrapper.addClass("ri");
						obj_this.Pointer.css({ left: parseInt(Marks_offset_left - obj_this._Marks_width / 8) + "px" });
						obj_this.Wrapper.css({ left: parseInt(Marks_offset_left - obj_this._Wrapper_width - obj_this._Marks_width / 8 + 21) + "px" });
					}
				})
				obj_this.Marks[p].onclick = function() { return false; };
			})();
		}
	},
	/*=====================================================================*/
	LoadImage: function() {
		var obj_this = this;
		for (var i = 0; i < this.Marks.length; i++) {
			this.ImgSrc.push($(this.Marks[i]).attr("href"));
			(function() {
				var p = i;
				$(obj_this.Marks[p]).bind("mouseenter", function() {
					obj_this.ImageLink.attr("href", "#blank");
					obj_this.BigImage.attr("src", "../Styles/images/2009/search/images/blank.gif");
					obj_this.BigImage.hide();
					obj_this.BigImage.after(obj_this.loadingImage);
					$(obj_this._img).load(function() {
						if (obj_this._img.width == 0 || obj_this._img.height == 0) return;
						obj_this.PreLoad(obj_this._img.src, $(obj_this.Link[p]).attr("href"));
						this._img = new Image();
					});
					obj_this._img.src = obj_this.ImgSrc[p];
				});
			})();
		}
	},
	/*=====================================================================*/
	PreLoad: function() {
		this.AutoScaling();
		this.BigImage.attr("src", arguments[0]);
		this.loadingImage.remove();
		this.BigImage.show();
		this.ImageLink.attr("href", arguments[1]);
	},
	/*=====================================================================*/
	AutoScaling: function() {
		if (this._img.width > 0 && this._img.height > 0) {
			if (this._img.width / this._img.height >= this.ImgWidth / this.ImgHeight) {
				if (this._img.width > this.ImgWidth) {
					this.BigImage.width(this.ImgWidth);
					this.BigImage.height((this._img.height * this.ImgWidth) / this._img.width);
				}
				else {
					this.BigImage.width(this._img.width);
					this.BigImage.height(this._img.height);
				}
			}
			else {
				if (this._img.height > this.ImgHeight) {
					this.BigImage.height(this.ImgHeight);
					this.BigImage.width((this._img.width * this.ImgHeight) / this._img.height);
				}
				else {
					this.BigImage.width(this._img.width);
					this.BigImage.height(this._img.height);
				}
			}
		}
		this._img = new Image();
	},
	/*=====================================================================*/
	Start: function() {
		this.SetPosition();
		this.LoadImage();
		var obj_this = this;

		for (var i = 0; i < this.Marks.length; i++) {
			$(this.Marks[i]).bind("mouseenter", function() {
				obj_this.Wrapper.css({ display: "block" });
				obj_this.Pointer.css({ display: "block" });
			}).bind("mouseleave", function() {
				obj_this.Wrapper.css({ display: "none" });
				obj_this.Pointer.css({ display: "none" });
			});
		}

		this.Pointer.bind("mouseenter", function() {
			obj_this.Wrapper.css({ display: "block" });
			obj_this.Pointer.css({ display: "block" });
		}).bind("mouseleave", function() {
			obj_this.Wrapper.css({ display: "none" });
			obj_this.Pointer.css({ display: "none" });
		});

		this.Wrapper.bind("mouseenter", function() {
			obj_this.Wrapper.css({ display: "block" });
			obj_this.Pointer.css({ display: "block" });
		}).bind("mouseleave", function() {
			obj_this.Wrapper.css({ display: "none" });
			obj_this.Pointer.css({ display: "none" });
		});
	}
};

