function inputTips($this){
	var inputVal = $this.val();	// 取得input 默认值
	$this.focusin(function(){	// input 得到焦点，重置 input 值，并改变input 样式
		if($this.val() === inputVal){
			$this.val('').addClass('focus');
		}
	}).focusout(function(){
		if($this.val() === ''){
			$this.removeClass().val(inputVal);
		}
	});
}
$(function(){
	if($('input[type="text"]').length){	//input 默认值（提示语句）		
		$('input[type="text"]').each(function(){
			inputTips($(this));
		})
	};
	if($('#nav > li').length){
		$('#nav > li').hover(function(){
			$('p', this).stop(false, true).slideDown(300);
		}, function(){
			$('p', this).stop(false, true).slideUp(300);
		});		
	};
	if($('#dropLang').length){
		$('#dropLang').toggle(function(){	// 多语言选择菜单
			$(this).addClass('on').prev('p').slideDown(200);
		}, function(){
			$(this).removeClass('on').prev('p').slideUp(500);
		})	
	};


	//首页 游戏列表 点击 效果
	if($('#gameList > a').length){
		var antoplay = 0;
		var aI = 1;
		var aPlayCount = $('#gameList > a').length;
		var clickI = 0;	//记录点击次数
		var goAhead = 1;	//鼠标尚未点击，在鼠标一开后，自动播放可以继续
		antoplay = setInterval(function(){ playAuto();}, 5000);	// 5000 为 自动播放时间间隔：5 秒
		function playAuto(){	// 自动播放
			aI = $('#gameList > a.curt').index()+1 === aPlayCount ? 0:$('#gameList > a.curt').index()+1;
			showNextImage($('#gameList > a').eq(aI));
		}
		function showNextImage(clicked) {		
			var imgurl = clicked.attr('href');
			clicked.addClass('curt').siblings().removeClass();
			clicked.parent().next().text(clicked.attr('title'));
			if(clickI === 0){
				$('#gamePre > img').after('<img src="'+imgurl+'" style="display:none;" />');
			};
			$('#gamePre > img').eq(clickI%2).stop(false,true).fadeOut(600)
				.siblings('img').attr('src', imgurl).stop(false,true).fadeIn(800);
			clickI++;
			return false;		
		}
		$('#gamePre').hover(function(){
			clearInterval(antoplay);
			$('div', this).stop(false,true).fadeIn(300);
		}, function(){
			if(goAhead){	//如果鼠标尚未点击，自动播放继续
				antoplay = setInterval(function(){ playAuto();}, 5000);
			}
			$('div', this).stop(false,true).fadeOut(300);
		});		
		
		$('#gameList > a').click(function(){
			showNextImage($(this));
			goAhead = 0;	//鼠标点击过后，动画不在继续
			return false;
		});
	};
	
	if($('#drawer').length){ // 侧边栏新闻存档、支付方式介绍 展开/关闭 效果
		$('#drawer > dt').toggle(function(){
			$(this).addClass('on').next('dd').slideDown(100);
		}, function(){
			$(this).removeClass().next('dd').slideUp(200);
		});	
	};	
	if($('#QAlist').length) {// 帮助列表
		$('#QAlist > dt').click(function(){
			if($(this).hasClass('on')){
				$(this).removeClass('on');
				$(this).next('dd').slideUp(200);
			}else{
				$(this).addClass('on').siblings('dt').removeClass();
				$(this).next('dd').slideDown(300).siblings('dd').slideUp(200);
			}
		})
	};
		// contact 表单
	if($('.btForm').length){ btForm();};
	if($('.whichOne').length){	// gift 选择
		$('.whichOne > a').click(function(){
			selectTypes($(this));
			return false;			
		})	
	}
	// 图片淡入淡出
	if($("#toturCon li").length > 1){
		var t = n = 0, count = $("#toturCon li").length;
		//循环得出幻灯导航 1 2 3 ...
		var recmtNav = '<span id="prev">&nbsp;</span><i class="on">1</i>';
		for(i=2;i<=count;i++){
			recmtNav += '<i>'+i+'</i>';
		}
		recmtNav += '<span id="next">&nbsp;</span>';
		$("#toturNav").append(recmtNav).children('i').click(function() {
			var i = $(this).text() - 1;
			n = i;
			if (i >= count) return;
			$("#toturCon li").eq(i).fadeIn(800).siblings().fadeOut(500);
			$(this).addClass('on').siblings().removeClass('on');
		});
		// 导航上的上一个 下一个
		$("#toturNav > span").click(function(){
			t = this.id == "prev" ? n-1:n+1;
			t = t == count ? 0:t;
			$("#toturNav").children('i').eq(t).trigger('click');
		});
		// 图片上的 上一个 下一个
		$('#picNav > i').hover(function(){
			$(this).addClass('on');
		}, function(){
			$(this).removeClass('on');
		}).click(function(){
			var dir = $(this).text();
			$('#' + dir).trigger('click');
		})
	};
	if($('#addTopic').length){		
		$('#addTopic').click(function(){
			$this = $(this);
			$('#douse').fadeIn(200);
			focusBox($('#focusBox'));
			$('#focusCon').css('width',650).load('prikbord_new.html #creatPost', function(){
				$('#focusCon').prepend('<h2>'+$this.text()+'</h2>');
				boxZoomIn(650,$('#focusCon').height(),'aform');
				$('#topiccon').rte('../public/js/jquery.rte.iframe.css');
				btForm();	// 表单控件变形	
			});
		})
	}
//	if($('.replyTo').length){
//		$('.replyTo').click(function(){
//			var $this = $(this);
//			var replyToID = $this.parent().parent().attr('id').slice(4);
//			alert('现在回复ID为 ' + replyToID + ' 的评论');
//			$('#douse').fadeIn(200);
//			var authorInfo = '<div class="author">' + $this.parent().siblings().eq(0).html() + '</div>';
//			var comtCon = '<div class="comtCon">' + $this.parent().prev().html() + '</div>';
//			$('#douse').fadeIn(200);
//			focusBox($('#focusBox'));
//			$('#focusCon').css('width',650).html(authorInfo + comtCon + '<div id="replyTo"></div>')
//			.find('#replyTo').load('/eccky/html/prikbord_reply.html #replyForm', function(){
//				$('#focusCon').prepend('<h2>'+$this.text()+'</h2>');
//				boxZoomIn(650,$('#focusCon').height(),'aform');				
//				$('#comment').rte( __BASE_URL__  + '/js/jquery.rte.iframe.css');
//			});			
//		})
//	}
	window.onresize = function(){cenMid($('#focusBox'));}
	
//	if($('#file_upload').length){
//		initUploadify('file_upload');
//	};
	
	if($('#projComt').length){
		$('#projComt .toggleComt').show().toggle(function(){
			$(this).addClass('on').next('ul').hide().end().prev('p').hide().prev('div').hide();
			cenMid($('#focusBox'));
		}, function(){
			$(this).removeClass('on').next('ul').show().end().prev('p').show().prev('div').show();
			cenMid($('#focusBox'));
		})
	};
	if($('.atGallery').length){	//帖子页面 图片放大/缩小
		//Handle click event for main image from slidehsow at bottom of post
		$('.atGallery > a').click(function(){
			var $this = $(this);
			var thisGroup = $this.parent().children('a');
			var imgLength = $(this).siblings('a').length + 1;
			$('#douse').fadeIn(200);
			focusBox($('#focusBox'));
			$('<img src="' + $this.attr('href') + '" id="fullImg" />').load(function(){
				if($('#focusImg').height() + 40 > $(window).height()){	// set max-height size for the picture
					var toHeight = $(window).height() - 40;
					$(this).css('maxHeight', toHeight);
				} else {
					var toHeight = $('#focusImg').height();
				}
				boxZoomIn($('#focusImg').width(),toHeight,'img');
			}).appendTo($('#focusImg'));
		// add the close button for img view box, it's different with the reply_box_close
			$('<a href="javascript:void(0)" onclick="xFocusBox();" class="xTip" id="img_box_close"></a>').appendTo($('#focusImg'));
			//Handle children click event
			if(thisGroup.length > 1){
				var i = $this.index();
				$('#focusImg').append('<div id="slideNav"><a href="#" title="preview" id="Prev">&nbsp;</a><a href="#" title="next" id="Next">&nbsp;</a></div>');
				$('#slideNav > a').click(function(){
					i = this.id === 'Next' ? i+1 : i-1;
					i = i === imgLength ? 0 : i;
					var imgUrl = thisGroup.eq(i).attr('href');
					$('#focusImg').fadeOut(300, function(){
						$('#fullImg').attr('src', imgUrl);
					});
					return false;
				});
			}
			return false;
		});
	}	
	
});

