// START: com_remiya_dialog
/*
 * var options = {
 *	    statusbar:false,
 *		x:"center",y:"center",w:400,h:300,modal:true,
 *		title:"Registration",content:data,status:"my status",
 *		resize_image:"images/dialog_resize.gif",
 *	    close_image:"images/dialog_close_13.gif"
 *	}
 *  var dialog = com.remiya.dialog(options).open();
 */
var com_remiya_dialog = function(options){
    if(com.remiya.simplest==undefined){alert("Please, include the file 'com.remiya.simplest.js'");return;}
	var inst = this;
	// Default options
    var d={
	    x:0,y:0,w:200,h:100,scroll:"auto",modal:false,
		titlebar:true,statusbar:true,maximizable:false,closeable:true,
	    title:"Title",
		content:"&nbsp;",status:"&nbsp;",
		title_background:"#3F3F3F",content_background:"white",status_background:"#EAEAEA",
		border:"2px solid #3F3F3F",
		close_image:"images/close.gif",min_image:"images/minimize.gif",max_image:"images/maximize.gif",restore_image:"images/restore.gif",
		resize_image:"images/resize.gif"		
	};
	if(options.id===undefined){d.id = com.remiya.simplest.rid(64);}
	// User options
    d = $.extend(d, options);
	this.dialog_template = function(){
	    if(d.x=="center"){var dim = com.remiya.simplest.size();d.x=dim.w/2-d.w/2;}
		if(d.y=="center"){var dim = com.remiya.simplest.size();d.y=dim.h/2-d.h/2;}
	    xhtml ="<table cellspacing='0' cellpadding='0' width='"+d.w+"' id='"+d.id+"' style='position:absolute;z-index: 100001;top:"+d.y+";left:"+d.x+";border:"+d.border+";height:"+d.h+";-moz-border-radius:5px; -webkit-border-radius:5px;';>";
		if(d.titlebar==true){xhtml+="<tr><td height='1' id='"+d.id+"_titlebar'>";xhtml+=this.titlebar_template();xhtml+="</td></tr>";}
		xhtml+="<tr><td valign='middle' id='"+d.id+"_content_wrapper' style='background:"+d.content_background+";'><div id='"+d.id+"_content' style='width:100%;height:100%;overflow:"+d.scroll+";text-align:center;'>"+d.content+"</div></td></tr>";
		if(d.statusbar==true){xhtml+="<tr><td height='1' id='"+d.id+"_titlebar'>";xhtml+=this.statusbar_template();xhtml+="</td></tr>";}
		xhtml+="</table>";
		if($.browser.msie)xhtml+="<iframe id='"+d.id+"_underlay' src='javascript:\"&lt;html&gt;&lt;/html&gt;\";' scrolling='no' frameborder='0' style='position:absolute;width:"+d.w+";height:"+d.h+";top:"+d.y+";left:"+d.x+";border:none;display:block;z-index:0'></iframe>";
		return xhtml;
	};
	this.id = function(){return d.id;}
	this.titlebar_template = function(){
	    xhtml=" <table cellspacing='0' cellpadding='1' width='100%' style='background:"+d.title_background+";color:white;font-size:12px;font-weight:bold;font-family:tahoma;'><tr>";
		xhtml+="   <td align='left' valign='middle' id='"+d.id+"_title' onmouseover='this.style.cursor=\"move\"'>&nbsp;"+d.title+"</span></td>";
		if(d.minimizable==true)xhtml+="<td align='left' width='1' valign='middle' style='padding:2px 2px 2px 2px;'><img class='titlebar_button' src='"+d.min_image+"' onclick='com_remiya_dialog({id:\""+d.id+"\"}).minimize()' onmouseover='this.style.cursor=\"pointer\";$(this).css(\"opacity\",1);' onmouseout='this.style.cursor=\"pointer\";$(this).css(\"opacity\",0.8);'></td>";
		if(d.maximizable==true)xhtml+="<td align='left' id='"+d.id+"_maximize' width='1' valign='middle' style='padding:2px 2px 2px 2px;'><img class='titlebar_button' src='"+d.max_image+"' onclick='com_remiya_dialog({id:\""+d.id+"\"}).maximize()' onmouseover='this.style.cursor=\"pointer\";$(this).css(\"opacity\",1);' onmouseout='this.style.cursor=\"pointer\";$(this).css(\"opacity\",0.8);'></td>";
		if(d.closeable==true)xhtml+="<td align='left' width='1' valign='middle' style='padding:2px 2px 2px 2px;'><img class='titlebar_button' src='"+d.close_image+"' onclick='com_remiya_dialog({id:\""+d.id+"\"}).close()' onmouseover='this.style.cursor=\"pointer\";$(this).css(\"opacity\",1);' onmouseout='this.style.cursor=\"pointer\";$(this).css(\"opacity\",0.8);'></td>";
		xhtml+=" </tr></table>";
		return xhtml;
	}
	this.statusbar_template = function(){
	    xhtml=" <table cellspacing='0' cellpadding='1' width='100%' style='background:"+d.status_background+";border-top:2px outset #EAEAEA;font-family:tahoma,verdana;'><tr>";
		xhtml+=" <td id='"+d.id+"_statusbar' valign='middle' style='text-align:left;height:18px;font-size:11px;'>"+d.status+"</td>";
		xhtml+=" <td id='"+d.id+"_resize' width='10' style='background-image:url(\""+d.resize_image+"\");background-repeat:no-repeat;background-position:bottom right;' onmouseover='this.style.cursor=\"nw-resize\"'></td>";
		xhtml+=" </tr></table>";
		return xhtml;
	}
	
	// In Firefox the content expands and goes out of the bottom of the page
	this._repair_scroll = function(){
		var wh = $("#"+d.id+"_content_wrapper").css("height");
		if(parseInt(wh)>d.h){
		    var tb = parseInt($("#"+d.id+"_title").css("height"));
		    var sb = parseInt($("#"+d.id+"_statusbar").css("height"));
		    var new_height = d.h - tb - sb;
			var content = $("#"+d.id+"_content").html();$("#"+d.id+"_content").html("&nbsp;");
			$("#"+d.id+"_content").css("height",new_height).css("overflow",d.scroll).css("height",new_height);
			//$("#"+d.id+"_content_wrapper").css("background","yellow").css("height",new_height);
			$("#"+d.id+"_content").html(content);
		}
	}
	this.open = function(modal){
	    if(d.modal==true){
		    var dim = com.remiya.simplest.size();
		    var modal = "<div id='"+d.id+"_modal' style='position:absolute;z-index: 100000;top:0px;left:0px;background:white;width:"+dim.w+";height:"+dim.h+"';overflow:hidden;>&nbsp;</div>";
			if($.browser.msie)modal+="<iframe id='"+d.id+"_modal_underlay' src='javascript:\"&lt;html&gt;&lt;/html&gt;\";' scrolling='no' frameborder='0' style='position:absolute;width:"+dim.w+";height:"+dim.h+";top:0;left:0;border:none;display:block;z-index:0'></iframe>";
		    $('body').append(modal);
			$("#"+d.id+"_modal").css("opacity",0.3);
			if($.browser.msie)$("#"+d.id+"_modal_underlay").css("opacity",0);
			$('body').append(this.dialog_template());			
		}else{
		    $('body').append(this.dialog_template());
		}
		
		if($.browser.mozilla)this._repair_scroll();
				
		$(".titlebar_button").each(function(){$(this).css("opacity",0.8);});
		
		$("#"+d.id+"_title").mousedown(function(e){
		    var w = parseInt($("#"+d.id).css("width").replace("px",""));var l = parseInt($("#"+d.id).css("left").replace("px",""));
			var h = parseInt($("#"+d.id).css("height").replace("px",""));var t = parseInt($("#"+d.id).css("top").replace("px",""));
		    var x = e.pageX;var y = e.pageY;
			var offset = x-l;
			$(document).mousemove(function(e){
		        $("#"+d.id).css("left",e.pageX-12-offset);
				$("#"+d.id).css("top",e.pageY-12);
				if($.browser.msie)$('#'+d.id+'_underlay').css("left",e.pageX-12-offset);
				if($.browser.msie)$('#'+d.id+'_underlay').css("top",e.pageY-12);
		        e.stopPropagation(); window.event.cancelBubble = true;
		        e.preventDefault();  window.event.returnValue = false;
	        });
		    $(document).mouseup(function(e){
		        $(document).unbind();
	        });
			
		});
		$("#"+d.id+"_title").mouseup(function(){
		    $(document).unbind();
		});
		$("#"+d.id+"_resize").mousedown(function(){
			$(document).mousemove(function(e){
			    var w = parseInt($("#"+d.id).css("width").replace("px",""));var l = parseInt($("#"+d.id).css("left").replace("px",""));
				var h = parseInt($("#"+d.id).css("height").replace("px",""));var t = parseInt($("#"+d.id).css("top").replace("px",""));
				var x = e.pageX;var y = e.pageY;
				var tl = x-l-w+w;var tt = y-t-h+h;
			    $("#"+d.id).css("width",tl);
				$("#"+d.id).css("height",tt);
				if($.browser.msie)$('#'+d.id+'_underlay').css("width",tl);
				if($.browser.msie)$('#'+d.id+'_underlay').css("height",tt);
		        e.stopPropagation(); window.event.cancelBubble = true;
		        e.preventDefault();  window.event.returnValue = false;
	        });
		    $(document).mouseup(function(e){
		        $(document).unbind();
	        });
			
		});
		$("#"+d.id+"_resize").mouseup(function(){
		    $(document).unbind();
		});
		$("#"+d.id).blur(function(e){$(document).unbind(); });
		return this;
	};
	this.close = function(){
	    $('#'+d.id).remove();
        if($.browser.msie)$('#'+d.id+'_underlay').remove();
		$('#'+d.id+'_modal').remove();
		if($.browser.msie)$("#"+d.id+"_modal_underlay").remove();
		return this;
	};
	this.restore = function(x,y,w,h){
	    $('#'+d.id).css("top",y);$('#'+d.id).css("left",x); $('#'+d.id).css("width",w);$('#'+d.id).css("height",h);
		if($.browser.msie)$('#'+d.id+'_underlay').css("top",y);$('#'+d.id+'_underlay').css("left",x);
		if($.browser.msie)$('#'+d.id+'_underlay').css("width",w);$('#'+d.id+'_underlay').css("height",h);
		//$('#'+d.id+'_maximize').html("<img src='"+d.max_image+"' onclick='com_remiya_dialog({id:\""+d.id+"\"}).maximize()' onmouseover='this.style.cursor=\"pointer\"'>");
	};
	this.minimize = function(){
	    var y=$('#'+d.id).css("top"); var x =$('#'+d.id).css("left"); var w =$('#'+d.id).css("width"); var h =$('#'+d.id).css("height");
	    var dim = com.remiya.simplest.size();		
	    //$('#'+d.id).css("top",0);$('#'+d.id).css("left",0);
		//$('#'+d.id).css("width",200);$('#'+d.id).css("height",100);
		$('#'+d.id+'_content_wrapper').parent().css("display","none");
		//if($.browser.msie)$('#'+d.id+'_underlay').css("top",0);$('#'+d.id+'_underlay').css("left",0);
		//if($.browser.msie)$('#'+d.id+'_underlay').css("width",dim.w);$('#'+d.id+'_underlay').css("height",dim.h);
		//$('#'+d.id+'_maximize').html("<img src='"+d.min_image+"' onclick='com_remiya_dialog({id:\""+d.id+"\"}).restore(\""+x+"\",\""+y+"\",\""+w+"\",\""+h+"\")' onmouseover='this.style.cursor=\"pointer\"'>");
	};
	this.maximize = function(){
	    var y=$('#'+d.id).css("top"); var x =$('#'+d.id).css("left"); var w =$('#'+d.id).css("width"); var h =$('#'+d.id).css("height");
	    var dim = com.remiya.simplest.size();		
	    $('#'+d.id).css("top",0);$('#'+d.id).css("left",0);
		$('#'+d.id).css("width",dim.w);$('#'+d.id).css("height",dim.h);
		if($.browser.msie)$('#'+d.id+'_underlay').css("top",0);$('#'+d.id+'_underlay').css("left",0);
		if($.browser.msie)$('#'+d.id+'_underlay').css("width",dim.w);$('#'+d.id+'_underlay').css("height",dim.h);
		//$('#'+d.id+'_maximize').html("<img src='"+d.restore_image+"' onclick='com_remiya_dialog({id:\""+d.id+"\"}).restore(\""+x+"\",\""+y+"\",\""+w+"\",\""+h+"\")' onmouseover='this.style.cursor=\"pointer\"'>");
	};
	this.title = function(title){if($("#"+d.id).length==0){d.title = title;}else{$("#"+d.id+"_title").html("&nbsp;"+title+"&nbsp;");}return this;};
	this.scroll = function(scroll){ if($("#"+d.id).length==0){d.scroll = scroll;}else{$("#"+d.id+"_content").css("overflow",scroll);} return this; };
	this.load_url = function(aurl){
	    var img = new Image();img.src="images/loading.gif";
		this.content(img);
	    if($("#"+d.id).length==0){
		   $.ajax({type: "GET",url:aurl,data:"",dataType:"html",success: function(data){$("#"+d.id+"_content").html(data);} });
		}else{
		   $.ajax({type: "GET",url:aurl,data:"",dataType:"html",timeout:20000, success: function(data){$("#"+d.id+"_content").html(data);},error:function(e,msg){alert(msg);}   });
		}
		return this;
	};  
	this.h = function(h){
	    if($("#"+d.id).length==0){
		    d.h=h;
		}else{
		    $("#"+d.id).css("height",h);
            if($.browser.msie)$('#'+d.id+'_underlay').css("height",h);		
		}
		return this;
	};
	this.x = function(x){
		if($("#"+d.id).length==0){
			d.x=x;		
		}else{
			if(x=="center"){var dim = com.remiya.simplest.size();x=$(window).scrollLeft()+dim.w/2-d.w/2;}
			$("#"+d.id).css("left",x);
			if($.browser.msie)$('#'+d.id+'_underlay').css("left",x);
		}
		return this;
	};
	this.y = function(y){
		if($("#"+d.id).length==0){
			d.y=y;
		}else{
			if(y=="center"){var dim = com.remiya.simplest.size();y=$(window).scrollTop()+dim.h/2-d.h/2;}
			$("#"+d.id).css("top",y);
			if($.browser.msie)$('#'+d.id+'_underlay').css("top",y);
		}
		return this;
	};
	this.w = function(w){
	    if($("#"+d.id).length==0){d.w=w;}
		else{
		    $("#"+d.id).attr("width",w);if($.browser.msie)$('#'+d.id+'_underlay').css("width",w);
		}		
		return this;
	};
	this.content = function(content){
	   if($("#"+d.id).length==0){d.content = content;}else{$("#"+d.id+"_content").html(content);}
	   return this;	   
	};
	this.status = function(status){
	    if($("#"+d.id).length==0){d.status_text = "&nbsp;"+status_text;}else{$("#"+d.id+"_statusbar").html("&nbsp;"+status);}return this;
	};
	return this;
};
// END: com_remiya_dialog

// START: Insert to namespace
if(com.remiya.dialog==undefined)com.remiya.dialog = com_remiya_dialog;
// END: Insert to namespace
    
