(function($) {
$.fn.stretchGallery = function(options) {

	if(options) if(!options.elements) options.elements = [$(this).attr('href')];
	else if(options) if(!options.elements) options.elements = [$(this).attr('href')];
	
	$.extend(defVals, options);

	prepare();
		
	$(this).click(function(){
		(defVals.turned == 0) ? turnLayerOn() : false;
		return false;
	});
	
	$('#stretchGallery_close').click(function(){
		(defVals.turned == 1) ? turnLayerOff() : false;
		return false;
	});
	
	return this;
};

turnLayerOn = function(){
	
	$('#stretchGallery_layerBg').height($('html').height()).width($('html').width()).fadeTo('slow', defVals.bgOpacity, function(){
		defVals.turned = 1;
	});
	$('#stretchGallery_content').height($(window).height()*0.7).width($(window).width()*0.7).css({'left': ($(window).width()*0.15), 'top': ($(window).height()*0.15)}).fadeTo('slow', 1);
	
	$(window).bind("resize.changeSize", function()
	{
		turnLayerOn();
	});

};

turnLayerOff = function(){	
	$('#stretchGallery_layerBg, #stretchGallery_content').fadeTo('slow', 0, function(){
		$('#stretchGallery_layerBg, #stretchGallery_content').hide();
		defVals.turned = 0;
	});
	$(window).unbind("resize.changeSize");

}

defVals = {
	turned: 0,
	bgOpacity: 0.6,
	elements: null
};

prepare = function () {
	$('head').append('<link rel="stylesheet" type="text/css" href="/teatr/css/stretchGallery/default.css" />');
	$('body').append('<div id="stretchGallery_layerBg" class="stretchGallery_default stretchGallery_layerBg"></div>');
	$('body').append('<div id="stretchGallery_content" class="stretchGallery_default stretchGallery_content"><div id="stretchGallery_actualImage" class="stretchGallery_actualImage"></div><div id="stretchGallery_thumbs" class="stretchGallery_thumbs"></div><a class="stretchGallery_close" id="stretchGallery_close" href="#">[x]</a></div>');
	
	
	$('#stretchGallery_layerBg').hide();
	$('#stretchGallery_content').hide();
};


})(jQuery);
