// Höhe des sichtbaren Browserbereichs
var visibleHeight = 0;
var visibleWidth  = 0;
var menuSpeed = 300;
var activeImage = 1;
var projects = new Array();
var activeProject = 0;

$(document).ready(function(){
$("#back-to-top").click(function(){void(0);$('html, body').animate({scrollTop:0}, 'slow');});

 $("#archive").bind('mousewheel', function(event, delta) {
    var dir = delta > 0 ? 'Up' : 'Down',
    vel = Math.abs(delta);
    if (dir=='Up' && vel>0){ $('#nav-up').click();return false;}
    if (dir=='Down'&& vel>0 ){ $('#nav-down').click();return false;}
    return false;
  });

  $(".bodyx").bind('mousewheel', function(event, delta) {
    var dir = delta > 0 ? 'Up' : 'Down',
    vel = Math.abs(delta);

    if (dir=='Up' && vel>0 && $('#nav-left').parent().hasClass('active')){ $('#nav-left').click();return false;}
    if (dir=='Down' && vel>0 && $('#nav-right').parent().hasClass('active')){ $('#nav-right').click();return false;}
    return false;
  });


	// Nutzbaren/sichtbaren Browserbereich ermitteln
	initVisibleBrowserDimensions();

	// Positioning the board vertically centered in content area
	positioningBoardVerticalCentered();

	scaleAndPositioningFullscreenImages();

	scaleHomeSliderImages();

	// Bei resize des Window
	$(window).resize(function() {
	  initVisibleBrowserDimensions();
	  positioningBoardVerticalCentered();
	  scaleAndPositioningFullscreenImages();
	  scaleHomeSliderImages();

	  //scaleBGImage();
	  //scaleFullscreenImage();
		//archivePositioning();
	});

  if(document.getElementById('bg-image') != null) {
		var FullscreenrOptions = {
	     width: 1440,
	     height: 900,
	     bgID: "#bg-image"
	    };
	   $.fn.fullscreenr(FullscreenrOptions);
  }

	$("div#firstLevel div.current").click(function(){
		$("div#firstLevel div.opener img").click();
	});

	// Klick auf Pfeil für Hauptmenue (Ebene 1)
  $("div#firstLevel div.opener img").click(function(){
     if ($("div#firstLevelMenu").is(":hidden")) {
       changeFirstLevelMenuOpener(arrowClose, altClose);
       $("div#firstLevel").css('background-image', 'url(fileadmin/images/layout/bg-menu-open.png)');
       $("div#firstLevel").animate( { height: getFirstLevelMenuHeight()+'px' }, menuSpeed);
       $("div#firstLevel div.bottom").show();
       $("div#firstLevelMenu").slideDown(menuSpeed);
     }
     else {
     	 changeFirstLevelMenuOpener(arrowOpen, altOpen);
       $("div#firstLevel").animate( { height: '31px' }, menuSpeed );
       $("div#firstLevelMenu").slideUp(menuSpeed, function(){
         $("div#firstLevel").css('background-image', 'url(fileadmin/images/layout/bg-menu.png)');
         $("div#firstLevel div.bottom").hide();
       });

     }
  });

  $("div#secondLevel div.current").click(function(){
		$("div#secondLevel div.opener img").click();
	});

  // Klick auf Pfeil für Hauptmenue (Ebene 2)
  $("div#secondLevel div.opener img").click(function(){
     if ($("div#secondLevelMenu").is(":hidden")) {
       changeSecondLevelMenuOpener(arrowClose, altClose);
       $("div#secondLevel").css('background-image', 'url(fileadmin/images/layout/bg-menu-open-second.png)');
       $("div#secondLevel").animate( { height: getSecondLevelMenuHeight()+'px' }, menuSpeed);
       $("div#secondLevel div.bottom").show();
       $("div#secondLevelMenu").slideDown(menuSpeed);
     }
     else {
     	 changeSecondLevelMenuOpener(arrowOpen, altOpen);
       $("div#secondLevel").animate( { height: '31px' }, menuSpeed );
       $("div#secondLevelMenu").slideUp(menuSpeed, function(){
         $("div#secondLevel").css('background-image', 'url(fileadmin/images/layout/bg-menu-secondLevel.png)');
         $("div#secondLevel div.bottom").hide();
       });
     }
  });


  // Accordions

    //Hide Accordion Content
	$('div.accordions div.content').hide();

	// Erstes Akkordion active setzen
	$('div.accordions div.accordion:first').addClass('active');
	$('div.accordions div.accordion:first .content').show();



	// Underline bei Links entfernen
	//$('div.accordions a').css('text-decoration', 'none');

	// Cursor fÃ¼r Headlines setzen
	$('div.accordions h1').css('cursor', 'pointer');


    // Akkordeons auf Click per JS oeffnen
	$('div.accordions').parents('div#content').css('width', '600px');

	$('div.accordion h1').click(function() {


	  if ($('div.accordions').length > 0) {
		var activeAccordion = false;
		var accordions = $('div.accordions div.accordion');
    $("div.accordions h2").css('display','none');
    $("div.accordions h1").css('display','block');


	    if($(this).parent().hasClass("active")) {


	       $(this +' div.content').slideUp();
	    	$(this).parent().removeClass("active");
	    	deactivateNavigator('down');
								activateNavigator('up');
	    	updateProjectNavigatorInfo();
	    }
	    else  {
	      $(this).parent().find("h2").css('display','block');
        $(this).css('display','none');

	       $('div.accordion div.content').slideUp();
		    $('.accordion').removeClass("active");
		    $(this).parent().addClass("active");
		    $(this).next().slideToggle('normal');
		    deactivateNavigator('down');
								activateNavigator('up');
		    updateProjectNavigatorInfo();
	    }



		for (var accordionCounter=0; accordionCounter<accordions.length; accordionCounter++) {
	  if ($(accordions[accordionCounter]).hasClass('active'))  activeAccordion = accordionCounter;
		}


		if (activeAccordion !== false) {

			if (activeAccordion > 0) {activateNavigator('up');deactivateNavigator('down');}
			if ((activeAccordion+1) <  accordions.length){ activateNavigator('up');activateNavigator('down');}
			if ((activeAccordion ==0 ) ){ deactivateNavigator('up');}
		}
	}



	});






  // Projekte-Board auf Bild 1 positionieren
  //positioningBoard(activeImage);



/*
$("#content").touchwipe({
   wipeLeft: function() { $("#nav-right").click(); return false;},
     wipeRight: function() { $("#nav-left").click(); return false;},
     min_move_x: 80,
     preventDefaultEvents: true
});
*/
/*
$("#content").touchwipe({
   wipeLeft: function() { $("#nav-up").click(); return false;},
     wipeRight: function() { $("#nav-down").click(); return false;},
     min_move_x: 10,
     preventDefaultEvents: true
});
*/

var swipeOptions=
			{
				swipe:swipe,
				threshold:0
			}

			$(function()
			{
				//Enable swiping...
				$("#content").swipe( swipeOptions );



			});

			//Swipe handlers.
			//The only arg passed is the original touch event object
			function swipe(event, direction)
			{
				if (direction=='left'){$("#nav-right").click(); return false;}
				if (direction=='right'){$("#nav-left").click(); return false;}
				if (direction=='up'){$("#nav-down").click(); return false;}
				if (direction=='down'){$("#nav-up").click(); return false;}
			}

  // Projekte klickbar machen, um es mittig zu positionieren
  $('div#board div.board ul li').live('click', function() {
  	if($(this).attr('id').split('board-').join('') > activeImage)
  	  $("img#nav-right").click();
  	else if($(this).attr('id').split('board-').join('') < activeImage)
  	  $("img#nav-left").click();
  });


  // Archiv-Filter-Highlight an die richtige Position setzen
  if ($('div#archive').length > 0) {

  	$("#archives .archive").mouseenter(function(){
  	  if(!$(this).hasClass("active"))
  	    $(this).addClass("active removable");
  	});

  	$("#archives .archive").mouseleave(function(){
  	  if($(this).hasClass("removable"))
  	    $(this).removeClass("active removable");
  	});

  	$('div#content').css('overflow', 'hidden');

  	// Archiv-Suchfeld erweitern, wenn Schrift rauslaeuft
  	$('div#archive div#archive-navigation input#archive-search-input')
  		.keydown(function() { setWidth('archive-search-input'); })
  		.keyup(function() { setWidth('archive-search-input'); });
  	  setWidth('archive-search-input');

  	// Such-Formular onclick oeffnen
  	$('#filter-search-image').click(function() {
  		$(this).css('visibility', 'hidden');
  		$('div#archive-navigation .filter-search form').toggle();

  		var filterOffset = $('div#archive img#filter-search-image').offset();
  	  var archiveFilterOffset = parseInt($('div#archive-navigation').css('padding-left'));
  	  archiveFilterOffset+= parseInt($('div#archive div#archive-navigation').css('margin-left'));
  	  filterOffset = filterOffset.left - archiveFilterOffset + 6;
  	  $('div#archive-navigation div.filter-highlight img').css('margin-left', filterOffset + 'px');

  	});

  	// Cancel-Button bei Suche geklickt
  	$('#archive-search-cancel').click(function() {
  		// Suchfeld ausblenden
  		$('#filter-search-image').css('visibility', 'visible');
  		$('div#archive-navigation .filter-search form').toggle();

  		// Weisse Highlight-Grafik wieder über die Lupe setzen
  		var filterOffset = $('div#archive img#filter-search-image').offset();
  	  var archiveFilterOffset = parseInt($('div#archive-navigation').css('padding-left'));
  	  archiveFilterOffset+= parseInt($('div#archive div#archive-navigation').css('margin-left'));
  	  filterOffset = filterOffset.left - archiveFilterOffset + 1;
  		$('div#archive-navigation div.filter-highlight img').css('margin-left', filterOffset + 'px');
  	});

    /*
  	  // Archiv in den Content-Bereich positionieren
  	  archivePositioning();
    */
    }


  $("#footer-content .latestnews h1").click(function(){
	 document.location.href = 'index.php?id=4&news='+$(this).index("h1");
  });


}); // end document ready




