
function getObjectHeight(obj) {
  var result = 0;
  if(obj.offsetHeight){
    result = obj.offsetHeight;
  } else if (obj.clip && obj.clip.height){
    result = obj.clip.height;
  } else if (obj.style && obj.style.pixelHeight) {
    result = obj.style.pixelHeight;
  }
  return strToInt(result);
}

function getElementStyle(obj,IEStyleProp,CSSStyleProp) {
  if(obj.currentStyle){
    return obj.currentStyle[IEStyleProp];
  } else if (window.getComputedStyle) {
    return window.getComputedStyle(obj,'').getPropertyValue(CSSStyleProp);
  } else if (document.defaultView.getComputedStyle) {
    return document.defaultView.getComputedStyle(obj,'').getPropertyValue(CSSStyleProp);
  }
  return '';
}

function getPlayerTag(url) {
  var playerWidth = 150;
  var playerHeightMovie = 120;
  var playerHeightSound = 45;
  var playerHeight = playerHeightMovie;
  var fileExtension = url.replace(/^.*\./,'');
  var tag;
  if(fileExtension == 'mp3' || fileExtension == 'asf' || fileExtension == 'wma' || fileExtension == 'wav' || fileExtension == 'aiff' || fileExtension == 'mpg' || fileExtension == 'mpeg' || fileExtension == 'avi'){
    if(fileExtension == 'mp3' || fileExtension == 'wav'){
      playerHeight = playerHeightSound;
    }
    tag = '<OBJECT CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
      + 'WIDTH="' + playerWidth + '" '
      + 'HEIGHT="' + playerHeight + '" '
      + 'ALIGN="middle" '
      + 'STANDBY="Loading Microsoft Windows Media Player components..." '
      + 'CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112" '
      + 'TYPE="application/x-oleobject">'
      + '<PARAM NAME="AutoStart" VALUE="false">'
      + '<PARAM NAME="FileName" VALUE="' + url + '">'
      + '<PARAM NAME="ShowControls" VALUE="true">'
      + '<PARAM NAME="ShowStatusBar" VALUE="false">'
      + '<EMBED WIDTH="' + playerWidth + '" HEIGHT="' + playerHeight + '" SRC="' + url + '" '
      + 'ALIGN="middle" AUTOSTART="0" SHOWCONTROLS="1 SHOWSTATUSBAR="0" TYPE="application/x-mplayer2" PLUGINSPAGE="http://www.microsoft.com/Windows/MediaPlayer/" />'
      + '</OBJECT>';
  } else if(fileExtension == 'qt' || fileExtension == 'mov'){
    tag = '<OBJECT CLASSID="CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
      + 'WIDTH="' + playerWidth + '" '
      + 'HEIGHT="' + playerHeight + '" '
      + 'ALIGN="middle" '
      + 'STANDBY="Loading QuickTime Player components..." ENABLEJAVASCRIPT="true" TYPE="video/quicktime" '
      + 'CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">'
      + '<PARAM NAME="AutoPlay" VALUE="false">'
      + '<PARAM NAME="Src" VALUE="',url,'">'
      + '<PARAM NAME="Controller" VALUE="true">'
      + '<EMBED WIDTH="' + playerWidth + '" HEIGHT="' + playerHeight + '" SRC="' + url + '" '
      + 'ALIGN="middle" AUTOPLAY="false" CONTROLLER="true" TYPE="video/quicktime" ENABLEJAVASCRIPT="true" '
      + 'PLUGINSPAGE="http://www.apple.com/quicktime/download/" />'
      + '</OBJECT>';
  } else if(fileExtension == 'rm'){
    tag = '<OBJECT CLASSID="CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" '
      + 'WIDTH="' + playerWidth + '" '
      + 'HEIGHT="' + playerHeight + '" '
      + 'ALIGN="middle">'
      + '<PARAM NAME="AutoStart" VALUE="false">'
      + '<PARAM NAME="Src" VALUE="' + url + '">'
      + '<PARAM NAME="Console" VALUE="oneyhkplayerdiv9">'
      + '<PARAM NAME="Controls" VALUE="imagewindow">'
      + '<EMBED WIDTH="' + playerWidth + '" '
      + 'HEIGHT="' + playerHeight + '" '
      + 'ALIGN="middle" AUTOSTART="false" CONTROLS="imagewindow" CONSOLE="oneyhkplayerdiv9" SRC="' + url + '" '
      + 'TYPE="audio/x-pn-realaudio-plugin" PLUGINSPAGE="http://www.real.com/" />'
      + '</OBJECT>';
  } else if(fileExtension == 'swf'){
    tag = '<OBJECT CLASSID="CLSID:D27CDB6E-AE6D-11CF-96B8-444553540000" '
      + 'WIDTH="' + playerWidth + '" '
      + 'HEIGHT="' + playerHeight + '" '
      + 'ALIGN="middle" TYPE="application/x-oleobject">'
      + '<PARAM NAME="MOVIE" VALUE="' + url + '">'
      + '<PARAM NAME="SWLIVECONNECT" VALUE="true">'
      + '<PARAM NAME="PLAY" VALUE="false">'
      + '<PARAM NAME="LOOP" VALUE="false">'
      + '<PARAM NAME="MENU" VALUE="false">'
      + '<PARAM NAME="QUALITY" VALUE="high">'
      + '<PARAM NAME="BGCOLOR" VALUE="#FFFFFF">'
      + '<PARAM NAME="FLASHVARS" VALUE="">'
      + '<PARAM NAME="SCALE" VALUE="default">'
      + '<PARAM NAME="WMODE" VALUE="window">'
      + '<PARAM NAME="ALLOWSCRIPTACCESS" VALUE="sameDomain">'
      + '<EMBED WIDTH="' + playerWidth + '" HEIGHT="' + playerHeight + '" ALIGN="middle" TYPE="application/x-shockwave-flash" '
      + 'SRC="' + url + '" SWLIVECONNECT="true" PLAY="false" LOOP="false" '
      + 'MENU="false" QUALITY="high" BGCOLOR="#FFFFFF" FLASHVARS="" SCALE="default" WMODE="window" ALLOWSCRIPTACCESS="sameDomain" '
      + 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" /></OBJECT>';
  }
  return tag;
}

