// JavaScript Document

// yGalbox
// JavaScript Document
(function($) {
		$.fn.yGalBox = function(settings) {
			settings = jQuery.extend({
				overlayBgColor: 		'#000',
				overlayOpacity:			0.8,
				imageLoading:			'yGalerie/css/images/yGal-loader.gif',
				beforeDelay:			200,
				resizeDelay:			800,
				fadeSlide:				500
			},settings);
			var yBoxObj 		= this;
			var yGalOverlay			= undefined;
			var yGalBox				= undefined;
			var ygMainWrapper 		= undefined;
			var ygObjWrapper		= undefined;
			var ygCaptionWrapper 	= undefined;
			var yGalLoader			= undefined;
			var yBoxObjSize 		= yBoxObj.size();
			var yBoxData			= {};

			jQuery.extend(this,{
				_init : function() {
					var counter = 1;
					yBoxObj.each(function() {
						tempData = {
							"type" 	: "img",
							"url"	: $(this).attr("href"),
							"title" : $(this).attr("title"),
							"slide"	: undefined
						};
						yBoxData['#'+counter] = tempData;
						$(this).attr("href","#"+counter);
						counter++;
					});
					yBoxObj.click(function() {
						slide = $(this).attr("href");
						yBoxObj._initOverlay();
						yBoxObj._initBox();
						yBoxObj._drawSlide(slide);
						return false;
					});
				}, 
				_drawSlide : function(idx) {
					imageData = yBoxData[idx];
					if(imageData.slide == undefined) {
						yBoxObj._loadSlide(idx);
						return false;
					}
					setTimeout(function() {
						ygMainWrapper.animate({
							"width":imageData.slide.width,
							"height":imageData.slide.height
						},settings.resizeDelay,function() {
								yGalLoader.hide();
								ygObjWrapper.append(imageData.slide);
								ygObjWrapper.fadeIn(settings.fadeSlide);
								ygCaptionWrapper.fadeIn(settings.fadeSlide);
						});
					},settings.beforeDelay);
				},
				_initOverlay : function() {
					/* OVERLAY */
					yGalOverlay = $('<div id="yGalOverlay"></div>');
					//newWidth 	= $(window).width();
					newWidth = "100%";
					newHeight 	= $(window).height() + $(window).scrollTop();
					yGalOverlay.css({
						"position":"absolute",
						"left":"0",
						"top":"0",
						"opacity":"0.6",
						"display":"block",
						"backgroundColor":settings.overlayBgColor,
						"width":newWidth,
						"height":newHeight
					});
					$(window).scroll(function() {
						newWidth 	= $(this).width()+$(this).scrollLeft();
						newHeight 	= $(this).height()+$(this).scrollTop();
						yGalOverlay.height(newHeight);
						yGalOverlay.width(newWidth);
					});
					$(window).resize(function() {
						newWidth 	= $(this).outerWidth();
						newHeight 	= $(this).outerHeight();
						yGalOverlay.height(newHeight);
						yGalOverlay.width(newWidth);
					});
					$("body").append(yGalOverlay);
				},
				_initBox : function() {
					yGalBox 			= $('<div id="yGalBox"></div>');
					yGalLoader 			= $('<div id="yGalLoader"><img src="'+settings.imageLoading+'" alt="loading"></div>');
					ygMainWrapper 		= $('<div class="main_w"></div>');
					ygMainWrapper.append('<div class="obj_w"></div><div class="caption_w"></div>',yGalLoader);
					ygObjWrapper 		= ygMainWrapper.find(".obj_w").hide();
					ygCaptionWrapper 	= ygMainWrapper.find(".caption_w").hide();
					yGalBox.append(ygMainWrapper);
					yGalBox.css({
						"position":"absolute",
						"width":"100%",
						//"top": $(window).scrollTop()
						"top":"0"
					});
					ygMainWrapper.css({
						"margin":"0px auto",
						"padding":"4px",
						"position":"relative",
						"backgroundColor":"#EEE",
						"width":"120px",
						"height":"120px"
					});
					yGalLoader.css({
						"position":"absolute",
						"width":"32px",
						"height":"32px",
						"left":"50%",
						"top":"50%",
						"marginTop":"-16px",
						"marginLeft":"-16px"
					});
					yGalBox.click(function() {
						yGalBox.remove();
						yGalOverlay.remove();
						return false;
					});
					$("body").append(yGalBox);
				},
				_loadSlide : function(idx) {
					imageData 	= yBoxData[idx];
					newSlide 	= new Image();
					newSlide.onload = function() {
						 yBoxData[idx].slide = newSlide;
						 yBoxObj._drawSlide(idx);
					}
					newSlide.src 	= imageData.url;
					newSlide.alt	= imageData.title;
				}
			});
			yBoxObj._init();
		};
})(jQuery);
