/**
 * @author Vlad Yakovlev (scorpix@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 * @version 0.1 (2009-08-11)
 * @requires jQuery
 * @requires jTweener
 * @requires jCommon
 */
$(function(){var
rootBlock=$('.rotate_human'),pictureBlock=rootBlock.find('.picture'),containerBlock=rootBlock.find('.shape_container');var
image,shaper;isHover=false,isAnimate=false,curAngle=0,tweenParams={time:0.4};function Shaper(rootBlock,image,height){var
canvas,radius=Math.ceil(Math.sqrt(Math.pow(image.height,2)+Math.pow(image.width,2)));init();function init(){rootBlock.css({height:height+Math.round((radius-image.height)/2),left:Math.round((image.width-radius)/2),top:Math.round((image.height-radius)/2),width:radius});jCommon.isCanvas?createSvg():createVml();}
function createCanvas(){canvas=$('<canvas class="shape"></canvas>').appendTo(rootBlock).attr({height:radius,width:radius});var ctx=canvas.get(0).getContext('2d');ctx.translate(radius/2,radius/2);ctx.save();ctx.drawImage(image,-image.width/2,-image.height/2,image.width,image.height);ctx.restore();}
function updateCanvas(angle){var ctx=canvas.get(0).getContext('2d');ctx.save();ctx.clearRect(-radius/2,-radius/2,radius,radius);ctx.rotate(Math.PI/180*angle);ctx.drawImage(image,-image.width/2,-image.height/2,image.width,image.height);ctx.restore();}
function createVml(){canvas=$(document.createElement('v:image')).addClass('shape vml').attr('src',image.src).appendTo(rootBlock).css({width:image.width,height:image.height,top:Math.round((radius-image.height)/2),left:Math.round((radius-image.width)/2)});}
function updateVml(angle){canvas.css('rotation',angle);}
function createSvg(){var svgNS='http://www.w3.org/2000/svg';var xlinkNS='http://www.w3.org/1999/xlink';var svg=document.createElementNS(svgNS,'svg');svg.attr({version:'1.1','class':'shape',preserveAspectRatio:'xMinYMin meet'});rootBlock.append(svg);canvas=document.createElementNS(svgNS,'image');canvas.attr({x:Math.round((radius-image.width)/2),y:Math.round((radius-image.height)/2),width:image.width,height:image.height});canvas.attr(xlinkNS+':href',image.src);svg.appendChild(canvas);}
function updateSvg(angle){var moveX=Math.round(image.width/2)+Math.round((radius-image.width)/2);var moveY=Math.round(image.height/2)+Math.round((radius-image.height)/2);canvas.attr('transform','rotate('+angle+','+moveX+','+moveY+')');}
return{update:jCommon.isCanvas?updateSvg:updateVml};};init();function init(){if(!rootBlock.size())return;if($.browser.opera&&9.7<parseFloat($.browser.version))return;image=new Image(pictureBlock.find('img').width(),pictureBlock.find('img').height());image.onload=load;image.src=pictureBlock.find('img').attr('src');}
function load(){shaper=new Shaper(containerBlock,image,pictureBlock.height());pictureBlock.css('opacity',0);rootBlock.find('a').hover(function(){isHover=true;hoverIn();},function(){isHover=false;hoverOut();});}
function hoverIn(transition){if(isAnimate)return;isAnimate=true;$t(rootBlock,tweenParams).tween({transition:transition?transition:'easeInCubic',moveX:function(value){shaper.update(Math.round(180*value));},onComplete:function(){isAnimate=false;isHover||hoverOut('easeNone');}});}
function hoverOut(transition){if(isAnimate)return;isAnimate=true;$t(rootBlock,tweenParams).tween({transition:transition?transition:'easeInCubic',moveX:function(value){shaper.update(180+Math.round(180*value));},onComplete:function(){isAnimate=false;isHover&&hoverIn('easeNone');}});}});if(!$.browser.msie){Element.prototype.attr=function(name,value){function getName(name){var splitterIndex=name.indexOf(':');var ns='';var attr='';if(-1<splitterIndex){var s=splitterIndex;while(-1<s){splitterIndex=s;s=name.indexOf(':',s+1);}
ns=name.substr(0,splitterIndex);attr=name.substr(splitterIndex+1);}else{attr=name;}
return[ns,attr];}
if('string'==typeof name&&undefined!==value){var aData=getName(name);''==aData[0]?this.setAttribute(aData[1],value):this.setAttributeNS(aData[0],aData[1],value);}else if('string'==typeof name&&undefined===value){var aData=getName(name);return''==aData[0]?this.getAttribute(aData[1]):this.getAttributeNS(aData[0],aData[1]);}else{for(prop in name){var aData=getName(prop);''==aData[0]?this.setAttribute(aData[1],name[prop]):this.setAttributeNS(aData[0],aData[1],name[prop]);}}};}