function callPopup(id, url, params) {
  if (!id) return;  
  var p = [];
  var target = 'x' + new Date().getTime();
  var ps = '';
  if (id==2){
    target="snackpopA";
    ps='width=966,height=610';
  }
  else if (id==3){
    target="snackpopB";
    ps='width=1010,height=720';
  }
  if (params) {
    var t = params.split(',');
    for(var i = 0; i < t.length; i++) {
      var e = t[i].split('=');
      if (e.length == 2) {
        if (e[0] == 'target') {
          target = e[1];
        }
        else {
          p[p.length] = e;
        }
      }
    }
    for(var i = 0; i < p.length; i++) {
      var e = p[i];
      if (i > 0) ps += ',';
      ps += e[0] + '=' + e[1];
    }
  }
  if (!url) return;  
  var w = window.open(url, target, ps);
  w.focus();
}

window.naviOpen = false;
$(document).ready(initNavi);
function initNavi() {
  var e = $('#navi');
  if (!e) return; // no navi found
  var esItm = e.find('.naviL1');
  var esItmKlapp = e.find('.naviL1Klapp');
  esItm.each(function(idx) {
    var e = $(this);
    e.bind('click', function() {
      var eKlapp = $("#" + this.id + 'Klapp');
      if (eKlapp.hasClass('hid')) {
        esItm.each(function(idx) {
          $("#" + this.id + 'Klapp').addClass('hid');
        });
        eKlapp.removeClass('hid');
        setStatusKlapp(eKlapp);
        esItm.each(function(idx) {
          $(this).removeClass('sel');
        });
        $(this).addClass('sel');
        window.naviOpen = true;
      }
      else {
        eKlapp.addClass('hid');
        $(this).removeClass('sel');
        window.naviOpen = false;
      }
    });
  });
  // klapp up/down
  esItmKlapp.each(function(idx) {
    var eKlapp = $(this);
    setStatusKlapp(eKlapp);
    var eCP = eKlapp.find('.prev');
    var eCN = eKlapp.find('.next');
    var eO = eKlapp.find('.outer');
    var eI = eKlapp.find('.inner');
    eCP.bind('click', function() {
      if (!$(this).hasClass('act')) return;
      $(this).removeClass('act');
      var top = Math.min(0, eI.position().top + (eO.height() - 50));
      eI.animate({top:top}, {duration:500, complete:function(){setStatusKlapp(eKlapp);}});
    });
    eCN.bind('click', function() {
      if (!$(this).hasClass('act')) return;
      $(this).removeClass('act');
      var top = Math.max(-eI.outerHeight() + eO.height(), eI.position().top - (eO.height() - 50));
      eI.animate({top:top}, {duration:500, complete:function(){setStatusKlapp(eKlapp);}});
    });
  });
}
function setStatusKlapp(eKlapp) {
  var eO = eKlapp.find('.outer');
  var eI = eKlapp.find('.inner');
  var eCP = eKlapp.find('.prev');
  var eCN = eKlapp.find('.next');
  var eITop = eI.position().top;
  var eIHeight = eI.outerHeight();
  var eOHeight = eO.height();
  if (eITop < 0) eCP.addClass('act'); else eCP.removeClass('act');
  if (eIHeight + eITop > eOHeight) eCN.addClass('act'); else eCN.removeClass('act');
}
// search
$(document).ready(initSearch);
function initSearch() {
  var BUT_WIDTH = 48;
  var MIN_WIDTH = 132;
  var MAX_WIDTH = 178;
  var e = $('#frmSearch');
  var eTxt = e.find('.txt');
  e.unbind();
  eTxt.unbind();
  if (e.width() < MIN_WIDTH) {
    window.searchWidth = e.width();
    e.bind('mouseenter', function() {
      if (!window.searchOpened) {
        window.searchOpened = true;
        e.animate({width:MAX_WIDTH}, {duration:150});
        eTxt.animate({width:MAX_WIDTH - BUT_WIDTH}, {duration:150});
      }
    });
    e.bind('mouseleave', function() {
      if (window.searchOpened && eTxt.css('height') != '24px') { // test for focus
        e.animate({width:window.searchWidth}, {duration:150});
        eTxt.animate({width:window.searchWidth - BUT_WIDTH}, {duration:150, complete:resetSearchBar});
      }
    });
    eTxt.bind('focusout', function() {
      if (window.searchOpened) {
        e.animate({width:window.searchWidth}, {duration:150});
        eTxt.animate({width:window.searchWidth - BUT_WIDTH}, {duration:150, complete:resetSearchBar});
      }
    });
  }
}
function resetSearchBar() {
  window.searchOpened = false;
  var e = $('#frmSearch');
  // remove style attributes
  e.attr('style', '');
  e.find('.txt').attr('style', '');
}
function doSearch(frm) {
  var v = frm.itwsearch.value;
  if (v.length == 0) return false;
  if (v.length < 2 || v == 'Suche') {
    try { frm.search.style.color='#ff0000'; } catch (ex) {}
    return false;
  }
  try { frm.search.style.color='#000000'; } catch (ex) {}
  
  var url = frm.action;
  var pos = url.indexOf('?');
  if (pos != -1) {
    var eF = $(frm);
    var params = url.substring(pos + 1).split('&');
    for (var i = 0; i < params.length; i++) {
      var nv = params[i].split('=');
      var n = nv[0];
      var v = decodeURIComponent(nv[1]);
      if (n.length <= 3 || (n.length > 3 && n.substring(0,3) != 'itw') || n == 'itwbox') {
        var eH = $('<input type="hidden" name="'+n+'" value="'+v+'" />');
        eF.append(eH);
      }
    }
    frm.action = url.substring(0, pos);
  }
  return true;
}

