function set_var(vars)
{
	return typeof(vars)!='undefined'&&vars!=''&&vars!=0&&vars!=false ? vars : '';
}
function is_element(id)
{
	return document.getElementById(id) ? 1 : 0;
}
function _G(id)
{
	return document.getElementById(id);
}

function redirect(url)
{
	window.location.replace(url);
}

function get_ext(path)
{
	return path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
}

function is_image(file)
{
	return $.inArray(get_ext(file),['jpg','gif','png','bmp','jpeg'])!=-1;
}

function preview(src,str,w,e)
{
	e = e || window.event;
	if(!w)	var w = 400;
	var elm = $('#img_preview');
	if(!src && !str)	return	elm.remove();
	if(!elm.attr('id'))	elm = $('<div></div>').attr('id','img_preview').appendTo('body');
	elm.empty().css({'overflow':'hidden','text-align':'left','line-height':'18px','background':'#999','border':'1px solid #333','padding':'5px'});
	
	if(is_image(get_ext(src)))
	{
		var oimg = $('<img/>').css({border:'none'}).attr('src',src).appendTo(elm);
		var loading = $('<img src="'+site_root+'stuff/img/default/loading.gif" border="0" />');
		var img = new Image();
		img.src=src;
		oimg.after(loading);
		
		oimg.attr('src','');
		$(img).ready(function()
		{
			var imgw = img.width,
				imgh = img.height;
			if(imgw>w)
			{
				if(imgw>imgh)
				{
					imgh = parseInt(w/imgw * imgh);
					imgw = w;
				}
				else
				{
					imgw = parseInt(w/imgh * imgw);
					imgh = w;
				}
			}
			else if(imgh>w)
			{
				imgw = parseInt(w/imgh * imgw);
				imgh = w;
			}
			loading.remove();
			oimg.attr('src',src).width(imgw).height(imgh).show();
			elm.width(imgw).xy(e);
		});
		$(img).error(function(){loading.remove();oimg.remove();});
	}
	if(str)
	{
		var img_w = src?elm.find('img').width():0;
		$('<p></p>').css({margin:0,padding:0}).html(decode(str)).appendTo(elm);
		elm.width(w).xy(e);
	}
}
function insert_img(path,maxwh){
	var str = '<img border="0" src="' + path + '" ';
	var img = new Image();
	img.src = path;
	var w = img.width;
	var h = img.height;
	if(maxwh && w>maxwh)
	{
		w = maxwh;
		h = maxwh/w * h;
	}
	if(maxwh && h>maxwh){
		h = maxwh;
		w = maxwh/h * w;
	}
	str += 'width="'+w+'" height="'+h+'"/>';
	return str;
}

function check_all(id,same)
{
	if(same)
	{
		(id ? $('#'+id+' input[type="checkbox"]') : $('form input[type="checkbox"]')).each(function(){$(this).attr('checked',(same ? true : false))});
	}
	else
	{
		(id ? $('#'+id+' input[type="checkbox"][name!="chkall"]') : $('form input[type="checkbox"][name!="chkall"]')).each(function(){$(this).attr('checked',($(this).attr('checked') ? false : true))});
	}
}
//jquery下显示采用display:block，这导致在火狐下的tbody格式乱套了
function show_hide(id)
{
	return $('#'+id).css('display',$('#'+id).css('display')=='none'?'':'none');
}

if(typeof(decode)!='function')
{
	function decode(str)
	{
		return decodeURIComponent(str).replace(/\+/g,' ');
	}
}

