function PictureGallery(ObjectName,ContentType, ContentID, currentIndex, CustomObject, Lang, Interval, ImageObjectName, autoplay, Continious, transition, transitionspeed, w, h, roundimageradius, bgColor, onLoaded, onStart, onStop, onEnd, onImageChanged, onSettingsChanged, noGalleryImage, errorImage) 
{
	var me=this;

	//Events
	if (onStart!=null){this.OnStart = function () {onStart(me);}}else{this.OnStart=blank;}
	if (onEnd!=null){this.OnEnd = function () {onEnd(me);}}else{this.OnEnd=blank;}
	if (onStop!=null){this.OnStop = function () {onStop(me);}}else{this.OnStop=blank;}
	if (onImageChanged!=null){this.OnImageChanged = function () {onImageChanged(me);}}else{this.OnImageChanged=blank;}
	if (onLoaded!=null){this.Loaded = function () {onLoaded(me);}}else{this.Loaded=blank;}
	if (onSettingsChanged!=null){this.OnSettingsChanged = function () {onSettingsChanged(me);}}else{this.OnSettingsChanged=blank;}
	
	//Fields
	var ImgObjectname = ImageObjectName
	var image = document.getElementById(ImageObjectName);
	var interval = (Interval*1000);
	var CurrentIndex = currentIndex;
	var PicGallery;
	var timer;
	var started = false;
	var continiousPlay = Continious;
	var transitionID = transition;
	var autoPlay = autoplay;
	var roundImageRadius = roundimageradius;
	var transitionSpeed = transitionspeed;
	var customObject = CustomObject;
	var language = Lang;
	
	//Methods
	this.AutoPlay = autoPlay;
	this.TimeSpan= getInterval;
	this.SetTimeSpan = setInterval;
	this.NextImage = nextImage;
	this.PreviousImage = previousImage;
	this.GoToImage = goToImage;
	this.GoToStart = goToStart;
	this.GoToEnd = goToEnd;
	this.Play = start;
	this.Stop = stop;
	this.ImagesCount = getImagesCount;
	this.CurrentImageNumber = currentImageNumber;
	this.CurrentImage = currentImage;
	this.SetContentID = setContentID;
	this.SetContentType = setContentType;
	this.SetContiniousPlay = setContiniousPlay;
	this.ContiniousPlay = fnContiniousPlay;
	this.SetImageObject = setImageObject;
	this.HasNext = hasNext;
	this.HasPrevious = hasPrevious;
	this.SetTransition = setTransition;
	this.GetTransition = getTransitionID;
	this.Refresh = Initialize;
	this.SetTransitionSpeed = setTransitionSpeed;
	this.SetRoundImageRadius = setRoundImageRadius;
	this.TransitionsSupported = transitionsSupported;
	this.SetCustomObject = setCustomObject;
	this.SetLanguage = SetLanguage;
	
	Initialize(false);
	
	function Initialize(FromStart)
	{
		setImageObject(ImgObjectname);
		try {
			if (customObject==0)
			{
				if (ContentType == 0) {PicGallery = XSitePictureGallery.PictureGalleryCollection.GetNewsGallery(ContentID).value;}
				if (ContentType == 1) {PicGallery = XSitePictureGallery.PictureGalleryCollection.GetPageGallery(ContentID).value;}
			}
			if (customObject==1)
			{
				PicGallery = XSitePictureGallery.PictureGalleryCollection.GetCastleGallery(ContentType,ContentID,language).value;
			}
			if (me.ImagesCount()==0)
			{
				me.Stop();
				if (image!=null){
				image.src =  "images/pic.ashx/0.jpg/" + w + "/" + h + "/" + roundImageRadius + "/" + bgColor + "/?PicPath=" + noGalleryImage;}			
				me.Loaded();
			}else{
				if (FromStart==null){
					CurrentIndex=-1;
					if ((me.HasNext()==true)){me.NextImage();}else{me.Stop();}
				}
				if ((autoPlay==true) && (me.HasNext()==true)){me.Play();}else{me.Stop();}
				me.Loaded();
			}
		}catch(e){
			me.Stop();
			if (image!=null){
			image.src =  "images/pic.ashx/0.jpg/" + w + "/" + h + "/" + roundImageRadius + "/" + bgColor + "/?PicPath=" + noGalleryImage;
			}
			me.Loaded();
		}
	}
	
	function transitionsSupported()
	{
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])
        if ((version >= 5.5) && (version < 7) && (document.body.filters)) {return true;}else{return false;}
	}
	
	function SetLanguage(value)
	{
		if (language!=value)
		{
			language = value;
			me.Refresh();
		}
	}
	
	function setCustomObject(value)
	{
		if (customObject!=value)
		{
			customObject = value;
			me.Refresh();
		}
	}
	
	//Blank function for events
	function blank(){}
	
	function setTransitionSpeed(value)
	{
		transitionSpeed = value;
		me.OnSettingsChanged();
	}
	
	function setRoundImageRadius(value)
	{
		roundImageRadius = value;
		me.OnSettingsChanged();
	}
	
	function getImagesCount()
	{
		return PicGallery.ImagesCount
	}
	
	function hasNext()
	{
	if ((PicGallery.ImagesCount>0) && ((continiousPlay==true) || (((CurrentIndex+1) != getImagesCount()) && (continiousPlay!=true))))
	{return true;}else{return false;}
	}
	
	function hasPrevious()
	{
	if ((PicGallery.ImagesCount>0) && ((continiousPlay==true) || (((CurrentIndex+1) > 1) && (continiousPlay!=true))))
	{return true;}else{return false;}
	}
	
	function getInterval(){
		return interval/1000;
	}
	
	function currentImageNumber()
	{
		return (CurrentIndex+1);
	}
	
	function currentImage()
	{
		return PicGallery.Pictures[CurrentIndex]
	}
	
	function fnContiniousPlay()
	{
		return continiousPlay
	}
	
	function setImageObject(imageObjectName)
	{
		ImgObjectname = imageObjectName;
		try {
			if (document.getElementById(ImageObjectName) != null)
			{
				image = document.getElementById(ImgObjectname);
			}
		}catch(e){}
	}
	
	function setContiniousPlay(value)
	{
		if (value!=continiousPlay)
		{
			 continiousPlay=value
			 me.OnSettingsChanged();	
		}
	}
	
	function setContentID(contentID)
	{
		if (ContentID!=contentID)
		{
			ContentID = contentID	
			me.Refresh();
		}
	}
	
	function setContentType(contentType)
	{
		if (ContentType!=contentType)
		{
			ContentType = contentType	
			me.Refresh();
		}
	}
	
	
	//Interval value in seconds.
	function setInterval(IntervalValue)
	{
		interval = (IntervalValue*1000)
		me.OnSettingsChanged();
	}
	
	function goToImage(ImageNumber)
	{
		if ((PicGallery.ImagesCount != 0) && (ImageNumber <= 1) && (ImageNumber<PicGallery.ImagesCount))
		{
			CurrentIndex = (ImageNumber-1)
			ChangePicture();
		}
	}
	
	function goToStart()
	{
		if (PicGallery.ImagesCount != 0)
		{
			CurrentIndex = (0)
			ChangePicture();
		}
	}
	
	function goToEnd()
	{
		if (PicGallery.ImagesCount != 0)
		{
			CurrentIndex = (PicGallery.ImagesCount-1)
			ChangePicture();
		}
	}
	
	function nextImage()
	{
		if (image != null){
			if ((PicGallery != null) && (PicGallery.ImagesCount != 0)){
				if (hasNext()==true){
					if (PicGallery.ImagesCount == (CurrentIndex+1)){
						if (continiousPlay == true) {CurrentIndex = 0;}
					}else{
						CurrentIndex +=1;
					}
					ChangePicture();
				}else{me.Stop(); me.OnEnd();}
			}
		}
	}
	
	function previousImage()
	{
		if (image != null) {
			if ((PicGallery != null) && (PicGallery.ImagesCount != 0)){
				if (hasPrevious()==true){
					if (CurrentIndex == 0){
						if (continiousPlay == true) {CurrentIndex = (PicGallery.ImagesCount -1);}
					}else{
						CurrentIndex -=1;
					}
					ChangePicture();
				}else{me.Stop(); me.OnEnd();}
			}
		}
	}
	
	function stop()
	{
		window.clearInterval(timer);
		started = false;
		me.OnStop();
	}
	
	function start()
	{
		me.OnStart();
		if (hasNext()==false){CurrentIndex=-1;}
		started = true;
		timer=window.setTimeout(me.NextImage, interval);
	}
	
	function ChangePicture()
    {
		var ImageSrc = "images/pic.ashx/" + PicGallery.Pictures[CurrentIndex].PictureID + ".jpg/" + w + "/" + h + "/" + roundImageRadius + "/" + bgColor;
        image.style.filter = getTransitionFilter();  
        var img  = new Image(); 
        img.onload = function(){ imageLoaded(this); }
        img.onerror = function(){ imageError(this); }
        img.onabort = function(){ imageError(this); }
        img.src = ImageSrc;                               
    }
    
    function setTransition(value)
    {
		transitionID = value;
		me.OnSettingsChanged();
    }
    
    function getTransitionID()
    {
		return transitionID;
    }
        
    function getTransitionFilter()
    {
		var returnValue;
		if (transitionID==23)
		{ 
			var ran_unrounded=Math.random()*22;
			var ran_number=Math.floor(ran_unrounded); 
				returnValue = "revealTrans(duration=" + transitionSpeed + ",transition=" + ran_number + ");"
		}else{
			if (transitionID==-1)
		    {
				returnValue = "blendTrans(Duration=" + transitionSpeed + ");"
		    }else{
				returnValue = "revealTrans(duration=" + transitionSpeed + ",transition=" + transitionID + ");"
			}
		}
		return  returnValue ;
	}
    
    function imageError(img)
    {
		image.src =  "images/pic.ashx/0.jpg/" + w + "/" + h + "/" + roundImageRadius + "/" + bgColor + "/?PicPath=" + errorImage;
    }
    
    function imageLoaded(img)
    {
		document.getElementById("State_" +ObjectName).value = (CurrentIndex+1);
		me.OnImageChanged();
        if (me.TransitionsSupported() == true) 
		{
			image.filters[0].apply();                       //Apply the transition effect
			image.filters[0].play();                        //Play the effect and display the new image
        }
        image.src = img.src;                            //Assign the image to the image control
        if (started==true){window.clearInterval(timer); timer=window.setTimeout(me.NextImage, interval);}
    }
}
        
       