

/*
 *    Common Javascripts for Vienna Blues Spring Site
 *    mostly based on the great Prototype JavaScript framework [ http://prototype.conio.net ]
 *    and the script.aculo.js effects [ http://script.aculo.us ]
 *    Initial Version  -- 25.2.2006
 *    Copyright: Alfred "Alf mit i" Scheibl
 *    http://www.alfmiti.net
 */

//-- for IE6 but not IE7, Moz, Safari, Opera...
try {
    if (typeof document.body.style.maxHeight == "undefined") {
        document.execCommand('BackgroundImageCache', false, true);
    }
} catch(e) { /* unless it's not IE6... */ }

var ua = navigator.userAgent.toLowerCase();
var isGecko     = (ua.indexOf("gecko") != -1) ;
var isIE        = (ua.indexOf("msie") != -1) && typeof(document.all) == 'object' && (ua.indexOf("opera") == -1);
var isOpera     = (ua.indexOf("opera") != -1) && !isIE;

var submenue_is_open = false;
var vbs_popup_is_open = false;
var vbs_detect_pos = false;

var saved_pref = getCookie('vbs_effects');
var useEffects = saved_pref == 'false' ? false : true;

// for now deaktivate effects for opera -> code a ajax workaround
if (isOpera)  useEffects = false;

/* switch prefs -> set global var and cookie and
   change link background  - simply not ? */
function setMyEffects() {
  var newValue  = useEffects ? false : true;
  var style = 'cbox_checked';

  if (isOpera && newValue) {
      alert('Derzeit koennen im Opera Browser die Effekte nicht verwendet werden !');
      newValue = false;
  }
  if (!newValue) {
     setCookie('vbs_effects','false',60);
     style = 'cbox_unchecked';
  } else {
     delCookie('vbs_effects');
  }
  setPrefBackground(style);
  useEffects = newValue;
}

function setPrefBackground(style) {
  $('pref_effects').style.background = ' url(/vbs/img07/' + style + '.gif) 0 2px no-repeat';
}

/* hold those main elements in global space */
var subContent=null;
var subWrapper=null;
var opacDiv=null;
var loading=null;

function init() {
  loading = $('loading');
  subContent = $('submenue');
  subWrapper = $('wrapper_submenue');
  opacDiv = $('opac_container');
  $('pref').style.visibility = 'visible';
  if (useEffects) setPrefBackground('cbox_checked');
}


function show_sub(page_id) {

  if (vbs_popup_is_open) return false;
  if (!useEffects) {
     self.location.href = 'detail.php?content=' + page_id;
     return true;
  }

  if (!vbs_detect_pos) setLeftPositions();
  setTopPositions();
  vbs_popup_is_open = true;

  if (isIE) subWrapper.style.display = 'block'; /* problem in IE is sometimes not displayed */
  new Ajax.Updater(subContent, 'detail_' + page_id + '.html?v=0.45',{method:'get', onComplete: show_subContent } );

  opacDiv.style.display = 'block';
  Element.setOpacity(opacDiv,0.1);
  new Effect.Appear(opacDiv,{ from:0.1, to: 0.4, duration: 0.5, queue:'front' });
}


function show_subContent() {

  window.setTimeout('hideLoadingImage()',1700);   
  Element.setOpacity(subWrapper,0.4); 
  subWrapper.style.display='none';
  Effect.Parallel( [ new Effect.BlindDown(subWrapper, { duration: 0.7 } ),
                     new Effect.Opacity(subWrapper, {from:0.5, to: 1, duration: 0.8, queue:'end'})],
                     {duration: 0.8 } 
                  );                 
}


function hide_submenue() {
  if (isIE)
     new Effect.DropOut(subWrapper, { duration: 0.8,queue: 'end'}); /* looks not so nice in foxie */
  else
     new Effect.Fold(subWrapper, { duration: 0.5, queue: 'end' });

  new Effect.Fade(opacDiv,{from:0.5, to: 0.0, duration: 0.5});
  opacDiv.style.display = loading.style.display = 'none'; 
  vbs_popup_is_open = false;
}

var curX = null;
var curY = null;
function hideLoadingImage() {
  setTopPositions();  /* check once again - maybe user scrolled down  */
  loading.style.display = 'none';
  /* fallback sometimes is the height lost */
  if (subWrapper.style.height != '470px') subWrapper.style.height = '470px';  
}


