gOffsetTop = 100;
hidePopupWindow = 0;
hidePopupMenu = 0;
currentPopup = null;

function openPopupHelp(obj,trgt) {
	var pos = getAbsolutePos(obj);
	var end_pos_x = pos.x+'px';
	var end_pos_y = pos.y+'px';
	var start_pos_y = pos.y+50+'px';
	$("#"+trgt).css('left',end_pos_x).css('top',start_pos_y).css('opacity',0).css('display','block').animate({opacity:1,left:end_pos_x,top:end_pos_y},500);
}

function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
function getAbsolutePos(el)
{
   var r = { x: el.offsetLeft, y: el.offsetTop };
   if (el.offsetParent)
   {
       var tmp = getAbsolutePos(el.offsetParent);
       r.x += tmp.x;
       r.y += tmp.y;
   }
   return r;
}

function showWaitWindow() {
	var tp = parseInt(getBodyScrollTop())+350;
	$("#loadImageDiv").css('top',tp+'px').show();
}
function closeWaitWindow() {
	$("#loadImageDiv").hide();
}
function openDialog(obj)
{
	var parms = obj.getAttribute('params');
	if (parms != '' && parms != undefined)
	{
		showWaitWindow();
		var lv = parseInt($("#spanservice").attr('counts'))+1;
		$("#spanservice").attr('counts',lv);

		var mas = parms.split(';');
		var out = '';
		for (var i = 0; i < mas.length; i ++) {
			var arr = mas[i].split('=');
			out += arr[0]+": '"+arr[1]+"',";
		}
		var tp = parseInt(getBodyScrollTop())+gOffsetTop;
		out += "top: '"+tp+"px'";
		eval("$.post('/dialog/open.html',{"+out+"},showDialog)");
	}
}
function showDialog(data)
{
	closeWaitWindow();
	$("#spanservice").append(data);
}
function showPopup(data)
{
	closeWaitWindow();
	data = data.split('--==--');
	$("#spanservice").append(data[1]);
	return data[0];

}
function showTooltip(obj, text, off_x, off_y) {
	var dt = new Date();
	var id = "_"+dt.getHours()+dt.getMinutes()+dt.getSeconds()+dt.getMilliseconds()+"myID";
	$("#spanservice").append('<div class="tooltip" id="'+id+'"><div class="close" onclick="$(\'#'+id+'\').hide()"></div>'+text+'</div>');
	var pos = getAbsolutePos(obj);
	$("#"+id).css('left',pos.x+off_x).css('top',pos.y+off_y).css('opacity',0).css('display','block').animate({opacity:1,left:pos.x+off_x+10,top:pos.y+off_y},500);
	eval("setTimeout('$(\"#"+id+"\").slideUp()',3000)");
}
function hideDialog(id)
{
	if ($("#"+id).attr('dlgtype') == 'modal') {
		var m_cnt = parseInt($("#modal").attr('cnt'));
		m_cnt --;
		$("#modal").attr('cnt',m_cnt);
		if (!m_cnt)
			$("#modal").animate({opacity:0},500,function(){$(this).hide();});
	}
	$("#"+id).remove();
}
function show_popup() {
	if (currentPopup) {
		$("#"+currentPopup).remove();
		currentPopup = null;
	}
	var parms = $(this).attr('params');
	if (parms != '' && parms != undefined)
	{
		showWaitWindow();
		var mas = parms.split(';');
		var out = '';
		for (var i = 0; i < mas.length; i ++) {
			var arr = mas[i].split('=');
			out += arr[0]+": '"+arr[1]+"',";
		}
		var tp = $(this).offset();
		out += "top: '"+(tp.top+parseInt($(this).css('height')))+"px',";
		out += "left: '"+tp.left+"px'";
		eval("$.post('/dialog/popup.html',{"+out+"},function(data){currentPopup = showPopup(data);})");
	}
}
function hide_popup() {
	if (currentPopup) {
		hidePopupMenu = setTimeout('$("#'+currentPopup+'").remove()',500,function(){currentPopup=null;});
	}
}
function hide_help_popup() {
	if (currentPopup) {
		hidePopupMenu = setTimeout('$("#'+currentPopup+'").css("display","none")',500,function(){currentPopup=null;});
	}
}
function set_cur_popup() {
	clear_hide_popup();
	currentPopup = $(this).attr('id');
}
function clear_hide_popup() {
	if (hidePopupMenu) {
		clearTimeout(hidePopupMenu);
	}
}
$(function(){
//	$("#modal").css('opacity',0.5);
	$(".switcher").live('click', function() {	
		var trgt = $(this).attr('trgt');
		if (trgt != undefined && trgt != '')
			$("#"+trgt).toggle(500);//animate({height:'toggle'},1000);
	});
	$(".reloadcaptcha").live('click',function() {
		var trgt = $(this).attr('trgt');
		if (trgt != undefined) {         
			showWaitWindow();	
			$.post('/ajax/reload_captcha.html',{},function(data) {
				$("#"+trgt).attr('src',data);
				closeWaitWindow();
			});
		}
	}).live('mouseover',function() {
		$(this).css('cursor','pointer');
	});
	$(".popup").live('mouseover', show_popup);
	$(".popupmenu").live('click', show_popup);
	$(".div_popup").live('mouseout', hide_popup);
	$(".div_popup").live('mouseover', clear_hide_popup);
	$(".popup_help").live('mouseout', hide_help_popup);
	$(".popup_help").live('mouseover', set_cur_popup);
});

