﻿function any_post_it_fDragging(obj, e, limit)
{
	//window.status = obj.id;

	if(typeof(obj) == "string")
		obj = document.getElementById(obj);

	if(obj.style.position != "absolute") obj.style.position = "absolute";
	if(!obj.style.left) obj.style.left = "1px";
	if(!obj.style.top) obj.style.top = "1px";

	//AllDIV_toDown(); 

	//obj.style.zIndex = 10;

	_SFCS_Object_AnyPostIt.BubbleUP(obj.id);

	if(!e) e=window.event;

	var x=parseInt(obj.style.left);
	var y=parseInt(obj.style.top);

	var x_=e.clientX-x;
	var y_=e.clientY-y;


	if(document.addEventListener)
	{
		document.addEventListener('mousemove', inFmove, true);
		document.addEventListener('mouseup', inFup, true);
		document.addEventListener('onselectstart', notSelect, true);
	}
	else if(document.attachEvent)
	{
		document.attachEvent('onmousemove', inFmove);
		document.attachEvent('onmouseup', inFup);
		document.attachEvent('onselectstart', notSelect);
	}

	inFstop(e);
	inFabort(e);

	function notSelect(e)
	{
		return false;
	}

	function inFmove(e)
	{
		if(!e)e=window.event;

		if(limit)
		{
			var op=obj.parentNode; //抓父節點...(用來設定 邊界範圍)

			var opX=parseInt(op.style.left);
			var opY=parseInt(op.style.top);

			if( (e.clientX - x_) < 0 ) return false;
			else if( (e.clientX - x_ + obj.offsetWidth + opX) > (opX + op.offsetWidth) ) return false;

			if( (e.clientY - y_) < 0 ) return false;
			else if( (e.clientY-y_+obj.offsetHeight+opY) > (opY+op.offsetHeight)) return false;
		}

		obj.style.left=e.clientX-x_+'px';
		obj.style.top=e.clientY-y_+'px';

		inFstop(e);
	}

	function inFup(e) //清除事件...
	{
		if(!e)e=window.event;
	
		if(document.removeEventListener)
		{
			document.removeEventListener('mousemove', inFmove, true);
			//document.removeEventListener('mousemove', myEvent, true);
			document.removeEventListener('mouseup', inFup, true);
			document.removeEventListener('onselectstart', notSelect, true);
		}
		else if(document.detachEvent)
		{
			document.detachEvent('onmousemove', inFmove);
			//document.detachEvent('onmousemove', myEvent);
			document.detachEvent('onmouseup', inFup);
			document.detachEvent('onselectstart', notSelect);
		}

		inFstop(e);
	}

	function inFstop(e)
	{
		//var x=parseInt(obj.style.left);
		//var y=parseInt(obj.style.top);

		//document.getElementById("mymsg").innerHTML = x + ' : ' + y;
		//document.getElementById("mymsg").innerHTML = obj.style.left + ' : ' + obj.style.top;

		_SFCS_Object_AnyPostIt.SavePosition();
		//_SFCS_Object_AnyPostIt.setCookie(_SFCS_Object_AnyPostIt.cookieName, "xxx", 30);

		if(e.stopPropagation) return e.stopPropagation();
		else return e.cancelBubble=true;
	}

	function inFabort(e)
	{
		if(e.preventDefault) return e.preventDefault();
		else return e.returnValue=false;
	}

	function dddt()
	{
		alert(11);
	}
}

function SFCS_Class_AnyPostIt()
{
	this.arrayTags = new Array(); //要變更屬性的 Tags
	this.arrayTagIDs = new Array(); //Tag的ID名稱...

	this.cookieName = "SFCS_Class_AnyPostIt_001"; //cookieName(如果同一頁有2組物件, 要另外指定其名稱)

	this.count=0; //計數器(總數)

	this.error = false;//建立的過程是否有發生錯誤...

	this.defaultInit(); //初始化(利用window.onload, 等到最後才執行)
}

SFCS_Class_AnyPostIt.prototype.CreatePostIt=function(TagID) //建立PostIt!!
{
	//appendChild(
}

SFCS_Class_AnyPostIt.prototype.AppendPostIt=function(TagID) //將現有的PostIt加入控制...
{
	if(this.error) return false;

	//儲存Obj以及ID名稱!!
	tempTag = document.getElementById(TagID);
	if(tempTag)
	{
		this.arrayTagIDs[this.count] = TagID;
		this.arrayTags[this.count] = tempTag;
	}
	else
	{
		this.error = true;
		alert("TagID not in page!!:[" + TagID + "]");
	}

	if(!this.error) this.count++; //如果沒有發生問題, 就把計數器加1
}

//往上浮...
SFCS_Class_AnyPostIt.prototype.BubbleUP=function(TagID)
{
	if(this.error)
	{
		alert(" Sorry!!\n some \"HTML TAG\" not be found !!\n Please Check \"HTML TAG\"");
		return false;
	}

	//全部往下移...
	for(i = 0; i < this.arrayTags.length; i++)
	{
		if(this.arrayTags[i].style.zIndex > 0)
			this.arrayTags[i].style.zIndex = this.arrayTags[i].style.zIndex - 1;
	}

	for(i = 0; i < this.arrayTagIDs.length; i++)
	{
		if(this.arrayTagIDs[i] == TagID)
		{
			this.arrayTags[i].style.zIndex = 500;
			break;
		}
	}
}

