function resizeIMG(o){
  if(o.width>530){
    o.resized=true; 
    o.width=530;
  }
}

function resizeIMG_wh(o, w, h){
  o.resized=true; 
  if(w>530 || o.width>530){
    o.width=530;
  }
  else if(w>0){
    o.width=w;
  }
 
  if(h>0){
    o.height=h;
  }
}


function attachIMG(o, event){
  if(!getCookie('BAHAID') && 'Y' == attachMediaLogin){
    if(confirm('您需要先登入才能觀看喔')){
      o.href = "https://user.gamer.com.tw/login.php";
      return true;
    }
    else{
      return false;
    }
  }

  imgid = 'IMG'+o.id;
  //newHTML = '<IMG src="'+o.id+'" border=0>';

//設定屬性
if('' == o.type){
  newHTML = '<IMG src="'+o.id+'" border=0 onload=resizeIMG(this)>';
}
else{
  //attribute = o.type.replace(/[&]/g, ' ');
  var ww = o.type.match(/width=[0-9]+/i);
  var hh = o.type.match(/height=[0-9]+/i);
  width = (ww)? ww[0].match(/[0-9]+/)[0]:0;
  height = (hh)? hh[0].match(/[0-9]+/)[0]:0;

  //width = o.type.match(/width=[0-9]+/i)[0].match(/[0-9]+/)[0];
  //height = o.type.match(/height=[0-9]+/i)[0].match(/[0-9]+/)[0];
  newHTML = '<IMG src="'+o.id+'" border=0 onload="resizeIMG_wh(this, '+width+', '+height+')">';
}

  if('click' == event){
    if(newHTML.toLowerCase().replace(/ border="?0"?/,'').replace(/["]/g,'').substr(0, 28) != o.innerHTML.toLowerCase().replace(/ border="?0"?/,'').replace(/["]/g,'').substr(0, 28)){
      o.innerHTML = newHTML;
      return false;
    }
    else if(o.id != o.href){
      confirmLink( o.href );
      return false;
    }
    else{
      window.open( o.href , '', '');
      return false;
    }
  }
  else if('show' == event){
    if(newHTML.toLowerCase().replace(/["]/g,'') != o.innerHTML.toLowerCase().replace(/["]/g,'')){
      o.innerHTML = newHTML;
      return false;
    }    
  }

  return false;
}

function attachMOVIE(o){
  //表示已經點過了
  if('javascript:void(0)' == o.href){
    return false;
  }

  osrc = o.href;

  if(!getCookie('BAHAID') && 'Y' == attachMediaLogin){
    if(confirm('您需要先登入才能觀看喔')){
      o.href = "https://user.gamer.com.tw/login.php";
      return true;
    }
    else{
      return false;
    }
  }
  
  //osrc = o.id;
  var width = 565,height = 340;
  if(o.href.match(/^http:\/\/mymedia\.yam\.com\/\*\/[0-9]+$/)){
    width = 450;
    height = 120;
  }

  if(osrc == o.href){
    embedid = 'embed'+osrc+o.id;

    var newtube_pattern = /^http:\/\/www\.youtube\.com\/embed\/([0-9a-zA-Z_-]{11})((?:(?:\&|\?|#|\&#)\w+=\w+)*)/;
    var xuite_pattern = /^http:\/\/vlog\.xuite\.net\/embed\/([a-zA-Z0-9]{24})((?:(?:\&|\?|#|\&#)\w+=[\w\.]+)*)/;
    if(osrc.match(newtube_pattern)){
      newHTML = '<iframe id="'+embedid+'" title="YouTube video player" width="' + width + '" height="' + height + '" src="' + osrc + '" frameborder="0" allowfullscreen></iframe>';
    }else if(osrc.match(xuite_pattern)){
		  newHTML = "<iframe id='" + embedid + "' marginwidth='0' marginheight='0' src='" + osrc + "' width='" + width + "' height='" + height + "' scrolling='no' frameborder='0'></iframe>";
    }else{
      newHTML = '<embed id="'+embedid+'" src="'+osrc+'" flashvars="'+o.id+'" quality="best" wmode="transparent" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"></embed>';
    }
    //newHTML = '<embed id="'+embedid+'" src="'+osrc+'" flashvars="'+o.id+'" quality="best" wmode="transparent" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"></embed>';
    o.innerHTML = newHTML;
    document.getElementById(embedid).src = osrc;
    o.href="javascript:void(0)";
  }

  return false;
}

function showMediaClick(t){
  if('img' == t){
    document.write('<font color="red" size="3">請點選觀看圖片</font>');
  }
  else if('movie' == t){
    document.write('<font color="red" size="3">請點選觀看影片</font>');
  }
}

