﻿// JScript 文件
var baseUrl = 'Upload/';
var maxIndex = minIndex = 90,lx,ly;	//物品z-index，基数90避免占用背景
var currentObj,lobby,editMenu,objTextbox;	//当前编辑的物件，场景容器
function PuttingObject(typeId,productId,productName,fileName,fileWidth,fileHight,edit,leftPx,topPx,index,isReverse){
	var object = GetObject(baseUrl + fileName);
	with(object){
		id = 'd' + typeId + '_' + productId + '_0';
		title = alt = productName;
		width = fileWidth;
		height = fileHight;
		with(style){
			width = fileWidth;
			height = fileHight;
			left = leftPx;
			top = topPx;
			zIndex = index;
			border = 0;
			position = "absolute";
			if(isReverse)filter = 'FlipH';
		}
		if(edit){
			style.cursor = "move";
			onmouseout = function(){this.style.border=0;};
		}
	}
	if(index > maxIndex)
		maxIndex = index;
	else if(index > 20 && index < minIndex)
		minIndex = index;
	lobby.appendChild(object);
}
function TrialObject(typeId,productId,productName,fileName,fileWidth,fileHight,edit,only,leftPx,topPx,zIndex){
	var object = GetObject(baseUrl + fileName);
	if(only){
		for(var i = 0; i < lobby.childNodes.length; i++){
			if(typeof(lobby.childNodes[i].id)=='string' && lobby.childNodes[i].id.indexOf('d' + typeId + '_') == 0){
				lobby.removeChild(lobby.childNodes[i]);
				break;
			}
		}
		object.style.zIndex = zIndex;
	}else{
		object.style.zIndex = ++maxIndex;
	}
	with(object){
		style.border = 0;
		style.position = "absolute";
		id = 'd' + typeId + '_' + productId + '_1';
		title = alt = productName;
		style.width = width = fileWidth;
		style.height = height = fileHight;
		if(leftPx)
			style.left = leftPx;
		else
			style.left = 0;
		if(topPx)
			style.top = topPx;
		else
			style.top = 0;
		if(edit){
			style.cursor = "move";
			onmousemove = function(){ObjectMove(this)};
			onmousedown = function(){ObjectFocus(this)};
			onmouseup = function(){with(this){style.border='solid 1px #000000';releaseCapture();}};
			onmouseover = function(){this.style.border='solid 1px #000000';};
			onmouseout = function(){this.style.border=0;};
		}
	}
	lobby.appendChild(object);
	FillTextbox();
}
function GetObject(Path){
	var object;
	switch(Path.substring(Path.lastIndexOf(".")+1).toLowerCase()){
		case "gif":
		case "jpg":
		case "bmp":
		case "jpeg":
		case "png":
			object = document.createElement("img");
			object.src = Path;
			break;
		case "swf":
			object = document.createElement("div");
			object.innerHTML = '<EMBED src="'+Path+'" quality="high" width="100%" height="100%" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="transparent"></EMBED>';
			break;
		case "mid":
		case "wav":
		case "mp3":
		case "asf":
		case "mpg":
		case "rm":
		case "wmv":
		case "wma":
			if(document.all){
				object = document.createElement("bgsound");
				object.src = Path;
			}else{
				object = document.createElement("embed");
				with(object){
					src = Path;
					setAttribute("type","audio/x-wav");
					setAttribute("loop","-1");
				}
			}
			break;
	}
	return object;
}
function ObjectFocus(obj){
	if(!document.all){
		lx = e.clientX; ly = e.clientY;
	}else{
		lx = event.x; ly = event.y;
	}
	if(event.button == 2){
		with(editMenu){
			style.left = document.body.scrollLeft + window.event.clientX;
			style.top = document.body.scrollTop + window.event.clientY;
			style.display='block';
		}
		currentObj = obj;
	}else if(event.button == 1){
		obj.setCapture();
		obj.style.border = 0;
	}
}
function ObjectMove(obj){
	if(!document.all){ x = e.clientX; y = e.clientY; }else{ x = event.x; y = event.y; }
	if(event.button==1){
		var ofsx = x - lx;
		obj.style.left = parseInt(obj.style.left) + ofsx;
		lx = x;
		var ofsy = y - ly;
		obj.style.top = parseInt(obj.style.top) + ofsy;
		ly = y;
	}
//	FillTextbox();
}
function RemoveObject(objId){
	currentObj = document.getElementById(objId);
	menuClick(5);
}
function menuClick(action){
	if(!currentObj){
		alert('对象丢失，无法设置！');
	}else{
		with(currentObj.style){
			switch(action){
				case 1:
					if(zIndex < maxIndex)
						zIndex = ++maxIndex;
					break;
				case 2:
					if(zIndex > minIndex)
						zIndex = --minIndex;
					break;
				case 3:
					var father = currentObj.parentNode;
					left = (father.offsetWidth - currentObj.offsetWidth)/2;
					top = (father.offsetHeight - currentObj.offsetHeight)/2;
					break;
				case 4:
					filter = filter=='FlipH' ? '':'FlipH';
					break;
				case 5:
					if(zIndex == minIndex)
						++minIndex;
					else if(zIndex == maxIndex)
						--maxIndex;
					if(currentObj.innerHTML)
						currentObj.innerHTML = '';
					currentObj.removeNode();
//					FillTextbox();
					break;
			}
		}
	}
	editMenu.style.display='none';
	FillTextbox();
}