SFCS_Class_AnyPostIt.prototype.SavePosition=function()
{
	tempPositionStr = "";

	for(i = 0; i < this.arrayTags.length; i++)
	{
		if(i > 0)
			tempPositionStr += ":" + this.arrayTagIDs[i] + "," + this.arrayTags[i].style.zIndex + "," + this.arrayTags[i].style.left + "," + this.arrayTags[i].style.top;
		else
			tempPositionStr += this.arrayTagIDs[i] + "," + this.arrayTags[i].style.zIndex + "," + this.arrayTags[i].style.left + "," + this.arrayTags[i].style.top;
	}

	this.setCookie(_SFCS_Object_AnyPostIt.cookieName, tempPositionStr, 30);
}

//cookie控制(設定cookie值) 用來記錄每個人 個別看到的結果...
SFCS_Class_AnyPostIt.prototype.setCookie = function(cookieName,cookieValue,nDays) //nDays:有效天數
{
    var today = new Date();
    var expire = new Date();
    if (nDays != null && nDays > 0)
    {
        expire.setTime(today.getTime() + 3600000*24*nDays);
        document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toUTCString();
    } else {
        document.cookie = cookieName + "=" + escape(cookieValue);
    }
}

//cookie控制(取得cookie值)
SFCS_Class_AnyPostIt.prototype.getCookie = function(cookieName)
{
    var allcookies = document.cookie;
    cookieName += "=";
    var pos = allcookies.indexOf(cookieName);
    if( pos != -1)
    {
        var start = pos + cookieName.length;
        var end = allcookies.indexOf(";",start);
        if(end == -1) end = allcookies.length;
        return unescape(allcookies.substring(start,end));
    }
}

SFCS_Class_AnyPostIt.prototype.getTagID_index=function(TagID)
{
	for(i = 0; i < this.arrayTagIDs.length; i++)
	{
		if(this.arrayTagIDs[i] == TagID)
		{
			return i;
		}
	}
}


/*
SFCS_Class_AnyPostIt.prototype.notSelectfunction(e)
{
	return false;
}
*/

/*
SFCS_Class_AnyPostIt.prototype.inFmove=function(e)
{
	if(!e)e=window.event;
	limit = false;

	if(limit)
	{
		var op=obj.parentNode; //抓父節點...(用來設定 邊界範圍)

		var opX=parseInt(op.style.left);
		var opY=parseInt(op.style.top);

		if( (e.clientX - x_) < 0 ) return false;
		else if( (e.clientX - x_ + obj.offsetWidth + opX) > (opX + op.offsetWidth) ) return false;

		if( (e.clientY - y_) < 0 ) return false;
		else if( (e.clientY-y_+obj.offsetHeight+opY) > (opY+op.offsetHeight)) return false;
	}

	obj.style.left=e.clientX-x_+'px';
	obj.style.top=e.clientY-y_+'px';

	inFstop(e);
}
*/

//初始化時, 要做的事...
SFCS_Class_AnyPostIt.prototype.myInit=function()
{
	//window.status = 

	if(this.error)
	{
		alert(" Sorry!!\n some \"HTML TAG\" not be found !!\n Please Check \"HTML TAG\"");
		return false;
	}

/*
	if(this.getCookie(this.cookieName))
	{
		var arr1 = this.getCookie(this.cookieName).split(":");
		for(i = 0; i < arr1.length; i++)
		{
			var arr2 = arr1[i].split(",");

			try { //加上try catch, 預防後續修改程式, 或cookie變更時發生異常...
				var objIndex = this.getTagID_index(arr2[0]);
				this.arrayTags[objIndex].style.zIndex = arr2[1];
				this.arrayTags[objIndex].style.left = arr2[2];
				this.arrayTags[objIndex].style.top = arr2[3];
			} catch	(e) {}
		}
	}
*/

//	var self = this;

	//顯示postIt 並設定事件...
	for(i = 0; i < this.arrayTags.length; i++)
	{
		this.arrayTags[i].style.display = "block";

/*
		if(this.arrayTags[i].addEventListener)
		{
			//this.arrayTags[i].addEventListener('mousemove', inFmove, true);
			//this.arrayTags[i].addEventListener('mouseup', inFup, true);
			//this.arrayTags[i].addEventListener('onselectstart', notSelect, true);
		}
		else if(document.attachEvent)
		{
			this.arrayTags[i].attachEvent('onmousemove', function() { self.inFmove; } );
			this.arrayTags[i].attachEvent('onmouseup', function() { self.inFup; } );
			this.arrayTags[i].attachEvent('onselectstart', function() { self.notSelect' } );
		}
*/
	}
}

SFCS_Class_AnyPostIt.prototype.defaultInit=function()
{
	//待網頁載入完畢後才執行真正的init!!!

	var self = this;

	if(window.addEventListener) //FireFox
	{
		window.addEventListener('load', function() { self.myInit(); }, false);
	}
	else if(window.attachEvent) //IE
	{
		window.attachEvent('onload', function() { self.myInit(); });
	}
}

var _SFCS_Object_AnyPostIt = new SFCS_Class_AnyPostIt();
