$(document).ready (maininit);

/*******************************************************************************
 Wird nach erstmaligem Seitenaufruf ausgeführt
------------------------------------------------------------------------------*/
function maininit() {
   
    $.historyInit (pageload); // callback-Funktion pageload an jquery.
    	
  	// Schriftgrad anpassen, falls Calibri nicht installiert  
    $(function()
    {      
      var detector = new FontDetector();
  
      if (!detector.test("Calibri"))
        $("body").addClass("notCalibri" );   
    });
    
    // FlashNavi einbinden 
    /*var flashvars = {};
    var params = {};
    var attributes = {};
    swfobject.embedSWF ("swf/SwfMenu.swf", "swfMenu", "706", "50", "9.0.0","swf/expressInstall.swf", flashvars, params, attributes);*/
};

/*******************************************************************************
 pageload-Function
------------------------------------------------------------------------------*/
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload (hash) 
{  
  var qpLangSel; 
  
  // für IE6 darf der Aufruf der Projekte nicht als "href='content.php?project=*'" erfolgen, da jquery.history sonst bei historyCheck zurückspringt, daher der workaround unten über url, data
  var url = "php/content.php"; 
  
  var hiddenLang = $("#lang").attr("value"); // hidden-field lang auslesen
  var qpLang = "lang="+hiddenLang; // QueryParameter Lang
  
  // anderen Sprach-QP ermitteln für Sprachwahl-Link
  if (hiddenLang != 'en')
    qpLangSel = "lang=en"; 
  else
    qpLangSel = "lang=de"; 
  
  var data = hash+"&"+qpLang;
  
  //alert(hash);  
  // container-div anhängig von Seitenaufbau mit oder ohne fixe Höhe
  /*if(hash == "id=2") 
    $("#container").height("auto");
  else 
    $("#container").height("516px");*/
    
  // wenn andere Seite als content.php aufgerufen wurde, beginnt hash mit "http://"
  //if (hash.slice(0,1) != "p") {
   
	// Abfrage, ob querystring übergeben wurde, falls ja project auslesen
  //if (hash.split("=")[0] == "project") 
  //  activeProject = hash.split("=")[1];
  //else
  //  activeProject = "overview";

  // Ajax -> httpRequest
  if (hash) {
   // activeID in Sprachwechsel-Hyperlink schreiben
   var activeID = data.split("id=")[1].split("&")[0];
   var href = "index.php?"+qpLangSel+"&sid="+activeID+"#id="+activeID;

   $("#languageSelector").attr ("href", href); 
   // Seitenaufruf über Hyperlink oder Back-Button
	 $.ajax ({
      url: url,
      data: data,
      dataType: "html",
      success: handleHttpRequestComplete
    })  
	} else {	
		// erster Seitenaufruf      
		$.ajax({
      url: "php/content.php",
      data: "id=0&"+qpLang,
      dataType: "html",
      success: handleHttpRequestComplete
    }) 
	}

}

function doHttpRequest (hash) 
{
  hash = hash.replace(/^.*#/, '');
	$.historyLoad (hash); 
}

/*******************************************************************************
 Alle Funktionen, die aufgerufen werden sollen, wenn content.php neugeladen wurde
------------------------------------------------------------------------------*/
function handleHttpRequestComplete (html) {

  // Inhalt schreiben
  $("#subPageWrapper").empty().append (html);
  
  // localScroll
  $.localScroll ({
		target: '#container', //could be a selector or a jQuery object too.
		axis: 'x', 
		duration: 400,
		onAfter: function(){ setMaskLink (); }
	});
	
	// Höhe theMask setzen
	$("#theMask").height ($("#container").height());
	
	// jQuery Scrolling für ScrollContainer
	/*$('.scrollContainer').jScrollPane({
    scrollbarWidth: 28, 
    wheelSpeed: 34,
    dragMinHeight: 34, 
    dragMaxHeight: 34, 
    showArrows:true});*/

	// zur ersten Seite springen
	$("#container").scrollTo (0, {
    axis: 'x',
    duration: 400,
		onAfter: function(){ setMaskLink (); }
	});
		
	// lightbox
  $("#container a[@rel='lightbox']").lightBox(); 
  $("#container a[@rel='lightbox-press']").lightBox(); 
  
  // png-fix
  $(document).pngFix();
  
  $("div.readmore").hover ( function() {  
    $(this).toggleClass('blueHover'); 
  }, function() {
    $(this).toggleClass('blueHover'); 
  });

  // OnClick Events für Textbutton read more
	$("div.readmore").toggle ( function() {      
	  // Klick für momentan aktiven Eintrag ausführen
	  $("div.blue").click();
	  // gewählten ausklappen
	  $(this).children("span.hide").slideDown("middle"); 
	  $(this).toggleClass('blue'); 
	}, function() {
	  // gewählten einfahren
	  $(this).children("span.hide").slideUp("middle"); 
	  $(this).toggleClass('blue'); 
  });

  // OnClick Events für Hyperlinks
	$("a[@rel='internal']").click ( function() {
	  var query = $(this).attr("href");
	  doHttpRequest (query); 
	  var activeId = query.split("#id=")[1];
	  sendToActionScript (activeId); // Linkziel übergeben
		return false;
	});
	
	// Tooltips
	$('.tooltip').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 50 
  });
	
	// Aktives Projekt an quicknav.swf übergeben
  //sendToActionScript (); 
}