// Inputbreite setzen
function setWidth(elementID) {
	var el = $('#'+elementID);
	if (el.length > 0) {
		if ($('#searchTmp').length == 0) $('body').append('<span id="searchTmp"></span>');
		var searchTmp = $('#searchTmp');
		searchTmp.empty();
		searchTmp.text(el.val());
		var searchWidth = Math.ceil(searchTmp.width())+20;
		if (searchWidth > parseInt(searchTmp.css('min-width'))) $('input#archive-search-input').css('width', Math.ceil(searchTmp.width())+'px');
	}
}

// Contentbereich auf eine feste Groesse setzen, damit er nicht in den Footer fliesst
function resizeContent() {
	var contentHeight = visibleHeight; // Browserhoehe
	contentHeight -= parseInt($('div#footer').height()) // Footer-Hoehe abziehen
	contentHeight -= parseInt($('div#content').css('top')) // Content-Abstand von oben abziehen

	$('div#content').css('height', contentHeight+'px');
}

function changeFirstLevelMenuOpener(src,alt) {
  $("div#firstLevel div.opener img").attr("src", src);
  $("div#firstLevel div.opener img").attr("title", alt);
  $("div#firstLevel div.opener img").attr("alt", alt);
}

function changeSecondLevelMenuOpener(src,alt) {
  $("div#secondLevel div.opener img").attr("src", src);
  $("div#secondLevel div.opener img").attr("title", alt);
  $("div#secondLevel div.opener img").attr("alt", alt);
}


