function piece(pparent,pi,ptitle,pcolor,pvalue)
{
var parent = pparent || null
var value = (typeof pvalue == "undefined") ? 1 : pvalue
var thisPiece = this
var myPieceIndex
var id = pi
this.title = ptitle || ''
this.color = pcolor || '#AABBCC'
this.block = null
this.setValue = function(nv)
{
value = Math.min(100,Math.max(nv,0))
updateInput()
}
this.getValue = function() { return value }
this.getWidth = function()
{
return Math.round(value * parent.getPieWidth() / 100)
}
var strInputClassName
this.knowInputByClassName = function(cn)
{
strInputClassName = cn
}
this.setKeyPressHandler = function()
{
var input = jQuery(thisPiece).find(strInputClassName)
var me = this
input
.keypress(function(e){
if (e.keyCode==13)
{
var v = input.val()
v = parseInt(v,10)
if (isNaN(v))
v = 0
v = Math.abs(v)
input.val(v)
me.setWidthByValue(v)
return false
}
})
/*
.focus(function(){
jQuery(me.block).find('.current').show()
})
.blur(function(){
jQuery(me.block).find('.current').hide()
})
*/
}
var jqInput
var updateInput = function()
{
if (typeof jqInput == "undefined")
jqInput = jQuery(parent.getContainer()).find(strInputClassName)
jqInput.val(value)
}
this.setValueByWidth = function(nw)
{
value = Math.min(Math.max(0,nw * 100 / parent.getPieWidth()),100)
updateInput()
}
this.set2WidthByValue = function(nv)
{
var block = this.block
var delta = nv - value
this.setValue(nv)
var nw = Math.round(value * parent.getPieWidth() / 100)
var prevBlock = block.previousSibling
var pcl = (prevBlock != null && prevBlock.className=='piece') ? parseInt(prevBlock.style.left,10) : 0
var pcw = (prevBlock != null && prevBlock.className=='piece') ? parseInt(prevBlock.style.width,10) : 0
var nl = pcl+pcw
block.style.width = nw + 'px'
block.style.left = nl + 'px'
}
this.setValueByDelta = function(d)
{
var oldValue = value
this.setValue(Number(value)+Number(d))
var newValue = value
jQuery(this.block).width(this.getWidth())
var cl = parseInt(jQuery(this.block).left(),10)
var newDelta = ((newValue - oldValue) * parent.getPieWidth()) / 100
jQuery(this.block).left(cl - newDelta)
//jQuery(this.block).animate({left:cl - newDelta},'normal')
}
this.setWidthByValue = function(newValue)
{
var myDiv = this.block
var next = parent.getPieceByBase(this,1)
if (next == null) // if it's a last piece
{
updateInput()
return
}
var nextValue = next.getValue()
// determine allowed newValue
var upperBound = Number(value)+Number(nextValue)
newValue = Math.min(Math.max(0,newValue),upperBound)
next.setValueByDelta(Number(-newValue) + Number(value))
this.setValue(newValue)
var nw = Math.round(value * parent.getPieWidth() / 100)
myDiv.style.width = nw + 'px'
jQuery(myDiv).animate({width: nw},'normal')
}
this.setResizing = function(i)
{
var myPie = parent
var myPiece = this
var myPreviousPiece = myPie.getPiece(i-1)
myPieceIndex = i
var onePercent = parent.op
this.block.title = this.title
jQuery(this.block)
.find('.resize')
.mousedown(function(e){
e.cancelBubble = true
this.saveX = e.clientX
/*
if (typeof e.saveX == "undefined")
this.saveX =
*/
var target = document.body
var me = this
//alert(myPiece.title)
var myDiv = me.parentNode
/*
var myPreviousDiv = myDiv.previousSibling
if (parseInt(myPreviousDiv.style.width,10)==0)
{
myPreviousDiv.e = e
jQuery(myPreviousDiv).find('.resize').mousedown()
return
}
*/
var stopResize = function(e){
e.cancelBubble = true
/*
myPie.hideAlt()
*/
jQuery(this)
.unbind('mousemove')
.unbind('mouseup')
.css('cursor','default')
.find('.piece')
.css('cursor','default')
}
jQuery(target)
.mouseup(stopResize)
.mousemove(function(e){
var delta = e.clientX - me.saveX
// snapping to 1%
delta = Math.round(delta / onePercent) * onePercent
// resize previous
var myPrevDiv = myDiv.previousSibling
var pcw = parseInt(myPrevDiv.style.width,10)
var pcl = parseInt(myPrevDiv.style.left,10)
//pnw = Math.max(onePercent,pcw+delta)
pnw = Math.max(0,pcw+delta)
// resize myDiv
var cw = parseInt(myDiv.style.width,10)
var cl = parseInt(myDiv.style.left,10)
var nl = Math.max(0,cl + delta)
//if (nl >= cw + cl + onePercent || nl <= pcl - onePercent)
if (nl >= cw + cl + onePercent || nl <= pcl - onePercent)
{
nl = cl
pnw = pcw
/*
var prev = myDiv
var pw = pnw
var pl
while(pw==0 && (prev = prev.previousSibling) != null)
{
pw = parseInt(prev.style.width,10)
pl = parseInt(prev.style.left,10)
prev.style.left = pl+delta+'px'
}
*/
}
var nw = cw + (cl - nl)
myDiv.style.width = nw + 'px'
myDiv.style.left = nl + 'px'
myPrevDiv.style.width = pnw+'px'
var x = e.clientX + document.body.scrollLeft
var y = e.clientY + 10 + document.body.scrollTop
myPreviousPiece.setValueByWidth(pnw)
myPiece.setValueByWidth(nw)
/*
myPie.showAlt(myPreviousPiece.getValue()+'%'+' - '+myPiece.getValue()+'%',y,x)
*/
me.saveX = delta+me.saveX
})
.css('cursor','e-resize')
.find('.piece')
.css('cursor','e-resize')
})
}
this.exportToXMLString = function()
{
if (typeof jqInput == "undefined")
jqInput = jQuery(parent.getContainer()).find(strInputClassName)
var s = ''
s = '