﻿if (!window.Spark) window.Spark=new Object();
if (!window.Spark.Portals) window.Spark.Portals=new Object();
if (!window.Spark.Portals.Lighthouse) window.Spark.Portals.Lighthouse=new Object();
if (!window.Spark.Portals.Lighthouse.Gallery) window.Spark.Portals.Lighthouse.Gallery=new Object();

var PreviousTime=0;
var CurrentIndex=0;

Spark.Portals.Lighthouse.Gallery.RightArrow=function(evt)
{
	var CurrentTime=(new Date()).getTime();
	if(CurrentTime-PreviousTime>1500)
	{
		if($('.GalleryThumbnailsHolder')[0].scrollLeft+(2*$('.GalleryThumbnailsHolder')[0].clientWidth)>=$('.GalleryThumbnailsHolder')[0].scrollWidth)
			$('#RightArrow').fadeOut('slow');
		$('#LeftArrow').fadeIn('slow');
		$('.GalleryThumbnailsHolder').scrollTo('+=360px', { speed: 1500, axis: 'x' });
		PreviousTime=CurrentTime;
    }
}

Spark.Portals.Lighthouse.Gallery.LeftArrow=function(evt)
{
	var CurrentTime=(new Date()).getTime();
	if(CurrentTime-PreviousTime>1500)
	{
		if($('.GalleryThumbnailsHolder')[0].scrollLeft-$('.GalleryThumbnailsHolder')[0].clientWidth<=0)
			$('#LeftArrow').fadeOut('slow');
		$('#RightArrow').fadeIn('slow');
		$('.GalleryThumbnailsHolder').scrollTo('-=360px', { speed: 1500, axis: 'x' });
		PreviousTime=CurrentTime;
    }
}

Spark.Portals.Lighthouse.Gallery.OnPreviousClick=function(evt,Source)
{
	var BigImage=document.getElementById('BigImage');
	var Thumbnails=$('.GalleryThumbnails img');
	var LightboxLink=document.getElementById('LightboxLink');
	
	if(BigImages.length>0)
	{
		if(CurrentIndex>0)
		{
			CurrentIndex--;
		}
		else
		{
			CurrentIndex=BigImages.length-1;
		}
		BigImage.src="/ImageResource.axd?d="+BigImages[CurrentIndex]+"&w="+MainImgWidth+"&h="+MainImgHeight+"&s=crop";
		LightboxLink.href="/ImageResource.axd?d="+BigImages[CurrentIndex]+"&w=800&h=600&s=fit";
		Thumbnails.removeClass('ActiveItem');
		Thumbnails.eq(CurrentIndex).addClass('ActiveItem');
	}
}

Spark.Portals.Lighthouse.Gallery.OnNextClick=function(evt,Source)
{
	var BigImage=document.getElementById('BigImage');
	var Thumbnails=$('.GalleryThumbnails img');
	var LightboxLink=document.getElementById('LightboxLink');
	
	if(BigImages.length>0)
	{
		if(CurrentIndex<BigImages.length-1)
		{
			CurrentIndex++;
		}
		else
		{
			CurrentIndex=0;
		}
		BigImage.src="/ImageResource.axd?d="+BigImages[CurrentIndex]+"&w="+MainImgWidth+"&h="+MainImgHeight+"&s=crop";
		LightboxLink.href="/ImageResource.axd?d="+BigImages[CurrentIndex]+"&w=800&h=600&s=fit";
		Thumbnails.removeClass('ActiveItem');
		Thumbnails.eq(CurrentIndex).addClass('ActiveItem');
	}
}

