var dialog = {
	show : function(dModule, sID, rfMod){
		this.dialogWidth 	= 580;
		this.dUtils 		= YAHOO.util.Dom;
		this.yTools			= YAHOO.Tools;
		this.dModule		= dModule;
		this.sID			= sID;
		this.rfMod 			= rfMod;
		this.addDialog();
	},
	
	getDialogNode : function(iHtml){
		var dhNode = document.createElement("div");
		dhNode.setAttribute("id", "dialogHolder");
		dhNode.style.height = (this.dUtils.getDocumentHeight()+600)+"px";
		dhNode.style.width = this.dUtils.getDocumentWidth()+"px";
		dhNode.style.zIndex = "100";
		this.dUtils.addClass(dhNode, "dialogHolder");
		this.dUtils.insertAfter(dhNode, this.dUtils.get("mainStage"));
		
		var dNode = document.createElement("div");
		dNode.setAttribute("id", "dialogBox");
		dNode.style.width = this.dialogWidth+"px";
		dNode.style.top  = ((this.dUtils.getViewportHeight()+this.dUtils.getDocumentScrollTop()-100)/2)+"px";
		dNode.style.left = ((this.dUtils.getDocumentWidth()/2)-(this.dialogWidth/2))+"px";
		dNode.style.zIndex = "101";
		dNode.innerHTML = iHtml;
		this.dUtils.addClass(dNode, "dialogBox");				
		return dNode;
	},
	
	addDialog : function(dNode){
		if(this.dUtils.inDocument("dialogBox")){
			tmp = this.dUtils.get("dialogBox");
			document.body.removeChild(tmp);
		}
		
		this.getDialogModule();
		
		sDialog = this.getDialogTemplate();
		this.dUtils.insertAfter(this.getDialogNode(sDialog), this.dUtils.get("dialogHolder"));
	},
	
	hideDialog : function(){
		var tmp = this.dUtils.get("dialogHolder");
		document.body.removeChild(tmp);
		tmp = this.dUtils.get("dialogBox");
		document.body.removeChild(tmp);
	},
	
	getDialogTemplate : function(iHtml){
		var tpl = "<div class='header' onmousedown='drag.init(\"dialogBox\")'><a href='javascript:dialog.hideDialog()'>X</a></div>"+
				  "<div class='content' id='dContent'><img src='img/default/icons/loading.gif' alt='Loading' style='margin-left:130px;'/></div>"+
				  "<div class='footer'></div>";

		return tpl;
	},
	
	getDialogModule : function(){
		ax.callMethod('md_dialog', 'getDialog', this.dModule, 'dialog.setDialog', '0815_id', this.sID, this.rfMod);
	},
	
	sendRequest : function(md, mdmethod, sID, params, isJson){
		ax.callMethod(md, mdmethod, params, 'dialog.setInnerDialog', '0815_id', sID, this.rfMod, isJson);
	},
	
	sendFormRequest : function(md, mdmethod, sID, fKeys, feContainer){
		var tmp   = fKeys.split(":");
		var fName = tmp[0];
		tmp   = tmp[1].split(",");
		
		var aData = new Array();
		for(i=0;i<tmp.length;i++){
			aData[i] = document.forms[fName].elements[tmp[i]].value;
		}
		
		var jArr = this.buildJsonArray(aData);
		//alert(jArr);
		
		cbMethod = "dialog.setInnerDialog";
		if(feContainer != undefined){
			cbMethod += ":"+feContainer;
		}
		
		ax.callMethod(md, mdmethod, jArr, cbMethod, "0815_id", sID, this.rfMod, 'true');
	},
	
	setDialog : function(r){
		this.dUtils = YAHOO.util.Dom;
		tmp 		= this.dUtils.get("dContent");
		tmp.innerHTML = r;
	},
	
	setInnerDialog : function(r, feContainer){
		this.dUtils = YAHOO.util.Dom;
		
		if(feContainer != undefined){
			tmp 		= this.dUtils.get(feContainer);
		}else{
			tmp 		= this.dUtils.get("innerContent");
		}
		
		tmp.innerHTML = "<p>"+r+"</p>";
	},
	
	buildJsonArray : function(fields){
		var jString = "[";
		for(i=0; i<fields.length; i++){
			if(fields.length-1 != i){
				jString += "\""+base64.encode(fields[i])+"\",";
				continue;
			}
			
			jString += "\""+base64.encode(fields[i])+"\"";
		}
		jString += "]";
		
		return jString;
	}
	
};

var drag = {

	obj : null,

	init : function(oID, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o = document.getElementById(oID);
		o.onmousedown	= drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.ondragStart	= new Function();
		o.root.ondragEnd	= new Function();
		o.root.ondrag		= new Function();
	},

	start : function(e)
	{
		var o = drag.obj = this;
		e = drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.ondragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= drag.drag;
		document.onmouseup		= drag.end;

		return false;
	},

	drag : function(e)
	{
		e = drag.fixE(e);
		var o = drag.obj;
		
		drag.obj.style.MozOpacity 	= "0.5";
		
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		drag.obj.lastMouseX	= ex;
		drag.obj.lastMouseY	= ey;

		drag.obj.root.ondrag(nx, ny);
		return false;
	},

	end : function()
	{
		
		document.onmousemove = null;
		document.onmouseup   = null;
		drag.obj.root.ondragEnd(	parseInt(drag.obj.root.style[drag.obj.hmode ? "left" : "right"]),
									parseInt(drag.obj.root.style[drag.obj.vmode ? "top" : "bottom"]));
		
		drag.obj.style.MozOpacity 	= "1";
		
		drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
