var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_safari = (userAgent.indexOf('webkit') != -1 || userAgent.indexOf('safari') != -1);
if(is_ie){
	var ie_version=navigator.appVersion.split(";"); 
	var trim_Version=ie_version[1].replace(/[ ]/g,"");
	var is_ie6 = navigator.appName == "Microsoft Internet Explorer" && trim_Version=="MSIE6.0";
}
var user_logined=false;

function inputTxtFocus(event){
	var it = $(event.target);
	var tmpTxt = it.attr("value");
	it.attr("value","");
	it.one("focusout",function(){
		if(it.attr("value") == ""){
			it.attr("value",tmpTxt);
			it.one("focusin",inputTxtFocus);
		}
	});
}
function setAutoHideTxt(input){
	input.one("focusin",inputTxtFocus);
}
function initGameSelecter(){
	if(!$.cookie)alert('undefined jquery-cookies.js');
	var gameids = $.cookie('last_play_ids');
	if(gameids == null){
		return;
	}
	gameids=gameids.split(',');
	
	for(var i=gameids.length-1;i>=0;i--){
		var game=gameids[i];
		var games=game.split(':');
		$('#game_selecter').append('<option value="'+games[0]+'">&nbsp;&nbsp;'+games[1]+'</option>');
	}
	$('#game_selecter').bind('change',selectGame);
}
function selectGame(){
	var id = $('#game_selecter').val();
	if(id == -1)return;
	var url='/game/play/'+id+'.html';
	window.location.href=url;
}
function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
function addFriend(url){
	$.get(url,null,addFriendBack);
}
function addFriendBack(data, textStatus){
	if(textStatus=="success"){
		if(data > 0){
			alert('添加好友成功!');
		}else{
			switch(data){
				case 0:
					alert('找不到相关用户,添加好友失败!');
					break;
				case -1:
					alert('找不到用户,也许TA还没有进行转接,添加好友失败!');
					break;
				case -2:
					alert('请登录后再添加好友!');
					break;
				default:
					alert('发生未知错误，添加好友失败!');
			}
		}
	}else{
		alert('服务器错误，添加好友失败!');
	}
}

function bookmark(){
	var title=document.title  
	var url=document.location.href  
	if (window.sidebar) window.sidebar.addPanel(title, url,"");  
	else if( window.opera && window.print ){  
	var mbm = document.createElement('a');  
	mbm.setAttribute('rel','sidebar');  
	mbm.setAttribute('href',url);  
	mbm.setAttribute('title',title);  
	mbm.click();}  
	else if( document.all ) window.external.AddFavorite( url, title);  
}

function setLastPlay(id,name){
	var gameids = $.cookie('last_play_ids');
	if(gameids == null){
		gameids=[];
	}else{
		gameids=gameids.split(',');
	}
	for(var i=0;i<gameids.length;i++){
		var ids=gameids[i];
		var iids=ids.split(':');
		var iid=iids[0];
		if(iid == id) gameids.splice(i,1);
	}
	gameids.push(id+':'+name);
	if(gameids.length > 20)gameids.shift();
	gameids=gameids.join(',');
	var time=3600*24*30;
	$.cookie('last_play_ids',gameids,{expires:time,path:'/'});
}

//google analytics
var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-7194785-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
