// Copyright (c) 2006. Adobe Systems Incorporated.
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of Adobe Systems Incorporated nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// Global variables:

var gThumbWidth = 24;
var gThumbHeight = 24;
var max_rows=0;
var gBehaviorsArray = [];
var gSlideShowTimer = null;
var gImageLoader = null;
var offset=max_rows;
var x=0;
var y=0;
var slides=0;
var finished_slides=0;
var rest=0;
var z=0;
var click_left=0;




// Register a callback on the thumbnails region so we can show the first
// image in the data set whenever the data changes.

Spry.Data.Region.addObserver("thumbnails", function(nType, notifier, data) {
	if (nType == "onPreLoad")
	{
		StopSlideShow();

	}
	else if (nType == "onPostUpdate")
	{
		max_rows=value_pics();
		StartSlideShow();
		ShowCurrentImage();
		show_first_pics();
		init_arrows();
	    
	
	}
});

function init_arrows()
{
offset=max_rows;
x=0;
y=0;
slides=0;
finished_slides=0;
rest=0;
z=0;

//if(max_rows>=value_pics())
//{
//document.getElementById("arrow_right").style.display="none";
//document.getElementById("begin").innerHTML="1-";
//document.getElementById("to").innerHTML=value_pics();
//document.getElementById("from").innerHTML=" from "+value_pics();
//}
//else
//{
//document.getElementById("arrow_right").style.display="";
//document.getElementById("begin").innerHTML="1-";
//document.getElementById("to").innerHTML=max_rows;
//document.getElementById("from").innerHTML=" from "+value_pics();
//
//}
document.getElementById("arrow_left").style.display="none";
}


function GetOpacity(ele)
{
  var o = Spry.Effect.getStyleProp(ele, "opacity");
  if (o == undefined || o == null)
    o = 1.0;
  return o;
}

function GetDimensions(element)
{
  // Spry.Effect.getDimensions() returns offsetWidth and offsetHeight
  // which includes the dimensions of the element and its padding.
  // We need the dimensions of the content area without the padding,
  // so we look up width and height in style first. If we get dimensions
  // back in pixels, we use it. If not, we fall back to using offsetWidth
  // and offsetHeight.

  dimensions = Spry.Effect.getDimensions(element);
  if (element.style.width)
    dimensions.width = parseInt(element.style.width);
  if (element.style.height)
    dimensions.height = parseInt(element.style.height);
  return dimensions;
}

function GetOffsetPosition(element)
{
  // Spry.Effect.getOffsetPosition() returns offsetLeft and offsetTop
  // which includes the position of the element content and its padding.
  // We need the position of the content area without the padding,
  // so we look up width and height in style first. If we get position
  // back in pixels, we use it. If not, we fall back to using offsetLeft
  // and offsetTop.

  pos = Spry.Effect.getOffsetPosition(element);
  if (element.style.left)
    pos.x = parseInt(element.style.left);
  if (element.style.top)
    pos.y = parseInt(element.style.top);
  return pos;
}

// Trigger the transition animation from the current image
// being displayed to the image at imgPath.

function SetMainImage(imgPath, width, height, tnID)
{
  var img = document.getElementById("mainImage");
  if (!img)
    return;

  CancelBehavior("mainImage");

  Spry.Utils.SelectionManager.clearSelection("thumbnailSelection");

  if (tnID)
    Spry.Utils.SelectionManager.select("thumbnailSelection", document.getElementById(tnID), "selectedThumbnail");

  if (gImageLoader)
  {
    gImageLoader.onload = function() {};
    gImageLoader = null;
  }

  gBehaviorsArray["mainImage"] = new Spry.Effect.Opacity(img, GetOpacity(img), 0, { duration: 400,
    finish: function()
	{
      gBehaviorsArray["mainImage"] = new Spry.Effect.Size(img.parentNode, GetDimensions(img.parentNode), { width: width, height: height}, {duration: 400, steps: 10,
	    finish: function()
	    {
		  // Use an image loader to make sure we only fade in the new image after
		  // it is completely loaded.
	      gImageLoader = new Image();
		  gImageLoader.onload = function()
		  {
	        img.src = gImageLoader.src;
			gImageLoader = null;
	        gBehaviorsArray["mainImage"] = new Spry.Effect.Opacity(img, 0, 1, { duration: 400, steps: 10, finish: function() { gBehaviorsArray["mainImage"] = null; }});
            gBehaviorsArray["mainImage"].start();
		  };
		  gImageLoader.src = "apps/tridolphin_gallery/"+imgPath;
	    }
	  });
      gBehaviorsArray["mainImage"].start();
    }
  });
  gBehaviorsArray["mainImage"].start();
}

