﻿
(function($){
	$.fn.galleryThumbEventsReg = function(options) {
		var opts = $.extend({}, $.fn.galleryThumbEventsReg.defaults, options);
		return this.each(function() {
			var gallery = $(this);
			var o = $.meta ? $.extend({}, opts, gallery.data()) : opts;

			$('li img, li a.zoom', gallery).click(function(ev){

				var thumbImage = $(this),
					parentListitem = thumbImage.parent(),
					bigImageSrc = parentListitem.find('a.zoom').attr('href'),
					coverHeight = ($(window).height() > $(document).height()) ? $(window).height() : $(document).height(),
					browserIE = /*@cc_on!@*/false,
					browserIE6 = (browserIE && document.compatMode && !window.XMLHttpRequest) ? true:false;

				ev.preventDefault();

				$('.dvCover').height(coverHeight).css({opacity:'0.8'}).fadeIn(400).click(hideCover);
				if (browserIE6) {
					$('.bigImage img.bigImg').attr('src', bigImageSrc).css({'max-width' : 958});
					$('.bigImage').css({'top':270}).fadeIn(400, function(){
								var imgWidth = $('.bigImage img.bigImg').width() +4;
								var imgHeight = $('.bigImage img.bigImg').height() +4;
								$('.bigImage img.bigImg').before('<iframe frameborder="0" src="/App_Themes/CATPortal/blank.html" width="'+imgWidth+'" height="'+imgHeight+'"></iframe>');
							}).click(hideCover);

				} else {
					$('.bigImage img.bigImg').attr('src', bigImageSrc).css({'max-width' : 958});
					$('.bigImage').css({'position':'fixed'}).fadeIn(400, function(){
								var imgHeight = $('.bigImage img.bigImg').height() +4;
								$('.bigImage').animate({'top' : Math.round($(window).height()-imgHeight)/2 }, 300);
							}).click(hideCover);
				}
			});
			
		});
	};

	$.fn.galleryThumbEventsReg.defaults = {};
})(jQuery);


function hideCover(){
	$('.bigImage').fadeOut('slow', function(){ $('.bigImage img.bigImg').attr('src', ''); });
	$('.dvCover').fadeOut('slow').unbind('click');
}


$(function(){
	$('ul.thumbnails').galleryThumbEventsReg();
});
