/*
 * @author:Siemon Huang
 * @version:1.0
 * @date:2011-3-22
 */
viewplugin.guestbicq = {
	init:function(load){
		var	self = this,
			mainpanel = this.mainpanel = $('<div id="'+this.id+'" class="bicq_guestsys"/>').appendTo(document.body),		
			//panel = this.panel = $('<div class="bicq_guestsys_panel"/>').appendTo(mainpanel),
			//header = $('<div class="bicq_guestsys_header"/>').appendTo(panel),
			//content = this.content = $('<div class="bicq_guestsys_content"/>').appendTo(panel),
			//inner = this.inner = $('<div class="bicq_guestsys_content-inner"/>').appendTo(content),
			//title = this.title = $('<div class="bicq_guestsys_title_right"><span></span></div>')
					//.appendTo(mainpanel);

			panel = this.panel = $('<div class="bicq_guestsys_panel"/>').appendTo(mainpanel),
			paneln = $('<div class="bicq_guestsys_n_right"/>').appendTo(panel),
			panelc = $('<div class="bicq_guestsys_c_right"/>').appendTo(panel),
			panels = $('<div class="bicq_guestsys_s_right"/>').appendTo(panel),
			content = this.content = $('<div class="bicq_guestsys_content"/>').appendTo(panelc),
			title = this.title = $('<div class="bicq_guestsys_title"/>').appendTo(mainpanel),
			titlen = $('<div class="bicq_guestsys_title_n_right"/>').appendTo(title),
			titlec = this.titlec = $('<div class="bicq_guestsys_title_c_right"/>').appendTo(title),
			txt = this.txt = $('<div class="bicq_guestsys_title_txt"/>').appendTo(titlec),
			titles = $('<div class="bicq_guestsys_title_s_right"/>').appendTo(title);
		if(load){
			var pos = this.position,
				top = pos[0],
				right = pos[1],
				left = pos[3],
				bodyContent = viewplugin.getContentBody();
			mainpanel.addClass('customize-plugin');
			if(right == 0){
				mainpanel.css({top:top+'px',right:'0px'});
			}
			else if(right<0){
				mainpanel.appendTo(bodyContent).css({top:top+'px',right:right+'px'});
			}
			else if(left<0){
				mainpanel.appendTo(bodyContent).css({top:top+'px',left:left+'px'});
			}
			else{
				mainpanel.css({top:top+'px',left:left+'px'});
			}
			title.toggle(
				function(){
					var parentNode = title.parents('.customize-plugin:eq(0)');
					if(right<0){
						self._animateMax(117,mainpanel,panel,parentNode,'right');
					}else if(left<0){
						self._animateMax(117,mainpanel,panel,parentNode,'left');
					}else{
						self._animateMax(117,mainpanel,panel);
					}
				},
				function(){
					var parentNode = title.parents('.customize-plugin:eq(0)');
					if(right<0){
						self._animateMin(0,mainpanel,panel,parentNode,'right');
					}else if(left<0){
						self._animateMin(0,mainpanel,panel,parentNode,'left');
					}else{
						self._animateMin(0,mainpanel,panel);
					}						
				}
			);
			this.loadList();
			this.scrolling();	
		}
		/*else{
			title.toggle(
				function(){
					var location = parseInt($(this).attr('location'),10);
					if(location%2>0){
						var parentNode = title.parents('.customize-plugin:eq(0)');
						self._animateMax(117,mainpanel,panel,parentNode);
					}else{
						self._animateMax(117,mainpanel,panel);
					}
				},
				function(){
					var location = parseInt($(this).attr('location'),10);
					if(location%2>0){
						var parentNode = title.parents('.customize-plugin:eq(0)');
						self._animateMin(0,mainpanel,panel,parentNode);
					}else{
						self._animateMin(0,mainpanel,panel);
					}							
				}
			);
		}*/
		this.dragStop(this.opposite);
		return mainpanel;
	},
	_animateMax:function(num,mainpanel,panel,parentNode,dict){
		var count = 0;
		clearInterval(this.interval);
		this.interval = setInterval(function(){
			if(count<num){
				mainpanel.width(mainpanel.width()+3);
				panel.width(panel.width()+3);
				parentNode&&parentNode.css(dict,parseInt(parentNode.css(dict))-3);
				count+=3;				
			}
		},10);
	},
	_animateMin:function(num,mainpanel,panel,parentNode,dict){
		var count = 117;
		clearInterval(this.interval);
		this.interval = setInterval(function(){
			if(count>num){
				mainpanel.width(mainpanel.width()-3);
				panel.width(panel.width()-3);
				parentNode&&parentNode.css(dict,parseInt(parentNode.css(dict))+3);
				count-=3;				
			}
		},10);		
	},
	dragStop:function(opposite){
		//this.title.attr('location',key);
		if(opposite){
			//this.title[0].className = this.title[0].className.replace(/bicq_guestsys_title_(\w+)/i,'bicq_guestsys_title_left');
			this.title.children().each(function(){
				this.className = this.className.replace(/(\w+)_right/i,'$1_left');
			});
			this.txt.css({left:null,right:4});
			this.panel.insertAfter(this.title);
		}else{
			this.title.children().each(function(){
				this.className = this.className.replace(/(\w+)_left/i,'$1_right');
			});
			this.txt.css({left:4,right:null});
			this.panel.insertBefore(this.title);
		}
		/*
		switch(key){
			case 0:
				this.title[0].className = this.title[0].className.replace(/bicq_guestsys_title_(\w+)/i,'bicq_guestsys_title_right');
				this.panel.insertBefore(this.title);
			break;
			case 1:
				this.title[0].className = this.title[0].className.replace(/bicq_guestsys_title_(\w+)/i,'bicq_guestsys_title_left');
				this.panel.insertAfter(this.title);
			break;
			case 2:
				this.title[0].className = this.title[0].className.replace(/bicq_guestsys_title_(\w+)/i,'bicq_guestsys_title_right');
				this.panel.insertBefore(this.title);
			break;
			case 3:
				this.title[0].className = this.title[0].className.replace(/bicq_guestsys_title_(\w+)/i,'bicq_guestsys_title_left');
				this.panel.insertAfter(this.title);
			break;
			default:
			break;
		}
		*/		
	},
	scrolling:function(){
		var parentNode = this.title.parents('.customize-plugin:eq(0)'),
			top = parentNode.position().top
					
		$(window).scroll(function(){
			var _top = $(document).scrollTop();
			parentNode.css('top',top+_top);
		});
	},
	loadList:function(){
		var self = this;
		$.ajax({
			url:'/admin/bicq.php?action=getservicelist',
			dataType:'json',
			success:function(data){
				self._createList(data);
			}
		})
	},
	_createList:function(data){
		this.content.empty();
		for(var i=0;i<data.length;i++){
			var item = data[i];
			$('<div class="bicq_guestsys_item"><img width="16" height="16" src="'+item.face+'"/><a id="bicq_'+item.bicq+'" href="javascript:;">'+item.username+'</a></div>').appendTo(this.content);
		}
		this.content.click(function(event){
			var target = event.target;
			if(target.nodeName.toLowerCase()=='a'){
				var id = target.id.split('_')[1];
				opencharframe(id);
			}
		});
		this.titlec.height(this.content.height());
	}
}

