/*
 * Thickbox 3 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
if (!possibleLanguages)
 var possibleLanguages = new Array ("at","en","es","fr","jp");
if (language) {
 for (i=0;i<possibleLanguages.length;i++) {
  if (possibleLanguages[i]==language)
   break;
 }
 if (i==possibleLanguages.length)
  language = "at";
 
}
else

 var language = "at";


var languageCaptions = new Array(new Object, new Object, new Object, new Object, new Object, new Object, new Object);
// closeCaptions
languageCaptions[0]["at"] = " Schlie&szlig;en";
languageCaptions[0]["en"] = " Close";
languageCaptions[0]["es"] = " cierre";
languageCaptions[0]["fr"] = " fin";
languageCaptions[0]["jp"] = " Nagasaki";
// nextCaption
languageCaptions[1]["at"] = " Weiter &raquo;";
languageCaptions[1]["en"] = " Next &raquo;";
languageCaptions[1]["jp"] = " Mitsubishi &raquo;";
// prevCaption
languageCaptions[2]["at"] = "&laquo; Zur&uuml;ck";
languageCaptions[2]["en"] = "&laquo; Previous";
languageCaptions[2]["es"] = "&laquo; anterior";
languageCaptions[2]["jp"] = "&laquo; Yokohama";
// counterCaption
languageCaptions[3]["at"] = " Bild <x> von <n>";
languageCaptions[3]["en"] = " Picture <x> of <n>";
languageCaptions[3]["es"] = " Cuadro <x> de <n>";
languageCaptions[3]["fr"] = " Image  <x> de <n>";
languageCaptions[3]["jp"] = " Yamaha <x> of <n>";

// Drucken
languageCaptions[4]["at"] = "Dieses Bild drucken";
languageCaptions[4]["en"] = "Print this picture";
// Ecard
languageCaptions[5]["at"] = "Dieses Bild als Ecard verschicken";
languageCaptions[5]["en"] = "Send this picture as ecard";
// Download
languageCaptions[6]["at"] = "Dieses Bild herunterladen";
languageCaptions[6]["en"] = "Download this picture";


//var tb_pathToImage = "images/layout/loading.gif";
var tb_pathToImage = "/scs/files/images/loadingAnimation.gif";
var tb_trenner = "";
// captions
var closeCaption = languageCaptions[0][language];
var nextCaption = languageCaptions[1][language];
var prevCaption = languageCaptions[2][language];
// Counter Caption : Bild <x> von <n> 
var counterCaption = languageCaptions[3][language];
/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
//on page load call tb_init
$(document).ready(function(){
 tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
 imgLoader = new Image();// preload image
 imgLoader.src = tb_pathToImage;
 $('a.galerie').click(function() {
 	
	var $div = $(this).siblings('div.thickboxContainer');
	
	$div.children().each(function(i) {
		var t = null;//this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
	
		if (i==0) {
			tb_show(t,a,g);
		}
	});
	this.blur();
	return false;
 });
});
//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
 $(domChunk).click(function(){
 var t = null;//this.title || this.name || null;
 var a = this.href || this.alt;
 var g = this.rel || false;
 tb_show(t,a,g);
 this.blur();
 return false;
 });
}
function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

 try {
  	$("embed, object").hide();
   if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
   $("body","html").css({height: "100%", width: "100%"});
   $("html").css("overflow","hidden");
   if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
    $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
    $("#TB_overlay").click(tb_remove);
   }
  }else{//all others
   if(document.getElementById("TB_overlay") === null){
    $("body").append("<div id='TB_overlay'></div><div id='TB_window'>");
    $("#TB_overlay").click(tb_remove);
   }
  } 

	if(tb_detectMacXFF()){
		$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
	}else{
		$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
	}  
  
  if(caption===null){caption="";}

 if ($('#TB_load').length == 0) {
	 $('body').append('<div id="TB_load"><img src="'+imgLoader.src+'" /></div>');//add loader to the page
	}
  $('#TB_load').show();//show loader


  var baseURL;
    if(url.indexOf("?")!==-1){ //ff there is a query string involved
   baseURL = url.substr(0, url.indexOf("?"));
    }else{ 
      baseURL = url;
    }

    var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g;
    var urlType = baseURL.toLowerCase().match(urlString);
  if(1|| urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp') {//code to show images
   TB_PrevCaption = "";
   TB_PrevURL = "";
   TB_PrevHTML = "";
   TB_NextCaption = "";
   TB_NextURL = "";
   TB_NextHTML = "";
   TB_imageCount = "";
   TB_FoundURL = false;

  if(imageGroup){
    TB_TempArray = $("a[@rel="+imageGroup+"]").get(); 

    for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
     var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);

//     	if(TB_TempArray[TB_Counter].id){
//     		alert(TB_TempArray[TB_Counter].id);	
//     	}
     
      if (!(TB_TempArray[TB_Counter].href == url)) {      
       if (TB_FoundURL) {
        TB_NextCaption = TB_TempArray[TB_Counter].title;
        TB_NextURL = TB_TempArray[TB_Counter].href;
        TB_NextHTML = "<a id='TB_next' href='#'><span>"+nextCaption+"</span></a>";      
       } else {
        TB_PrevCaption = TB_TempArray[TB_Counter].title;
        TB_PrevURL = TB_TempArray[TB_Counter].href;
        TB_PrevHTML = "<a id='TB_prev' href='#'><span>"+prevCaption+"</span></a>";
       }
      } else {
       TB_FoundURL = true;
       TB_imageCount = counterCaption;
       TB_imageCount =  TB_imageCount.replace(/<x>/g,(TB_Counter + 1));
       TB_imageCount = TB_imageCount.replace(/<n>/g, (TB_TempArray.length)); 
       TB_imageNow = TB_Counter+1;
       TB_downloadURL = TB_TempArray[TB_Counter].title;
      }
      
      
    }
   }

   imgPreloader = new Image();
   imgPreloader.onload = function(){  
   imgPreloader.onload = null;
   
   // Resizing large images - orginal by Christian Montoya edited by me.
   var pagesize = tb_getPageSize();
   var x = pagesize[0] - 150;
   var y = pagesize[1] - 150;
   var imageWidth = imgPreloader.width;
   var imageHeight = imgPreloader.height;
   if (imageWidth<480) {
	mul = 480/imageWidth;
	imageWidth = 480;
//	imageHeight = Math.round(imageHeight * mul);
   }
   if (imageWidth > x) {
    imageHeight = imageHeight * (x / imageWidth); 
    imageWidth = x; 
    if (imageHeight > y) { 
     imageWidth = imageWidth * (y / imageHeight); 
     imageHeight = y; 
    }
   } else if (imageHeight > y) { 
    imageWidth = imageWidth * (y / imageHeight); 
    imageHeight = y; 
    if (imageWidth > x) { 
     imageHeight = imageHeight * (x / imageWidth); 
     imageWidth = x;
    }
   }
   // End Resizing
   TB_WIDTH = imageWidth + 30;
   TB_HEIGHT = imageHeight + 160;
   if (TB_WIDTH<300) {
    mul = Math.round(300/TB_WIDTH);
    TB_WIDTH = 300;
    TB_HEIGHT = TB_HEIGHT * mul;
   }
   TB_trenner = "";
   if (TB_NextHTML!="" && TB_PrevHTML!="")
    TB_trenner = tb_trenner;
   
   $("#TB_window").html('').append("<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + TB_imageCount +"</div></div>"
   +"<div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>" + closeCaption + 
   "</a></div>"
   +"<a href='' id='TB_ImageOff' title='Close'>"
   +"<img id='TB_Image' src='"+url
/*   width='"+imageWidth+"' height='"+imageHeight+"' */
   +"' alt='"+caption+"'/></a>" + 

   "</div>"  + "</div><div id='TB_thumbnails'></div>"); 
	
	var limit = 5;
	// get Anfangs ding
	var $imageGroup =  $('a[@rel='+imageGroup+']');
	var $pictureNow = $imageGroup.eq(TB_imageNow-1).clone(true);

	$('div#TB_thumbnails').append($pictureNow);
	$('div#TB_thumbnails').children().addClass('activePicture');
	var thumbnailCounter = 1;
	var spacing = 1;
	var startIndex = TB_imageNow-1;

	do {
		if ($imageGroup[startIndex-spacing]) {
			var $pic = $imageGroup.eq(startIndex-spacing).clone(true);
			$('div#TB_thumbnails').prepend($pic);

			thumbnailCounter++;
		}
		if ($imageGroup[startIndex+spacing]) {
			var $pic = $imageGroup.eq(startIndex+spacing).clone(true);
			$('div#TB_thumbnails').append($pic);

			thumbnailCounter++;
		}
		// Fix von Helmut, 09.06.2009
		// Folgende If-Abfrage wurde eingefügt, damit bei einer Galerie, die aus weniger als 5 Bildern besteht, keine Endlos-Schleife zustande kommt
		if (!$imageGroup[startIndex+spacing]) { // Wenn Kein Bild mehr vorhanden ist...
			thumbnailCounter++; // .. muss der Zähler trotzdem erhöht werden, weil es sonst in der while-Abfrage zu einer Endlosschleife kommt.
		}
		spacing++;
	} while (limit >thumbnailCounter);
	
		
	$('div#TB_thumbnails').prepend(TB_PrevHTML).append(TB_NextHTML);
	if (TB_PrevHTML === "")
		$('div#TB_thumbnails').prepend('<a id="TB_prevSpacer"></a>');
	$('div#TB_thumbnails').append('<div class="clear">&nbsp;</div>');
	$('div#TB_thumbnails').prepend('<a id="print" href="/scs/staticpages/print.php">'+languageCaptions[4][language]+'</a><a id="ecard" href="/scs/staticpages/ecard.php/scs/staticpages/ecard.en.php">'+languageCaptions[5][language]+'</a><div class="clear">&nbsp;</div>');
	
	if(TB_downloadURL){
		$('div#TB_thumbnails').prepend('<a id="download" href="#">'+languageCaptions[6][language]+'</a>');
	}	
	
	
	
	$('a#ecard').click(function() {
		window.open('/scs/staticpages/ecard.php'+'?file='+url+'&site='+document.location.href, 'Ecard','width=1024,height=768, scrollbars=yes');
		return false;
	});
	$('a#print').click(function() {
		//alert('hi');
		window.open($(this).attr('href')+'?file='+url, 'Picture', 'width=1024,height=768,scrollbars=yes');
		return false;
	}); 
	$('a#download').click(function() {
		window.open(TB_downloadURL, 'Download','width=1024,height=768, scrollbars=yes');
		return false;
	});
	
	
	
	$('div#TB_thumbnails a').attr("rel","").removeClass("thickbox").not('.activePicture, #print, #ecard, #download').addClass('picture');
	$('div#TB_thumbnails a').not('#print,#ecard,#download, #TB_next, #TB_prev').click(function() {
		$('#TB_Image').css('visibility','hidden');
		tb_show(this.rel, this.href, imageGroup);    
		return false; 	
	});

	
	 
   $("#TB_closeWindowButton").click(tb_remove);
   
   if (!(TB_PrevHTML === "")) {
    function goPrev(){
     if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
     //$("#TB_window").remove();
     //$("body").append("<div id='TB_window'></div>");
     $('#TB_Image').css("visibility","hidden");
     tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
     return false; 
    }
    $("#TB_prev").click(goPrev);
   }
   
   if (!(TB_NextHTML === "")) {  
    function goNext(){
//     $("#TB_window").remove();
 //    $("body").append("<div id='TB_window'></div>");
 	$('#TB_Image').css('visibility','hidden');
    tb_show(TB_NextCaption, TB_NextURL, imageGroup);    
     return false; 
    }
    $("#TB_next").click(goNext);
    
   }
  
   document.onkeydown = function(e){  
    if (e == null) { // ie
     keycode = event.keyCode;
    } else { // mozilla
     keycode = e.which;
    }
    if(keycode == 27){ // close
     tb_remove();
    } else if(keycode == 39){ // display previous image; 190
     if(!(TB_NextHTML == "")){
      document.onkeydown = "";
      goNext();
     }
    } else if(keycode == 37){ // display next image
     if(!(TB_PrevHTML == "")){
      document.onkeydown = "";
      goPrev();
     }
    } 
   };
   tb_position();
   
   $("#TB_ImageOff").click(tb_remove);
   $("#TB_window").css({display:"block"}); //for safari using css instead of show
	
	$("#TB_load").hide();
	
};
   
   imgPreloader.src = url;
   
  }
	
  
 } catch(e) {

  //nothing here
 }
}
//helper functions below
function tb_showIframe(){
 $("#TB_load").remove();
 $("#TB_window").css({display:"block"});
}
function tb_remove() {
 
  $("#TB_imageOff").unbind("click");
 $("#TB_overlay").unbind("click");
 $("#TB_closeWindowButton").unbind("click");
 $("#TB_window").fadeOut("fast",function(){
  $("#TB_iframeContent").attr("src","about:blank");
  $('#TB_window,#TB_overlay,#TB_HideSelect').remove();
   $("embed, object").show();
 });
 $("#TB_load").remove();
 if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
 
  $("body","html").css({height: "auto", width: "auto"});
  $("html").css("overflow","");
    
 }

 
 document.onkeydown = "";
 return false;
}

function tb_position() {
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});

	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}
