var shortBangs = null;
var allBangs = null;
var utInfo = null;

var BangShortcut = new Class({
	Implements: Events,
	initialize: function(userId, from, actPoints, panel, exclusiveness, excluFrom){
		this.userId = userId;
		this.from = from;
		this.actPoints = actPoints;
		this.panel = panel;
		this.select = this.panel.getElement("select");
		this.target = this.panel.getElement("ul");
		this.exclusiveness = exclusiveness;
		this.excluFrom = excluFrom;
		var current = this;
		this.actPoints.each(function(item, index){
			item.addEvent("click", function(){current.switchShow(); return false;});
		});
		this.select.addEvent("change", function(){current.switchFunc();});
	},
	switchShow: function(){
		if(this.panel.style.display == 'none'){
			if(this.exclusiveness&&this.exclusiveness.style.display=='block'){
				this.excluFrom.removeClass("mps-a0-on");
				this.exclusiveness.setStyle('display','none');
				this.excluFrom.getElement("img.topimg").className = this.excluFrom.getElement("img.topimg").className.replace("on", "off");
			}
			if(!this.from.hasClass("bsc-a0-on")){
				this.from.addClass("bsc-a0-on");
				this.from.getElement("img.topimg").className = this.from.getElement("img.topimg").className.replace("off", "on");
			}
			this.setPosition();
			this.panel.style.display = "block";
			this.select.fireEvent("change");
		}else{
			this.from.removeClass("bsc-a0-on");
			this.from.getElement("img.topimg").className = this.from.getElement("img.topimg").className.replace("on", "off");
			this.panel.style.display = "none";
		}
	},
	setPosition: function(){
		var pos = this.from.getCoordinates(this.from.getParent("div.nav"));
		this.panel.setStyles({'left': pos.left-18, 'top': pos.top+24});
	},
	switchFunc: function(){
		this.panel.getElement("span.tt-name").innerHTML = this.select.options[this.select.selectedIndex].text;
		if(this.select.options[this.select.selectedIndex].value=='all'){
			this.panel.getElement("span.tt-admin img").style.display = "none";
			if(allBangs&&allBangs.length>0){
				this.waitFunc();
				this.displayResults(allBangs);
			}
			else this.getTargetGroups("all", "getAllJoinedGroups.tr");
		}else{
			this.panel.getElement("span.tt-admin img").style.display = "block";
			if(shortBangs&&shortBangs.length>0){
				this.waitFunc();
				this.displayResults(shortBangs);
			}
			else this.getTargetGroups("shortcut", "getShortcutGroups.tr");
		}
	},
	getTargetGroups: function(type, url){
		var current = this;
		var datas = {'groupId': this.groupId};
		var jax = new Request.JSON({
			url: url,
			onSuccess: function(response){
				if(!response||response.results.length<1) {
					current.target.innerHTML = "<li>没有查询结果。</li>";
					return;
				}
				if(type=='all'){allBangs=null; allBangs = new Array(); allBangs = response.results;}
				else{shortBangs=null; shortBangs = new Array(); shortBangs = response.results;}
				current.displayResults(response.results);
			},
			onFailure: function(){
				current.target.innerHTML = "<li> 查询出错，请稍后再次尝试。</li>";
			}
		});
		jax.post(datas);
		this.waitFunc();
	},
	displayResults: function(results){
		this.target.innerHTML = "";
		if(results.length>18){
			this.target.setStyle('height', '340px');
		}
		for(var i=0; i<results.length; i++){
			var li = new Element("li").inject(this.target);
			var span = new Element("span").setStyles({'width':'15px'}).inject(li);
			var img = new Element("img").inject(span);
			img.set('src','http://www.oobang.com/images/level/group'+results[i].levePackageId+'/'+results[i].sequence+'.gif');
			var span1 = new Element("span").addClass('bnormal').inject(li);
			if(results[i].isBangzhu=='y')span1.addClass('bmy');
			var ahref = new Element("a").inject(span1);
			ahref.set('href','http://www.oobang.com/'+results[i].path);
			ahref.set('text',' '+results[i].name);
			if(results[i].hasNewArticle==true) {
				var span2 = new Element("span").inject(li);
				span2.set('class','suoyou_new');
			}
		}
	},
	waitFunc: function(){
		this.target.innerHTML = "<li style='height:80px'><table width='100%' height='100%' valign='middle'><tr><td style='text-align:center'>" +
				"<img style='vertical-align:middle' src='http://bangimg.oobang.com/images/common/wait-s-1.gif'/></td></tr></table></li>";
	}
});