function strToInt(str) {
  num = parseInt(str);
  if(isNaN(num)){
    return 0;
  } else if(!num) {
    return 0;
  }
    return num;
}

function init() {
  var rssMikleType = '';
  var anchorTarget = '_blank';
  var itemPodcast = 'off';

  var containerObj = document.getElementById('feed_container');
  var headerObj = document.getElementById('feed_header') ? document.getElementById('feed_header') : "";
  var contentObj = document.getElementById('feed_content');
  var footerObj = document.getElementById('feed_footer');

  var totalHeight = getObjectHeight(containerObj);
  var headerHeight = headerObj ? getObjectHeight(headerObj) + strToInt(getElementStyle(headerObj,'marginTop','margin-top')) + strToInt(getElementStyle(headerObj,'marginBottom','margin-bottom')) : 0;
  var contentMargin = strToInt(getElementStyle(contentObj,'marginTop','margin-top')) + strToInt(getElementStyle(contentObj,'marginBottom','margin-bottom'))
  var contentPadding = strToInt(getElementStyle(contentObj,'paddingTop','padding-top')) + strToInt(getElementStyle(contentObj,'paddingBottom','padding-bottom'));
  var footerHeight = getObjectHeight(footerObj) + strToInt(getElementStyle(footerObj,'marginTop','margin-top')) + strToInt(getElementStyle(footerObj,'marginBottom','margin-bottom'));
  var contentHeight = totalHeight - headerHeight - contentMargin - footerHeight;
  var tmpHeight = contentHeight - contentPadding;

  if(rssMikleType == 'ticker'){
    var spanElem = document.getElementsByTagName('span');
    for(var i=0; i < spanElem.length; i++){
      if(spanElem[i].className == 'feed_item_title'){
      } else if(spanElem[i].className == 'feed_item_podcast') {
        spanElem[i].innerHTML = "";
      } else if(spanElem[i].className == 'feed_item_description') {
        spanElem[i].innerHTML = spanElem[i].innerHTML.replace(/<.*?>/g,'');
      }
    }
  } else {
    var divElem = document.getElementsByTagName('div');
    var noneFlag = 0;
    var itemCounter = 1;
    for(var i=0; i < divElem.length; i++){
      if(divElem[i].className == 'feed_item'){
        if(!noneFlag){
          for(var j=0; j < divElem[i].childNodes.length; j++) {
            if(divElem[i].childNodes[j].className == 'feed_item_title'){
            } else if(divElem[i].childNodes[j].className == 'feed_item_podcast') {
              if(itemPodcast == 'player'){
                divElem[i].childNodes[j].innerHTML = getPlayerTag(divElem[i].childNodes[j].childNodes[0].href) ? getPlayerTag(divElem[i].childNodes[j].childNodes[0].href) : divElem[i].childNodes[j].innerHTML;
              }
            } 
          }
          var tmpObjHeight = getObjectHeight(divElem[i]);
          var tmpMarginTop = strToInt(getElementStyle(divElem[i],'marginTop','margin-top'));
          var tmpMarginBottom = strToInt(getElementStyle(divElem[i],'marginBottom','margin-bottom'));
          tmpHeight -= tmpObjHeight - tmpMarginTop;
          if(tmpHeight < 0 && itemCounter != 1){
            divElem[i].style.display = 'none';
            noneFlag = 1;
          }
          tmpHeight -= tmpMarginBottom;
        } else {
          divElem[i].style.display = 'none';
        }
        itemCounter++;
      }
    }
    contentObj.style.height = contentHeight + 'px';
  }

  var aElem = document.getElementsByTagName('a');
  for(var i=0; i < aElem.length; i++){
    aElem[i].target = anchorTarget;
  }
}