function ji(x, y) {
  var sub='/'+new Date().getTime();
  if(sub=='/')return;
  if(!window.jis)window.jis=[];
  var i=window.jis.length;
  window.jis[i]=new Image();
  
  if (y) {
    window.jis[i].src=y + '/j'+'i/'+x+sub+'/ji.gif';
  }
  else {
    window.jis[i].src='/j'+'i/'+x+sub+'/ji.gif';
  }
}

/* ensure a dom element is visible (vertical) */
function ensureVisible(eVis,eOut,eInn) {
  eInn = eInn || $(document);
  eOut = eOut || $(window);
  var tInn = eInn.scrollTop();
  var hOut = eOut.height();
  var tVis = eVis.offset().top;
  var hVis = eVis.outerHeight();
  if (tVis <= tInn) eInn.scrollTop(tVis - 20); // scroll up
  else if (tVis + hVis >= tInn + hOut) eInn.scrollTop(tVis + hVis - hOut + 20); // scroll down
}

$(document).ready(initConVidMore);
function initConVidMore() {
  var conVidMore = $(".conVidMore");
  var eItms=conVidMore.find(".itm").each(function(i) {
    $(this).hover(function() {
      $(this).find(".itmKlappText").animate({ height: '100%', opacity: 1 }, { duration:500 });
    },
    function() {
      $(this).find(".itmKlappText").animate({ height: '35px', opacity: 0 }, { duration:500 });
    });
  });
}

// lightbox tools
$(document).ready(function(){
  var ePage = $('#pageOuter');
  if (ePage && ePage.length > 0) { // we are boxpage
    
    // the url tool
    var eTool = $('#barTool');
    var eUrl = eTool.find('.toolUrl');
    if (eUrl && eUrl.length > 0) {
      eUrl.html(location.href);
    }
    $(document).keydown(function(event){
      var target = event.target;
      var tag = target.tagName;
      if (tag == 'INPUT' || tag == 'TEXTAREA') {
        return;
      }
      var keyCode = event.which;
      if (keyCode == '27') {
        event.preventDefault();
        unbox();
      }
    });
    loadbox(eTool.outerHeight() + ePage.outerHeight());
  }
});

