/* Default JS */
var mediaRotator = new Array();
var ocTimeout;
$(document).ready(function() {
    $("div.ae-compact").each(function(i) {
        var myHeight = 0;
        $("div.ae-body", this).each(function(i) {
            if($(this).height() > myHeight) {
                myHeight = $(this).height();
            }
        });
        $("div.ae-body", this).height(myHeight);
    });
    
    $("div.ae-big").each(function(i) {
        var myHeight = $(this).height();
        myHeight -= $("div.gb-head", this).height();
        myHeight -= $("div.gb-foot", this).height();
        if($("div.ae-big-news-col", this).height() < myHeight) {
            $("div.ae-big-news-col div.ae-body", this).height(myHeight);
        }
        myHeight -= $("div.ae-full", this).height();
        myHeight -= parseInt($("div.ae-full", this).css("margin-bottom"));
        $("div.ae-big-col div.ae-body", this).height(myHeight);
    });

    $("div.par_column").each(function(i) {
        var myHeight = $(this).height();
        myHeight -= $("div.gb-head", this).height();
        myHeight -= $("div.gb-foot", this).height();
        $("div.pc_body", this).height(myHeight);
    });

    $("div.media_rotator").each(function(i) {
        if($("div.rotator_item", this).size() > 1) {
            var myId = "mr_"+i;
            $(this).attr("id", myId).hover(function() {
                window.clearInterval(mediaRotator[$(this).attr("id")]);
            }, function() { 
                mediaRotator[$(this).attr("id")] = window.setInterval("mediaRotatorNext('"+$(this).attr("id")+"')", 11000);
            });
            mediaRotator[myId] = window.setInterval("mediaRotatorNext('"+$(this).attr("id")+"')", 11000);
        }
    });

    $("div.par-hnf").each(function(i) {
        var myHeight = $(this).height();
        if($("div.hnf-news-box").height() < myHeight) {
            var myNewsBox = $("div.hnf-news-box", this).get(0);
            myHeight -= $("div.gb-head", myNewsBox).height();
            myHeight -= $("div.gb-foot", myNewsBox).height();
            $("div.gb-main", myNewsBox).height(myHeight);
        }
        else {
            myHeight -= $("div.hnf-media-box", this).height();
            myHeight -= parseInt($("div.hnf-media-box", this).css("margin-bottom"));
            var myTextBox = $("div.hnf-text-box", this).get(0);
            myHeight -= $("div.gb-head", myTextBox).height();
            myHeight -= $("div.gb-foot", myTextBox).height();
            $("div.gb-main", myTextBox).height(myHeight);
        }
        
        var myItems = $("div.hnf-media-content", this).get(0);
        myItems.curItem = 0
        
        $("div.hnf-icon-left", this).click(function() {
            moveHnfItems(myItems, -1);
        });
        
        $("div.hnf-icon-right", this).click(function() {
            moveHnfItems(myItems, 1);
        });
    });    
    
    $("div.oc-item", "div.oc-one,div.oc-three").hover(function() {
        if($("div#oc-overlay").size() == 0) {
            $("body").append('<div id="oc-overlay"></div>');
            $("div#oc-overlay").hover(function() {
                window.clearTimeout(ocTimeout);
            }, function() {
                ocTimeout = window.setTimeout('$("div#oc-overlay").hide()', 300);
            });
        }
        window.clearTimeout(ocTimeout);
        var myOffset = $(this).offset();

        var myPosTop = parseInt($("div.oc-content", this).css("top"));
        var myPosLeft = parseInt($("div.oc-content", this).css("left"));
        
        window.defaultStatus = myPosTop+":"+myPosLeft;
        
        var myTop = myOffset.top + myPosTop;
        var myLeft = myOffset.left + myPosLeft;
        
        $("div#oc-overlay").css({"top":myTop, "left":myLeft}).html($("div.oc-content", this).html()).show();
    }, function() { 
        ocTimeout = window.setTimeout('$("div#oc-overlay").hide()', 300);
    });
    
    $("div.sub-click").click(function() {
        var clickId = "div#"+$(this).attr("id");
        var subId = clickId+"-content";
        $("div.sub-click").not(clickId).removeClass("hi");
        $("div.sub-content").not(subId).hide();
        if($(subId).is(":visible")) {
            $(subId).hide();
            $(this).removeClass("hi");
        }
        else {
            $(subId).show();
            $(this).addClass("hi");
        }
    });
    
    $("a", "div.sub-click").focus(function() { this.blur() });
    $("input#isearch").focus(function() { 
        $(this).parents("div#h-search").addClass("hi");
    }).blur(function() {
        if(this.value == "") {
            $(this).parents("div#h-search").removeClass("hi");
        }
    });
});

function mediaRotatorNext(id) {
    var obj = "div#"+id;
    $("div.rotator_item:visible", obj).fadeOut(function() {
        if(!$(this).next("div.rotator_item").length) {
            $("div.rotator_item:first", obj).fadeIn();
        }
        else {
            $(this).next().fadeIn();
        }
    });
}

/* Readspeaker */

/* The selected text function */

var selectedString="";

function getSelectedHTML() {
  selectedString="";
  var rng=undefined;
  if (window.getSelection) {
    selobj = window.getSelection();
    if (!selobj.isCollapsed) {
      if (selobj.getRangeAt) {
        rng=selobj.getRangeAt(0);
      }
      else {
        rng = document.createRange();
        rng.setStart(selobj.anchorNode,selobj.anchorOffset);
        rng.setEnd(selobj.focusNode,selobj.focusOffset);
      }
      if (rng) {
        DOM = rng.cloneContents();
        object = document.createElement('div');
        object.appendChild(DOM.cloneNode(true));
        selectedString=object.innerHTML;
      }
      else {
        selectedString=selobj;
      }
    }
  }
  else if (document.selection) {
    selobj = document.selection;
    rng = selobj.createRange();
    if (rng && rng.htmlText) {
      selectedString = rng.htmlText;
    }
    else if (rng && rng.text) {
      selectedString = rng.text;
    }
  }
  else if (document.getSelection) {
    selectedString=document.getSelection();
  }
}

