// 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) {
		// hash doesn't contain the first # character.
		var newLocation = hash.split("?historyData=");
		
		if(hash) {
			// restore ajax loaded state
			//$("#content").load('scripts/processURL.php?newLocation='+newLocation+'&historyData='+historyData);
		
      contentWidth = $("#content").width();
      if(contentWidth == 735 ){
        $("#content").html("<div style='height:380px'></div>");
      }  
      
      $("#main").slideUp('slow', function () {
        ajaxInit();
        http_request.onreadystatechange = show;
        http_request.open('GET', 'scripts/processURL.php?newLocation='+newLocation[0]+'&historyData='+newLocation[1], true);
        http_request.send(null);
        setPage(newLocation[0]); 
      });
      
    } else {
			$.historyLoad("Startseite");
		}
		
		_gaq.push(['_trackPageview', "/#" + newLocation[0]]);
	}
	
	$(document).ready(function(){
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload);
		
		// set onlick event for buttons
		$("a[@rel='historyIndex']").click(function(){
			// 
      var hash = this.href;
      hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			$.historyLoad(hash);
			return false;
		});
		
		
	});
	
function loadShow(showID) {
  $.historyLoad("Shows?historyData=" + showID);
}

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

	//check if Flash object exists
	if (window.document.mymovie) {
		//set page variable in _root timeline of Flash movie
		window.document.mymovie.SetVariable("active", newPage);
		//window.document.mymovie.SetVariable("fromflash", "0");
	}
}

var http_request;
function ajaxInit(){

if (window.XMLHttpRequest) {
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {
      http_request.overrideMimeType('Content-Type');
   }
    } else if (window.ActiveXObject) { // IE
    try {
       http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
       http_request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {}
    }
    }
   if (!http_request) {
       alert('Das Request Objekt konnte nicht erzeugt werden! Bitte ueberpruefen Sie Ihre JAVA Einstellungen!');
        return false;
    }
   
} 

function show() {
  
  if (http_request.readyState == 4 && http_request.status==200) {
  //document.getElementById("content").style.visibility = "hidden";
      
  var arrResponse = http_request.responseText.split("<!-- segment -->");
  if(arrResponse[3] == ""){
    var contentWidth = 600 + "px";
  } else {
    var contentWidth = arrResponse[3];
  }
  //alert(contentWidth);
      $("#content").width(contentWidth);
      $("#breadcrumbs").html(arrResponse[0]);
      $("#subnavi").html(arrResponse[1]);
      
      if(contentWidth == "735px" ){
        $("#content").html("<div style='height:380px'></div>");
        $("#main").slideDown('slow',function(){
          $("#content").html(arrResponse[2]);
        });
      } else {
        $("#content").html(arrResponse[2]);
        
        $("#main").slideDown('slow', function(){
          format();
        });
      }
      //document.getElementById("content").style.visibility = "visible";
      
    $("a[@rel='history']").click(function(){
			var hash = this.href;
      hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
			return false;
		});  

    $('.showinfo').tooltip(
    {
      track: true,
      showURL: false,
    	showBody: " ### ",
    	opacity: 1,
    	left: -20
    }); 
  
    $('.ticketinfo a').tooltip(
    {
      track: true,
      showURL: false,
    	showBody: " ### ",
    	opacity: 1,
    	left: -120
    });
  
    if(arrResponse[4] == "News"){
      var detail = arrResponse[5];
      $('#commentForm').bind('submit', function() {
        if(getFormStatus()){
          $(this).ajaxSubmit(function(request, settings){
            setFormStatus();
            $.historyLoad("News?historyData=" + detail);
          });
        }
        return false; 
      });
      /* ajaxInit();
      http_request.onreadystatechange = showRating;
      http_request.open('GET', 'news/rating.php?detail='+detail, true);
      http_request.send(null); 
      */
    } else if (arrResponse[4] == "Gaestebuch") {
      var detail = arrResponse[5];
      $('#guestbookForm').bind('submit', function() {
        if(getFormStatus()){
          $(this).ajaxSubmit(function(request, settings){
            setFormStatus();
            $.historyLoad("Gaestebuch");
          });
        }
        return false; 
      });
    }
      //ajaxInit();
      //http_request.onreadystatechange = showRating;
      //http_request.open('GET', 'news/rating.php?detail=guestbook', true);
      //http_request.send(null); 
    }
}