//open lightbox with url
function box(url) {
  sendMessage(encodeURIComponent('box' + url));
  return false; // because of onclick
}
//close lightbox
function unbox() {
  sendMessage('boxun');
}
function loadbox(h) {
  sendMessage('boxload'+h);
}
function sendMessage(msg) {
  if (typeof parent.postMessage == 'function') {
    parent.postMessage(msg, '*');
  } 
  else {
    if (window==parent) {
      location.href = location.href + "#" + msg;
    }
    else {
      if (!window.name || window.name.indexOf('http') < 0) window.name = document.referrer;
      var oururl = window.name;
      var newlocation=oururl.replace(/\#.*$/,'') + '#' + msg;
      parent.location.href = newlocation;
    }
  }
}

function sendGAEvent(eventType, eventValue) {
  sendGAEvent(eventType, eventValue, '');
}

function sendGAEvent(eventType, eventValue, eventSubValue) {
  try {
    _gaq.push(['_setCustomVar', 1, 'Publisher', gaPublisher ]);
    _gaq.push(['_setCustomVar', 2, 'SeitenTyp', gaPageType ]);
    _gaq.push(['_setCustomVar', 3, 'Lizenzgeber', gaLicenser ]);
    _gaq.push(['_setCustomVar', 4, 'Videoid', gaVidId ]);
    _gaq.push(['_setCustomVar', 5, 'Videoname', gaVidName  ]);
    _gaq.push(['_anonymizeIp']);
    if (eventSubValue.length > 0) {
      _gaq.push(['_trackEvent', eventType, eventValue ]);
    }
    else {
      _gaq.push(['_trackEvent', eventType, eventValue, eventSubValue ]);
    }
    
    console.log("Publisher: " + gaPublisher + 
        "; SeitenTyp: " + gaPageType + 
        "; Lizenzgeber: " + gaLicenser + 
        "; Videoid: " + gaVidId + 
        "; Videoname: " + gaVidName + 
        "; eventType: " + eventType + 
        "; eventValue:  " + eventValue + 
        "; eventSubValue: " + eventSubValue);
    
  } catch(e){};
}

function sendGAVrectEvent(eventType, eventValue, vidId, vidTitle) {
  try {
    _gaq.push(['_setCustomVar', 1, 'Publisher', gaPublisher ]);
    _gaq.push(['_setCustomVar', 2, 'VRecId', gaVRectId ]);
    _gaq.push(['_setCustomVar', 3, 'Type', gaVRectType ]);
    _gaq.push(['_setCustomVar', 4, 'Videoid', vidId ]);
    _gaq.push(['_setCustomVar', 5, 'Videoname', vidTitle ]);
    _gaq.push(['_anonymizeIp']);
    _gaq.push(['_trackEvent', eventType, eventValue ]);
    
    console.log("Publisher: " + gaPublisher + 
        "; VRecId: " + gaVRectId + 
        "; Type: " + gaVRectType + 
        "; Videoid: " + vidId + 
        "; Videoname: " + vidTitle + 
        "; eventType: " + eventType + 
        "; eventValue: " + eventValue);
    
  } catch(e){ console.log(e); };
}
  
function sendGAVrectEvent(eventType, eventValue, vidId, vidTitle) {
  try {
    var mytype = 'ondemand';
    if (gaAutoPlay == 0) {
      mytype = 'autoplay';
    }

    _gaq.push(['_setCustomVar', 1, 'Publisher', gaPublisher ]);
    _gaq.push(['_setCustomVar', 2, 'VRecId', gaVRectId ]);
    _gaq.push(['_setCustomVar', 3, 'Type', mytype ]);
    _gaq.push(['_anonymizeIp']);
    _gaq.push(['_trackEvent', eventType, eventValue ]);
    
    console.log("Publisher: " + gaPublisher + 
        "; VRecId: " + gaVRectId + 
        "; Type: " + mytype + 
        "; eventType: " + eventType + 
        "; eventValue: " + eventValue);
    
  } catch(e){ console.log(e); };
}

if ("undefined" == typeof(console)) {
  console = {
    log:function(a) {}
  };
}

