/*
photoeffect.js v1.2 for ppblog by martin $ 2008/05/20 22:03:28
Copyright (c) 2007-2008, martin. http://p2b.jp
Usage:
 main-className: photo-effect
  sub-className: photo-frame (default)
                 stamp-frame
                 photo-angle[from -360 to 360]
  e.g. <img src="foo.jpg" alt="photo-effect" class="photo-effect photo-angle5" />
  ** Recommended `photo-angle` is -5 to 5.
*/

if(document.namespaces){
 if(!document.namespaces['v']){
  document.namespaces.add('v', 'urn:schemas-microsoft-com:vml'); 
  document.createStyleSheet().addRule('v\\:*', 'behavior: url(#default#VML); display: inline-block;'); 
 }
}

var photoEffect = {
 imgs : [],
 init : function(ob){
  var d = document, nodes = [], item; ob = ob || d;
  photoEffect.imgs = [];
  try {
   var xp = d.evaluate(
    './/img[contains(concat(" ", @class, " "), " photo-effect ")]',
    ob, null, 0, null
   );
   for (item = xp.iterateNext(); item; item = xp.iterateNext()){
    nodes.push(item);
   }
  } catch(e){
   var node, cls, items = ob.getElementsByTagName('IMG');
   for(var i = 0, l = items.length; i < l; i++){
    item = items[i];
    if(item.className){
     cls = item.className.split(/\s+/);
     for(var j = 0, k = cls.length; j < k; j++){
      if(cls[j]=='photo-effect'){
       nodes[nodes.length] = item; break;
      }
     }
    }
   }
  }
  if(nodes.length){
   if(!photoEffect.photoframe){
    photoEffect.photoframe = new Image();
    photoEffect.photoframe.src = 'Images/photoframe.png' + (d.evaluate ? '' : '?' + Number(new Date));
   }
   for(var i = 0, l = nodes.length; i < l; i++){
    node = nodes[i];
    if(node.className.indexOf('stamp-frame') >= 0){
     if(!photoEffect.stampframe){
      photoEffect.stampframe = new Image();
      photoEffect.stampframe.src = 'Images/stampframe.png' + (d.evaluate ? '' : '?' + Number(new Date));
     }
    }
    try {
     photoEffect.imgs.push(node);
    } catch (e) {};
   }
  } else return;
  if(photoEffect.stampframe){
   photoEffect.stampframe.onload = photoEffect.fire;
   if(photoEffect.stampframe.width) photoEffect.fire();
  } else {
   photoEffect.photoframe.onload = photoEffect.fire;
   if(photoEffect.photoframe.width) photoEffect.fire();
  }
 },
 fire : function(){
  var d = document, img, iw, ih, rot, arot, ar, ab, bx, by, fw, fh, w, h, canvas, cntxt, frame;
  for (var i = 0, len = photoEffect.imgs.length; i < len; i++){
   var img = photoEffect.imgs[i];
   img.onload = function(){ var K = this;
    if(K.photoEffected) return;
    iw = K.width; ih = K.height;
    rot = K.className.match(/photo-angle(\-?\d+)/);
    rot = rot ? Math.PI*rot[1]/180 : 0;
    arot = Math.abs(rot);
    frame = d.namespaces ? d.createElement('v:*') : new Image();
    if(K.className.indexOf('stamp-frame') != -1){
     ar = 1.02; ab = 0.85;
     frame.src = photoEffect.stampframe.src;
    } else {
     ar = 1.07; ab = 0.7;
     frame.src = photoEffect.photoframe.src;
    }
    bx = parseInt(Math.sqrt(iw) * ab); by = parseInt(Math.sqrt(ih) * ab);
    fw = iw + 2 * bx; fh = Math.round((ih + by) * ar) + by;
    w = parseInt(fw * Math.cos(arot) + fh * Math.sin(arot));
    h = parseInt(fw * Math.sin(arot) + fh * Math.cos(arot));
   
    if(d.namespaces){
     var dx = dy = 0;
     canvas = d.createElement('<v:* style="width:'+w+'px; height:'+h+'px;">');
     if(rot > 0){ dx = fh * Math.sin(rot); dy = 0;}
     if(rot < 0){ dx = 0; dy = fw * Math.sin(arot);}
     canvas.innerHTML = '<?xml:namespace prefix = v /><v:group style="position: relative; width:'+w+'px; height:'+h+'px; rotation:'+(180*rot/Math.PI)+';" coordsize="'+w+','+h+'"><v:rect filled="f" stroked="f" style="position:absolute; left:'+dx+'px; top:'+dy+'px; width:'+fw+'px; height:'+fh+'px;"><v:imagedata src="'+frame.src+'" /></v:rect><v:rect stroked="f" style="position:absolute; padding:0; left:'+(bx+dx)+'px; top:'+(by+dy)+'px; width:'+iw+'px; height:'+ih+'px;"><v:imagedata src="'+K.src+'" /></v:rect></v:group>';
     canvas.style.cssText = K.style.cssText;
     canvas.title = K.title;
     K.parentNode.replaceChild(canvas, K);
    } else {
     canvas = d.createElement('canvas');
     canvas.setAttribute('id', 'photo_canvas_K'+i);
     d.body.appendChild(canvas);
     canvas.style.cssText = K.style.cssText;
     canvas.title = canvas._title = (typeof K._title != 'undefined') ? K._title : K.title;
     canvas.height = h; canvas.style.height = h + 'px';
     canvas.width = w; canvas.style.width = w + 'px';
     cntxt = canvas.getContext('2d');
     if(K.parentNode) K.parentNode.replaceChild(canvas, K);
     if(rot > 0) cntxt.translate(fh * Math.sin(rot), 0);
     if(rot < 0) cntxt.translate(0, fw * Math.sin(arot));
     if(rot != 0) cntxt.rotate(rot);
     cntxt.drawImage(frame, 0, 0, fw, fh);
     try { cntxt.drawImage(K, bx, by, iw, ih);} catch(e){}
    }
    canvas.className = K.className;
    if(canvas.parentNode.parentNode) canvas.parentNode.parentNode.className += ' photo-canvas';
    if(K.getAttribute('onclick') != ''){
     canvas.setAttribute('onclick', K.getAttribute('onclick'));
    }
    K.photoEffected = true;
   }
   if(img.width && !img.photoEffected) img.onload();
  }
 }
}