///////////////////////////////////////////
///////////////////////////////////////////

function updateKalender(newMonth) {
    ajaxInit();
    http_request.onreadystatechange = showKalender;
    http_request.open('GET', 'termine/kalender.php?monat='+newMonth, true);
    http_request.send(null);
}

function showKalender() {
    if (http_request.readyState == 4) {
      document.getElementById("subnavi").innerHTML = http_request.responseText;
    } 

    $('.showinfo').tooltip(
    {
      track: true,
      showURL: false,
    	showBody: " ### ",
    	opacity: 1,
    	left: -20
    });    
}

///////////////////////////////////////////
///////////////////////////////////////////

function showRating(){
  if (http_request.readyState == 4 && http_request.status == 200) {
    var arrResponse = http_request.responseText.split("<!-- segment -->");
    var arrRating = arrResponse[1].split("<!-- subsegment -->");
    var arrID = arrResponse[2].split("<!-- subsegment -->");

    for(var i=1;i<=arrResponse[0]; i++){
    $('#rater_'+i).rater('news/rating.php?update='+arrID[i], {
          style: 'basic', 
          curvalue: arrRating[i]
      });
    }
  }
  active_request = false;
}
///////////////////////////////////////////
///////////////////////////////////////////

function formBooking() {


    ajaxInit();
    http_request.onreadystatechange = showBooking;
    http_request.open('GET', 'inc/Booking.inc.php', true);
    http_request.send(null);
    $("body").append("<div id='overlay'></div><div id='overlayContent'><div id='sub'><div id='load'>Inhalte werden geladen...</div><div id='ajax_load'><img src='gfx/ajax_loading.gif' style='position: relative; margin: auto auto;'></div></div></div>");
    if(tb_detectMacXFF()){
			     $("#overlay").addClass("overlayMacHack");//use png overlay so hide flash
		    }else{
			     $("#overlay").addClass("overlayBG");//use background and opacity
		    }

}

function formBookingClose() {
    $("#sub").slideUp('slow', function () {
      $("#overlay").remove();
      $("#overlayContent").remove();
    });
}

function showBooking() {
    if (http_request.readyState == 4 && http_request.status == 200) {
      $("#sub").slideUp('slow', function () {
        $("#sub").html(http_request.responseText);    
        $("#sub").slideDown('slow', function() {
          format();
        });
      });
      
    }

}

function dateDetails(dateID) {
  if($("#detail_" + dateID).css("display") == "none") {
      $("#detail_" + dateID).css("display", "block");
      $("#detail_" + dateID).css("display", "table-row");
  } else {
      $("#detail_" + dateID).css("display", "none");
  }
}

function format() {
 
  $.addthis('ShowBiss');
 
  $.dpText = {
	TEXT_PREV_YEAR		:	'DE Previous year',
	TEXT_PREV_MONTH		:	'DE Previous month',
	TEXT_NEXT_YEAR		:	'DE Next year',
	TEXT_NEXT_MONTH		:	'DE Next month',
	TEXT_CLOSE			:	'DE Close',
	TEXT_CHOOSE_DATE	:	'DE Choose date'
}

            // bind 'myForm' and provide a simple callback function 
 
      $('#bookingForm').bind('submit', function() {
        if(getFormStatus()){
          $(this).ajaxSubmit(function(request, settings){
            setFormStatus();
            $.historyLoad("Danke");
          });
        }
        return false; 
      });  
  
      $('#kontaktForm').bind('submit', function() {
        if(getFormStatus()){
          $(this).ajaxSubmit(function(request, settings){
            setFormStatus();
            $.historyLoad("Danke");
          });
        }
        return false; 
      }); 
  
  Date.format = 'dd.mm.yyyy';

	
  //$.NiceJForms.build();
  $(".datePick").datePicker({clickInput:true});
  $(".datePick").focus();
  $(".datePick").blur();
  $("#time").timePicker();
  
  

  
// remove the registerOverlay call to disable the controlbar
	hs.registerOverlay(
    	{
    		thumbnailId: null,
    		overlayId: 'controlbar',
    		position: 'top right',
    		hideOnMouseOut: true
		}
	);
}
///////////////////////////////////////////


function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

