
(function($){var formUpdateTimer,formUpdating=false,lastWidth,lastHeight,lastAspect,$cropper,$keepAspect,$productGroup,$picture,pWidth,pHeight,pRatio,$wallW,$wallH;$(document).ready(function(){$('#blog_share').blogDialog('#dialog_blog_share');});$(document).ready(initForms);$(window).load(function(){$cropper=$('div.cropper');$picture=$('#picture');pWidth=$picture.width();pHeight=$picture.height();pRatio=pWidth/pHeight;updateDimensions();});function initForms(){$keepAspect=$('#input_keepAspect');$productGroup=$('#input_productGroup');$wallW=$('#input_wallWidth');$wallH=$('#input_wallHeight');lastWidth=parseInt($wallW.val());lastHeight=parseInt($wallH.val());lastAspect=pw.limitations.keepAspect;$('div.product div.description').removeClass('loading');$('#form_setCustomDimensions').bind('submit',function(event){event.preventDefault();var $form=$(this);$('div.product div.description').addClass('loading');clearTimeout(formUpdateTimer);formUpdateTimer=setTimeout(function(){$.get($form.attr('action'),$form.serialize(),function(responseBody,status){if(!formUpdating){$('div.product div.description').html(responseBody);$('div.product p.price span.price').blink(1,2,150);initForms();updateDimensions();}},'html');},500);updateDimensions();});$('#form_cancel').bind('submit',function(event){event.preventDefault();$('div.product div.description').load($(this).attr('action'),$(this).serialize(),function(){$('div.product p.price span.price').blink(1,2,150);initForms();updateDimensions();});});$('div.dimensions input:not(:submit), div.widthHeight input:not(:submit)').bind('keyup change',function(){var $input=$(this);clearTimeout(formUpdateTimer);formUpdating=true;$('div.product div.description').addClass('loading');if($keepAspect.is(':checked')||pw.limitations.keepAspect){updateDimensions();}
formUpdateTimer=setTimeout(function(){formUpdating=false;$input.closest('form').submit();},1000);});}
function updateCoordinates(x,y){$('#input_x').val(x/pWidth);$('#input_y').val(y/pHeight);}
function updateDimensions(){var wWidth=parseInt($wallW.val()),wHeight=parseInt($wallH.val());if(wWidth>=0&&wHeight>=0&&typeof wWidth==='number'&&typeof wHeight==='number'){var keepAspect=$keepAspect.is(':checked')||pw.limitations.keepAspect,maxWidth=pw.limitations.maxWidth||0,maxHeight=pw.limitations.maxHeight||0,maxShortSide=pw.limitations.maxShortSide||0,maxLongSide=pw.limitations.maxLongSide||0,orgWidth=wWidth,orgHeight=wHeight;if(keepAspect){if(lastHeight!=orgHeight){wWidth=Math.round(wHeight*pRatio);}
if(lastWidth!=orgWidth||lastAspect!=keepAspect){wHeight=Math.round(wWidth/pRatio);}}
if(maxWidth&&wWidth>maxWidth){wWidth=maxWidth;if(keepAspect){wHeight=Math.round(wWidth/pRatio);}
$('#maxWidth').blink(1,5,150);}
if(maxShortSide&&wWidth>maxShortSide&&wWidth<wHeight){wWidth=maxShortSide;if(keepAspect){wHeight=Math.round(wWidth/pRatio);}
$('#maxShortSide').blink(1,5,150);}
if(maxLongSide&&wWidth>maxLongSide&&wWidth>wHeight){wWidth=maxShortSide;if(keepAspect){wHeight=Math.round(wWidth/pRatio);}
$('#maxLongSide').blink(1,5,150);}
if(maxHeight&&wHeight>maxHeight){wHeight=maxHeight;if(keepAspect){wWidth=Math.round(wHeight*pRatio);}
$('#maxHeight').blink(1,5,150);}
if(maxShortSide&&wHeight>maxShortSide&&wHeight<=wWidth){wHeight=maxShortSide;if(keepAspect){wWidth=Math.round(wHeight*pRatio);}
$('#maxShortSide').blink(1,5,150);}
if(maxLongSide&&wHeight>maxLongSide&&wHeight>=wWidth){wHeight=maxLongSide;if(keepAspect){wWidth=Math.round(wHeight*pRatio);}
$('#maxLongSide').blink(1,5,150);}
if(wWidth!=orgWidth||(keepAspect&&wHeight!=orgHeight)){$wallW.val(wWidth);}
if(wHeight!=orgHeight||(keepAspect&&wWidth!=orgWidth)){$wallH.val(wHeight);}
lastWidth=wWidth;lastHeight=wHeight;lastAspect=keepAspect;var wRatio=wWidth/wHeight,widthRatio=pWidth/wWidth,heightRatio=pHeight/wHeight,ratio=Math.abs(pRatio/wRatio);if(keepAspect){$cropper.hide();$('h2',$cropper).show();updateCoordinates(0,0);}else if(ratio&&ratio!='Infinity'&&ratio>1.02||ratio<0.98){$cropper.fadeIn(1000);if(widthRatio>heightRatio){$cropper.imageCropper({width:pWidth,height:pHeight,axis:'x',size:Math.round(wWidth*heightRatio),init:function(handle){updateCoordinates(handle.left,0);},start:function(){$('h2',$cropper).fadeOut(1000);},stop:function(event,handle){updateCoordinates(handle.position.left,0);}});}else{$cropper.imageCropper({width:pWidth,height:pHeight,axis:'y',size:Math.round(wHeight*widthRatio),init:function(handle){updateCoordinates(0,handle.top);},start:function(){$('h2',$cropper).fadeOut(1000);},stop:function(event,handle){updateCoordinates(0,handle.position.top);}});}}else{$cropper.hide();$('h2',$cropper).show();}}else{$cropper.hide();}}
$.fn.imageCropper=function(options){var defaults={axis:'x',size:0,width:null,height:null,init:function(position){},start:function(event,ui){},drag:function(event,ui){},stop:function(event,ui){}};var opts=$.extend(defaults,options);return this.each(function(){var $cropper=$('div.holder',this);var $handle=$('div.handle',this);var $mask1=$('div.mask1',this);var $mask2=$('div.mask2',this);var marginWidth=$handle.outerWidth()-$handle.width();var marginHeight=$handle.outerHeight()-$handle.height();var position=$handle.position();$cropper.css({width:opts.width+marginWidth,height:opts.height+marginHeight});if(!$handle.hasClass('ui-draggable')){$handle.draggable({containment:'parent',cursor:'move',axis:opts.axis,start:opts.start,drag:function(event,ui){resizeMasks(ui.position);opts.drag(event,ui);},stop:opts.stop});}else{$handle.draggable('option','axis',opts.axis);$handle.draggable('option','start',opts.start);$handle.draggable('option','drag',function(event,ui){resizeMasks(ui.position);opts.drag(event,ui);});$handle.draggable('option','stop',opts.stop);}
$handle.css({width:null,height:null});switch(opts.axis){case'x':$handle.css({height:$cropper.height()-marginHeight,width:opts.size+'px',left:Math.round((position.left+opts.size<$cropper.width())?position.left:($cropper.width()-opts.size)/2)+'px',top:0});break;case'y':$handle.css({width:$cropper.width()-marginWidth,height:opts.size+'px',top:Math.round((position.top+opts.size<$cropper.height())?position.top:($cropper.height()-opts.size)/2)+'px',left:0});break;}
function resizeMasks(position){$mask1.css({width:(opts.axis=='x')?position.left:$cropper.width(),height:(opts.axis=='y')?position.top:$cropper.height()});$mask2.css({width:(opts.axis=='x')?$cropper.width()-($handle.outerWidth()+position.left):$cropper.width(),height:(opts.axis=='y')?$cropper.height()-($handle.outerHeight()+position.top):$cropper.height()});}
resizeMasks($handle.position());opts.init($handle.position());});}})(jQuery);