/*!
 * Copyright (C) 2006-2011 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 * 
 * @require jade/ArrayUtils.js
 * @require jade/HTMLUtils.js
 */
function PrintsBasketFormat(g,d,c,f,e,a,b){this.format=g;
this.formatName=d;this.formatPrintWidth=f;this.formatPrintHeight=e;this.depotId=c;
this.parent=null;this.orderItems=[];this.lastPBFile=null;this.price=0;this.qualityWarning=false;
this.amountInfo=false;this.aspectRatioWarning=false;this.aspectRatioCoef=0.01;this.count=0;
this.detailDepotThumbSize=null;this.simpleDepotThumbSize={width:40,height:33};this.startDrag=false;
this.dragPicture=null;this.startDragX=0;this.startDragY=0;this.dragPictureX=0;this.dragPictureY=0;
this.dropTarget=null;this.dragElement=null;this.qualityMeter=new QualityMeter(f,e);
this.qualityMeter.dpm=a;this.qualityMeter.coefficient=b;this.onRemoveOrderItem=null;
this.options={}}PrintsBasketFormat.prototype.setThumbnailSize=function(b){var c,a;
this.detailDepotThumbSize=b;for(c=0,a=this.orderItems.length;c<a;c++){this.orderItems[c].detailThumbSize=b;
if(this.parent.detailDepotVisible){this.orderItems[c].detailDepotUpdate()}}};PrintsBasketFormat.prototype.addOrderItem=function(a,d){var b,e,c;
if(d===undefined){d=true}a.qualityWarning=!this.qualityMeter.getQualityLevel(a.pbFile.width,a.pbFile.height);
a.aspectRatioWarning=!this.checkAspectRatio(a.pbFile.width,a.pbFile.height);a.parent=this;
a.detailThumbSize=this.detailDepotThumbSize;for(e=-1,b=0;b<this.orderItems.length;
b++){if(this.orderItems[b].pbFile.uniqueId==a.pbFile.uniqueId){e=b;break}}if(e==-1){this.orderItems.push(a);
c=a}else{this.orderItems[e].count+=a.count;c=this.orderItems[e]}this.lastPBFile=a.pbFile;
this.count+=a.count;this.qualityWarning=(!this.qualityWarning&&a.qualityWarning)?true:this.qualityWarning;
this.amountInfo=(!this.amountInfo&&(a.count>1))?true:this.amountInfo;this.aspectRatioWarning=(!this.aspectRatioWarning&&a.aspectRatioWarning)?true:this.aspectRatioWarning;
this.parent.isSomethingChanged=true;if(d){this.updateSimpleDepot();if(this.parent.detailDepotVisible){this.updateDetailDepot("add",c)
}}};PrintsBasketFormat.prototype.removeAllOrderItems=function(){var a;a=this.orderItems;
this.orderItems=[];this.lastPBFile=null;this.count=0;this.qualityWarning=false;this.aspectRatioWarning=false;
this.amountInfo=false;this.updateSimpleDepot();this.updateDetailDepot("new");this.parent.isSomethingChanged=true;
if(this.parent.orderItemRemoveHandler!==null){this.parent.orderItemRemoveHandler(null,this.format)
}return a};PrintsBasketFormat.prototype.updateFormatInfo=function(){var b,a;this.qualityWarning=false;
this.amountInfo=false;this.aspectRatioWarning=false;for(b=0,a=this.orderItems.length;
b<a;b++){this.qualityWarning=(!this.qualityWarning&&this.orderItems[b].qualityWarning)?true:this.qualityWarning;
this.amountInfo=(!this.amountInfo&&this.orderItems[b].count>1)?true:this.amountInfo;
this.aspectRatioWarning=(!this.aspectRatioWarning&&this.orderItems[b].aspectRatioWarning)?true:this.aspectRatioWarning
}this.lastPBFile=a>0?this.orderItems[a-1].pbFile:null};PrintsBasketFormat.prototype.cutOrderItem=function(a,b){if(b===undefined){b=true
}this.count-=a.count;jade.ArrayUtils.remove(this.orderItems,a);if(b){this.updateFormatInfo();
this.updateSimpleDepot();this.updateDetailDepot("cut",a)}};PrintsBasketFormat.prototype.removeOrderItem=function(a,b,c){if(typeof(b)=="undefined"){b=a.count
}if(c===undefined){c=true}this.count-=b;a.count-=b;if(a.count==0){jade.ArrayUtils.remove(this.orderItems,a)
}this.updateFormatInfo();this.updateSimpleDepot();this.updateDetailDepot("del",a);
this.parent.isSomethingChanged=true;if(c&&this.parent.orderItemRemoveHandler!==null){this.parent.orderItemRemoveHandler(a,this.format)
}};PrintsBasketFormat.prototype.editOrderItem=function(a,b){if((this.parent.onEditItem!==null)&&(typeof(this.parent.onEditItem)=="function")){this.parent.onEditItem(a,b)
}};PrintsBasketFormat.prototype.cropOrderItem=function(a,b){if((this.parent.onCropItem!==null)&&(typeof(this.parent.onCropItem)=="function")){this.parent.onCropItem(a,b)
}};PrintsBasketFormat.prototype.removeOrderItemClick=function(a,b){if(this.control){return this.control.removeOrderItemClick(a?a:event,this)
}this.removeOrderItem(b.orderItem,b.orderItem.count)};PrintsBasketFormat.prototype.editOrderItemClick=function(a,c){var b;
if(this.control){return this.control.editOrderItemClick(a?a:event,this)}b=this.formatPrintWidth+"x"+this.formatPrintHeight;
this.editOrderItem(c.orderItem,b)};PrintsBasketFormat.prototype.cropOrderItemClick=function(a,c){var b;
if(this.control){return this.control.cropOrderItemClick(a?a:event,this)}b=this.formatPrintWidth+"x"+this.formatPrintHeight;
this.cropOrderItem(c.orderItem,b)};PrintsBasketFormat.prototype.orderItemMouseOver=function(a,c){var d,b;
if(this.control){return this.control.orderItemMouseOver(a?a:event,this)}d=jade.HTMLUtils.getElementsByClassName(c,"remove");
b=jade.HTMLUtils.getElementsByClassName(c,"edit");if(d.length>0){d[0].style.display="block"
}if(b.length>0){b[0].style.display="block"}};PrintsBasketFormat.prototype.orderItemMouseOut=function(a,c){var d,b;
if(this.control){return this.control.orderItemMouseOut(a?a:event,this)}if(!this.parent.detailDepotShowDeleteAlways){d=jade.HTMLUtils.getElementsByClassName(c,"remove");
if(d.length>0){d[0].style.display="none"}}b=jade.HTMLUtils.getElementsByClassName(c,"edit");
if(b.length>0){b[0].style.display="none"}};PrintsBasketFormat.prototype.qtyOrderItemClick=function(a,c){var b;
if(this.control){return this.control.qtyOrderItemClick(a?a:event,this)}if(this.parent.detailDepotAmountContext!==null){b=jade.HTMLUtils.getParentByClassName(c,"format-details-img");
b.orderItem.selected=true;this.parent.detailDepotAmountContext.orderItemCount=b.orderItem.count;
this.parent.detailDepotAmountContext.display(a,c)}};PrintsBasketFormat.prototype.updateSimpleDepot=function(){var b,c,a,d;
d=document.getElementById("format-"+this.format+"-lastimg");a=document.getElementById("format-"+this.format+"-count");
a.firstChild.data=this.count;a.style.visibility=this.count>0?"visible":"hidden";if(this.lastPBFile===null){jade.HTMLUtils.getElementsByClassName(d,"image")[0].style.visibility="hidden"
}else{b=jade.ThumbnailUtils.getThumbnailData(PictureBar.getPictureWidth(this.lastPBFile),PictureBar.getPictureHeight(this.lastPBFile),this.simpleDepotThumbSize.width,this.simpleDepotThumbSize.height);
c=jade.HTMLUtils.getElementsByClassName(d,"image")[0];if(this.parent.spacerImg!==null){c.src=this.parent.spacerImg.src
}c.alt="";c.title=c.alt;c.src=PictureBar.getPictureSrc(this.lastPBFile,this.simpleDepotThumbSize.width,this.simpleDepotThumbSize.height);
c.width=b.width;c.height=b.height;c.style.margin=(b.y*2)+"px "+b.x+"px 0px "+b.x+"px";
c.style.visibility="visible"}document.getElementById("format-"+this.format+"-quality").style.visibility=this.qualityWarning?"visible":"hidden";
if((this.parent.onSimpleDepotChange!==null)&&(typeof(this.parent.onSimpleDepotChange)=="function")){this.parent.onSimpleDepotChange(this.format,this.count,b)
}};PrintsBasketFormat.prototype.addDetailDepotImg=function(k,i){var e,l,h,m,f,g,c,a,b,d,j;
e="orderitem-"+this.format+"-"+i.pbFile.source+"-"+i.pbFile.id;i.detailImgId=e;l=document.getElementById(e);
if(l===null){j=document.getElementById("format-"+this.format+"-details-img-dummy");
l=j.cloneNode(true);l.id=e;l.orderItem=i;l.style.display="block";l.onmouseover=this.orderItemMouseOver;
l.onmouseout=this.orderItemMouseOut;l.control=this;l.onmousedown=this.handlePictureMouseDown;
l.ondragstart=function(){return false};l.onclick=function(){return false};h=jade.HTMLUtils.getElementsByClassName(l,"image")[0];
h.alt=i.pbFile.name+" ("+PictureBar.getPictureWidth(i.pbFile)+"x"+PictureBar.getPictureHeight(i.pbFile)+")";
h.title=h.alt;h.onclick=this.selectOrderItemHandler;if(this.parent.detailViewDisableImgContextMenu){h.oncontextmenu=function(){return false
}}h.control=this;h.orderItem=i;m=jade.HTMLUtils.getElementsByClassName(l,"remove")[0];
if(this.parent.detailDepotShowDeleteAlways){m.style.display="block"}else{m.style.display="none"
}m.onclick=this.removeOrderItemClick;m.control=this;m.format=this.format;m.orderItem=i;
f=jade.HTMLUtils.getElementsByClassName(l,"removeLink")[0];if(f){f.onclick=this.removeOrderItemClick;
f.control=this;f.format=this.format;f.orderItem=i}g=jade.HTMLUtils.getElementsByClassName(l,"edit")[0];
c=jade.HTMLUtils.getElementsByClassName(l,"editLink")[0];if(g){g.style.display="none"
}if(i.pbFile.source==PictureBarFile.SOURCE_REMOTE){if(c){c.style.display="none"}}else{if(g){g.onclick=this.editOrderItemClick;
g.control=this;g.format=this.format;g.orderItem=i}if(c){c.onclick=this.editOrderItemClick;
c.control=this;c.format=this.format;c.orderItem=i}}b=jade.HTMLUtils.getElementsByClassName(l,"aspectRatioWarning")[0];
d=jade.HTMLUtils.getElementsByClassName(l,"cropLink")[0];if(i.pbFile.source==PictureBarFile.SOURCE_REMOTE){if(d){d.style.display="none"
}}else{if(b){b.onclick=this.cropOrderItemClick;b.control=this;b.format=this.format;
b.orderItem=i}if(d){d.onclick=this.cropOrderItemClick;d.control=this;d.format=this.format;
d.orderItem=i}}a=jade.HTMLUtils.getElementByClassName(l,"qty1");a.onclick=this.qtyOrderItemClick;
a.control=this;i.detailDepotUpdate(l);if(this.parent.detailDepotInsertBefore){k.insertBefore(l,j.nextSibling)
}else{k.appendChild(l)}}else{i.detailDepotUpdate(l)}};PrintsBasketFormat.prototype.selectOrderItemHandler=function(a,c){var b;
if(this.control){return this.control.selectOrderItemHandler(a?a:event,this)}b=jade.HTMLUtils.getParentByClassName(c,"format-details-img");
b.orderItem.selected=!b.orderItem.selected;b.orderItem.detailDepotUpdate(b);if(this.parent.orderItemSelectHandler!==null){this.parent.orderItemSelectHandler(c.orderItem,this.format)
}};PrintsBasketFormat.prototype.selectAllOrderItems=function(){var c,a,b;for(b=0,c=0,a=this.orderItems.length;
c<a;c++){if(!this.orderItems[c].selected){b++;this.orderItems[c].selected=true;this.orderItems[c].detailDepotUpdate()
}}return b};PrintsBasketFormat.prototype.deselectAllOrderItems=function(){var c,a,b;
for(b=0,c=0,a=this.orderItems.length;c<a;c++){if(this.orderItems[c].selected){b++;
this.orderItems[c].selected=false;this.orderItems[c].detailDepotUpdate()}}return b
};PrintsBasketFormat.prototype.removeDetailDepotImg=function(f,c,d){var g,e,b,a;if(d===undefined){d=true
}g="orderitem-"+this.format+"-"+c.pbFile.source+"-"+c.pbFile.id;e=document.getElementById(g);
if(e!==null){if(!d||(d&&c.count==0)){a=document.getElementById(c.detailImgId);if(a!==null){f.removeChild(a)
}}else{b=jade.HTMLUtils.getElementsByClassName(e,"count")[0];b.firstChild.data=c.count;
b.style.visibility=c.count>1?"visible":"hidden"}}};PrintsBasketFormat.prototype.updateDetailDepot=function(f,b){var c,d,a,e;
d=jade.HTMLUtils.getElementByClassName(document.getElementById("format-"+this.format+"-details-container"),"canvas");
switch(f){case"new":a=jade.HTMLUtils.getElementsByClassName(d,"format-details-img");
for(c=0;c<a.length;c++){if(a[c].id!="format-"+this.format+"-details-img-dummy"){d.removeChild(a[c])
}}for(c=0;c<this.orderItems.length;c++){this.addDetailDepotImg(d,this.orderItems[c])
}break;case"add":this.addDetailDepotImg(d,b);break;case"del":this.removeDetailDepotImg(d,b);
break;case"cut":this.removeDetailDepotImg(d,b,false);break;case"refresh":break}document.getElementById("detail-format-"+this.format+"-qty").firstChild.data=this.count;
d.style.display=this.parent.detailDepotShowEmptyFormats||this.orderItems.length>0?"block":"none";
LnkStatus.changeStatus(document.getElementById("format-"+this.format+"-selector"),this.orderItems.length>0?"":"off");
LnkStatus.changeStatus(document.getElementById("format-"+this.format+"-selectorImg"),this.orderItems.length>0?"":"off");
e=document.getElementById("format-"+this.format+"-details-noItem");if(e){e.style.display=this.orderItems.length>0?"none":"block"
}e=document.getElementById("detail-format-"+this.format+"-smallestSingleprice");if(e){e.style.display=this.orderItems.length>0?"none":"inline"
}e=document.getElementById("detail-format-"+this.format+"-singleprice");if(e){e.style.display=this.orderItems.length>0?"inline":"none"
}if((this.parent.onDetailDepotChange!==null)&&(typeof(this.parent.onDetailDepotChange)=="function")){this.parent.onDetailDepotChange()
}};PrintsBasketFormat.prototype.getSelectedOrderItems=function(){var c,a,b;b=[];for(c=0,a=this.orderItems.length;
c<a;c++){if(this.orderItems[c].selected){b.push(this.orderItems[c])}}return b};PrintsBasketFormat.prototype.getSelectedOrderItemsCount=function(){var b,a,c;
for(c=0,b=0,a=this.orderItems.length;b<a;b++){if(this.orderItems[b].selected){c++
}}return c};PrintsBasketFormat.prototype.stopDragging=function(){this.startDrag=false;
this.dragging=false;window.dragStartFormat=false;document.dragStartFormat=false;window.onmousemove=window._onmousemove;
document.onmousemove=document._onmousemove;window.onmouseup=window._onmouseup;document.onmouseup=document._onmouseup;
if(this.dragElement){if(this.dropTarget){this.parent.hideDetailDepotHover(this.dropTarget[1]);
if(this.format!=this.dropTarget[0]){this.parent.moveSelectedOrderItemsToFormat(this.dropTarget[0]);
this.parent.deselectAll()}this.dropTarget=null}this.dragElement.className=""}};PrintsBasketFormat.prototype.handlePictureMouseDown=function(c,b){var a;
if(this.control){return this.control.handlePictureMouseDown(c?c:window.event,this)
}this.startDrag=true;this.dragPicture=b;this.startDragX=this.getAbsoluteMouseX(c);
this.startDragY=this.getAbsoluteMouseY(c);a=b.getElementsByTagName("img")[0];this.dragPictureX=this.getMouseX(c)-(c.layerX?parseInt(a.style.marginLeft):0);
this.dragPictureY=this.getMouseY(c)-(c.layerY?parseInt(a.style.marginTop):0);window.dragStartFormatControl=this;
document.dragStartFormatControl=this;window._onmousemove=window.onmousemove?window.onmousemove:null;
document._onmousemove=document.onmousemove?document.onmousemove:null;window._onmouseup=window.onmouseup?window.onmouseup:null;
document._onmouseup=document.onmouseup?document.onmouseup:null;window.onmousemove=this.handleWindowMouseMove;
document.onmousemove=this.handleWindowMouseMove;window.onmouseup=this.handleWindowMouseUp;
document.onmouseup=this.handleWindowMouseUp;return false};PrintsBasketFormat.prototype.handleWindowMouseMove=function(j){var h,f,d,g,c,b,a,e;
if(this.dragStartFormatControl){return this.dragStartFormatControl.handleWindowMouseMove(j?j:window.event)
}h=this.getAbsoluteMouseX(j);f=this.getAbsoluteMouseY(j);if(this.startDrag){if((Math.abs(h-this.startDragX)>5)||(Math.abs(f-this.startDragY)>5)){this.startDrag=false;
this.dragging=true;c=this.dragPicture.getElementsByTagName("img")[0];if(!this.dragPicture.orderItem.selected){this.selectOrderItemHandler(null,c)
}if(this.dragElement===null){this.dragElement=document.getElementById("pbDragElement").cloneNode(true);
document.getElementsByTagName("body")[0].appendChild(this.dragElement)}b=this.dragElement.getElementsByTagName("img")[0];
b.width=c.width;b.height=c.height;b.src=c.src;this.dragElement.style.left=(h-this.dragPictureX)+"px";
this.dragElement.style.top=(f-this.dragPictureY)+"px";this.dragElement.className="enabled "+(this.parent.selectedDetailDepotItems==1?"single":"multiple");
this.parent.updateDropTargetPositions()}}if(this.dragging){this.dragElement.style.left=(h-this.dragPictureX-4)+"px";
this.dragElement.style.top=(f-this.dragPictureY)+"px";a=null;for(d=0,g=this.parent.dropTargets.length;
d<g;d++){e=this.parent.dropTargetPositions[d];if(h>e.x&&h<e.x+e.width&&f>e.y&&f<e.y+e.height){a=this.parent.dropTargets[d]
}}if(this.dropTarget!=a){if(this.dropTarget){this.parent.hideDetailDepotHover(this.dropTarget[1])
}if(a){if(this.format!=a[0]){jade.HTMLUtils.addClassName(this.dragElement,"dropable");
this.parent.showDetailDepotHover(a[1])}}else{jade.HTMLUtils.removeClassName(this.dragElement,"dropable")
}this.dropTarget=a}}};PrintsBasketFormat.prototype.handleWindowMouseUp=function(a,b){if(this.dragStartFormatControl){return this.dragStartFormatControl.handleWindowMouseUp(a?a:window.event,this)
}if(b){this.stopDragging()}};PrintsBasketFormat.prototype.checkAspectRatio=function(c,a){var b,d;
if(this.formatPrintWidth>=this.formatPrintHeight){d=this.formatPrintHeight/this.formatPrintWidth
}else{d=this.formatPrintWidth/this.formatPrintHeight}if(c>=a){b=a/c}else{b=c/a}if(Math.abs(b-d)<this.aspectRatioCoef){return true
}return false};PrintsBasketFormat.prototype.setOption=function(a,b){this.options[a]=b;
if(this.parent.onSetFormatOption!==null){this.parent.onSetFormatOption(this.format,a,b)
}};PrintsBasketFormat.prototype.setAspectRatioCoef=function(a){if(0<a&&a<=1){this.aspectRatioCoef=a
}};PrintsBasketFormat.prototype.getOptions=function(){return this.options};PrintsBasketFormat.prototype.getMouseX=function(a){return a.layerX||a.offsetX||0
};PrintsBasketFormat.prototype.getMouseY=function(a){return a.layerY||a.offsetY||0
};PrintsBasketFormat.prototype.getAbsoluteMouseX=function(a){if(a.pageX){return a.pageX
}else{if(a.clientX){return a.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft)
}else{return 0}}};PrintsBasketFormat.prototype.getAbsoluteMouseY=function(a){if(a.pageY){return a.pageY
}else{if(a.clientY){return a.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)
}else{return 0}}};
