// Copyright (c) 2008 bshuzhang http://www.66feng.com

var Block = Class.create({
  initialize: function(id) {
  	this.id=id;
  	 this.options=Object.extend({
		className:         "dialog",
      closable:          true,
      draggable:         true,
      userData:          null,
      userPara:			 null,
      showEffect:        (this.hasEffectLib ? Effect.Appear : Element.show),
      hideEffect:        (this.hasEffectLib ? Effect.Fade : Element.hide),
      showEffectOptions: {},
      hideEffectOptions: {},
      effectOptions:     null,
      parent:            document.getElementsByTagName("body").item(0),
      title:             "&nbsp;",
      url:               null,
      onload:            Prototype.emptyFunction,
      width:             200,
      height:            300,
      handle:			"handle"
    }, arguments[1] || {});
    this.element = this._createWindow(id);
    
    var onload=this.options.onload;
    if(onload&& Object.isFunction(onload)) onload=onload(this.options.parent);
    BlockGroup.addBlock(this);
  },
  _createWindow:function(id){
  	var className = this.options.className;
  	var div1= new Element('div', { 'class': className+'_handle',id:id+"-handle" }).update("&nbsp;");
  	var div2= new Element('div', { 'class': className+'_content',id:id+"-content" }).update("");
  	var div3= new Element('div', { 'class': className+'_title',id:id+"-title" }).update(id);
  	var div= new Element('div', { 'class': className,id:id}).update(div1);
  	div.insert(div2);
  	div.insert(div3);

  	$(this.options.parent).insert(div);
	return div;
  },
  setHandle:function(handle){
  	this.options.handle=handle?handle:this.options.handle;
  },
  setTitle:function(title){
  	this.options.title=title?title:"";
  	$(this.element.id+"-title").innerHTML=this.options.title;
  },
  getTitle:function(){
  	return this.options.title;
  },
   getId:function(){
  	return this.element.id;
  },
  setContent:function(content){
  	$(this.element.id+"-content").innerHTML=content?content:"";
  },
  remove:function(){
  	this.options={};
  	this.element.remove();
  	//delete this;
  }
});
var BlockGroup={
	sequence:[],
	blocks:[],
	realUpdate:true,
groupBlocks:function(elementId,handle,setSeq){
	//alert("sortable:"+elementId);
	Sortable.create(elementId,{handle:handle,ghosting:false,constraint:false, tag:'div',
    onUpdate:function(sortable){
    	BlockGroup.sequence=Sortable.sequence(sortable.id);
    	setGroupSequence(sortable.id);
    	BlockGroup.updateInf(Sortable.serialize(sortable));}
	});
	/*
	if(isLogin) {
		alert(elementId);
		alert("this.sequence:"+Sortable.sequence(elementId)+" s:"+Sortable.sequence(elementId));
	}
	*/
	if(setSeq) this.sequence=Sortable.sequence(elementId);
	
	},
unGroupBlocks:function(elementId){
		Sortable.destroy(elementId);
	},
updateInf:function(inf){
		if(this.realUpdate){
		}
		
	},
addBlock:function(block){
	//if(isLogin) alert("addBlock:"+block.id);
	this.blocks.push(block);
	},
findBlock:function(blockId){
	return this.blocks.detect(function(block){return block.id==blockId});
	},
removeBlock: function(blockId) {
	//alert("remove:"+blockId);
	var target=this.findBlock(blockId);
	target.remove();
	var bId=blockId.substring(blockId.lastIndexOf("_"+1),blockId.length);
    this.blocks = this.blocks.reject(function(d) { return d.id==blockId});
    this.sequence = this.sequence.reject(function(d) { return d==bId});
  }
};

