// check if open video window
function checkIfOpenVideo()
{
  // video iframe size
  video_iframe_width = '270px';
  video_iframe_height = '375px';

  // check if bring #
  url = location.href;
  // url format (ex: #1:2)
  re = /#[0-9]:[0-9]+$/;
  
  if (re.test(url))
  {
    // if window width > 270 (small window width) , let 調整為適當大小 hidden
    if( getViewWidth() > 270)
    {
      document.getElementById('yam_tv_auto').style.display = '';
      document.getElementById('yam_tv_download').style.display = 'none';
    } else {
      document.getElementById('yam_tv_auto').style.display = 'none';
      document.getElementById('yam_tv_download').style.display = '';
    }
        
    document.getElementById('video_area').style.display = '';
    document.getElementById('index_area').style.display = 'none';
    
    // get iframe url
    iframeUrl = getIframeUrl();
    
    document.getElementById('video_iframe').style.width = video_iframe_width;
    document.getElementById('video_iframe').style.height = video_iframe_height;
    document.getElementById('video_iframe').src = iframeUrl;

    //window.resizeTo(280,420);
       
    return true;
  } else {
    document.getElementById('video_area').style.display = 'none';
    document.getElementById('index_area').style.display = '';
    
    return false;
  }  
}

// open video window with selected url, ex: index.html#1:0
function openVideo(channelId,sortId)
{
  // new window size
  newWidth = 266;
  newHeight = 502;
    
  // new window title , maybe it is not work.
  newTitle = 'yam video';
  newUrl = 'http://www.yam.com/index.html' + '#' + channelId + ':' + sortId;
  newTop = 150;
  newLeft = -1;

  // monitor size
  monitorWidth = screen.width;
  monitorHeight = screen.height;
    
  // get View window size
  viewWidth = getViewWidth();
  viewHeight = getViewHeight();
    
  // get video window position near left
  newLeft = monitorWidth - newWidth;
    
  // fix the height , because firefox have url bar
  if (navigator.appName == 'Netscape')
    newHeight = newHeight + 5;

  newAttr = 'height=' + newHeight + ', width=' + newWidth + ', top=' + newTop + ', left=' + newLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no';
  window.open (newUrl,'_blank',newAttr);
}

// get window width
function getViewWidth()
{
  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerWidth != 'undefined')
  {
    viewWidth = window.innerWidth;
  } else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0) {
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)   
    viewWidth = document.documentElement.clientWidth;
  } else {
    // older versions of IE
    viewWidth = document.getElementsByTagName('body')[0].clientWidth;
  } 
  return viewWidth;
}

// get window height
function getViewHeight()
{
  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerWidth != 'undefined')
  {
    viewHeight = window.innerHeight;
  } else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0) {
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)   
    viewHeight = document.documentElement.clientHeight;
  } else {
    // older versions of IE
    viewHeight = document.getElementsByTagName('body')[0].clientHeight;
  } 
  return viewHeight;
}

// when click 展開yam天空首頁 return to home
function returnHome()
{
  try{  
    parentUrl = opener.location.href;
  } catch(e) {
    window.open('http://www.yam.com','yam');
    window.close();
    return true;    
  }
  
  opener.focus(); 
  window.close();
}

// get url(it can be used for iframe)
function getIframeUrl()
{
  url = location.href;
  re = /#[0-9]:[0-9]+$/;
  if (re.test(url))
  {
    //document.title = 'yam';
    document.getElementById('video_area').style.display = '';
    document.getElementById('index_area').style.display = 'none';
    
		//document.body.style.overflow='';
    channelId = url.substr( (url.length-3) , 1);
    sortId = url.substr( (url.length-1) , 1);

    // channel url
    iframeUrl = 'http://tv.yam.com/hot/?' + channelId + ':' + sortId;
  }
  return iframeUrl;
}

// open the suitable window
function openSmallWindow()
{
  url = location.href;
  re = /#[0-9]:[0-9]+$/;
  if (re.test(url))
  {
    //document.title = 'yam';
    document.getElementById('video_area').style.display = '';
    document.getElementById('index_area').style.display = 'none';
    
		//document.body.style.overflow='';
    channelId = url.substr( (url.length-3) , 1);
    sortId = url.substr( (url.length-1) , 1);

    // channel url
    iframeUrl = 'http://tv.yam.com/hot/?' + channelId + ':' + sortId;
  }
  
  // new window size
  newWidth = 266;
  newHeight = 502;
      
  newTitle = 'video';
  newUrl = 'http://www.yam.com/index.html' + '#' + channelId + ':' + sortId;
  newTop = 150;
  newLeft = -1;
    
  // monitor size
  monitorWidth = screen.width;
  monitorHeight = screen.height;
    
  // get View window size
  viewWidth = getViewWidth();
  viewHeight = getViewHeight();
  
  newLeft = monitorWidth - newWidth;
    
  newAttr = 'height=' + newHeight + ', width=' + newWidth + ', top=' + newTop + ', left=' + newLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no';
  window.open (newUrl,newTitle,newAttr);
  window.opener = null;

  // for IE8 to close window
  window.open('','_self','');
  window.close();
}
