/*

dyPreview
a jQuery Plugin

by: Ing. Martin Dostal
http://www.dysoft.cz

Version: 1.0

Note: Do with it anything ;-)

*/
(function($) {
	$.dyPreview = {
		defaults: {
			timeShow: 500
		}
	}
	$.fn.extend({
		dyPreview:function(config) {


			// ozancime si vsechny prvky, abychom pak mezi nimi mohli prochazet

			var config = $.extend({}, $.dyPreview.defaults, config);
			var obj = $(this);
			var timeShow = config.timeShow;
			var imgCount = 0;
			var curId = -2;
			var previewImg;
			var previewNext;
			var previewPrev;
			var previewClose;
			var pageX;
			var pageY;

			$.each($(this), function(i, val) {
				//$("body").append(i + " => " + val + "<br/>");
				//$("body").append(i + " => " + $(this).attr('src') + "<br/>");
				$(this).attr('id','dypreview-'+i);
				$(this).css('cursor','pointer');
				imgCount = i;
			});

			function remove(){
				previewImg.clearQueue();
				/* trocha divocily kvuli spravnemu smazani, blblo v opere, pokud obrazek zasahoval do prostoru, kde uz nebyl <html>*/
				$(".dyPreviewDark,.dypreviewImg,.dyPreviewNext,.dyPreviewPrev").fadeTo(60,0.01,function(){$(this).remove();});
				previewClose.fadeTo(60,0.01,function(){$(this).remove();});
				curId=-1;

			}
			function next(){
				previewImg.clearQueue();
				if(curId<imgCount){curId++;}else{curId=0;}
				previewNext.fadeTo(100,0.01);
				previewClose.fadeTo(100,0.01);
				previewImg.fadeOut(300,function(){
					show(curId);

				});
			}
			function prev(){
				previewImg.clearQueue();
				if(curId>0){curId--;}else{curId=imgCount;}
				previewPrev.fadeTo(100,0.01);
				previewClose.fadeTo(100,0.01);
				previewImg.fadeTo(300,0.01,function(){
					show(curId);

				});
			}
			function show(id){

				var img=$("img#dypreview-"+id);
				var src = img.attr("src");
				var regexp = /__[0-9x]+\./;
				var h = parseInt($(window).height() / 100)*100-50;
				
				var heightMax = parseInt($(window).height())-70;	// max vyska fotky
				var widthMax = parseInt($(window).width())-70;	// max sirka fotky
				
				if(curId < 0){
					var txt = '';
					txt+='<div class="dyPreviewDark" style="position:fixed;top:0px;left:0px;width:100%;height:100%;margin:0px;padding:0px;background:#000;display:none;z-index:1010"></div>';
					//txt+='<img class="dypreviewImg" src="'+src.replace(regexp,"__1000x"+h+"tmp.")+'" alt="Loading.." style="z-index:1011;position:fixed;left:50%;top:50%;width:auto;height:auto;display:none;border:3px solid #000;-moz-box-shadow: 0 0 20px #000000;-webkit-box-shadow: 0 0 20px #000000;box-shadow: 0 0 20px #000000;"/>';
					txt+='<img class="dypreviewImg" src="'+src.replace(regexp,".")+'" alt="Loading.." style="z-index:1011;position:fixed;left:50%;top:50%;width:auto;height:auto;display:none;border:3px solid #000;-moz-box-shadow: 0 0 20px #000000;-webkit-box-shadow: 0 0 20px #000000;box-shadow: 0 0 20px #000000;"/>';
					if(imgCount > 0){
						txt+='<div class="dyPreviewNext" style="background:url(/data/icons/none.gif);cursor:pointer;z-index:1012;position:fixed;left:50%;top:50%;margin:0px;padding:0px;display:none;text-align:right;"><span style="margin:0px 5px;" class="ico64 ico64next"></span></div>';
						txt+='<div class="dyPreviewPrev" style="background:url(/data/icons/none.gif);cursor:pointer;z-index:1012;position:fixed;left:50%;top:50%;margin:0px;padding:0px;display:none;"><span style="margin:0px 5px;" class="ico64 ico64prev"></span></div>';
					}
					txt+='<div class="dyPreviewClose ico64 ico64no" style="z-index:1013;cursor:pointer;position:fixed;left:50%;top:50%;margin:0px;padding:0px;display:none;" title="Zavřít"></div>';
					$("body").append(txt);
					$(".dyPreviewDark").fadeTo(1,0.2);
					previewImg = $(".dypreviewImg");
					previewNext = $(".dyPreviewNext");
					previewPrev = $(".dyPreviewPrev");
					previewClose = $(".dyPreviewClose");
				}

				if(curId>=0){
					//previewImg.attr('src',src.replace(regexp,"__1000x"+h+"tmp."));
					previewImg.attr('src',src.replace(regexp,"."));
				}
				previewNext.fadeTo(1,0.01);
				previewPrev.fadeTo(1,0.01);
				previewClose.fadeTo(1,0.01);


				if(curId<0){
					previewImg.load(function(){
						$(this).css({
							width:"auto",
							height:"auto",
							display:"block",
							padding:0
						});
						var widthImg = $(this).width();
						var heightImg = $(this).height();
						
						if(widthImg > widthMax){
							widthImg = widthMax;
							heightImg = parseInt(heightImg * (widthMax / widthImg));
						}
						if(heightImg > heightMax){
							widthImg = parseInt(widthImg * (heightMax / heightImg));
							heightImg = heightMax;
						}
					
					
						
						
						//var widthImg = previewImg.width() > widthMax ? widthMax : previewImg.width();
						//var heightImg = previewImg.height() > heightMax ? heightMax : previewImg.height();
						
						
						
						
						
						$(this).css({
							width:widthImg,
							height:heightImg,
							display:"block",
							padding:0
						});
						var width = -(previewImg.width()/2);
						var height = -(previewImg.height()/2);

						$(this).css({
							marginTop:height,
							marginLeft:width
						});
						previewClose.css({
							marginTop:(height)-32,
							marginLeft:(-width)-32,
							display:'block'
						});

						previewPrev.css({
							marginTop:height+3,
							marginLeft:width+3,
							width:-width-3,
							height:(-height)+32,
							paddingTop:(-height)-32
						});
						previewNext.css({
							marginTop:height+3,
							width:-width+3,
							height:(-height)+32,
							paddingTop:(-height)-32
						});
						$(this).fadeTo(300,1.0);
						previewClose.fadeTo(300,1.0);
						
						
						if(previewNext.offset().left<pageX &&
						previewNext.offset().left+previewNext.width()>pageX &&
						previewNext.offset().top<pageY &&
						previewNext.offset().top+previewNext.height()>pageY){
							previewNext.fadeTo(100,1.0);
						}
						if(previewPrev.offset().left<pageX &&
						previewPrev.offset().left+previewNext.width()>pageX &&
						previewPrev.offset().top<pageY &&
						previewPrev.offset().top+previewNext.height()>pageY){
							previewPrev.fadeTo(100,1.0);
						}

						//previewImg.fadeIn(100);
						//previewImg.fadeOut(timeShow);


						//previewImg.fadeTo(timeShow,1.0);
					});
				}
				curId = id;



			}
			function setup(timeShow) {
				$(".dyPreviewDark").live('click',function(){
					remove();
				});
				obj.click(function(){
					id=$(this).attr('id').split('-');id=id[1];
					show(id);
					//					alert(id);
				});

			};

			$(".dyPreviewDark, .dyPreviewClose").live('click',function(){remove();});	// odstaneni nahledu
			//$(".dypreviewImg").live('click',function(e){console.log(e);next();});
			$(".dyPreviewNext").live('click',function(){next();});
			$(".dyPreviewPrev").live('click',function(){prev();});
			$(".dyPreviewNext,.dyPreviewPrev").live('mouseenter',function(){
				$(this).fadeTo(100,1.0);
			});
			$(".dyPreviewNext,.dyPreviewPrev").live('mouseleave',function(){
				$(this).fadeTo(100,0.01);
			});
			$("html").live('mousemove',function(e){
				pageX=e.pageX;
				pageY=e.pageY;
			});

			setup(timeShow);
			return this;
		}
	});

})(jQuery);







