//Image plugin
$.fn.image = function(src, f){ 
   return this.each(function(){ 
     var i = new Image();
	 //onload event must be defined before setting image source (IE6 requirement)
	 i.onload = f;
     i.src = src; 
	 this.appendChild(i);
   }); 
 } 
//eof image plugin

function Paging()
{
	this.imagesCount =0,
	this.imagesPerSlide = 7,
	this.currentPage = 1,
	this.currentSet = new Array();
}

function Gallery()
{
	this.timeout = 0;
	this.pager = "#pager";
	this.speed = 1000;
	this.delay = 0;
	this.cssBefore = null;
	this.cssAfter = null;
	this.pause =  true;
	
	this.transitionSpeed = 1000;
	
	this.galleryPath = 'http://phplug.net/modules/components/templates/jquery-gallery/images/';
	
	this.images = new Array();
	this.paging = new Paging();
	
	this.getSet();
}

Gallery.prototype.getSet=function()
{
	 
	var $l = $('<div></div>').appendTo('body');
	$l.addClass('loader');
	

	$l.ajaxStart(function() { $(this).fadeTo(100,1); } );
	$l.ajaxStop(function() { $(this).fadeTo(1000,0); } );

	var ob = this;
	$.ajax({
	   type: "GET",
	   url: "http://phplug.net/modules/components/templates/jquery-gallery/gallery.php",
	   dataType: "html",
	   success: function(set)
	   {
		   ob.storeImages.call(ob,set)
	   }
	 });
}

Gallery.prototype.storeImages=function(set)
{
	var imageSet = set.split(/\n+\s*/gi);

	for (i=0;i<imageSet.length;i++)
	{
		var im = imageSet[i].replace(/\n*\s*/i,'').split('|');
		this.images.push(im[1]);
	}
	
	this.createPaging();
	this.build();
}
Gallery.prototype.createPaging=function()
{
	this.paging.imagesCount = this.images.length;
	this.paging.currentSet.push(0);
	this.paging.currentSet.push(this.paging.imagesPerSlide);
	
}
Gallery.prototype.build=function( set )
{
	//Remove old thumbnails 
	$('#imagebox > img').remove();
	$('#pager > img').remove();
	

	for (i=this.paging.currentSet[0];i<this.paging.currentSet[1];i++)
	{
		$('#imagebox').image(this.galleryPath + this.images[i],function() { /* Call back function while loading images */ });
	}

	$(this.pager).fadeTo(1000,1);
	
	pGallery.init();
}

Gallery.prototype.init=function()
{	
	$('#imagebox').cycle(
	{
		timeout : this.timeout,
		speed : this.speed,
		delay : this.delay,
		pager : this.pager,
		pause : this.pause,
		cssBefore : this.cssBefore,
		cssAfter : this.cssAfter
	});
	
	var ob = this;
	
	$('#nextset').click(function() {  ob.slideNext.call(ob) } );
	$('#prevset').click(function() {  ob.slidePrev.call(ob) } );	
}

Gallery.prototype.slideNext=function()
{
	var p = $(this.pager);
	var o = this;
	
	$(this.pager).fadeTo(o.transitionSpeed,0,
		function() 
		{ 
			if (o.paging.currentSet[1] + o.paging.imagesPerSlide < o.paging.imagesCount)
			{
				nextSetEnd = o.paging.currentSet[1] + o.paging.imagesPerSlide;
			}
			else 
			{
				nextSetEnd = o.paging.imagesCount;
			}
			if (nextSetEnd == o.paging.imagesCount)
			{
				o.paging.currentSet[0] = nextSetEnd - o.paging.imagesPerSlide;
				o.paging.currentSet[1] = nextSetEnd;
			}
			else 
			{
				o.paging.currentSet[0] += o.paging.imagesPerSlide;
				o.paging.currentSet[1] = nextSetEnd;
			}
			o.build();
		} );
}

Gallery.prototype.slidePrev=function()
{
	var p = $(this.pager);
	var o = this;
	$(this.pager).fadeTo(o.transitionSpeed,0,
		function() 
		{ 
			if (o.paging.currentSet[1] - o.paging.imagesPerSlide > 0)
			{
				nextSetEnd = o.paging.currentSet[1] - o.paging.imagesPerSlide;
			}
			else 
			{
				nextSetEnd = o.paging.imagesPerSlide;
			}
			if (nextSetEnd == o.paging.imagesPerSlide)
			{
				o.paging.currentSet[0] = 0;
				o.paging.currentSet[1] = nextSetEnd;
			}
			else 
			{
				o.paging.currentSet[0] -= o.paging.imagesPerSlide;
				o.paging.currentSet[1] = nextSetEnd;
			}
			o.build();
		} );
}

var pGallery = null;

$(document).ready(function()
{
	pGallery = new Gallery();
});