function addGroup(parentId,groupId,title,className){
	//alert("addGroup");
	var ogroupId=groupId;
	groupId=replace_(groupId);
	var gparent=$(parentId);
	groupId=groupId+"-menu"
	//alert(gparent);
	var group_tag=new Element('a', { 'class': "group_tag plus_group",'id':ogroupId+"_tag"}).update("&nbsp;&nbsp;&nbsp;");
	//var setingTag="<input type=image src='../../images/icon/12_12tool.jpg' align='absmiddle' onclick='pageSetting('"+ogroupId+"','"+className+"')'>";
	var setting_Tag=new Element('input', {'class':className+"_pageSeting",'type': "image","src":"../../images/icon/12_12tool.jpg","align":"absmiddle"});
	//var setting_Tag=new Element('div', { 'class': className+"_pageSeting"}).update("&nbsp;&nbsp;设置");
	setting_Tag.observe("click",function(event){pageSetting(ogroupId,className)});
	var div_id= new Element('div', { 'class': className+"_id",'id':ogroupId}).update(setting_Tag);
	div_id.addClassName("display_hide");
	var div_titile= new Element('div', { 'class': className+"_title",'id':groupId+"-title"}).update(title);
	
	//div_id.observe("click",function(event){pageSetting(ogroupId,className)});

	div_titile.insert(div_id);
	var div= new Element('div', { 'class': className,'id':groupId}).update("");
	group_tag.observe("click",function(event){ 
		updatePagePara();
		var cPageId=$("pageId").value;
		if(cPageId!=ogroupId) changePage(ogroupId);
		});
	div_titile.observe("click",function(event){ 
		updatePagePara();
		var cPageId=$("pageId").value;
		if(cPageId!=ogroupId) changePage(ogroupId);
		});
	div_titile.observe("dblclick",function(event){ removeSetting(ogroupId);updatePageName(ogroupId);});
	
	gparent.insert(group_tag);
	gparent.insert(div_titile);
	gparent.insert(div);
	Droppables.add(groupId+"-title", {accept:"dialog",hoverclass: className+"_hover",
	onDrop:function(element,dropon, event){
		//alert("update:"+element.id+" drop:"+dropon.down(0).id);
		if(!isLogin){
			alert(NORIGHT);
			return;
		}
		var pageId=dropon.down(0).id;
		var targetPage=dropon.id.substring(0,dropon.id.lastIndexOf("-menu"));
		var elePage=element.id.substring(0,element.id.lastIndexOf("_"));
		if(elePage.substring(elePage.lastIndexOf("-menu"),elePage.length)=="-menu"){
		elePage=elePage.substring(0,elePage.lastIndexOf("-menu"));
		}
		if(targetPage!=elePage){
		Effect.Puff(element);
		var blockId=element.id.substring(element.id.lastIndexOf("_")+1,element.id.length);
		//alert("blockId:"+blockId);
		moveToPage(blockId,pageId);
		BlockGroup.removeBlock(element.id);
		var cpageId=element.id.substring(0,element.id.lastIndexOf("_"));
		synBlockGroupSeq(cpageId);
		}			
		}
	});
	
}
function addDir(dirId,acceptClass,hoverClass){
	Droppables.add(dirId, {accept:acceptClass,hoverclass: hoverClass,
	onDrop:function(element,dropon, event){
		if(dropon.id=="recycle"){
			var isdel=confirm(DELETECONFIRM);
			if(isdel){
			if(!isLogin){
			alert(NORIGHT);
			return;
			}
			var blockId=element.id.substring(element.id.lastIndexOf("_")+1,element.id.length);
			Effect.Puff(element);
			delSiteNaviBlock(blockId);
			BlockGroup.removeBlock(element.id);
			var pageId=element.id.substring(0,element.id.lastIndexOf("_"));
			synBlockGroupSeq(pageId);
			}
		}
		}	
		});
}
function setGroupSequence(ele,seq,ini){
	//if(isLogin) alert("seq:"+seq);
	//return;
	BlockGroup.sequence=BlockGroup.sequence.reject(function(d) { return d==""});
	if(ini&&seq){
		seq=seq.reject(function(d) { return d==""});
		var isSyn=seq.all(function(n){return BlockGroup.sequence.include(n);});
		if(!isSyn||seq.length!=BlockGroup.sequence.length) {
			/*
			if(isLogin){
				alert("bs:"+BlockGroup.sequence);
			 alert("not set Seq:"+seq.length+"-"+BlockGroup.sequence.length+" -:"+BlockGroup.sequence[0]+"+"+BlockGroup.sequence[1]);
			}
			*/
			return;
		}
	}
	if(ele.indexOf("-menu")>0) ele=ele.substring(0,ele.lastIndexOf("-menu"));
	var seqArr=[];
	if(!seq){
		seqArr=BlockGroup.sequence;
	}else if(!Object.isArray(seq)){
		return;
	}else{
		seqArr=seq;
	}
	BlockGroup.sequence=seqArr;
	ele=replace_(ele);
	if($(ele).down(0)) Sortable.setSequence(ele,BlockGroup.sequence);
	if($(ele+"-menu").down(0)) Sortable.setSequence(ele+"-menu",BlockGroup.sequence);
}
function synBlockGroupSeq(pageId){
	pageId=replace_(pageId);
	BlockGroup.sequence=Sortable.sequence(pageId);
	updatePagePara();
}

		