(function($){

	
$.fn.gsom = function(options) {

	var loadCounter;
	var loadingMessage;
	
	var gDark;
	var gLoad;
	var gBox;
	var gContent;
	var gSource;
	var gWidth;
	var gWrapper;
	var gTable;
	var gItem;

	
	var targets = this;
	var sourcelinks = this;
	
	//var position = ($.browser.msie) ? 'absolute':'fixed';
	//Контент обрезается, если не поместился во вьюпорт. Fail.
	
	var position = 'absolute';	
	
	
	options = $.extend({
		scroll: true,
		loadingImage: '',
		galleryClass: '.gallery',
		loadingMessage: 'Loading...',
		closeClass: 'gclose',
		closeByContentClick: false
	}, options || {});

	/*
	targets.each(function(i, n){
			
		n.gallery = $(n).closest(options.galleryClass)[0];
		n.siblings = jQuery.grep(targets, function(m, k){
			//console.log($(m).closest(options.galleryClass)[0] ==  n.gallery);
			return ( $(m).closest(options.galleryClass)[0] == n.gallery);
    	});
    	
	})
	*/

	
/*--------------------------------*/
	
	if (options.loadingImage) {} else
	if (options.loadingString) {}


/*--------------------------------*/
	function getSource(what) {

		var splitted = what.href.split('#');

		if (splitted.length<2) {
			return what;
		} else {
			var source = $(document.getElementById(splitted[1]));
			return source
		}
		
	}	
/*--------------------------------*/
	
	function AjaxedLink(dom, all_links) {
		var self = this;
		self.html = dom;
		self.$html = $(dom);
		self.$html.click(function(){
			self.clicked();
			return false;
		})
		
		//console.log($(all_links[0]).parents());
		self.gallery = self.$html.closest(options.galleryClass)[0];
		self.siblings = $.grep(all_links, function(n, i){
			return ( $(n).closest(options.galleryClass)[0] == self.gallery);
    	});
    	
	}
	
	
	var t2 = targets;
	targets = [];
	t2.each(function(i,n){
		targets[i] = new AjaxedLink(n, sourcelinks);
	})
	
/*--------------------------------*/	
	
	AjaxedLink.prototype.clicked = function() {
		
		var instance = this;
		
		
		gItem = instance.html;
		
		$('.gsom').remove();
		
		createWrappers();
		
		createShadow();
		
		
		gSource = getSource(instance.html);

		createLoadbox();
		
		if (gSource.href) {

			
			
			if (options.justImage) {
				var img = document.createElement('img');
				img.src = gSource.href;
				instance.createBox(img);
				
			} else {
				
				$.get(gSource.href, {gsom: true, lulz: Math.random()}, function(data){
					
					instance.createBox(data);
						
					return false;
				});
			}
		} else {
			instance.createBox(gSource.html());
		}
		return false;
	};

/*--------------------------------*/

	
	
/*--------------------------------*/

	function createWrappers(){
		
		gBox = $(document.createElement('div'))
			.addClass('gsom')
			.appendTo('body');



		gSecond = $(document.createElement('div'))
			.addClass('gsecond')
			.appendTo(gBox)
		

		gWrapper = $(document.createElement('div'))
			.addClass('gWrapper')
			.appendTo(gSecond)
	

			
		gBox.css({
			position: position,
			height: $(document).height(),
			width: '100%',
			top: 0,
			left: 0,
			overflow: 'hidden',
			zIndex: 666

		});


		
		if ($.browser.msie) {
				gBox.css({
					height: $(document).height()
				});
			
			gSecond.css({
				position: 'absolute',
				top: $(window).scrollTop() + $(window).height()/2,
				left: '50%',
				cssFloat: 'left',
				height: 'auto'			
			});
			gWrapper.css({
				position: 'relative',
				top: '-50%',
				left: '-50%'			
			});			
			
		} else {
			
			gSecond.css({
				display: 'table',
				margin: '0 auto',
				height: $(window).height(),
				position: 'relative',
				top: $(window).scrollTop()
			});
			gWrapper.css({
				display: 'table-cell',
				verticalAlign: 'middle'
			});		
			
		}
		
		

	}

	function createShadow() {
		gDark = $(document.createElement('div'))
			.addClass('gDark')
			.css({
				position: position,
				top: 0,
				left: 0,
				background: 'black',
				opacity: 0.7,
				height: $(document).height(),
				width: '100%',
				overflow: 'hidden',
				display: 'none'
			})
			.click(gsomClose)
			.prependTo(gBox);
			
			if ($.browser.msie) {
				selectFix();
				gDark.show();
			} else {
				gDark.fadeIn(300);
			}
	}

	function createLoadbox() {
		gLoad = $(document.createElement('div'))
			.addClass('gLoad')
			.text(options.loadingMessage)
			.css({
				position: 'relative',
				cssFloat: 'left',
				color: 'white',
				fontWeight: 'bold'
			})
			.appendTo(gWrapper);
	}
	

	AjaxedLink.prototype.createBox = function(data) {
			var self = this;

			gTable = $('<table><tr><td></td></tr></table>')
			.css({
				margin: '0 auto',
				display: 'none',
				position: 'relative'
			})
			.appendTo(gWrapper);

			gContent = $('td', gTable)
				.addClass('gContent')
				.html(data)
				.css('position', 'relative')

			
			$('.' + options.closeClass, gBox).click(gsomClose);
			
			gDark.click(gsomClose);
			gLoad.remove();

			
			
			if (options.scroll) createControlz(self);
			gTable.fadeIn(300)
			
			if ($.browser.msie && ($.browser.version <8) ) {
				var diff = gWrapper.height()+gWrapper.offset().top - $(document).height() ;
			} else {
				var diff = gSecond.height()+gSecond.offset().top - $(document).height() ;
			}

			if (diff > 0) {
				gSecond.css('top', gSecond.offset().top - diff - 100)
			}
			
			
			
	}
/*--------------------------------*/

function createControlz(instance) {
	
	
	var arrow;
	var id;
	var zero;
	
	
	var gControlz = $(document.createElement('div'))
	.css({
		clear: 'both',
		position: 'relative'
	})
	.appendTo(gContent);
	
	
	
	
	for (var i=0; i<2; i++) {
		var control = $(document.createElement('span'))
		.addClass('gControl')
//		.attr('href', '#')
		.css({
			cursor: 'pointer',
			'_cursor': 'hand',
			position: 'absolute',
			top: 0,
			width: '50%',
			padding: '5px 0',
			textAlign: 'center',
			textDecoration: 'none',
			color: 'white',
			fontWeight: 'bold',
			fontFamily: 'arial',
			fontSize: '20px'
		})
		.one('click', function(){
			gScroll.call(this, instance);
			return false;
		})
		.appendTo(gControlz);
	
		if (i==0) {
			control
				.html('&#x25C4;')
				.data('dir', -1)
				.css('left', 0)
	
		} else {
			control
				.html('&#x25BA;')
				.data('dir', 1)
				.css('right', 0)
		}
	
	}
	
	//gScrollPosition();
	
}

function gScroll(instance) {
	var button = $(this);	
	var dir = button.data('dir');
	
  	var sameGalleryItems = instance.siblings;
  	
	var index = $(sameGalleryItems).index(gItem) + dir;
	
	if (index==sameGalleryItems.length) {
		index = 0;
	} else if (index<0) {
		index = sameGalleryItems.length -1;
	}
	var nextItem = sameGalleryItems[index];
	
	
	

	
	
	if (options.justImage) {
		var img = '<img src="'+ nextItem.href +'" alt=""/>';
		gContent.html(img);
		button.one('click', gScroll);
		gItem = nextItem;
		createControlz(instance);		
	} else {
		$.get(nextItem.href, {gsom: true, lulz: Math.random()}, function(data){
			gContent.html(data);
			$('.' + options.closeClass, gBox).click(gsomClose);
			button.one('click', gScroll);
			gItem = nextItem;
			createControlz(instance);
		});
	}
	return false;
	
	
}	

/*--------------------------------*/		
/*--------------------------------*/	
	function gsomClose(){
		gLoad.remove();
		
		if ($.browser.msie) {
			gBox.remove();
			gDark.remove();
		} else {
			gBox.fadeOut(300, function(){
				gBox.remove();
				gDark.fadeOut(300, function(){
					gDark.remove();
				});
			});
		}
			return false;

		
	}
/*--------------------------------*/	
	

	
	function selectFix() {
		$('select')
		.each(function(){
			var s = $(this);	
			var iframe = $(document.createElement('iframe'))
			.css({
				position: 'absolute',
				left: s.offset().left,
				top: s.offset().top,
				width: s.outerWidth(),
				height: s.outerHeight(),
				filter: 'mask()',
				zIndex: -1
			})
			.appendTo(gDark)
		})
		
	}
	
}

})(jQuery)