/*var ImageWidth = 131;//101;//dodane

Spark.Portals.CLO.Gallery.OnThumbnailClick=function(evt, Direction, Thumbnails) {
	if (!evt) evt=window.event;
	var Source=(evt.srcElement?evt.srcElement:evt.target);
	Thumbnails=document.getElementById(Thumbnails);
	if (Thumbnails.childNodes.length>0) {
		if (((Thumbnails.offsetLeft-909>-Thumbnails.childNodes.length*ImageWidth)&&(Direction>0))||((Thumbnails.offsetLeft<0)&&(Direction<0)))
			Thumbnails.style.left=(Thumbnails.offsetLeft-Direction*ImageWidth)+"px";
	}
	evt.cancelBubble=true;
	return false;
}

Spark.Portals.CLO.Gallery.OnImageClick=function(evt, Direction, Thumbnails, MainImage, MainImageDescription, MainImageContainerID) {
	if (!evt) evt=window.event;
	var ThumbnailsContainer=document.getElementById(Thumbnails);
	var ThumbnailsId=Thumbnails;
	Thumbnails=document.getElementById(Thumbnails).getElementsByTagName("img");
	MainImage=document.getElementById(MainImage);
	MainImageDescription=document.getElementById(MainImageDescription);
	for (var Index=0; Index<Thumbnails.length; Index++) {
		if (MainImage.src==Thumbnails[Index].getAttribute("imageurl")) {
			if (((Direction>0)&&(Index<Thumbnails.length-1))||((Direction<0)&&(Index>0))) {
				MainImage.src=Thumbnails[Index+Direction].getAttribute("imageurl");
				MainImageDescription.innerHTML=(Thumbnails[Index+Direction].getAttribute("imagedescription")!=null?Thumbnails[Index+Direction].getAttribute("imagedescription"):"");
				break;
			}
		}
	}

	//scrolling thumbnails
	ThumbnailsHolder = (Thumbnails[0].parentNode);
	var ScrollPos = ThumbnailsHolder.parentNode.parentNode.scrollLeft;
	var Current=0;
	for (var i=0;i<Thumbnails.length;i++)
	{
		if (Thumbnails[i].getAttribute("imageurl")==MainImage.src)
		{
			Current=i;
			break;
		}
	}
	
	var Begin = 0;
	var End = 524;
	if(ScrollPos>0)
	{
		Begin=ScrollPos;
		End=ScrollPos+524;
	}
	var CurrentPos = Current*ImageWidth;
	if(Direction>0)
	{
		if(CurrentPos>End)
		{
			$('.ThumbnailListContainer').scrollTo('+=655px', { speed: 1500, axis: 'x' });
		}
	}else
	{
		if(CurrentPos<Begin)
		{
			$('.ThumbnailListContainer').scrollTo('-=655px', { speed: 1500, axis: 'x' });
		}
	}
	//scrolling thumbnails END
	
    for (var i=0;i<Thumbnails.length;i++)
    {
        if (Thumbnails[i].getAttribute("imageurl")==MainImage.src)
        {
			
			Thumbnails[i].className = 'ImageGalleryThumbnail ActiveItem';
        }
        else
        {
			Thumbnails[i].className = 'ImageGalleryThumbnail'; 
        }
    }
	evt.cancelBubble=true;
	

	return false;
}
*/

Spark.Modules.Gallery.Gallery.OnThumbnailClick=function(evt,MainImage,MainImageTitle,MainImageDescription)
{
	if (!evt) evt=window.event;
	var Source=(evt.srcElement?evt.srcElement:evt.target);
	var BigImage=document.getElementById('BigImage');
	var LightboxLink=document.getElementById('LightboxLink');
    Thumbnails=Source.parentNode.parentNode.getElementsByTagName("img");
	MainImage=document.getElementById(MainImage);
	MainImage.src=Source.getAttribute("imageurl");
	for (var i=0;i<Thumbnails.length;i++)
    {
        if (Thumbnails[i].getAttribute("imageurl")==MainImage.src)
        {
			BigImage.src="/ImageResource.axd?d="+BigImages[i]+"&w="+MainImgWidth+"&h="+MainImgHeight+"&s=crop";
			Thumbnails[i].className = 'ImageGalleryThumbnail ActiveItem';
			CurrentIndex=i;
			LightboxLink.href="/ImageResource.axd?d="+BigImages[i]+"&w=800&h=600&s=fit";
        }
        else
        {
			Thumbnails[i].className = 'ImageGalleryThumbnail'; 
        }
    }
	
	MainImage.imageurl=null;
	if (MainImageTitle!=null)
	{
		MainImageTitle=document.getElementById(MainImageTitle);
		if (MainImageTitle!=null)
			MainImageTitle.innerHTML=(Source.getAttribute("imagetitle")!=null?Source.getAttribute("imagetitle"):"");
	}
	if (MainImageDescription!=null)
	{
		MainImageDescription=document.getElementById(MainImageDescription);
		if (MainImageDescription!=null)
			MainImageDescription.innerHTML=(Source.getAttribute("imagedescription")!=null?Source.getAttribute("imagedescription"):"");
	}
}


///////////////////////////////////////////////////////


