// JavaScript Document
var ajaxURL_CD="/f/apps/card/card_ajax.php";
function GetXmlHttpObject()
{
	try
	{ // Firefox, Opera 8.0+, Safari
		var fm_xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{ // Internet Explorer
		try
		{
			fm_xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		 catch (e)
		{
			try 
			{
				fm_xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX, please try with another browser or on different computer!");
			}
		}
	}
	return fm_xmlHttp;
}
function fm_getOffsetTop(elm) 
{
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;	
	while(mOffsetParent)
	{
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}	
	return mOffsetTop;
}
function fm_getOffsetLeft(elm) 
{
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;

	while(mOffsetParent)
	{
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}
function fm_getYOffset() 
{
    var pageY;
    if(typeof(window.pageYOffset)=='number') 
	{	pageY=window.pageYOffset;}
    else {	pageY=document.documentElement.scrollTop;}
    return pageY;
}
function fm_getXOffset() 
{
    var pageX;
    if(typeof(window.pageXOffset)=='number') 
	{	pageX=window.pageXOffset;}
    else {	pageX=document.documentElement.scrollLeft;}
    return pageX;
}
function isArray(obj) {
    return obj.constructor == Array;
}
function fm_colse(id_name)
{
	document.getElementById(id_name).innerHTML='loading...';
	document.getElementById(id_name).style.display='none';
}
function fm_closeBox(id_name,not_move)
{
	if (not_move==-1)//move
		return "<div id='"+id_name+"_move' style='cursor:move;float:left;padding:2px' class='fm_btn'>Move</div><div align=right style='padding:0 0 2px 5px;'> <span onclick=\"fm_colse('"+id_name+"')\" style='cursor:pointer'>X</span></div>";
	else return "<div align=right style='padding:0 0 2px 5px;'> <span onclick=\"fm_colse('"+id_name+"')\" style='cursor:pointer;font-weight:bold'>X</span></div>";
		//return "<table cellspacing=0 cellpadding=0 border=0><tr><td><div id='"+id_name+"_move' style='cursor:move' class='fm_btn' style='padding:3px'>Move</div></td><td align=right style='padding:0 0 2px 5px;'> <img src='/images/close_window.gif' onclick=\"fm_colse('"+id_name+"')\" style='cursor:pointer'></td></tr></table>";
}
//id_name nothing, with value show close btn
//id_name -1 add value to innerHTML
function fm_getAjaxContent(obj,url,id_name,no_close)
{
	var fm_xmlHttp=GetXmlHttpObject();
	if (fm_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	url=url+"&y="+Math.random();
	fm_xmlHttp.onreadystatechange=function (){
		if (fm_xmlHttp.readyState==4 || fm_xmlHttp.readyState=="complete")
		{
			if (id_name==-1)	obj.innerHTML+=fm_xmlHttp.responseText;			
			else if (id_name && !no_close)	
				obj.innerHTML=fm_closeBox(id_name,0)+fm_xmlHttp.responseText;
			else	obj.innerHTML=fm_xmlHttp.responseText;
		}
	};
	fm_xmlHttp.open("GET",url,true);
	fm_xmlHttp.send(null);	
}
var Drag = {
	obj : null,move : null,
	init : function(o,target)
	{//target is the moving target instead of o, mousedown on o but move target
		Drag.move=o;
		Drag.move.onmousedown= Drag.start;	
		if (target)	Drag.obj=target;
		else Drag.obj=o;
	},

	start : function(e)
	{
		//var o =Drag.obj= this;
		e = Drag.fixE(e);
		var y = parseInt(Drag.obj.style.top);
		var x = parseInt(Drag.obj.style.left);
		Drag.obj.lastMouseX	= e.clientX;
		Drag.obj.lastMouseY	= e.clientY;
		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(Drag.obj.style.top);
		var x = parseInt(Drag.obj.style.left);
		var nx, ny;

		nx = x + ex - Drag.obj.lastMouseX;
		ny = y + ey - Drag.obj.lastMouseY;

		Drag.obj.style.left = nx + "px";
		Drag.obj.style.top= ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;		
		ajax_boxStartingTop=parseInt(Drag.obj.style.top);
		ajax_boxStartingLeft=parseInt(Drag.obj.style.left);
	},

	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;
	}
};
//type 0 box showing right side cur obj, 1 same left and top as cur obj
//array show in the middle of the page 0 is width 1 is height
//array if the 3rd element is 1, width and height is related to the top left point of the current obj
//type <0 box showing lef side cur obj, abs(type) is the width of cur obj
//if url =-1 no ajax
//if no_close =2 no border and no close
function fm_contenBoxMaker(id_name,url,cur_obj,type,frame_html,no_close,is_grey_border)
{
	if (document.getElementById(id_name)==null)
	{
		var fm_frame=document.createElement('div');//frame
		document.body.appendChild(fm_frame);
		fm_frame.id=id_name;
		fm_frame.style.position="absolute";	
		fm_frame.style.textAlign="left";	
		fm_frame.style.backgroundColor="#FFFFFF";	
		if (no_close!=2)
		{
			if (is_grey_border)
			{
				fm_frame.style.border="2px solid #CCCCCC";	
				fm_frame.style.padding="10px";
			}
			else
			{
				fm_frame.style.padding="5px";
				fm_frame.style.border="5px solid #f17d7e";	
			}			
		}
		fm_frame.style.zIndex="3000";
	}		
	else	var fm_frame=document.getElementById(id_name);
	//set size
	if (isArray(type))	
	{
		if (type[2])	fm_frame.style.top=(fm_getOffsetTop(cur_obj)+type[1])+"px";		
		else	fm_frame.style.top=(fm_getYOffset()+(document.documentElement.clientHeight-type[1])/2)+"px";		
	}
	else	fm_frame.style.top=fm_getOffsetTop(cur_obj)+"px";	
	
	if (isArray(type))	
	{
		if (type[2])	fm_frame.style.left=(fm_getOffsetLeft(cur_obj)+type[0])+"px";		
		else	fm_frame.style.left=(fm_getXOffset()+(document.documentElement.clientWidth-type[0])/2)+"px";
	}
	else if (type<0)	fm_frame.style.left=(fm_getOffsetLeft(cur_obj)-Math.abs(parseInt(type))-15)+"px";
	else if (type==1)	fm_frame.style.left=fm_getOffsetLeft(cur_obj)+"px";
	else	fm_frame.style.left=(fm_getOffsetLeft(cur_obj)+cur_obj.offsetWidth+5)+"px";

	fm_frame.style.display="block";
	if (url==-1)	
	{
		fm_frame.innerHTML='';
		if (no_close<1)	fm_frame.innerHTML=fm_closeBox(id_name,no_close);
		fm_frame.innerHTML+=frame_html;
		if (no_close==-1) 
			Drag.init(document.getElementById(id_name+"_move"),document.getElementById(id_name));
	}
	else
	{
		fm_frame.innerHTML="<center>Loading...</center>";
		fm_getAjaxContent(fm_frame,url,id_name,no_close);
	}
	
}
/* end of contentBoxMaker and getAjaxContent*/
function addCommnetBox(obj,id,width,height,edit)
{
	fm_contenBoxMaker('print_view',-1,obj,new Array(-width,0,1),"<iframe  name='showing_card' src=\"/f/add_commnets.php?id="+id+"&edit="+edit+"\" frameborder=0 scrolling='no' allowtransparency='true' width='"+width+"px' height='"+height+"px'></iframe>",0,1);
}
function load_featured_site(ring_id,type)
{
	var obj=document.getElementById('show_featured_site');
	var fm_xmlHttp=GetXmlHttpObject();
	if (fm_xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	url='/f/featured_site/ajax_show.php?ring_id='+ring_id+'&type='+type;
	url=url+"&y="+Math.random();
	fm_xmlHttp.onreadystatechange=function (){
		if (fm_xmlHttp.readyState==4 || fm_xmlHttp.readyState=="complete")
		{
			if (fm_xmlHttp.responseText)
			{
				obj.innerHTML=fm_xmlHttp.responseText;
				obj.style.display="block";
			}
		}
	};
	fm_xmlHttp.open("GET",url,true);
	fm_xmlHttp.send(null);	
}