/**
  -------------------------------------------------------------------------
	                    
	Copyright 2006 GORSKI Ventures. All rights reserved.
	You use this code in your Web pages, provided these opening credit
    lines are kept intact.


    You may not reprint or redistribute this code without permission from 
    GORSKI Ventures.
    -------------------------------------------------------------------------  
*/

	//Slideshow pic variables defined on page

	var imgSegmentSource = new Array();
	var imgSegmentTextSource = new Array();
	var img_pool = new Array();

	var step;
	var selectedImage;
	var slideshow_on = false;
	var running = null;
	var stepLagTime = 5100;
	var faderLagTime = 90;
	var opacityRate = 100;
	
	var containerDiv = null;
	var containerTextDiv = null;
	var imageDiv = null;
	var segButton = null;

	var arrPreload = new Array();
	var countPreloaded = 0;


	if (document.getElementById)
	{
		slideshow_on = true;
	}

	if ( (slideshow_on) && ((navigator.userAgent.toLowerCase()).indexOf("opera")==-1) )
	{
		step = 0;   
	}
	else
	{
		step = Math.floor(Math.random() * img_pool.length);
	}
	selectedImage = eval(img_pool[step]);
	

	function applyRegexSrc( strImgTag, i )
	{
		var arrResult = null;
		arrResult = regexSrc.exec( strImgTag );

		if (arrResult)
		{
			arrPreload[i] = new Image;
			arrPreload[i].onload = function(){countPreloaded++};
			arrPreload[i++].src = arrResult[1];
		}

		return i;
	}

	function loadImages()
	{
		var imgPos = 0;
		var currentImageSource = 0;
		
		regexSrc = new RegExp;
		regexSrc.compile('src="([^"]+)"');
	
		
		for (next=0; next < img_pool.length; next++)
		{
			currentImageSource = eval(img_pool[next]);
			
			if (currentImageSource == selectedImage)
				continue;

			imgPos = applyRegexSrc( imgSegmentSource[currentImageSource], imgPos);
		}

		regexSrc = null;
	}


	function doSlideshow(action)
	{
		if (!slideshow_on)
		{
			return;
		}

		stop();

		if (action != 0)
		{
			swapImages(action);
		}
	}


	function useIEFilter()
	{
		var randId = 17;
		
		while (randId > 16)
		{
			randId = 10 + Math.floor(Math.random()*11); //from 13 to 16
		}
		
		containerDiv.style.filter = "blendTrans(duration=0.9) revealTrans(duration=1.9,transition="+randId+")";
		if(containerTextDiv) containerTextDiv.style.filter = "blendTrans(duration=0.9) revealTrans(duration=1.9,transition=6)";
		
		containerDiv.filters(0).apply();
		containerDiv.filters(1).apply();
		
		if(containerTextDiv) containerTextDiv.filters(0).apply();
		if(containerTextDiv) containerTextDiv.filters(1).apply();
		
		swapImages();
		
		containerDiv.filters(0).play();
		containerDiv.filters(1).play();
		
		if(containerTextDiv) containerTextDiv.filters(0).play();
		if(containerTextDiv) containerTextDiv.filters(1).play();
		
		running = setTimeout('useIEFilter()', stepLagTime);
	}

	
	function transformImages(fade)
	{
		if (fade == true)
		{
			opacityRate -= 10;

			if (opacityRate > 0)
				running = setTimeout( 'transformImages(true)', faderLagTime );
		}
		else
		{
			opacityRate += 10;

			if (opacityRate < 100)
				running = setTimeout( 'transformImages(false)', faderLagTime );
		}

		containerDiv.style.filter = "alpha(opacityRate=" + opacityRate/100 + ")";
		containerDiv.style.opacity = opacityRate/100;   
		
		if(containerTextDiv) containerTextDiv.style.filter = "alpha(opacityRate=" + opacityRate/100 + ")";
		if(containerTextDiv) containerTextDiv.style.opacity = opacityRate/100;   
		
		if (opacityRate == 0)
		{
			swapImages();
			transformImages(false);
		}

		if (opacityRate == 100)
		{
			running = setTimeout( 'transformImages(true)', stepLagTime );
		}
	}

	function swapImages(action)
	{
		if (action)
		{
			step = step + action;
		}
		else
		{
			step = step + 1;  
		}


		if (step >= img_pool.length)
		{
			step = 0;
		}

		if (step < 0)
		{
			step = img_pool.length-1;
		}

		var nextImageSource = eval(img_pool[step]);
		imageDiv.innerHTML = imgSegmentSource[nextImageSource];
		if(containerTextDiv) goNext();
	}

	
	function run()
	{
		
		
		if (countPreloaded < arrPreload.length)
		{
			
			running = setTimeout( 'run()', 1500 );
			return;
		}


		if ( (is_ie) && !(is_opera) )
		{
			
			running = setTimeout( 'useIEFilter()', stepLagTime );

		}
		else if ( !(is_nav6) && !(is_opera) )
		{
			running = setTimeout( 'transformImages(true)', stepLagTime );
		}
	}

	function stop()
	{
		if (running)
			garbageCollect(running);
	}

	function initSlideshow(flag, isMain)
	{
		
		if (!slideshow_on)
		{
			return null;
		}

		//init vars
		containerDiv = document.getElementById("containerDiv");
		if(isMain) containerTextDiv = document.getElementById("containerTextDiv");
		imageText = document.getElementById("img_prayer");
		imageDiv = document.getElementById('imageDiv');
		
		loadImages();

		run();
	}