function getFirstLevelMenuHeight() {
  return $("div#firstLevelMenu").innerHeight() + $("div#firstLevel div.current").innerHeight()+1;
}

function getSecondLevelMenuHeight() {
  return $("div#secondLevelMenu").innerHeight() + $("div#secondLevel div.current").innerHeight()+1;
}


function getWinSize(win) {
  if(!win) win = window;
  var s = new Object();

  if(typeof win.innerWidth != 'undefined') {
    s.width = win.innerWidth;
    s.height = win.innerHeight;
  }
  else {
    var obj = getBody(win);
    s.width = parseInt(obj.clientWidth);
    s.height = parseInt(obj.clientHeight);
  }
  return s;
}


// Der IE hat 2 verschiedene Objekte für den strict und quirks Mode.
function getBody(w) {
  return (w.document.compatMode && w.document.compatMode == "CSS1Compat") ? w.document.documentElement : w.document.body || null;
}


function initVisibleBrowserDimensions() {
	// Höhe des sichtbaren Browserbereichs
  visibleHeight = Number(getWinSize(window).height);

  // Breite des sichtbaren Browserbereichs
  visibleWidth  = Number(getWinSize(window).width);
}


function positioningBoardVerticalCentered() {
  var boardHeight = $("#board").outerHeight();
  var margin = Math.ceil((visibleHeight - boardHeight) / 2) - parseInt($("#header").outerHeight()) - parseInt($("#header").css('margin-top')) - ($("#content").css("margin-top").split('px')[0]);
  $("#board").css('margin-top', margin + 'px');

}