function FillTextbox(){
	var objects = '';
    for(var i = 0; i < lobby.childNodes.length; i++){
		var objId = lobby.childNodes[i].id;
		if(typeof(objId) == 'string' && objId.indexOf('DF_') < 0){
			var x = objId.split('_');
			if(x.length < 3)
				break;
			else{
				if(objects.length > 0)objects += ',';
				objects += x[1];
				objects += '|' + lobby.childNodes[i].title.replace('|','_');
				with(lobby.childNodes[i].style){
					objects += '|' + left;
					objects += '|' + top;
					objects += '|' + zIndex;
					objects += '|' + (filter=='FlipH' ? '1':'0');
					objects += '|' + x[2];
				}
				objects += '|' + objId;
			}
		}
	}
	objTextbox.value = objects;
	objTextbox.onchange();
}
function RemoveEvent(){
    for(var i = 0; i < lobby.childNodes.length; i++){
		with(lobby.childNodes[i]){
			if(typeof(onmousedown)=='function'){
				onmousedown = function(){}
			}
			if(typeof(onmousemove)=='function'){
				onmousemove = function(){}
			}
			if(typeof(onmouseover)=='function'){
				onmouseover = function(){}
			}
		}
	}
}
function AddEvent(){
    for(var i = 0; i < lobby.childNodes.length; i++){
		with(lobby.childNodes[i]){
			if(typeof(onmouseout)=='function'){
				onmousemove = function(){ObjectMove(this)};
				onmouseover = function(){this.style.border='solid 1px #000000';};
				onmousedown = function(){ObjectFocus(this)};
				onmouseup = function(){with(this){style.border='solid 1px #000000';releaseCapture();}};
			}
		}
	}
}
function OfferEvent(repairIds){
	var repairId = repairIds.split(',');
    for(var i = 0; i < lobby.childNodes.length; i++){
		with(lobby.childNodes[i]){
			if(typeof(onmouseout)=='function'){
				var addEvent = false;
				for(var j = 0;j < repairId.length;j++){
					if(lobby.childNodes[i].id.indexOf('d'+repairId[j]+'_') >= 0){
						addEvent = true;
						break;
					}
				}
				if(addEvent){
					onmousemove = function(){ObjectMove(this)};
					onmouseover = function(){this.style.border='solid 1px #000000';};
					onmousedown = function(){ObjectFocus(this)};
					onmouseup = function(){with(this){style.border='solid 1px #000000';releaseCapture();}};
				}else{
					onmousemove = function(){};
					onmouseover = function(){};
					onmousedown = function(){};
					onmouseup = function(){};
				}
			}
		}
	}
}
function RepairEvent(repairIds){
	var repairId = repairIds.split(',');
    for(var i = 0; i < lobby.childNodes.length; i++){
		with(lobby.childNodes[i]){
			if(typeof(onmouseout)=='function'){
				var addEvent = true;
				for(var j = 0;j < repairId.length;j++){
					if(lobby.childNodes[i].id.indexOf('d'+repairId[j]+'_') >= 0){
						addEvent = false;
						break;
					}
				}
				if(addEvent){
					onmousemove = function(){ObjectMove(this)};
					onmouseover = function(){this.style.border='solid 1px #000000';};
					onmousedown = function(){ObjectFocus(this)};
					onmouseup = function(){with(this){style.border='solid 1px #000000';releaseCapture();}};
				}else{
					onmousemove = function(){};
					onmouseover = function(){};
					onmousedown = function(){};
					onmouseup = function(){};
				}
			}
		}
	}
}