function copyselected()
{
  setTimeout("getSelectedHTML()",50);
  return true;
}

function openAndRead() {
  window.open('','rs','width=380,height=180,toolbar=0');
  setTimeout("document.rs_form.submit();",500);
}

document.onmouseup = copyselected;
document.onkeyup = copyselected;


/* The expanding function */

function readspeaker(rs_call, rs_file_name)
{
  if (selectedString.length>0) {
    rs_call=rs_call.replace("/cgi-bin/rsent?","/enterprise/rsent_wrapper.php?");
  }
  savelink=rs_call+"&save=1&audiofilename="+rs_file_name;
  rs_call=rs_call+"&output=audio";
  rs_call=escape(rs_call);
  start_rs_table="<table style='border:1px solid #aeaeae; font-size: 10px;'><tr><td>";
  rs_embed="<object type='application/x-shockwave-flash' data='http://media.readspeaker.com/flash/readspeaker20.swf?mp3="+rs_call+"&autoplay=1&rskin=bump' height='20' width='250'><param name='movie' value='http://media.readspeaker.com/flash/readspeaker20.swf?mp3="+rs_call+"&autoplay=1&rskin=bump' /><param name='quality' value='high' /><param name='SCALE' value='exactfit' /><param name='wmode' value='transparent' /><embed wmode='transparent' src='http://media.readspeaker.com/flash/readspeaker20.swf?mp3="+rs_call+"&autoplay=1&rskin=bump' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwaveflash' scale='exactfit' height='20' width='250' /></embed></object>";
  rs_downloadlink="<br /><a href='"+savelink+"' style=\"margin-left:5px;\"><img src=\"/static/layout/portal/image/icon/mp3.gif\" width=\"18\" height=\"12\" alt=\"\" align=\"texttop\">&nbsp;Download mp3 Datei</a>";
  close_rs="";
  end_rs_table="</td></tr></table>";

  var x=document.getElementById('rs_div');

  x.innerHTML=start_rs_table+rs_embed+rs_downloadlink+close_rs+end_rs_table;
}

function close_rs_div()
{
  var x=document.getElementById('rs_div');
  x.innerHTML="";
}

/* /Readspeaker */

function downloadReadspeaker(url, rs_file_name) {
    var savelink = url+'&selectedhtml='+escape(selectedString)+"&save=1&audiofilename="+rs_file_name;
    window.open(savelink);
}

function toggleReadspeaker(url, rs_file_name) {
    if($("div#rs_div").is(":visible")) {
        $("div#rs_div").hide().html("");
    }
    else {
        readspeaker(url+'&selectedhtml='+escape(selectedString), rs_file_name);                
        $("div#rs_div").show();
    }
}

function toggleInlineReadspeaker(rs_call, rs_file_name) {
    if($("div.rs-inline").is(":visible")) {
        $("div.rs-inline").hide().html("").prev("a.rs-read").show();
    }
    else {
        $("div.rs-inline").html(inlineReadspeaker(rs_call, rs_file_name)).show().prev("a.rs-read").hide();
    }
}

function inlineReadspeaker(rs_call, rs_file_name) {
    if (selectedString.length>0) {
        rs_call=rs_call.replace("/cgi-bin/rsent?","/enterprise/rsent_wrapper.php?");
    }
    rs_call=rs_call+"&output=audio";
    rs_call=escape(rs_call);
    return "<object type='application/x-shockwave-flash' data='http://media.readspeaker.com/flash/readspeaker20.swf?mp3="+rs_call+"&autoplay=1&rskin=bump' height='20' width='250'><param name='movie' value='http://media.readspeaker.com/flash/readspeaker20.swf?mp3="+rs_call+"&autoplay=1&rskin=bump' /><param name='quality' value='high' /><param name='SCALE' value='exactfit' /><param name='wmode' value='transparent' /><embed wmode='transparent' src='http://media.readspeaker.com/flash/readspeaker20.swf?mp3="+rs_call+"&autoplay=1&rskin=bump' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwaveflash' scale='exactfit' height='20' width='250' /></embed></object>";
}

/* HNF Items */
function moveHnfItems(myItems, direction) {
    var countItems = $("div.hnf-media", myItems).size() - 3;
    var curItem = myItems.curItem;
    curItem += direction;
    if(((curItem) >= 0) && (curItem <= countItems) ) {
        var myItemWidth = $("div.hnf-media", myItems).width();
        var mySlider = $("div.hnf-media-slide", myItems).get(0);
        var newPos = -1 - (myItemWidth * curItem);
        myItems.curItem = curItem;
        $(mySlider).animate({ "left":newPos }, function() {
            curHnfItem = curItem;
            var myButtons = $(myItems).parents("div.gb-main").get(0);
            if(curHnfItem == 0) {
                $("div.hnf-icon-left", myButtons).fadeOut(); 
            }
            else {
                $("div.hnf-icon-left", myButtons).fadeIn(); 
            }
            if(curHnfItem == countItems) {
                $("div.hnf-icon-right", myButtons).fadeOut(); 
            }
            else {
                $("div.hnf-icon-right", myButtons).fadeIn(); 
            }
        });
    }
}
