// JavaScript Document


function $  ($id){
	return document.getElementById($id); 
}
function mktime ()
{
	var $dobj= new Date ();
	return $dobj.getTime();
}
function datetime ($timestamp)
{
	var d = new Date();
	d.setTime($timestamp*1000);
	return (d.getFullYear()+ '/' + (d.getMonth()+1)+ '/' + d.getDate() + ' ' + d.getHours()+':' + d.getMinutes()+':' + d.getSeconds());

}
function datetime_date ($timestamp){
	var d = new Date();
	//alert($timestamp);
	d.setTime($timestamp*1000);
	return (d.getFullYear()+ '/' + (d.getMonth()+1)+ '/' + d.getDate());
}
function trim ($str){
	//return $str.replace("\r","").replace("\n","<br />");
	return $str.replace(/(^\s*)|(\s*$)/g, "");
}
function uri_q($arr){
	var $uri;
	var $uri_arr=[];
	$uri_arr.push('q_time='+mktime());
	for(var $i in $arr){
		$uri_arr.push($i+'='+encodeURIComponent($arr[$i]));
	}	
	return $uri_arr.join('&');
}

// customized typeOf, by Douglas Crockford
function typeOf(value) {
	var s = typeof value;
	if (s === 'object') {
		if (value) {
			if (value instanceof Array) {
				s = 'array';
			}
		} else {
			s = 'null';
		}
	}
	return s;
}

user={
	list:{},
	ssi:'index.php', //server side interface
	check_list:{},
	check:function($datas,$hs,$hf){

		j.ajax({
			url:this.ssi+'?a=user_check&view=json&'+uri_q($datas),  dataType:'json',
			success:function($json){
				if ($json['return']){
					if ($json['user']){
						user.check_list[$json['user']['id']]=$json['user'];
					}else{
						user.check_list[$json['id']]=$json['reason'];
					}
				}
				if ($hs) { $hs($json); }
			}
		});


	}	

};

ui={
	el:{},
	combo_box:function($selector,$data,$cfg){
		ui.el[$selector]=this;
		if ($data){
			this.data=$data;	
		}else{
			this.data=[];
		}
		//this.test='123';
		var $this=this;
		this.target=j($selector);
		this.set_position=function(){
			var $offset=this.target.offset();
			this.list_box.css({
				'position':'absolute',
				'left':$offset.left+'px',
				'top':($offset.top+this.target.height())+'px'
			});

		}
		this.toggle=function(){
			this.set_position();
			this.list_box.toggle();
		}
		this.render=function($data2){
			this.list_box=j("<div></div>").appendTo('body').addClass('combo_box_list').width(this.target.width()).hide();

			this.set_position();

			if ($data2){
				this.data=$data2;
			}
			var $html='';
			for (var $i in this.data){
				$html+='<a href="#">'+this.data[$i]+'</a>';

			}
			if ($html){
				//alert($html);	
			}else{

			}
			this.list_box.html($html);
			this.items=this.list_box.children('a').click(function(){
				j($selector).val(j(this).text());
				$this.toggle();
				if ($this.cb){
					$this.cb(j(this).text());	
				}
				return false;
			});

		}
		if ($cfg){
			if($cfg['btn'])	{
				this.btn=j($cfg['btn']).click(function(){

					$this.toggle();
					return false;
				});
			}
			if($cfg['cb'])	{
				this.cb=$cfg['cb'];
			}
		}


	}	

};

