var d = document;

function $HEX(ob, color){
 var m = null, color2hex = {
aliceblue:"f0f8ff",antiquewhite:"faebd7",aqua:"00ffff",aquamarine:"7fffd4",azure:"f0ffff",beige:"f5f5dc",bisque:"ffe4c4",black:"000000",blanchedalmond:"ffebcd",blue:"0000ff",blueviolet:"8a2be2",brown:"a52a2a",burlywood:"deb887",cadetblue:"5f9ea0",chartreuse:"7fff00",chocolate:"d2691e",coral:"ff7f50",cornflowerblue:"6495ed",cornsilk:"fff8dc",crimson:"dc143c",cyan:"00ffff",darkblue:"00008b",darkcyan:"008b8b",darkgoldenrod:"b8860b",darkgray:"a9a9a9",darkgreen:"006400",darkgrey:"a9a9a9",darkkhaki:"bdb76b",darkmagenta:"8b008b",darkolivegreen:"556b2f",darkorange:"ff8c00",darkorchid:"9932cc",darkred:"8b0000",darksalmon:"e9967a",darkseagreen:"8fbc8f",darkslateblue:"483d8b",darkslategray:"2f4f4f",darkslategrey:"2f4f4f",darkturquoise:"00ced1",darkviolet:"9400d3",deeppink:"ff1493",deepskyblue:"00bfff",dimgray:"696969",dimgrey:"696969",dodgerblue:"1e90ff",firebrick:"b22222",floralwhite:"fffaf0",forestgreen:"228b22",fuchsia:"ff00ff",gainsboro:"dcdcdc",ghostwhite:"f8f8ff",gold:"ffd700",goldenrod:"daa520",gray:"808080",green:"008000",greenyellow:"adff2f",grey:"808080",honeydew:"f0fff0",hotpink:"ff69b4",indianred:"cd5c5c",indigo:"4b0082",ivory:"fffff0",khaki:"f0e68c",lavender:"e6e6fa",lavenderblush:"fff0f5",lawngreen:"7cfc00",lemonchiffon:"fffacd",lightblue:"add8e6",lightcoral:"f08080",lightcyan:"e0ffff",lightgoldenrodyellow:"fafad2",lightgray:"d3d3d3",lightgreen:"90ee90",lightgrey:"d3d3d3",lightpink:"ffb6c1",lightsalmon:"ffa07a",lightseagreen:"20b2aa",lightskyblue:"87cefa",lightslategray:"778899",lightslategrey:"778899",lightsteelblue:"b0c4de",lightyellow:"ffffe0",lime:"00ff00",limegreen:"32cd32",linen:"faf0e6",magenta:"ff00ff",maroon:"800000",mediumaquamarine:"66cdaa",mediumblue:"0000cd",mediumorchid:"ba55d3",mediumpurple:"9370db",mediumseagreen:"3cb371",mediumslateblue:"7b68ee",mediumspringgreen:"00fa9a",mediumturquoise:"48d1cc",mediumvioletred:"c71585",midnightblue:"191970",mintcream:"f5fffa",mistyrose:"ffe4e1",moccasin:"ffe4b5",navajowhite:"ffdead",navy:"000080",oldlace:"fdf5e6",olive:"808000",olivedrab:"6b8e23",orange:"ffa500",orangered:"ff4500",orchid:"da70d6",palegoldenrod:"eee8aa",palegreen:"98fb98",paleturquoise:"afeeee",palevioletred:"db7093",papayawhip:"ffefd5",peachpuff:"ffdab9",peru:"cd853f",pink:"ffc0cb",plum:"dda0dd",powderblue:"b0e0e6",purple:"800080",red:"ff0000",rosybrown:"bc8f8f",royalblue:"4169e1",saddlebrown:"8b4513",salmon:"fa8072",sandybrown:"f4a460",seagreen:"2e8b57",seashell:"fff5ee",sienna:"a0522d",silver:"c0c0c0",skyblue:"87ceeb",slateblue:"6a5acd",slategray:"708090",slategrey:"708090",snow:"fffafa",springgreen:"00ff7f",steelblue:"4682b4",tan:"d2b48c",teal:"008080",thistle:"d8bfd8",tomato:"ff6347",turquoise:"40e0d0",violet:"ee82ee",wheat:"f5deb3",white:"ffffff",whitesmoke:"f5f5f5",yellow:"ffff00",yellowgreen:"9acd32"
};
 if(ob) color = ob.css('background-color');
 color = (color==''||color=='transparent'||color=='inherit') ? "rgb(246, 245, 247)" : color;
 color = color.toLowerCase();
 m = /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/.exec(color);
 if(m){
  return [ Number(hex2dec(m[1])), Number(hex2dec(m[2])), Number(hex2dec(m[3])) ];
 }; m = null; m = /#([0-9a-f]{3})/.exec(color);
 if(m){
  color = m[1] + m[1];
  return [ hex2dec(color.slice(0,2)), hex2dec(color.slice(2,4)), hex2dec(color.slice(4,6)) ];
 }; m = null; m = /rgb\((\d+?), (\d+?), (\d+?)\)/.exec(color);
 if(m){
  return [ Number(m[1]), Number(m[2]), Number(m[3]) ];
 }; m = null; m = /(\w+)/.exec(color);
 if(m){
  for (var colorName in color2hex){
   if(colorName == m[1]){
    color = color2hex[colorName];
    return [ hex2dec(color.slice(0,2)), hex2dec(color.slice(2,4)), hex2dec(color.slice(4,6)) ];
   }
  }
 }
 return [ Number(246), Number(245), Number(247) ];
}
function dec2hex(d){ return (d < 16) ? ("0" + Number(d).toString(16)) : Number(d).toString(16);}
function hex2dec(h){ return Number(parseInt(h, 16));}
function cornerPlay(){
 var RS = oParts.query('.corner-play', d), client = oParts.client;
 var UD = 'undefined', cord = 0.4;
 var content, inlineCSS, bg, pbg, color, textAlign, borderColor, margin, padding, background, width, b, base1, base2, base3;
 var $r, $g, $b, c3d0, c3d1, c3d2, c3d3, c3d4, reg;
 var frame = '<span><span></span><span></span><span></span><span></span></span>';
 var i = 0, l = RS.length, RSi, P, RP, type;
 while(i < l){
  RSi = o(RS[i]);
  RP = RSi.parent();
  if(RSi.$.corner_played == true) break;
  if(typeof RSi.$.corner_played == UD){
   RSi.corner_played = false;
   while (RP){
    if(RP.klass('article')) break;
    RP = RP.parent();
   }
  }
  if(RSi.corner_played == true) break;
  type = RSi.className.indexOf('outset') > 0 ? 'outset' : 'rounded';
  type = RSi.className.indexOf('square') > 0 ? 'square' : type;
  content = RSi.html();
  inlineCSS = (' '+RSi.$.style.cssText.toLowerCase().replace(/;$/,'')+';'), bg = $HEX(RSi);
  width = (m = / (width: ?[^;]+);/.exec(inlineCSS)) ? m[1]+';' : '';
  margin = (m = /margin[^;]+?;/g.exec(inlineCSS)) ? m.join('') : '';
  padding = (m = /padding[^;]+?;/g.exec(inlineCSS)) ? m.join('') : '';
  color = (m = / (color: ?[^;]+);/.exec(inlineCSS)) ? m[1]+';' : '';
  borderColor = (m = /border\-([^:]+?|)color:\s?([^;]+?);/g.exec(inlineCSS)) ? m[0].split(/: /)[1].replace(/;/,'') : '';
  textAlign = (m = / (text\-align: ?[^;]+);/.exec(inlineCSS)) ? m[1]+';' : '';
  width = width=='' ? 'width: ' + RSi.css('width') + (client.Opera?'px;':';') : width;
  if(margin=='') margin = 'margin: 1em auto;';
  if(padding=='') padding = 'padding: .2em 1em;';
  color = color=='' ? 'color: ' + RSi.css('color') + ';' : color;
  textAlign = textAlign=='' ? 'text-align: ' + RSi.css('text-align') + ';' : textAlign;
  borderColor = borderColor=='' ? 'rgb(196, 196, 197)' : borderColor;
  if(borderColor=='' || borderColor=='transparent'){
   pbg = $HEX(RSi.parent());
   borderColor = 'rgb('+parseInt((pbg[0]+bg[0])*cord)+','+parseInt((pbg[1]+bg[1])*cord)+','+parseInt((pbg[2]+bg[2])*cord)+')';
  } else {
   borderColor = $HEX(null, borderColor);
   borderColor = 'rgb('+borderColor[0]+','+borderColor[1]+','+borderColor[2]+')';
  }
  background = 'background-color: rgb('+bg[0]+','+bg[1]+','+bg[2]+');';
  RSi.css('border-color: transparent; background-color: transparent; '+width+margin+color+textAlign);
  if(RSi.$.nodeName=='P'){
   P = RSi; RSi = oParts.create('div', P.$.style.cssText, null, true);
   P.addBefore(RSi); P.away();
  }
  RSi.html(frame + '<div>' + content + '</div>' + frame);
  b = RSi.$.getElementsByTagName("span");
  base1 = 'display:block; overflow:hidden; font-size:1px;'; base2 = 'height:1px;';
  
  switch (type) {
   case 'rounded' :
    base3 = background+' border-left:1px solid '+borderColor+'; border-right:1px solid '+borderColor+';';
    b[0].style.cssText = base1;
    b[1].style.cssText = base1 + base2 + 'margin:0 5px; background:'+borderColor+';';
    b[2].style.cssText = base1 + base2 + base3 + 'margin:0 3px; border-width:0 2px;';
    b[3].style.cssText = base1 + base2 + base3 + 'margin:0 2px;';
    b[4].style.cssText = base1 + base3 + 'height:2px; margin:0 1px;';
    b[5].style.cssText = b[0].style.cssText; b[6].style.cssText = b[4].style.cssText;
    b[7].style.cssText = b[3].style.cssText; b[8].style.cssText = b[2].style.cssText;
    b[9].style.cssText = b[1].style.cssText;
    RSi.child(1).css(background+'; border:0px solid '+borderColor+'; border-width:0 1px; '+padding);
    break;
   case 'outset' :
    borderColor = borderColor.replace(/rgb\(/,'').replace(/\)/,'').split(',');
    $r = ($r=borderColor[0]) > 255 ? 255 : $r;
    $g = ($g=borderColor[1]) > 255 ? 255 : $g;
    $b = ($b=borderColor[2]) > 255 ? 255 : $b;
    c3d0 = 'rgb('+$r+','+$g+','+$b+')';
    c3d1 = 'rgb('+parseInt($r*.9)+','+parseInt($g*.9)+','+parseInt($b*.9)+')';
    c3d2 = 'rgb('+parseInt($r*.8)+','+parseInt($g*.8)+','+parseInt($b*.8)+')';
    c3d3 = 'rgb('+parseInt($r*.7)+','+parseInt($g*.7)+','+parseInt($b*.7)+')';
    c3d4 = 'rgb('+parseInt($r*.6)+','+parseInt($g*.6)+','+parseInt($b*.6)+')';
    b[0].style.cssText = b[5].style.cssText = base1;
    b[1].style.cssText = base1 + base2 + 'margin:0 5px; background:'+c3d0+';';
    b[2].style.cssText = base1 + base2 + background
      + 'border-left:2px solid '+c3d0+'; border-right:2px solid '+c3d1+'; margin:0 3px;';
    b[3].style.cssText = base1 + base2 + background
      + 'border-left:1px solid '+c3d0+'; border-right:1px solid '+c3d2+'; margin:0 2px;';
    b[4].style.cssText = base1 + background
      + 'border-left:1px solid '+c3d1+'; border-right:1px solid '+c3d3+'; height:2px; margin:0 1px;';
    b[6].style.cssText = base1 + background
      + 'border-left:1px solid '+c3d1+'; border-right:1px solid '+c3d4+'; height:2px; margin:0 1px;';
    b[7].style.cssText = base1 + base2 + background
      + 'border-left:1px solid '+c3d1+'; border-right:1px solid '+c3d4+'; margin:0 2px;';
    b[8].style.cssText = base1 + base2 + background
      + 'border-left:2px solid '+c3d3+'; border-right:2px solid '+c3d4+'; margin:0 3px;';
    b[9].style.cssText = base1 + base2 + 'margin:0 5px; background:'+c3d4+';';
    RSi.child(1).css(background + 'border-left:1px solid '+c3d0+'; border-right:1px solid '+c3d4+';'+padding);
    break;
   case 'square' :
    b[0].style.cssText = b[5].style.cssText = base1;
    b[1].style.cssText = base1 + background
      + 'height:5px; margin:0 6px; border:solid 1px '+borderColor+'; border-bottom-width:0;';
    b[4].style.cssText = b[6].style.cssText = base1 + base2 + background
      + 'margin:0; border-style:solid; border-width:0 7px; border-color:'+borderColor+';';
    b[9].style.cssText = base1 + background
      + 'height:5px; margin:0 6px; border:solid 1px '+borderColor+'; border-top-width:0;';
    RSi.child(1).css(background + padding + 'border-left:1px solid '+borderColor+'; border-right:1px solid '+borderColor+';');
    break;
  }
  RSi.$.corner_played = true;
  i++;
 }
};

imagePop = function (e, path, w, h){
 if(typeof imgPop == "undefined") imgPop = null;
 if(imgPop==null){
  imgPop = oParts.create("IMG", "position: absolute;\
           left: " + Math.round((oParts.metrics(0)-w) / 2) + "px;\
            top: " + Math.round((oParts.metrics(1)-h) / 2 + oParts.metrics(3)) + "px;\
           margin: 0; z-index: 1000; border: 4px groove teal; display: none"
  );
  imgPop.src(path);
  if(imgPop.$.complete)imgPop.show();
  imgPop.on("load", function(){imgPop.show();});
  imgPop.on("click", function(){imgPop.away(); imgPop = null;});
  imgPop.title("マウスクリックで閉じます");
 }
}

oParts.start(cornerPlay);