// Cancel the animation behavior of the object with the given id.

function CancelBehavior(id)
{
  if (gBehaviorsArray[id])
  {
    gBehaviorsArray[id].cancel();
    gBehaviorsArray[id] = null;
  }
}

function SizeAndPosition(id, toX, toY, toWidth, toHeight, callback)
{
  CancelBehavior(id);
  var effectCluster = new Spry.Effect.Cluster;
  var ele = Spry.Effect.getElement(id); 
  var moveEffect = new Spry.Effect.Move(ele, GetOffsetPosition(ele), { x: toX, y: toY}, { duration: 400 });
  var sizeEffect = new Spry.Effect.Size(ele, GetDimensions(ele), { width: toWidth, height: toHeight}, { duration: 400 });
  
  effectCluster.addParallelEffect(moveEffect);
  effectCluster.addParallelEffect(sizeEffect);

  effectCluster.finish = callback;
 
  gBehaviorsArray[id] = effectCluster;
  gBehaviorsArray[id].start();
}

// Trigger the animation of the thumbnail growing.

function GrowThumbnail(img, width, height)
{
  Spry.Utils.addClassName(img, "inFocus");
  img.style.zIndex = 150;

  var id = img.getAttribute("id");

  var twidth = Math.floor(width * .75);
  var theight = Math.floor(height * .75);
  var tx = (gThumbWidth - twidth) / 2;
  var ty = (gThumbHeight - theight) / 2;

  SizeAndPosition(id, tx, ty, twidth, theight, function(b){gBehaviorsArray[id] = null;});
}

// Trigger the animation of the thumbnail shrinking.

function ShrinkThumbnail(img)
{
  Spry.Utils.addClassName(img, "inFocus");
  img.style.zIndex = 1;

  var id = img.getAttribute("id");

  SizeAndPosition(id, 0, 0, gThumbWidth, gThumbHeight, function(b){gBehaviorsArray[id] = null; Spry.Utils.removeClassName(img, "inFocus");});
}

// Show the image of the current selected row inside the dsPhotos data set.

function ShowCurrentImage()
{
  var curRow = dsPhotos.getCurrentRow();
  SetMainImage("galleries/" + dsGalleries.getCurrentRow()["@base"] + "images/" + curRow["path"], curRow["width"], curRow["height"], "tn" + curRow["ds_RowID"]);
}

// Utility function to advance (forwards or backwards) the current selected row
// in dsPhotos. This has the side effect of "selecting" the thumbnail and image
// of the new current row.

function AdvanceToNextImage(moveBackwards)
{
  var rows = dsPhotos.getData();
  var curRow = dsPhotos.getCurrentRow();
 if (rows.length < 1)
    return;
	
	  
  for (var i =0; i < rows.length; i++)
  { 
    if (rows[i] == curRow)
    {
      if (moveBackwards)
	  {
	  click_left=1;
        --i;
	  }
	  else
       {
		   ++i;
	   }
      break;
    }
  }

  if (!moveBackwards && i >= rows.length)
    {i =  0;
	offset=max_rows;
	set_start_row('right');
	
	}
  //else if (moveBackwards && i <  0)
 //  i = rows.length - 1;
  if(i>-1)
  curRow = rows[i];

  if(curRow["ds_RowID"]==offset)
  {
	  
	  set_start_row('right'); 
	
  }
  

   
    if(curRow["ds_RowID"]==(offset-1)-(max_rows)&&moveBackwards)
  {
		
		
        set_start_row('left');
		
 	    }

      if(curRow["ds_RowID"]==(value_pics()-rest-1)&&moveBackwards)
  {
set_start_row('left'); 

  }
  
 
    if(curRow["ds_RowID"]==offset-max_rows&&finished_slides==slides)
  {
	  set_start_row('restart'); 
	  finished_slides=0;

if(max_rows>=value_pics())
{
document.getElementById("arrow_right").style.display="none";
document.getElementById("begin").innerHTML="1-";
document.getElementById("to").innerHTML=value_pics();
document.getElementById("from").innerHTML=" from "+value_pics();
}
else
{
document.getElementById("arrow_right").style.display="";
document.getElementById("begin").innerHTML="1-";
document.getElementById("to").innerHTML=max_rows;
document.getElementById("from").innerHTML=" from "+value_pics();

}
document.getElementById("arrow_left").style.display="none";


	
  }
  dsPhotos.setCurrentRow(curRow["ds_RowID"]);
  ShowCurrentImage();
}