function positioningBoard(imageCounter) {
	var imageWidth = 580;
	var imageMargin = 60;
	var board = $('div#board');
	imageWidth = imageWidth+ (0);
	if (board.length > 0) {

		var leftOffset = (visibleWidth/2)-(imageWidth/2)-((imageCounter-1)*(imageWidth+imageMargin));
		$('div#board div.board ul').animate({ left: leftOffset+'px' }, 300);
		$('div#board div.board ul li').removeClass('active');
		$('div#board div.board ul li#board-'+imageCounter).addClass('active');
		activeImage = imageCounter;
	}
positioningBoardVerticalCentered();
scaleBGImage();
  if(document.getElementById('bg-image') != null) {
		var FullscreenrOptions = {
	     width: 1440,
	     height: 900,
	     bgID: "#bg-image"
	    };
	   $.fn.fullscreenr(FullscreenrOptions);
  }

}

function scaleFullscreenImage() {

	var image = $('img.fullscreen');
	image.show();
	if (image.length > 0) {
		if (visibleHeight > visibleWidth)
			image.css({height: visibleHeight+'px', width: 'auto'});
		else
			image.css({width: visibleWidth+'px', height: 'auto'});
	}
}

function scaleBGImage() {
	var $this = $('#bg-image');
	var ratio = $this.height() / $this.width();
	if ((visibleHeight/visibleWidth) > ratio){
	    $this.height(visibleHeight);
	    $this.width(visibleHeight / ratio);
	} else {
	    $this.width(visibleWidth);
	    $this.height(visibleWidth * ratio);
	}
}

function scaleAndPositioningFullscreenImages() {
	if($("#content").find(".fullscreen").length > 0) {
		var options = { width: 1440, height: 900 };
		var ratio = options.height / options.width;

		// Get browser window size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();

		var images = $("#content .fullscreen").each(function(index){

		//alert("asd");
			// Scale the image
			if ((browserheight/browserwidth) > ratio){
			    $(this).height(browserheight);
			     $(this).width(browserheight / ratio);
			} else {
			    $(this).width(browserwidth);
			    $(this).height(browserwidth * ratio);
			}

			// Center the image
			$(this).css('left', index*visibleWidth -((activeImage-1)*visibleWidth) + 'px');




		});
	}
}


function scaleHomeSliderImages() {


		var FullscreenrOptions = {
	     width: 1440,
	     height: 900,
	     bgID: ".fullscreenhome"
	    };
	   $.fn.fullscreenr(FullscreenrOptions);


/*
	$("img.fullscreenhome").each(function(index){
	  $('div#footer div#navigator').hide();
		// Get height of original image
		$imgHeight = $(this).height();

		// Get width of original image
		$imgWidth  = $(this).width();

		// Calculate ratio
		$ratio = $imgHeight / $imgWidth;

		// Get browser window size
		$browserwidth = $(window).width();
		$browserheight = $(window).height();

		// Scale the image
		if (($browserheight/$browserwidth) > $ratio){
		    $(this).height($browserheight);
		    $(this).width($browserheight / $ratio);
		} else {
		    $(this).width($browserwidth);
		    $(this).height($browserwidth * $ratio);
		}

		$left  = ($browserwidth - $(this).width())/2;
		$top = (($browserheight - $(this).height())/2);

		if(index > 0) {

    homeSliderTopPositions[index] = Math.floor(Math.abs($top))* -1;

		  $top = Math.abs($prevImg.height()) - (Math.abs(Math.floor($prevImg.css("top").split('px')[0])));
		}

		$prevImg = $(this);

		// Center the image
		//$(this).css('left', $left);
		$(this).css('top', $top);

		//$(this).animate({ top: index*visibleHeight -((activeImage-1)*visibleHeight) + 'px' });

		return this;
	});
	*/
}





function openProject(pageuid, params) {
  document.location.href = '/index.php?id=' + pageuid + params;
}