/* set left positions only once or on resizing page */
var curLeftPos = null;
function setLeftPositions() {
  var pageHeight = document.body.clientHeight;
  curLeftPos = Position.cumulativeOffset($('leftbox'))[0];
  opacDiv.style.height = ( pageHeight - 90) + 'px';
  opacDiv.style.left = curLeftPos + 'px';
  subWrapper.style.left = (curLeftPos + 20) + 'px';
  loading.style.left = (curLeftPos + 240) + 'px';
  vbs_detect_pos = true;
}

var curTopPos = null;
function setTopPositions() {
  Position.prepare();
  curTopPos = Position.deltaY;
  var topPos = curTopPos + 50;
  subWrapper.style.top = topPos + 'px';
  loading.style.top  = (topPos-3) + 'px';
  loading.style.display = 'block';
}

/* add mouseevents and effects for programm overview - so the page
 * is full accessable without javascript */
function addMouseEvents() {
  var a = null;
  var para = null;
  var links = $('programm').getElementsByTagName('a');

  for (var i=0; i< links.length; i++)  {
      a = links[i];
      if (a.className == 'tt') {
          a.onmouseover = showToolTipp;
          a.onmouseout = hideToolTippDelayed; 
          a.id = a.name;          
      } else if (a.id != 'pref_effects') {         
          para = a.href.split('=');
          if (para.length == 2)
             a.href = "javascript:show_sub('" + para[1] + "');";
      }
  } 
  /* add nice scrollTo Effects to the Calendar */
  var links = $('calender_navi').getElementsByTagName('a');
  for (var i=0; i< links.length; i++)  {
       a = links[i].href.split('#');    
       links[i].href = "javascript:gotoDate('" + a[1] + "');";
  }      
}

/* scrollTo functionpair - execute scrolling and highlight h4 element
 * FIX ME !!! Problem in MSIE -> return value for Y Position is twice as much
 * currently direct in effects.js hacked */ 
var curHighlight=null;
function gotoDate(id) {   
   new Effect.ScrollTo(id,{offset:-20, afterFinish:endGotoDate});   
   curHighlight = $(id).parentNode;   
}
function endGotoDate() {
   new Effect.Highlight(curHighlight, {endcolor:'#F2F3EE',duration:4.0});
}

/* ToolTipp functions - not in the prototype.js style -> FIX ME! */
var active_tt = null;
function showToolTipp(event) {
  if (active_tt != null) return;
  var id='';
  var txt = this.firstChild.data.trim();
  switch (txt.charAt(0)) {
    case 'R': id='tt_reigen';break;
    case 'F': id='tt_fernwaerme';break;
    case 'H': id='tt_hdm';break;
  }
  if (id=='') return;
  active_tt = $(id);
  if (isIE) event = window.event;  
  active_tt.style.left = (Event.pointerX(event) - 10)  + "px";
  active_tt.style.top  = (Event.pointerY(event) + 5) + "px";
  active_tt.style.display = "block"; 
}
function hideToolTippDelayed() {
  setTimeout('hideToolTipp()',100);
}

function hideToolTipp() {
  if (active_tt == null) return;
  active_tt.style.display = "none";
  active_tt = null;
}

String.prototype.trim = function() {
    return this.replace(/(^\s+|\s+$)/g, '');
};

// Cookie-Funktionen
function setCookie(name, value, expdays) {   // valid expdays days
  var now = new Date();
  var exp = new Date(now.getTime() + (1000*60*60*24*expdays));
  document.cookie = name + "=" + escape(value) + ";" +
                    "expires=" + exp.toGMTString() + ";" +
                    "path=/vbs/";
}

function delCookie(name) {
  var now = new Date();
  var exp = new Date(now.getTime() - 1);
  document.cookie = name + "=;" +
                    "expires=" + exp.toGMTString() + ";" +
                    "path=/vbs/";
}

function getCookie(name) {
  var cname = name + "=";
  var dc = document.cookie;
  if (dc.length > 0) {
     var start = dc.indexOf(cname);
     if (start != -1) {
        start += cname.length;
        var stop = dc.indexOf(";", start);
        if (stop == -1) stop = dc.length;
        return unescape(dc.substring(start,stop));
     }
  }
  return null;
}