var MyPageShortCut = new Class({
	Implements: Events,
	initialize: function(userId, from, actPoints, panel, exclusiveness, excluFrom){
		this.userId = userId;
		this.from = from;
		this.actPoints = actPoints;
		this.panel = panel;
		this.exclusiveness = exclusiveness;
		this.excluFrom = excluFrom;
		var current = this;
		this.actPoints.each(function(item, index){
			item.addEvent("click", function(){current.switchShow(); return false;});
		});
		this.timer = null;
	},
	switchShow: function(){
		if(this.panel.style.display == 'none'){
			if(this.exclusiveness&&this.exclusiveness.style.display=='block'){
				this.excluFrom.removeClass("bsc-a0-on");
				this.exclusiveness.setStyle('display','none');
				this.excluFrom.getElement("img.topimg").className = this.excluFrom.getElement("img.topimg").className.replace("on", "off");
			}
			if(!this.from.hasClass("mps-a0-on")){
				this.from.addClass("mps-a0-on");
				this.from.getElement("img.topimg").className = this.from.getElement("img.topimg").className.replace("off", "on");
			}
			this.setPosition();
			this.panel.style.display = "block";
			if(this.timer&&utInfo!=null){
				this.displayResults(utInfo);
			}
			else this.getDatas();
		}else{
			this.from.removeClass("mps-a0-on");
			this.from.getElement("img.topimg").className = this.from.getElement("img.topimg").className.replace("on", "off");
			this.panel.style.display = "none";
		}
	},
	setPosition: function(){
		var pos = this.from.getCoordinates(this.from.getParent("div.nav"));
		this.panel.setStyles({'left': pos.left, 'top': pos.top+24});
	},
	getDatas: function(){
		var datas = {"userId": this.userId};
		var current = this;
		var jax = new Request.JSON({
			url: 'ajaxInfoShortcut.tr',
			onSuccess: function(response){
				current.timer = new Array();
				(function(){current.cache();}).delay(1000*60*2);
				utInfo = response.result;
				current.displayResults(response.result);
			},
			onFailure: function(){
			}
		});
		jax.post(datas);
	},
	displayResults: function(result){
		this.panel.getElement("span.messageNum").innerHTML = result.messageNum;
		this.panel.getElement("span.bangInvationNum").innerHTML = result.bangInvationNum;
		this.panel.getElement("span.friendInvationNum").innerHTML = result.friendInvationNum;
		if(result.newMessageNum+result.newBangInvationNum+result.newFriendInvationNum>0)
			this.from.getElement("img.mp-flag").addClass("newmp");
		else
			this.from.getElement("img.mp-flag").removeClass("newmp");
		if(result.newMessageNum>0&&!this.panel.getElement("span.newMessage").getElement("img").hasClass("img-new"))
			this.panel.getElement("span.newMessage").getElement("img").addClass("img-new");
		if(result.newBangInvationNum>0&&!this.panel.getElement("span.newBangInvation").getElement("img").hasClass("img-new"))
			this.panel.getElement("span.newBangInvation").addClass("img-new");
		if(result.newFriendInvationNum>0&&!this.panel.getElement("span.newFriendInvation").getElement("img").hasClass("img-new"))
			this.panel.getElement("span.newFriendInvation").addClass("img-new");
	},
	cache: function(){
		this.timer = null;
	}
});