// Logik zur Übergabe des neuen Linkziels für maskLink-Grafik
function setMaskLink () 
{    
    var nextPageHref;
    nextPageHref = $("#p0 h1 .next").attr('href');
    
    // aktive Seite ermitteln
    if ($("#p0").offset().left <= 0) {
      if ($("#p1 h1 .next").attr('href'))
        nextPageHref = $("#p1 h1 .next").attr('href');
      else
        nextPageHref = 'disabled';
    }
    
    if ($("#p0").offset().left <= -1000) {
      if ($("#p2 h1 .next").attr('href'))
        nextPageHref = $("#p2 h1 .next").attr('href');
      else
        nextPageHref = 'disabled';
    }

    if ((nextPageHref != 'disabled') && (nextPageHref != undefined) && (nextPageHref != '')) {
      $("#maskLink").css ("cursor", "pointer"); 
      $("#maskLink").attr ("href", nextPageHref);  
    }        
    else {
      $("#maskLink").css ("cursor", "default");  
      $("#maskLink").removeAttr ("href", nextPageHref);  
    }
}

/*******************************************************************************
 Aktives Projekt an quicknav.swf übergeben
------------------------------------------------------------------------------*/
function sendToActionScript(activeId) 
{
  if (thisMovie("swfMenu").sendToActionScript)
    thisMovie("swfMenu").sendToActionScript (activeId);
}

// wird benötigt von sendToActionScript
function thisMovie(movieName) 
{
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}    

/*******************************************************************************
 Font Detector (Schriftgrad anpassen, falls Calibri nicht installiert)
------------------------------------------------------------------------------*/

/* http://www.lalit.org/lab/fontdetect.php */
var FontDetector = function() {
  var h = document.getElementsByTagName("body")[0];
  var d = document.createElement("div");
  var s = document.createElement("span");

  d.appendChild(s);
  d.style.display = "inline"; //FR: zur Sicherheit auf inline setzen, damit nicht falsch geerbt wird
  d.style.fontFamily = "serif";    //font for the parent element DIV.
  s.style.fontFamily = "serif";    //serif font used as a comparator.
  s.style.fontSize   = "72px";      //we test using 72px font size, we may use any size. I guess larger the better.
  s.innerHTML        = "mmmmmmmmmml";    //we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
  h.appendChild(d);

  var defaultWidth   = s.offsetWidth;    //now we have the defaultWidth
  var defaultHeight  = s.offsetHeight;  //and the defaultHeight, we compare other fonts with these.
  h.removeChild(d);

  function test(font) {
    h.appendChild(d);
    var f = [];
    f[0] = s.style.fontFamily = font;  // Name of the font
    f[1] = s.offsetWidth;        // Width
    f[2] = s.offsetHeight;        // Height
    h.removeChild(d);

    font = font.toLowerCase();
    if (font == "arial" || font == "serif") {
      f[3] = true;  // to set arial and serif true
    } else {
      f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);  // Detected?
    }
    return f[3];
  }
  this.test = test;
}