// 关闭 focusBox 
function xFocusBox(){
	$('#focusBox').hide().css({width:'60px',height:'60px'});
	$('#douse').fadeOut(300);
	$('#focusImg').hide().html('');
}
// focusBox 居中
function cenMid(theBox){
	var windowW = $('body').width();
	var windowH = $(window).height();
	theBox.css('top',windowH/2-parseInt(theBox.height())/2-15+$(document).scrollTop() + 'px');
	theBox.css('left',windowW/2-parseInt(theBox.width())/2+'px');	
	$('#douse').height($(document).height());
}
// 打开弹出层
function focusBox($this){
	cenMid($this);
	$this.fadeIn(300);
	return false;
}
function boxZoomIn(toWidth, toHeight, type){
	var con = type === 'img' ? $('#focusCon') : $('#focusImg');
	var windowW = $('body').width();
	var windowH = $(window).height();
	toTop = windowH/2-toHeight/2-8+$(document).scrollTop();
	toLeft = windowW/2-(toWidth + 24)/2;
	$('#focusBox').animate({height:toHeight,width:toWidth,top:toTop,left:toLeft},800, function(){
		$('#focusImg').fadeIn();
		if(type==='aform'){
			initUploadify('file_upload');
		} else {
			$('#slideNav').children('a').height(toHeight);
		}
	});
}
// 分享到 ... 
function shareTo(site,$this){
	if($this.parent().attr('id')){
		var sUrl = location.href;
		var sTitle = $('#newTit').text();
	} else {
		var sUrl = __DOMAIN__ + $this.parent().prev().attr('href');
		var sTitle = $this.parent().prev().text();
	}
	//alert('分享到：' + site + '\n地址：' + sUrl + '\n内容：' + sTitle);
	if( site === 'twitter'){
		window.open('http://twitter.com/intent/tweet?status='
			+ sTitle
			+ ' '
			+ sUrl
			+ ' %23Eccky', site,
			'scrollbars=no,width=600,height=450,status=no,resizable=yes,left='
			+ (screen.width - 600) / 2 + ',top='
			+ (screen.height - 450) / 2);
	} else {
		window.open('http://www.addtoany.com/add_to/' + site + '?linkurl='
			+ sUrl
			+ '&type=page&linkname='
			+ sTitle
			+ '&linknote=&via=Eccky', site,
			'scrollbars=no,width=600,height=450,status=no,resizable=yes,left='
			+ (screen.width - 600) / 2 + ',top='
			+ (screen.height - 450) / 2);
	}
	
}
function selectTypes($this){
	$this.addClass('on').siblings().removeClass('on').parent().next('input').val($(this).index());
}
function insertImgs(response){
	$('#filePreview').append('<li><img src="'+ __BASE_URL__ + response +'"><input type=hidden value='+response+' name=attachment[]><a href="javascript:void(0);" class="delete">&nbsp;</a></li>');
}
function initUploadify(fileInputID){	// 初始化 图片上传插件
	$('#' + fileInputID).uploadify({
		'uploader'  : __BASE_URL__ + '/uploadify/uploadify.swf',
		'script'    : __BASE_URL__ + '/forum/imageupload',
		'sizeLimit'   : 512000,
		'multi'     : true,
		'wmode'     : 'transparent',
		'hideButton': true,
		'auto'      : 1,
		'onComplete': function(event, queueID, fileObj, response, data) {            
			fileObjName = fileObj.name;
			if(fileObjName.length > 16) fileObjName = fileObjName.substr(0,16) + '...';
			insertImgs(response);
			if($('#focusBox').is(":visible")){	// 如果是在弹出层，则重新定位 focusBox
				cenMid($('#focusBox'));
			}
		},
		'onError'	: function (event,ID,fileObj,errorObj) {
			if(errorObj.type === 'File Size'){
				alert(fileObj.name+' Error:  \nLimitted Size:' + errorObj.info/1024 + 'kb\nThis Picture Size:' + fileObj.size/1024 + 'kb');
				$('#' + fileInputID).uploadifyCancel(ID);
			}
		}
	});
	$('.delete').live('click', function(){
		$(this).parent().css('background', 'red');
		if(confirm('Are you sure to delete this picture?')){

			$(this).parent().remove();
		} else {
			$(this).parent().css('background', '#E5E5E5');
		};
	});
}
function btForm(){
	var flagInpt = 1;
	var inSelect = 0;
	$('.btForm input[type="radio"]').each(function(){	// 模拟input:radio
		var radio = $(this);
			radio.hide().after('<em class="radio"></em>');
		var radiobox = $(this).next('em');
		var input = radiobox.next('span').children('input');
		if(radio.attr('checked')){
			radioChecnked(radio,radiobox);
		};			
		radiobox.click(function(){	// 模拟 box 被点击后，将 相应的 input:radio 值更改
			var radioName = $(this).prev().attr("name");
			$('input[name="'+ radioName +'"]').next('em').removeClass('checked');
			radioChecnked(radio,radiobox);
			if(flagInpt && input.length){	// 判断 input 是否存在 （登陆状态下可能不会有） 及其是否已经得到焦点
				input.focus();
			}
		});
		radio.prev('label').click(function(){	// IE 7,8 需要如此
			radiobox.trigger('click');
		});
		input.focusin(function(){
			$(this).parent().parent().children('i').hide();	// 隐藏可能因验证出现的错误提示
			flagInpt = 0;	// 标记 input 是否已经得到焦点，以判断焦点来源，防止 radiobox 点击后需要使其得到焦点 > 陷入死循环
			if(!radio.attr('checked')){ 
				radiobox.trigger('click');
			};
		}).focusout(function(){
			flagInpt = 1;
		});
	});
	$('.btForm input[type="checkbox"]').each(function(){	//模拟input:checkbox 单选
		var checkbox = $(this);				
			checkbox.hide().after('<em class="ckbox"></em>');
		var ckbox = $(this).next('em');
		if(checkbox.attr('checked')){	// 如果 checkbox 已经是 checked，改变模拟box 的样式
			ckbox.addClass('checked');
		};
		ckbox.click(function(){	// 模拟 box 被点击后，将 相应的 input:radio 值更改
			if(checkbox.attr('checked')){
				ckbox.removeClass('checked').prev('input').attr('checked', false);
			} else {
				ckbox.addClass('checked').prev('input').attr('checked', true);					
			}
		});
		checkbox.click(function(){	// input:checkbox 得到焦点后（即：点击label 进入）
			if(checkbox.attr('checked')){
				ckbox.addClass('checked');
			}else{
				ckbox.removeClass('checked');
			}
		});
	});
	
	$('.btForm select').each(function(){	//模拟 select 下拉菜单效果
		var thisSelect = $(this);
		var selectedI = 0;
		if($(':selected', thisSelect).length){
			selectedI = $(':selected', thisSelect).index();
		}
		var selectTit = '<dt><small>'+$('option', this).eq(selectedI).text()+'</small><em></em></dt>';	// 取 第一个option作为下拉菜单的标题
		var selectList = '';
		var selectSize = $('option', this).length;
		for(i=1;i<selectSize;i++){	// 循环 取得 select 各项
			selectList += '<em>'+$('option', this).eq(i).text()+'</em>';
		}
		thisSelect.hide().before('<dl>'+selectTit+'<dd>'+selectList+'</dd>'+'</dl>')	//创建 模拟下拉菜单
		.prev('dl').addClass(thisSelect.attr('id')).children('dt').click(function(){	// 	给菜单标题添加点击事件
			$(this).next('dd').slideDown(300)
			.children('em').click(function(){	//	给菜单项添加点击事件
				var index = $(this).index() + 1;
				var selText = $(this).text();
				$(this).addClass('selected').siblings().removeClass().parent().slideUp(200);
				$('option', thisSelect).eq(index).attr("selected", true);
				$(this).parent().prev('dt').html(selText+'<em></em>');
				thisSelect.next('i').hide();	// 隐藏 错误 提示
			});			
		});
		thisSelect.prev('dl').hover(function(){	//记录鼠标是在 dl 里边还是外边，以模拟鼠标点击非下拉菜单区域，下拉菜单隐藏
			inSelect = 1;
		}, function(){
			inSelect = 0;
		})
	});
	$('body').click(function(){	// 鼠标点击 模拟下拉菜单 以外的区域，模拟菜单 隐藏
		if(!inSelect) {
			$('.btForm dl').children('dd').hide();
		}
	});
//	if($('#login4email').length){
//		var name = $('#logName').val();
//		$('#login4email').click(function(){	// AJAX 登录			
//			if($('#name').val() === name || $('#paswd').val() === ''){
//				$(this).parent().next('i').css('display', 'block');
//				return flase;
//			} else {
//				$('#sign2cont > p').html('<strong>Riant.har</strong>').prev().hide();
//				$('#name').val('Riant.har');
//			}
//		});
//	}
//			
//	// 验证联系表单
//	var type = $('#contType').val(), comment = $('#comment').val(), email = $('#email').val();	
//	$('#contact').submit(function(){
//		var ntype = $('#contType').val(), ncomment = $('#comment').val(), nemail = $('#email').val(), nname = $('#logName').val();
//		var anotherMail = $('input[name="aEmail"]:checked').attr('id');
//		if(ntype === type){
//			$('#contType').parent().children('i').css('display','block');
//			return false;
//		} else if(ncomment === comment){
//			$('#comment').parent().children('i').css('display','block');
//			return false;
//		} else if(anotherMail === 'aEmail'){	// 如果选择 直接输入 email 地址
//			if(nemail === email){
//				$('#email').parent().next('i').css('display','block');
//				return false;
//			} else {
//				alert('ok,Now submit');
//				return false;
//			}
//		} else {	// 选择登陆账号
//			if($('#name').val() === ''){	// 如果尚未登陆
//				$('#sign2cont').append('<i style="display:block">您还没有登陆！请先登录，或者您可以选择左侧的直接输入 Email 地址...</i>');
//				return false;
//			} else {
//				// 正确登录的
//				alert('ok,Now submit');
//				return false;
//			}
//		}
//	});
	$('#contact textarea').focusin(function(){	// textarea 验证
		$(this).next('i').hide();
	}).focusout(function(){
		if($(this).val == ''){
			$(this).next('i').css('display','block');
		}			
	});
	$('#contact input[type="password"]').focusin(function(){	//密码框得到焦点——隐藏错误提示
		$('#sign2cont i').hide();
	});
}
function radioChecnked(radio,radiobox){
	radiobox.addClass('checked');
	radio.attr('checked', true);
	
	//hack
	if(radio.attr('id') === "aEmail"){
		Eccky.Page.Parent.Contact.Instance().chooseEmail();
	}
	
	if(radio.attr('id') === "aLogin"){
		Eccky.Page.Parent.Contact.Instance().chooseLogin();
	}
	
	if(radio.attr('id') === "aUser"){
		Eccky.Page.Parent.Contact.Instance().chooseUser();
	}	
}