// Start the slide show that runs forwards through all
// the rows in dsPhotos.



// Kill any slide show that is currently running.

function StopSlideShow()
{
  if (gSlideShowTimer)
    clearInterval(gSlideShowTimer);
  gSlideShowTimer = null;
  var playLabel = document.getElementById("playLabel");
  if (playLabel)
  	playLabel.firstChild.data = "Start";
}

function HandleThumbnailClick(id)
{
  StopSlideShow();
  dsPhotos.setCurrentRow(id);
  ShowCurrentImage();
}




function show_first_pics()
{
		init_arrows();
	for(i=0;i<max_rows;i++)
	{
	
	document.getElementById("tn"+i).style.display='';
	document.getElementById("t_div"+i).style.display='';
	}
}

function show_pics_right()
{ 
value_pic=value_pics();

	if(finished_slides<slides)
	{
		if((x+offset-(max_rows*2))<(value_pic-max_rows-x))
		{
		  dsPhotos.setCurrentRow(offset-max_rows+x+1);
		  ShowCurrentImage();
		 }

	document.getElementById("tn"+(x+offset-max_rows)).style.display='none';
	document.getElementById("tn"+(x+offset-max_rows)).style.display='none';	
	document.getElementById("tn"+(x+offset)).style.display='';	
	document.getElementById("t_div"+(x+offset-max_rows)).style.display='none';
	document.getElementById("t_div"+(x+offset-max_rows)).style.display='none';	
	document.getElementById("t_div"+(x+offset)).style.display='';
	}


	if(slides==finished_slides)
	{
	
	dsPhotos.setCurrentRow(value_pics()-(rest));
	ShowCurrentImage();

	document.getElementById("t_div"+((value_pics()-(max_rows+rest))+x)).style.display='none';
	document.getElementById("tn"+((value_pics()-(max_rows+rest))+x)).style.display='none';
	if((value_pics()-(rest)+x)<value_pics())
	{
	document.getElementById("tn"+(value_pics()-(rest)+x)).style.display='';
	document.getElementById("t_div"+(value_pics()-(rest)+x)).style.display='';
	}
	}

}


function show_pics_left()
{ 
//alert(offset-max_rows-y-1);
	if(finished_slides<slides)
	{
			
	dsPhotos.setCurrentRow(offset-max_rows-y-1);	
	ShowCurrentImage();
	document.getElementById("t_div"+(offset-max_rows-y-1)).style.display='';
	document.getElementById("t_div"+(offset-y-1)).style.display='none';
	document.getElementById("tn"+(offset-max_rows-y-1)).style.display='';
	document.getElementById("tn"+(offset-y-1)).style.display='none';

	}
	
	if(finished_slides==slides)
	{
	
		dsPhotos.setCurrentRow(offset-y-1);
	  ShowCurrentImage();
 
		if(y<rest)
			{
			//document.getElementById("tn"+(value_pics()-y-1)).style.display='none';
			document.getElementById("t_div"+(value_pics()-y-1)).style.display='none';
			document.getElementById("tn"+(value_pics()-y-1)).style.display='none';
				if(y==0)
				{
				finished_slides++;
				}
			}
	

			document.getElementById("t_div"+(offset-y-1)).style.display='';
			document.getElementById("tn"+(offset-y-1)).style.display='';
			}
			

			
	}




function show_pics_restart()
{ 

		dsPhotos.setCurrentRow(offset-z-1);

	  ShowCurrentImage();
		if(z<rest)
			{
			document.getElementById("t_div"+(value_pics()-z-1)).style.display='none';
			document.getElementById("tn"+(value_pics()-z-1)).style.display='none';
				if(z==0)
				{
				finished_slides++;
				}
			}

	
    document.getElementById("tn"+(offset-z-1)).style.display='';
	document.getElementById("t_div"+(offset-z-1)).style.display='';
	
}



