var arrPageSizes = new Array();
var arrPageScroll = new Array();
var arrLbImages = new Array();
var closeText = "schlie&szlig;en";
var lbLang = "de";
var currentIndex = 0;

$(document).ready(function() {


  $("#lightbox-activate").click(function(e) {
    e.preventDefault();   
    showLightbox(7);
  });
  
  
  
  $("#lightbox-nav-btnNext").live("click",function(e) {
    e.preventDefault();    
    currentIndex++;
    if(currentIndex >= arrLbImages.length-1)
  	$("#lightbox-nav-btnNext").hide();
    if(currentIndex >= 0)
    	$("#lightbox-nav-btnPrev").show();  	
    $("#lightbox-image").attr("src",arrLbImages[currentIndex]);       
  });
  
  $("#lightbox-nav-btnPrev").live("click",function(e) {
    e.preventDefault();
    currentIndex--;
    if(currentIndex <= arrLbImages.length-1)
  	$("#lightbox-nav-btnNext").show();
    if(currentIndex <= 0)
    	$("#lightbox-nav-btnPrev").hide();  	
    $("#lightbox-image").attr("src",arrLbImages[currentIndex]);    
  });
  
  
  /* Aufklapp-Language-Navigation
  $("ul#lang-navi li div").hide();
  $("ul#lang-navi li.active div").show();
  
  var removedLi = $("ul#lang-navi li.active").detach();
  removedLi.appendTo($("ul#lang-navi"));
  
  $("ul#lang-navi").hover(function() {
    //$("ul#lang-navi li span").css("display","block");
    $("ul#lang-navi li div").fadeIn(200);
  },function() {
    //$("ul#lang-navi li span").css("display","none");
    $("ul#lang-navi li div:not(ul#lang-navi li.active div)").fadeOut(200);
    //$("ul#lang-navi li.active span").show();    
  });
  */
  $("ul#lang-navi li.inactive a").click(function(e) {
    e.preventDefault();
  });
  
  if ($(".slideshow").length)
  {
    $('ul#slideshow-content div.slideshow-content-right span.slideshow-link-text').hide();

    initCycles();

    $('ul#slideshow-content').mouseenter(function() {
      $('.slideshow-link-text').show();
    });

    $('ul#slideshow-content').mouseleave(function() {
      $('.slideshow-link-text').hide();
    });
  }

  
});

$(window).resize(function() {
	arrPageSizes = ___getPageSize();
	arrPageScroll = ___getPageScroll();
});



function showLightbox(id) {
  arrPageSizes = ___getPageSize();
  arrPageScroll = ___getPageScroll();	
  currentIndex = id;
  
  if(lbLang=="en")
     closeText = "close";
  if(lbLang=="es")
     closeText = "cerrar";
  if(lbLang=="fr")
     closeText = "fermer";
     
  var currentImage = arrLbImages[id];
  $('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-data-box"><a href="#" id="lightbox-secNav-btnClose">'+closeText+'</a></div><div id="lightbox-container-image-box"><div id="lightbox-container-image"><img id="lightbox-image" src="'+currentImage+'"><div id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a></div></div></div></div>');	
	// Style overlay and show it
  $('#jquery-overlay').css({
		backgroundColor:	"#000",
		opacity:			0.7,
		width:				"100%",
		height:				arrPageSizes[1],
		position:     "absolute"
	}).fadeIn();

	// Calculate top and left offset for the jquery-lightbox div object and show it
	$('#jquery-lightbox').css({
		top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
		left:	arrPageScroll[0]
	}).show();
	// Assigning click events in elements to close overlay
	$('#jquery-overlay').click(function() {
		_finish();
		return false;								
	});
	// Assign the _finish function to lightbox-secNav-btnClose objects
	$('#lightbox-secNav-btnClose').click(function() {
		_finish();
		return false;
	});
  
  if(id<=0)
  	$("#lightbox-nav-btnPrev").hide();
  if(id==(arrLbImages.length-1))
  	$("#lightbox-nav-btnNext").hide();
  	
}





function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
function ___getPageScroll() {
var xScroll, yScroll;
if (self.pageYOffset) {
	yScroll = self.pageYOffset;
	xScroll = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
	yScroll = document.documentElement.scrollTop;
	xScroll = document.documentElement.scrollLeft;
} else if (document.body) {// all other Explorers
	yScroll = document.body.scrollTop;
	xScroll = document.body.scrollLeft;	
}
arrayPageScroll = new Array(xScroll,yScroll);
return arrayPageScroll;
};

function _finish() {
	$("#lightbox-nav-btnNext,#lightbox-nav-btnNext").die("click");
	$('#jquery-lightbox').remove();
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
	
}

function initCycles()
{
    if ($(".slideshow").length)
    {
      /* <![CDATA[ */
      $('.slideshow ul').after('<ul id="nav">').cycle({
    		fx:                 'fade',
    		timeout:            8000,
    		pager:              '#nav',
    		pagerAnchorBuilder: function(idx, slide) {
          //var src = $('img',slide).attr('src');
          return '<li><a class="nav-'+idx+'" href="#"><\/a><\/li>';
        }
      });
      /* ]]> */
    }
}