function set_tr_class(id)
{
	if(!id && is_element("mytable"))	id = 'mytable';
	if(!$('#'+id+' tr'))	return false;
	$("#"+id+" tr:odd[class!='title'][class!='tfoot']").addClass("altbg1");
	$("#"+id+" tr:even[class!='title'][class!='tfoot']").addClass("altbg2");
	var cl;
	$("#"+id+' tr').each(function(k){
		$(this).hover(
			function()
			{
				$(this).addClass("altbg3");
			},
			function ()
			{
				$(this).removeClass("altbg3");
			}); 
	});
}
function set_td_class(id)
{
	if(!id && is_element("mytable"))	id = 'mytable';
	if(!$('#'+id+' td'))	return false;
	$('#'+id+' td[class!="tfoot"][class!="header"]').each(function(k){
		if($(this).attr('colspan')>1)	return true;
		if(k%2==0)	$(this).addClass('subject');
	});
}
//在ie6下,select的级别最高，通常情况下所有弹出的窗口总是在选择框之下
function toggle_select()
{
	$('select').each(function(){$(this).toggle()});
}
function top_window()
{
	var tw = window.parent;
	if(window==tw)	return window;
	while(tw && tw.parent!=tw)
	{
		try
		{
			if ( tw.parent.document.domain != document.domain )	break ;
			if ( tw.parent.document.getElementsByTagName( 'frameset' ).length > 0 )	break ;
		}
		catch ( e )	{break ;}
		tw = tw.parent ;
	}
	return tw;
}
//顶层的载入图片
function show_loading(id)
{
	top_window().$('#loading').show();
	if(id)	top_window().$('#loading').html('<img src="'+site_root+'stuff/img/default/loading'+id+'.gif" align="absmiddle" />');
}
function hide_loading()
{
	top_window().$('#loading').hide();
}

function show_msg(msg,time)
{
	top_window().$('#message').show().html('<p><span class="icon"></span>'+msg+'</p>');
	if(time)	hide_msg(time);
}
function hide_msg(time)
{
	if(time)
	{
		setTimeout(function(){top_window().$('#message').empty().hide();},time);
	}
	else	top_window().$('#message').empty().hide();
}

function is_check(id)
{
	var c = 0;
	id = id ? '#'+id : 'form';
	$(id+' input[type="checkbox"][name!="chkall"]').each(function(){if($(this).attr('checked')){c=1;return false;}});
	return c;
}
function datepicker()
{
	$(function() {
		$.datetimepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datetimepicker.regional['zh-CN']));
		$('.datepicker').datetimepicker({
			//numberOfMonths: 2,
			showButtonPanel: true,
			altFormat:'yy-mm-dd',
			dateFormat: 'yy-mm-dd',
			timeFormat: ' hh:ii:ss',
			changeMonth: true,
			changeYear: true,
			showOn: 'button', buttonImage: site_root+'stuff/img/icon/calendar.gif', buttonImageOnly: true
		});
	});
}

function png()
{
	if(!$.browser.msie) return;
	var vs = parseInt((navigator.userAgent.match(/msie\s+(\d+)/i))[1]);
	if(vs > 6)	return;
	$('img').each(
		function()
		{
			if(get_ext($(this).attr('src'))=='png')
			{
				$(this).css({'filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+$(this).attr('src')+")",'width':$(this).attr('width')+'px','height':$(this).attr('height')+'px'});
				$(this).attr('src',site_root+'stuff/img/admin/none.gif');
				
			}
		});
}

function make_select(arr,name,selectid,ext,first_option)
{
	var s = '<select name="'+name+'" '+(ext?ext:'')+'>'+(first_option?first_option:'');
	$.each(arr,function(i,n)
	{
		s += '<option value='+n+(n==selectid ? ' selected':'')+'>'+i+'</option>';
	});
	s += '</select>';
	return s;
}
function show_tip(id)
{
	$('#'+id).dialog('destroy').dialog({
		title:"小贴士",
		bgiframe: true,
		resizable:false,
		autoOpen: false,
		width:500
	}).dialog('open');
}
//根据图片的宽度等比例缩放某张图片
function resize_img(obj,w)
{
	var img_w = obj.width;	//图片的宽度
	var img_h = obj.height;	//图片的高度
	if(w && img_w>w)
	{
		obj.style.width = w+'px';
		obj.style.height = w/img_w*img_h+'px';
		obj.alt += ' [点击图片在新窗口打开]';
		obj.onclick = function(){window.open(this.src);}
		obj.ondblclick = function(){obj.style.display="none";}
		obj.style.cursor = 'pointer';
		//obj.align = 'right';
		return;
	}
}