function right()
	{ 
		    if(x<max_rows)
			{
	
			show_pics_right();
			x++;
			setTimeout("right()",50);
			document.getElementById("arrow_left").style.display="";
			}
	
			else if (finished_slides<slides)
			
			{
			x=0;
			document.getElementById("begin").innerHTML=offset+1+"-";
      		document.getElementById("to").innerHTML=offset+max_rows;
      		document.getElementById("from").innerHTML=" from "+value_pics();
			offset=offset+max_rows; 
			}
			else
			{
			x=0;	
			}
			
			 if(finished_slides==slides)
			{
			document.getElementById("arrow_right").style.display="none";
			document.getElementById("begin").innerHTML=offset+1+"-";
        	document.getElementById("to").innerHTML=value_pics();
			document.getElementById("from").innerHTML=" from "+value_pics();
			}

			
}

		
function left()
		{
			
		if(y<max_rows&&left!=1)
		{
		show_pics_left();
		
		y++;
		setTimeout("left()",50);
		}
		else if(y==max_rows&&click_left==1)
		{
		
		
		if(finished_slides==slides)
		{	
		dsPhotos.setCurrentRow(offset-1);	
		ShowCurrentImage();
		offset=offset;
		finished_slides--;
		click_left=0;
		}
		else
		{
			
		dsPhotos.setCurrentRow(offset-max_rows-1);	
		ShowCurrentImage();
		offset=offset-max_rows;
		click_left=0;
		}
		
		}
		
		else if(finished_slides<slides-1)
		{	
		y=0;
			offset=offset-max_rows;
			document.getElementById("begin").innerHTML=offset-max_rows+1+"-";
      		document.getElementById("to").innerHTML=offset;
      		document.getElementById("from").innerHTML=" from "+value_pics();

		
		}
		else if(finished_slides=slides-1)
		{	

			document.getElementById("begin").innerHTML=offset-max_rows+1+"-";
      		document.getElementById("to").innerHTML=offset;
      		document.getElementById("from").innerHTML=" from "+value_pics();

		y=0;offset=offset;		
			
		}
		

		
		
	
		
		if(finished_slides==0)
		{	
		
			document.getElementById("arrow_left").style.display="none";
		}
		
		
		
	
		 if(y>=max_rows)
		 {
				
			document.getElementById("arrow_right").style.display="";	
			document.getElementById("begin").innerHTML="1-";
       		document.getElementById("to").innerHTML=max_rows;
			document.getElementById("from").innerHTML=" from "+value_pics();
	

		 }		

		}
function restart()
		{
		x=0;
		y=0;
		if(z<max_rows)
		{
		show_pics_restart();
		z++;
		setTimeout("restart()",50);
		}
		else
		{
		z=0;y=0;x=0;
		}
		}

		
function set_start_row(val)
{

slides=parseInt(value_pics()/max_rows);
rest=value_pics()%max_rows;

	if(val=="right")
	{ 
	  
    if(finished_slides<=slides-1&&value_pics()>max_rows&&x==0)
	{	 
		document.getElementById("arrow_right").style.display="";
		right();
		finished_slides++;

	}

	   
	}		
	
	if(val=="left" )
	{
	
	y=0;
	  if(finished_slides>0)
	{
	left();
	finished_slides--;
	}
	else
	{
		
	}
	}
	
	if(val=="restart" )
	{
		

restart();
		}
	}

function value_pics()
{
  var rows = dsPhotos.getData();
    return rows.length;
}



function click (e) {
  if (!e)
    e = window.event;
  if ((e.type && e.type == "contextmenu") || (e.button && e.button == 2) || (e.which && e.which == 3)) {
    if (window.opera)
      window.alert("Funktion ist deaktiviert.");
    return false;
  }
}
if (document.layers)
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = click;
document.oncontextmenu = click;

function toggle_div_albums()
{

if(document.getElementById("div_albums").style.display=="none")
{
document.getElementById("div_albums").style.display="";
}
else
{
document.getElementById("div_albums").style.display="none";
}
}

