﻿(function($)
{
	var default_group='default';
	var ie6=(navigator.userAgent.indexOf('MSIE 6') >= 0);
	var images={
		list: {},
		active: null,
		visible: false,
		add: function(image){
			if(typeof this.list[image["group"]]=="undefined") this.list[image["group"]]=[];
			this.list[image["group"]].push(image);
			return this.list[image["group"]].length-1;
		},
		show: function(image, step){
			if(!this.visible){
				this.show_window();
				this.resize_box();
			}

			var image=image;
			this.active=image;

			var step=step||0;

			var image_el=$('#imagebox .photo_image')[0];
			var image_old_size=[image_el.width, image_el.height];

			image_el.src=image.src;

			if(!step && (!image["width"] || !image["height"])){
				image_el.src='/images/blank.gif';
				image_el.width=image_old_size[0];
				image_el.height=image_old_size[1];

				var preloader = new Image();
				preloader.onload = function()
				{
					image["width"]=image_el.width=preloader.width;
					image["height"]=image_el.height=preloader.height;

					preloader.onload = null;
					preloader = null;

					images.show(image, 1);
				};
				preloader.src = image.src;
			}
			else {
				image_el.width=image["width"];
				image_el.height=image["height"];

				this.resize_box();
			}

			$('#imagebox .photo_title').html(image["title"]);
			$('#imagebox .photo_count').html((image.index+1)+' из '+this.list[image["group"]].length);

			if(this.prev()){
				$('#imagebox .popup_arr_prev').show();
			}
			else {
				$('#imagebox .popup_arr_prev').hide();
			}
			if(this.next()){
				$('#imagebox .popup_arr_next').show();
			}
			else {
				$('#imagebox .popup_arr_next').hide();
			}

			this.preload_neighbors();
		},
		show_window: function(){
			this.visible=true;

			$('#imagebox,#imagebox-overlay').show();

			$(document).click(function(e){
				if(e.button>0) return true;
				e.preventDefault();
				images.hide();

				$(document).unbind("click");
			});
		},
		hide: function(){
			this.visible=false;

			$('#imagebox,#imagebox-overlay').hide();
		},
		resize_box: function(){
			var $body = $(this.ie6 ? document.body : document);
			$('#imagebox-overlay').css({
				width:		$body.width(),
				height:		$body.height()
			});
			delete $body;

			var pageScroll = getPageScroll();
			
			var nHeight = parseInt($('#imagebox').height(),10);
			var nWidth = parseInt($('#imagebox').children().first().width(),10);

			var nTop = pageScroll.yScroll + ($(window).height() /*frame height*/ - nHeight) / 2;
			var nLeft = pageScroll.xScroll + ($(window).width() /*frame height*/ - nWidth) / 2;

			$('#imagebox').css("left", nLeft).css("top", nTop);
		},
		prev: function(){
			if(!this.active) return false;
			if(this.active.index>0) return this.list[this.active.group][this.active.index-1];
			else return false;
		},
		next: function(){
			if(!this.active) return false;
			if(this.active.index<this.list[this.active.group].length-1) return this.list[this.active.group][this.active.index+1];
			else return false;
		},
		preload_neighbors: function(){
			if(!this.active) return;

			var prev = this.prev();
			var objPrev;
			if ( prev ) {
				objPrev = new Image();
				objPrev.onload = function()
				{
					prev["width"]=objPrev.width;
					prev["height"]=objPrev.height;

					objPrev.onload = null;
					objPrev = null;
				};
				objPrev.src = prev.src;
			}
			var objNext;
			var next = this.next();
			if ( next ) {
				objNext = new Image();
				objNext.onload = function()
				{
					next["width"]=objNext.width;
					next["height"]=objNext.height;

					objNext.onload = null;
					objNext = null;
				};
				objNext.src = next.src;
			}
		},
		show_prev: function(){
			if(!this.active) return false;
			var prev = this.prev();
			if(prev){
				this.show(prev);
			}
		},
		show_next: function(){
			if(!this.active) return false;
			var next = this.next();
			if(next){
				this.show(next);
			}
		}
	};

	$.fn.imagebox = function ( options )
	{
		$(this).each(function(){
			var obj = $(this);
			var image = {};
			image["src"]=$(obj).attr('href') || "";
			if(!image["src"]) return this;

			image["group"] = $(obj).attr('rel') || default_group;
			image["title"] = $(obj).attr('title') || "";

			$(this).unbind('click').click(function(e){
				e.preventDefault();
				e.stopPropagation();

				images.show(image);
			});

			image["index"]=images.add(image);
		});
		return this;
	}

	$(function(){
		$('#imagebox,#imagebox-overlay').remove();
		$('body').append('<div id="imagebox-overlay" style="display: none;"></div><div style="display: none;" id="imagebox">\
        <div class="ph-g-main_popup">\
            <div class="ph-g-main_p_top">\
            	<span class="photo_count">0 из 0</span>\
                <a class="close_popup" href="#"><b>закрыть</b></a>\
            </div>\
            <div class="ph-g-main_p_cen">\
                <div class="ph-g-main_p_inner">\
                    <a class="popup_arr_prev" href="#"><img src="/images/house_list_arr_left.png" alt=""/></a>\
                    <a class="popup_arr_next" href="#"><img src="/images/house_list_arr_right.png" alt=""/></a>\
                    <div class="ph-g-image_cont">\
                        <img src="" alt="" class="photo_image" />\
                    </div>\
                </div>\
            </div>\
            <div class="ph-g-main_p_bot">\
            	<span class="photo_title"></span>\
            </div>\
        </div>\
</div>');

		$('#imagebox .photo_image,#imagebox .ph-g-main_p_inner').unbind('click').click(function(e){
			e.preventDefault();
			e.stopPropagation();
		});
		$('#imagebox .popup_arr_prev').unbind('click').click(function(e){
			e.preventDefault();
			e.stopPropagation();
			images.show_prev();
		});
		$('#imagebox .popup_arr_next').unbind('click').click(function(e){
			e.preventDefault();
			e.stopPropagation();
			images.show_next();
		});
	});


		function getPageScroll( ) {
			var xScroll, yScroll;
			if (self.pageYOffset)
			{	// Some browser
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop)
			{	// Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body)
			{	// All other browsers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		}
})(jQuery);
