function CropfmHeader(the_frame, the_element, instance_name, active_but, hasNews )
{
  /****************************************************************************
   *                        -- JS Class CropfmHeader --                       *
   *                                                                          *
   * Instance with "instance_name" must be definde in top frame.              *
   * JSP to get header data (getheaderbuttonstatus.jsp) must be available.    *
   * Page must contain an (i)frame with id "header_channel".                  *
   *																		  *
   ****************************************************************************/
  
  // *** public methods ***
  this.paint = paint;
  this.activateCropcom = activateCropcom;
  this.deactivateCropcom = deactivateCropcom;
  this.turnOnCropcom = turnOnCropcom;
  this.turnOffCropcom = turnOffCropcom;
  this.activateListenlive = activateListenlive;
  this.deactivateListenlive = deactivateListenlive;
  this.alertListenlive = alertListenlive;
  this.dealertListenlive = dealertListenlive;
  this.setCallback = setCallback;
  this.startChecking = startChecking;
  this.setHelpURL = setHelpURL;
  
  // public members that hold status for CropfmHeader buttons 
  this.cropcom_active = false;
  this.cropcom_on = true;
  this.listenlive_active = false;
  this.listenlive_alerted = false;
  
  
  // news methods
  this.setNewsPos = setNewsPos;
  this.setNewsText = setNewsText;
  this.startShifting = startShifting;
  this.startZooming = startZooming;
  this.pauseZooming = pauseZooming;
  this.continueZooming = continueZooming;
  this.gotoCurrentNews = gotoCurrentNews;
  
  this.stopAction = stopAction;
  this._zoomText = _zoomText;
  this._shiftText = _shiftText;
  

  // *** private methods ***
  this._printOutput = _printOutput;
  this._contWriteln = _contWriteln;
  this._contWrite = _contWrite;
  this._getHeaderData = _getHeaderData;
  
  // *** private variable ***
  if ( top.help_url )
    var the_help_url = top.help_url;    
  var callback = null;
  var the_html = new Array("");
  var target_frame = ( the_frame ? top.frames[the_frame] : top );
  var target_elem = ( the_frame ? top.frames[the_frame].document.getElementById(the_element) : top.document.getElementById(the_element) );
  var com_frame = ( the_frame ? top.frames[the_frame].frames['header_channel'] : top.frames['header_channel'] );
 
  // news ticker 
  var zoom_speed = 30;
  var shift_speed = 50;
  var spacing=250;
  var ypos=0;
  var news_text_array = null;
  var current_news = -1;
  var news_ref;
  var zoom_ref;
  var content;
  var is_zooming = false;
  var is_shifting = false;
   	
  function paint() {
    _contWriteln( [ "<table width='100%' border='0' cellspacing='0' cellpadding='0'  style='background-image:url(", icon_location, "header_tesla.gif); background-repeat:no-repeat'>" ] );
    _contWriteln( "<tr>" ); 
    _contWriteln( [ "<td><a title='Zur Startseite...' href='", top.cropfm_home_url, "' target='_top'><img src='", icon_location, "header_cropfm_logo.gif' width='160' height='160' border='0' ", 
                    "onmouseover='this.src=\"", icon_location, "header_cropfm_logo_over.gif\"' onmouseout='this.src=\"", icon_location, "header_cropfm_logo.gif\"'></a></td>" ] );
    if ( active_but == 0 ) {
      _contWriteln( [ "<td align='center' valign='top'><img id='header_button0' src='", icon_location, "header_button_listenlive_active.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'></td>" ] );
    } else {
      _contWriteln( [ "<td align='center' valign='top'>",
                      "<img id='header_button0' src='", icon_location, "header_button_listenlive.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'><br>",
                      "<a href='", top.listenlive_url, "' target='_top'><img id='header_over0' src='", icon_location, "empty.gif' border='0' style='position:relative; top:-24px; z-index:0' width='120' height='95' ",
                      "onmouseover='if ( !top." + instance_name + ".listenlive_active && !top." + instance_name + ".listenlive_alerted ) top.header_button_0.startAni(\"header_button_listenlive.gif\", \"header_button_listenlive_over.gif\")' ",
                      "onmouseout='if ( !top." + instance_name + ".listenlive_active && !top." + instance_name + ".listenlive_alerted ) top.header_button_0.buttonMouseOut(\"header_button_listenlive.gif\")'></a></td>" ] );
    }
    if ( active_but == 1 ) {
      _contWriteln( [ "<td align='center' valign='top'><img id='header_button1' src='", icon_location, "header_button_pastshows_active.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'></td>" ] );
    } else {
      _contWriteln( [ "<td align='center' valign='top'>",
                      "<img id='header_button1' src='", icon_location, "header_button_pastshows.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'><br>",
                      "<a href='", top.past_shows_url, "' target='_top'><img id='header_over1' src='", icon_location, "empty.gif' border='0' style='position:relative; top:-24px; z-index:0' width='120' height='95' ",
                      "onmouseover='top.header_button_1.startAni(\"header_button_pastshows.gif\", \"header_button_pastshows_over.gif\")' ",
                      "onmouseout='top.header_button_1.buttonMouseOut(\"header_button_pastshows.gif\")'></a></td>" ] );
    }
    if ( active_but == 2 ) {
      _contWriteln( [ "<td align='center' valign='top'><img id='header_button2' src='", icon_location, "header_button_podcast_active.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'></td>" ] );
    } else {
      _contWriteln( [ "<td align='center' valign='top'>",
                      "<img id='header_button2' src='", icon_location, "header_button_podcast.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'><br>",
                      "<a href='", top.podcast_url, "' target='_top'><img id='header_over2' src='", icon_location, "empty.gif' border='0' style='position:relative; top:-24px; z-index:0' width='120' height='95' ",
                      "onmouseover='top.header_button_2.startAni(\"header_button_podcast.gif\", \"header_button_podcast_over.gif\")' ",
                      "onmouseout='top.header_button_2.buttonMouseOut(\"header_button_podcast.gif\")'></a></td>" ] );
    }
    if ( active_but == 3 ) {
      _contWriteln( [ "<td align='center' valign='top'><img id='header_button3' src='", icon_location, "header_button_clips_active.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'></td>" ] );
    } else {
      _contWriteln( [ "<td align='center' valign='top'>",
                      "<img id='header_button3' src='", icon_location, "header_button_clips.gif' width='120' height='57' style='position:relative; top:52px; z-index:0'><br>",
                      "<a href='", top.clips_url, "' target='_top'><img id='header_over3' src='", icon_location, "empty.gif' border='0' style='position:relative; top:-24px; z-index:0' width='120' height='95' ",
                      "onmouseover='top.header_button_3.startAni(\"header_button_clips.gif\", \"header_button_clips_over.gif\")' ",
                      "onmouseout='top.header_button_3.buttonMouseOut(\"header_button_clips.gif\")'></a></td>" ] );
    }
    if ( active_but == 4 ) {
      _contWriteln( [ "<td align='center' valign='top' id='td4'><img id='header_button4' src='", icon_location, "header_button_cropcom_active.gif' width='120' height='57' style='position:relative; top:52px; z-index:1'></td>" ] );
    } else {
      _contWriteln( [ "<td align='center' valign='top' id='td4' title='CROPcom: Keine Benutzer online...'>",
                      "<img id='header_button4' src='", icon_location, "header_button_cropcom.gif' width='120' height='57' style='position:relative; top:52px; z-index:1'><br>",
                      "<a href='", top.cropcom_url, "' target='_top'><img id='header_over4' src='", icon_location, "empty.gif' border='0' style='position:relative; top:-24px; z-index:1' width='120' height='95' ",
                      "onmouseover='if ( !top." + instance_name + ".cropcom_active ) top.header_button_4.startAni(\"header_button_cropcom.gif\", \"header_button_cropcom_over.gif\")' ",
                      "onmouseout='if ( !top." + instance_name + ".cropcom_active ) top.header_button_4.buttonMouseOut(\"header_button_cropcom.gif\")'></a></td>" ] );
    }
    _contWriteln( [ "<td align='center' valign='top' width='40'><img src='", icon_location, "header_tesla_wrapper.gif' width='40' height='160'></td>" ] );
    _contWriteln( "</tr>" );
    _contWriteln( "</table>" );    
    _contWriteln( "<div style='position:absolute; top: 145px; left:170px'>" );
    _contWriteln( [ "<a href='", top.email_url, "' title='", email_title_ge , "'><img src='", icon_location, "header_icon_email.gif' border='0' width='32' height='32' style='margin:2px' onmouseover='this.src=\"", top.icon_location, "header_icon_email_over.gif\"' onmouseout='this.src=\"", top.icon_location, "header_icon_email.gif\"'></a>",
                    "<a href='", top.newsletter_url, "' title='", newsletter_title_ge , "' target='_blank'><img src='", icon_location, "header_icon_newsletter.gif' border='0' width='32' height='32' style='margin:2px' onmouseover='this.src=\"", top.icon_location, "header_icon_newsletter_over.gif\"' onmouseout='this.src=\"", top.icon_location, "header_icon_newsletter.gif\"'></a>",
                    "<a href='", top.friend_url, "' title='", friend_title_ge , "'><img src='", icon_location, "header_icon_2friend.gif' border='0' width='32' height='32' style='margin:2px' onmouseover='this.src=\"", top.icon_location, "header_icon_2friend_over.gif\"' onmouseout='this.src=\"", top.icon_location, "header_icon_2friend.gif\"'></a>",
                    "<a href='", the_help_url, "' title='", help_title_ge , "' target='cropfm_help'><img src='", icon_location, "header_icon_help.gif' border='0' width='32' height='32' style='margin:2px' onmouseover='this.src=\"", top.icon_location, "header_icon_help_over.gif\"' onmouseout='this.src=\"", top.icon_location, "header_icon_help.gif\"'></a>" ] );
    _contWriteln( "</div>" );
    
    if ( hasNews ) {
      _contWriteln( '<table id="newsTable" border="0" cellpadding="0" cellspacing="0" style="display: inline; cursor: pointer; position:absolute; top: 0px; left: 0px" onmouseover="top.' + instance_name + '.pauseZooming()" onmouseout="top.' + instance_name + '.continueZooming()" onclick="top.' + instance_name + '.gotoCurrentNews()">' );
      _contWriteln( [ '<tr><td rowspan="3"><img src="' , icon_location, 'standard_button_left.gif" height="30" width="10"></td><td style=" height: 6px; background: url(' , icon_location, 'standard_button_top.gif) repeat-x"><img src="' , icon_location, 'empty.gif" height="1" width="250"></td><td rowspan="3"><img src="', icon_location, 'standard_button_right.gif" height="30" width="10"></td></tr><tr>' ].join("") );
      _contWriteln( '<td id="news_td" class="newsTickerContent">' );
      _contWriteln( '<div id="contentWrapper" style="overflow:hidden; width:250px; height:16px; white-space:nowrap">' );
      _contWriteln( '<span id="newsContent" class="news_item" style="white-space:nowrap; position:relative; left:450; top:0px; margin-left: 5px"></span>' );
      _contWriteln( [ '</div></td></tr><tr><td style="height: 6px; background: url(', icon_location, 'standard_button_bottom.gif) repeat-x"></td></tr></table>' ] );
    }
    
    // write html to target
    _printOutput();
    
    content = target_frame.document.getElementById("newsContent");
  }
      
  function startChecking(time_int) {
    //*** must be called AFTER setNewsText or news will beretrieved anyway...
    if ( CropfmHeader_interval == null ) {
      this._getHeaderData();
      CropfmHeader_interval = window.setInterval( "top." + instance_name + "._getHeaderData()", time_int);
    }
  }
   
  function activateCropcom(usersOnline) {
    target_frame.document.getElementById("td4").title = "CROPcom: " + usersOnline + " Benutzer online...";
	if ( this.cropcom_active == false ) {
      this.cropcom_active = true;
      if ( top.isCom == null ) {
        target_frame.document.getElementById("header_button4").src = icon_location + "header_button_cropcom_blink.gif";
      }
  	}
  }
  
  function deactivateCropcom() {
    this.cropcom_active = false;
    target_frame.document.getElementById("header_button4").src = icon_location + "header_button_cropcom.gif";
    target_frame.document.getElementById("td4").title = "CROPcom: Keine Benutzer online...";
  }

  function turnOffCropcom() {
	this.cropcom_on = false;
    target_frame.document.getElementById("td4").innerHTML = [ "<img id='header_button4' src='", icon_location, "header_button_cropcom_active.gif' width='120' height='57' style='position:relative; top:52px; z-index:1; opacity: 0.55; filter: alpha(opacity=55)'>" ].join("");
    target_frame.document.getElementById("td4").title = "CROPcom derzeit inaktiv...";
  }

  function turnOnCropcom() {
    this.cropcom_on = true;
    this.paint();
    cropfmHeader.setNewsPos(320, 147);
  }

  function activateListenlive() {
    this.listenlive_active = true;
    target_frame.document.getElementById("header_button0").src = icon_location + "header_button_onair_blink.gif";
    if ( callback ) 
      callback ( LISTENLIVE_STATE_ACTIVE );
  }
  
  function deactivateListenlive() {
    this.listenlive_active = false;
    target_frame.document.getElementById("header_button0").src = icon_location + "header_button_listenlive.gif";
    if ( callback ) 
      callback ( LISTENLIVE_STATE_IDLE );
  }

  function alertListenlive() {
    this.listenlive_alerted = true;
    target_frame.document.getElementById("header_button0").src = icon_location + "header_button_listenlive_blink_orange.gif";
    if ( callback ) 
      callback ( LISTENLIVE_STATE_ALERTED );
  }

  function dealertListenlive() {
    this.listenlive_alerted = false;
    target_frame.document.getElementById("header_button0").src = icon_location + "header_button_listenlive.gif";
    if ( callback ) 
      callback ( LISTENLIVE_STATE_IDLE );
  }

  function setCallback(aCallback) {
    callback = aCallback;
  }
  
  function setHelpURL(aUrl) {
    if (aUrl) {
      the_help_url = aUrl;
    }
  }
  
  /********************* News Ticker methods *********************/
  
  function setNewsText(aNewsArray) {
    news_text_array = aNewsArray;
  }

  function setNewsPos(x, y) {
    target_frame.document.getElementById("newsTable").style.top = y + "px";
    target_frame.document.getElementById("newsTable").style.left = x + "px";
  }

  function startShifting() {
    is_shifting = true;
    is_zooming = false;
    content.style.letterSpacing = "5px";
    content.style.left = "15px";
    content.innerHTML = top.cropfm_news_text;
  
    content.style.top = "18px";
  
    ypos=18;
  
    if (news_ref)
      window.clearInterval(news_ref);
        
    news_ref = window.setInterval ("top." + instance_name + "._shiftText()", shift_speed);
  }

  function startZooming() {
    is_zooming = true;
    is_shifting = false;
    current_news++;

	if ( current_news > news_text_array.length -1 ) {
	  current_news = -1; // i.e. the start, is incremented!
      this.stopAction();
      is_zooming = false;
      this.startShifting();
      return;
    }
    	  
    spacing=250;

    content.style.top = "0px";
    content.style.left = spacing + "px";
    content.style.letterSpacing = spacing + "px";
    content.innerHTML = news_text_array[current_news][0] + "&nbsp;";
      
    if (news_ref)
      window.clearInterval(news_ref);
        
    news_ref = window.setInterval ("top." + instance_name + "._zoomText()", zoom_speed);
  }
  
  function stopAction() {
    if (news_ref) {
      window.clearInterval(news_ref);
      news_ref = null;
    }
  }
  
  function pauseZooming() {
  
    content.className = "news_item_active";
    target_frame.document.getElementById("contentWrapper").className = "news_item_active";
     
    if ( zoom_ref )
      window.clearTimeout( zoom_ref );
           
    if ( !is_shifting ) {
      target_frame.document.getElementById("contentWrapper").style.overflow = "visible";
      target_frame.document.getElementById("contentWrapper").style.width = "400px";
 	        
      if (news_ref) {
        window.clearInterval(news_ref);
        news_ref = null;
      }  
    }
    
    if ( is_zooming ) {
      content.style.letterSpacing = "0px";;
      content.style.left = "0px";
    } 
  }
  
  function continueZooming() {

    content.className = "news_item";
    target_frame.document.getElementById("contentWrapper").className = "news_item";
    target_frame.document.getElementById("contentWrapper").style.overflow = "hidden";
    target_frame.document.getElementById("contentWrapper").style.width = "250px";

    if ( spacing == 0 && !is_shifting ) // should be in a pause then...
      zoom_ref = window.setTimeout( "top." + instance_name + ".startZooming()", 1000);
    
    if (is_zooming) {
      window.clearInterval(news_ref);
      news_ref = window.setInterval ("top." + instance_name + "._zoomText()", zoom_speed);  
    }
  }
  
  function gotoCurrentNews() {
    if ( is_shifting )
      return;
      
    var url = news_text_array [current_news][1];

    if ( lib_isExtTarget(url) ) {
       window.open (url,"news_window");
    } else {
       top.location.href = url;
    }
  }
     
  function _zoomText() {
    
    spacing -= Math.ceil ( spacing/9 );
    
    if ( spacing < 0 )
      spacing = 0;

    content.style.letterSpacing = [spacing, "px"].join("");
    content.style.left = [spacing, "px"].join("");

    if ( spacing == 0 ) {
      this.stopAction();
      is_zooming = false;
      zoom_ref = window.setTimeout( "top." + instance_name + ".startZooming()", 3000);
    }
  }
  
  function _shiftText() {
    ypos = ypos - 1;
    content.style.top = [ypos, "px"].join("");
    if (ypos <= -20) { 
      this.stopAction();
      if ( news_text_array && 
           news_text_array.length > 0 ) {
        this.startZooming();
      } else {
        this.startShifting();
      }
    }
  }
  
  

  /*** Private Methods, should not be called from outside althuogh possible ***/
      
  function _getHeaderData() {
    if ( com_frame ) {
    
      if (!news_text_array)
        news_flag = "true";
      else 
        news_flag = "false";
      com_frame.location.replace ( top.host_name + "/cropfm/jsp/getheaderbuttonstatus.jsp?cb=" + instance_name + 
                                   "&getnews=" + news_flag + "&ts=" + new Date().getTime() );
    }
  }

  function _printOutput() {
    target_elem.innerHTML = the_html.join("");
    the_html.length = 1;
  }

  function _contWriteln(html) {
    if ( typeof html == "object" )
      html = html.join("");
    if (the_html.push) {
      the_html.push( [html,"\n"].join("") );
    } else {
      the_html[the_html.length] = [html,"\n"].join("");
    }
  }
  
  function _contWrite(html) {
    if ( typeof html == "object" )
      html = html.join("");
    if (the_html.push) {
      the_html.push( html );
    } else {
      the_html[the_html.length] = html;
    }
  }  
}

