﻿function ajaxGallery(objName,div,file,imgpath,shape,group,gallery)
{
	this.name = objName;
	this.onoff = true;
	this.filepath = imgpath;
	this.pointer = 0;
	this.pageSize = 1;
	this.shape
	this.id = [];
	this.img = [];
	this.type = [];
	this.resolution = [];
	this.author = [];
	this.lead = [];
	this.title = [];
	this.arrayshape = [];
	this.url = [];
	this.mouseoverBol = 0;
	this.xml = null;
	this.showdiv = div;
	this.pagerdiv = div + "Pager";
	this.counter = div + "Counter";
	this.images = new Array();
	this.shape = shape;
	this.group = group;
	this.gallery = gallery;
	this.imgsizebig = "200";
	
	var smallImgSize;
			
	var galleryContainer = jQuery('<div id="'+div+'" class="galleryja" style="z-index:200"><div id="galleryHeader" style="display:none;"><a href="#close" id="closeLink">zamknij</a></div><div id="bgGallery"><div id="bigPhotoBox"></div><div id="thumbnailScroller"></div><div id="bigPhotoDesc" style="display:none;"></div></div></div>');

	jQuery.ajax({
		 type: "GET",
		 url: file,
		 dataType: "xml",
		 success: function(xml) {
			 jQuery(xml).find('photo:first').each(function(){
				 var imgName = jQuery(this).find('filename').text();
				 jQuery('.gallery_intext').prepend( '<img src="'+ imgpath + imgName + '" class="smallphoto" />');	
				 smallImgSize =  jQuery('.gallery_intext img').width(); 						    
			 }); 
		 }
	 }); 

	var bigfile = imgpath + "gallery_500.xml";
    var smImgOff = jQuery('.smallphoto').offset();

	var startGallery =function(){	
		jQuery('.gallery_intext').append(galleryContainer);
		jQuery.ajax({
			type: "GET",
			url: bigfile,
			dataType: "xml",
			success: function(xml) {						
				jQuery(xml).find('photo:first').each(function(){
					var smImgOff = jQuery('.smallphoto').offset();
					var imgName = jQuery(this).find('filename').text();
					var author = jQuery(this).find('author').text();
					var title = jQuery(this).find('title').text();
					var description = jQuery(this).find('desc').text();

					jQuery(galleryContainer).find('#bigPhotoBox').html('<img src="'+ imgpath + imgName + '" class="bigphoto" />');
					jQuery(galleryContainer).find('img').width(smallImgSize);
					jQuery(galleryContainer).find('#bigPhotoDesc').html('<span class="galleryPhotoAuthor">fot: '+author+'</span>');
					jQuery(galleryContainer).find('#bigPhotoDesc').append('<span class="galleryPhotoTitle">'+title+'</span>');
					jQuery(galleryContainer).find('#bigPhotoDesc').append('<span class="galleryPhotoDescription">'+description+'</span>');

					jQuery(galleryContainer).addClass('run');
				});
			},
			complete: function(){
				gallery_intext_bg();
				var galConOf = jQuery('.smallphoto').offset();
				jQuery(galleryContainer).addClass("run");
				jQuery(galleryContainer).find("#galleryTitle").text(jQuery('.gallery_intext .title').text());

				jQuery(galleryContainer).find('img.bigphoto').animate({width:"500px"}, 1500, function(){
					var imgW = jQuery(galleryContainer).find('img').width();
					var imgH = jQuery(galleryContainer).find('img').height();

					jQuery(galleryContainer).width(imgW);
					jQuery(galleryContainer).height(imgH);

					jQuery(galleryContainer).animate({width:(imgW + 204)+"px", height:(imgH +100)+"px", top:"35px"}, 500);	
					loadThumbnailList();

					jQuery('#galleryHeader').slideDown(400);
					jQuery('#bigPhotoDesc').slideDown(400);

					jQuery('#closeLink').click(function(){
						jQuery('#'+div).fadeOut("normal", function(){
							//jQuery('#TB_overlay').hide();
							jQuery('.gallery_intext .smallphoto, .gallery_intext .title').click(function(){
								jQuery('#'+div).fadeIn("normal");
								gallery_intext_bg();
								//jQuery('#TB_overlay').show();
							});
							 $bg2 = "#eae9de url(/kultura/_img/tGaleriaW.jpg) no-repeat";
							jQuery(".gallery_intext").css("background", $bg2);
						});
					});					
				});	
			}
		});
		//jQuery('body').prepend(overlay);
	};

	var gallery_intext_bg = function() {
		$bg2 = "#eae9de url(/kultura/_img/tGaleriaWd.jpg) no-repeat";
  		jQuery(".gallery_intext").css("background", $bg2);
	};

	var loadThumbnailList = function(){	
		var div = jQuery('#thumbnailScroller');
		jQuery.ajax({
			type: "GET",
            url: file,
            dataType: "xml",
            success: function(xml) {
				jQuery(xml).find('photo').each(function(){
                	var imgName = jQuery(this).find('filename').text();
					var imgId = jQuery(this).attr('id');
					div.append('<p><img id="'+imgId+'" src="'+imgpath + imgName+'" class="thumbnail"/></p>')
                });
            },
			complete:function(){
				//jQuery(galleryContainer).find('#thumbnailScroller').append(ul);
				jQuery(galleryContainer).find('#thumbnailScroller').addClass("scroll-pane");
				var imgH = jQuery(galleryContainer).find('img').height() + 15;
				jQuery(galleryContainer).find('.scroll-pane').height(imgH);
				jQuery(galleryContainer).find('#thumbnailScroller').jScrollPane({showArrows:true});
				jQuery(galleryContainer).find('#thumbnailScroller img').bind('click', thumbnailClick);
			}
		}); 
	}

	var thumbnailClick = function() {
		var imgId = jQuery(this).attr("id");
			jQuery.ajax({
				type: "GET",
				url: bigfile,
				dataType: "xml",
				success: function(xml) {
					jQuery(xml).find('photo[id='+imgId+']').each(function(){
					var imgName = jQuery(this).find('filename').text();
					var author = jQuery(this).find('author').text();
					var title = jQuery(this).find('title').text();
					var description = jQuery(this).find('desc').text();

					jQuery(galleryContainer).find('#bigPhotoBox').html('<img src="'+ imgpath + imgName + '" class="bigphoto" />');
					jQuery(galleryContainer).find('#bigPhotoDesc').html('<span class="galleryPhotoAuthor">fot: '+author+'</span>');
					jQuery(galleryContainer).find('#bigPhotoDesc').append('<span class="galleryPhotoTitle">'+title+'</span>');
					jQuery(galleryContainer).find('#bigPhotoDesc').append('<span class="galleryPhotoDescription">'+description+'</span>');
				});
			}
		});
	}

	jQuery(document).ready(function()
	{
		jQuery('.gallery_intext').one('click',startGallery);
		jQuery(galleryContainer).find('.thumbnail-list img').click(thumbnailClick);
	});
}