
var AutoShip={openAddMoreId:0,parentIds:new Array(),rows:new Array(),addParent:function(parentId){if(parentId>0){if(!this.parentIds.isArray()){this.parentIds=new Array();}
if(!this.parentIds.inArray(parentId)){this.parentIds.push(parentId);this.rows[parentId]=new Array();}}},addChild:function(parentId,childId){if(parentId>0&&childId>0){if(!this.rows[parentId]){this.rows[parentId]=new Array();}
if(!this.rows[parentId].inArray(childId)){this.rows[parentId].push(childId);}}},initRows:function(){for(var i=0;i<this.parentIds.length;i++){if(!this.rows[this.parentIds[i]]){this.rows[this.parentIds[i]]=new Array();}}},printRows:function(){var tmp='';for(var i=0;i<this.parentIds.length;i++){tmp+=this.parentIds[i]+'=>'+this.rows[this.parentIds[i]]+'\n';}
alert(tmp);},replaceParentId:function(search,replace){for(var i=0;i<this.parentIds.length;i++){if(search>0&&replace>0){if(this.parentIds[i]==search){this.parentIds[i]=replace;if(!this.rows[replace]){this.rows[replace]=new Array();}
this.rows[replace]=this.rows[search];this.rows[search]=new Array();}}}},deleteParentId:function(parentId,newParentId){if(parentId>0&&this.rows[parentId]){if(newParentId>0){if(!this.rows[newParentId]){this.rows[newParentId]=new Array();}
this.rows[newParentId]=this.rows[parentId];this.rows[newParentId].remove(newParentId);this.rows[parentId]=new Array();}
this.parentIds.remove(parentId);}},findParent:function(childId){var parentId=0;for(var i=0;i<this.parentIds.length;i++){if(this.rows.arrayKeyExists(this.parentIds[i])){if(this.rows[this.parentIds[i]].inArray(childId)){parentId=this.parentIds[i];break;}}}
return parentId;},moveChildId:function(childId,newParentId){if(childId>0){var parentId=this.findParent(childId);if(parentId>0&&this.rows[parentId].isArray()){var row=new Array();for(var i=0;i<this.rows[parentId].length;i++){if(this.rows[parentId][i]!=childId){row.push(this.rows[parentId][i]);}}
this.rows[parentId]=row;}
if(newParentId>0){if(!this.rows[newParentId]){this.rows[newParentId]=new Array();}
if(!this.rows[newParentId].inArray(childId)){this.rows[newParentId].push(childId);}}}},hasChildren:function(parentId){if(this.rows[parentId]!=null&&this.rows[parentId].length>0){return true;}
return false;},toggleAddMore:function(id){if(this.openAddMoreId>0&&this.openAddMoreId!=id&&$('updateBox_'+this.openAddMoreId)!=null){this.toggleElement('updateBox_'+this.openAddMoreId);}
if($('updateBox_'+id)!=null){this.toggleElement('updateBox_'+id);this.openAddMoreId=this.openAddMoreId==id?0:id;}
return false;},toggleElement:function(elementId){if($(elementId)!=null){if($(elementId).visible()){new Effect.BlindUp(elementId,{duration:0.2});}else{if(Prototype.Browser.IE){new Effect.BlindDown(elementId,{duration:0.5});}else{new Effect.BlindDown(elementId,{duration:0.5,transition:Effect.Transitions.Bounce});}}}},makeSortable:function(id){Sortable.create(id,{tag:"tr",containment:["qtable"],ghosting:true,onChange:function(element){},onUpdate:function(element){AutoShip.updateShipmentOrder();AutoShip.saveQueue();},starteffect:scrollStart,endeffect:scrollEnd});this.initRows();},makeDraggable:function(dragId,handleId){new Draggable(dragId,{ghosting:true,revert:true,zindex:3000,handle:handleId,starteffect:scrollStart,endeffect:scrollEnd});},makeDroppable:function(dropId){Droppables.add(dropId,{accept:'child_qrows',onDrop:function(draggable_element,droppable_element,event){if(draggable_element.id.split("_")[1]!=droppable_element.id.split("_")[1]){AutoShip.mergeRows(draggable_element,droppable_element);draggable_element.style.zIndex='';}}});},destroyDroppable:function(dropId){Droppables.remove(dropId);},showRowControls:function(id){var shipmentBox=$('shipmentBox_'+id);var mtBox=$('mtBox_'+id);var addBox=$('addBox_'+id);if($(shipmentBox)!=null){$(shipmentBox).show();}
if($(mtBox)!=null){$(mtBox).show();}
if($(addBox)!=null){$(addBox).show();}},hideRowControls:function(id){var shipmentBox=$('shipmentBox_'+id);var mtBox=$('mtBox_'+id);var addBox=$('addBox_'+id);if($(shipmentBox)!=null){$(shipmentBox).hide();}
if($(mtBox)!=null){$(mtBox).hide();}
if($(addBox)!=null){$(addBox).hide();}},mergeRows:function(dragRow,dropRow){var dragId=dragRow.id.split("_")[1];var dropId=dropRow.id.split("_")[1];var innerTable=$('qtableInner_'+dropId);var updateBox=$('updateBoxRow_'+dropId);innerTable.insertBefore(dragRow,updateBox);if(!this.hasChildren(dragId)&&$('qrow_'+dragId)!=null){$('qrow_'+dragId).remove();}
this.hideRowControls(dragId);if($('rowparent_'+dragId)!=null){$('rowparent_'+dragId).value=dropId;}
if(this.parentIds.inArray(dragId)){var newParentId=null;if(this.hasChildren(dragId)){var children=this.rows[dragId];for(var i=0;i<children.length;i++){if(i==0){newParentId=children[0];if(newParentId<0){alert('newParentId:'+newParentId+' breaking');break;}
this.addParent(newParentId);if($('rowparent_'+newParentId)!=null){$('rowparent_'+newParentId).value=0;}
this.showRowControls(newParentId);if($('updateBoxLink_'+newParentId)!=null){$('updateBoxLink_'+newParentId).onclick=function(){return AutoShip.toggleAddMore(newParentId)};}
if($('updateBoxRow_'+dragId)!=null){$('updateBoxRow_'+dragId).id='updateBoxRow_'+newParentId;}
if($('qtableInner_'+dragId)!=null){$('qtableInner_'+dragId).id='qtableInner_'+newParentId;}
if($('updateBox_'+dragId)!=null){this.destroyDroppable('updateBox_'+dragId);$('updateBox_'+dragId).id='updateBox_'+newParentId;if($('updateBox_'+newParentId)!=null){this.makeDroppable('updateBox_'+newParentId);}}}else{var childId=children[i];if($('rowparent_'+childId)!=null){$('rowparent_'+childId).value=newParentId;}}}}
if(!this.rows[dropId]){this.rows[dropId]=new Array();}
this.rows[dropId].push(dragId);this.deleteParentId(dragId,newParentId);}else{this.moveChildId(dragId,dropId);}
this.updateShipmentOrder();this.saveQueue();this.toggleAddMore(dropId);},saveQueue:function(){new Ajax.Request('/auto_ship/ajax_update_queue',{method:'post',asynchronous:true,parameters:Form.serialize($('autoShipForm'))});},flashRow:function(id){if($(id)!=null){new Effect.Highlight(id,{startcolor:"#7AE3E7",endcolor:"#ffffff",restorecolor:"#ffffff",duration:1});}},getFirstRow:function(){var rows=jQuery("table#qtbl tr:first-child");return jQuery(rows[0]);},moveToTop:function(id){var row=jQuery('#'+id);var firstRow=this.getFirstRow();if(firstRow!=null&&row!=null){this.animateMoveToTop(firstRow,row);}
return false;},animateMoveToTop:function(firstRow,clickedRow){var clonedRow=this.dragMetaData.cloneQueueRow(clickedRow);var clickedRowPos=this.getElemPosition(clickedRow[0]);var firstRowPos=this.getElemPosition(firstRow[0]);clickedRow.children().css("visibility","hidden");clonedRow.addClass("ddrowtable");clonedRow.css({position:"absolute",top:clickedRowPos.top,left:clickedRowPos.left});clonedRow.css("visibility","visible");clonedRow.show();clonedRow.animate({top:"+=4",left:"+=1"},"fast","swing",function(){new Effect.ScrollTo('header-row',{duration:.2});clonedRow.animate({top:firstRowPos.top,borderWidth:"2px"},"slow","swing",function(){if(jQuery("td.mt img",clickedRow)[0]!=null){jQuery("td.mt img",clickedRow)[0].src="/images/pixel_trans.gif";}
if(jQuery("td.mt img",firstRow)[0]!=null){jQuery("td.mt img",firstRow)[0].src="/images/blu_arrow.jpg";}
clonedRow.hide();firstRow.before(clickedRow);clickedRow.children().css("visibility","visible");AutoShip.updateShipmentOrder();AutoShip.saveQueue();});});},updateShipmentOrder:function(){var shipmentIds=Sortable.sequence('qtable');if(shipmentIds.length>0){var shipmentNumber=1;for(var i=0;i<shipmentIds.length;i++){if($('mtimage_'+shipmentIds[i])!=null&&i==0){$('mtimage_'+shipmentIds[i]).src="/images/blu_arrow.jpg";}
if($('mtimage_'+shipmentIds[i])!=null&&i==0){$('mtimage_'+shipmentIds[i]).src="/images/pixel_trans.gif";}else if($('mtimage_'+shipmentIds[i])!=null){$('mtimage_'+shipmentIds[i]).src="/images/blu_arrow.jpg";}
if($('sn_'+shipmentIds[i])!=null){$('sn_'+shipmentIds[i]).value=shipmentNumber;if(this.rows[shipmentIds[i]]){for(var j=0;j<this.rows[shipmentIds[i]].length;j++){var rowId=this.rows[shipmentIds[i]][j];if($('sn_'+rowId)!=null){$('sn_'+rowId).value=shipmentNumber;}}}
shipmentNumber++;}}}},dragMetaData:{helperTable:null,cloneQueueRow:function(row){this.helperTable=this.createDragRowTable(row);return this.helperTable.html(jQuery(row).clone());},createDragRowTable:function(row){var helperTable=this.helperTable=jQuery("<table class='qtbl' id='ddrowtable' border='1' cellspacing='0' cellpadding='0'><tbody></tbody></table>");helperTable.appendTo(jQuery(document.body));var height=34;var width=910;helperTable[0].outerWidth=function(){return width;};helperTable[0].outerHeight=function(){return height;};var marginLeft=0;if(jQuery.browser.msie){marginLeft+=(!jQuery.boxModel)?-1:1;}else{marginLeft++;}
helperTable.css("margin-left",marginLeft);this.helperTable=helperTable;return helperTable;}},getElemPosition:function(el){var pos={top:0,left:0}
if(el!=null){do{pos.left+=el.offsetLeft;pos.top+=el.offsetTop;}while(el=el.offsetParent);}
if(jQuery.browser.msie&&!jQuery.boxModel){var e=jQuery(document.body)[0];pos.top+=e.scrollTop;pos.left+=e.scrollLeft;}
return pos;},getFirstLastPos:function(){var rowIds={first:{top:0,left:0,id:null},last:{top:0,left:0,id:null}}
if(this.parentIds.length>0){var firstId=this.parentIds[0];var lastId=this.parentIds[this.parentIds.length-1];if(this.hasChildren(lastId)){lastId=this.rows[lastId][this.rows[lastId].length-1];}
rowIds.first.id='qrowInner_'+firstId;rowIds.last.id='qrowInner_'+lastId;var posTop=this.getElemPosition($('qrowInner_'+firstId));var posBottom=this.getElemPosition($('qrowInner_'+lastId));rowIds.first.top=posTop.top;rowIds.first.left=posTop.left;rowIds.last.top=posBottom.top;rowIds.last.left=posBottom.left;}
return rowIds;}};var itemBeingDragged;var scrollPoll;function getWindowScroll(){var T,L,W,H;var w=window;with(w.document){if(w.document.documentElement&&documentElement.scrollTop){T=documentElement.scrollTop;L=documentElement.scrollLeft;}else if(w.document.body){T=body.scrollTop;L=body.scrollLeft;}
if(w.innerWidth){W=w.innerWidth;H=w.innerHeight;}else if(w.document.documentElement&&documentElement.clientWidth){W=documentElement.clientWidth;H=documentElement.clientHeight;}else{W=body.offsetWidth;H=body.offsetHeight}}
return{top:T,left:L,width:W,height:H};}
function findTopY(obj){var curtop=0;if(obj.offsetParent){while(obj.offsetParent){curtop+=obj.offsetTop;obj=obj.offsetParent;}}else if(obj.y){curtop+=obj.y;}
return curtop;}
function findBottomY(obj){return findTopY(obj)+obj.offsetHeight;}
function scrollSome(){if(itemBeingDragged!=null){var scroller=getWindowScroll();var yTop=findTopY(itemBeingDragged);var yBottom=findBottomY(itemBeingDragged);var rowPos=AutoShip.getFirstLastPos();var yfirstTop=findTopY($(rowPos.first.id));var ylastBottom=findBottomY($(rowPos.last.id));if(yTop>(ylastBottom)){}else if(yBottom<(yfirstTop)){}
if(yBottom>(scroller.top+scroller.height-10)){window.scrollTo(0,scroller.top+40);}else if(yTop<(scroller.top+10)){window.scrollTo(0,scroller.top-40);}}}
function scrollStart(e){itemBeingDragged=e;scrollPoll=setInterval(scrollSome,13);}
function scrollEnd(e){clearInterval(scrollPoll);}