/***** static variable definitions *****/

// interval for checking button states
var CropfmHeader_interval = null;
// header button animation, must be defined here (scope top), will be instantiated by init
var header_button_0, header_button_1, header_button_2, header_button_3, header_button_4;


function CropfmHeader_init(header_frame) {
  // *** creates instances of the header button helper class for animation ***
  for ( hb=0; hb<5; hb++) {
    eval ( "header_button_" + hb + " = new CropfmHeader_HeaderButton('header_button_" + hb + "', " + hb + ",'" + header_frame + "')" );
  }
}

/***** helper class fo animation *****/

function CropfmHeader_HeaderButton(anID, aButtonNum, aFrame) {
  // *** public methods ***
  this.startAni = startAni;
  this.buttonMouseOut = buttonMouseOut;
  
  // *** private methods
  this._playAniFrame = _playAniFrame;
  
  // *** public variables ***
  this.HB_ID = anID;
  this.button_ref = "header_button" + aButtonNum;
  this.over_ref = "header_over" + aButtonNum;
  
  // *** private variables ***
  var my_ani_counter = 0;
  var my_frame = ( aFrame == 'false' ? top : top.frames[aFrame] );
  var ani_timeout = null;
  var ani_running = false;
  var mouse_over_but = false;
   

  function startAni(buttonImage, buttonOverImage) {
    my_frame.document.getElementById(this.button_ref).src = top.icon_location + buttonOverImage;
    mouse_over_but = true;
    window.clearTimeout(ani_timeout);
    my_ani_counter = 0;
    ani_running = true;
    this._playAniFrame(buttonImage);
  }
  
  function buttonMouseOut(buttonImage) {
    if (!ani_running) {
      my_frame.document.getElementById(this.button_ref).src = top.icon_location + buttonImage;
    }
    mouse_over_but = false;
  }

  function _playAniFrame(buttonImage) {

    if ( my_ani_counter ==  24 ) {
      ani_running = false;
      my_frame.document.getElementById(this.over_ref).src = top.empty_image.src;
      if (!mouse_over_but) {
        my_frame.document.getElementById(this.button_ref).src = top.icon_location + buttonImage;
      }
      return;
    }

    my_frame.document.getElementById(this.over_ref).src = top.header_button_over[my_ani_counter].src;
    my_ani_counter ++;
    ani_timeout = window.setTimeout ( this.HB_ID + "._playAniFrame('" + buttonImage + "')", Math.round ( Math.random()*80 ) );    
  }
}
