﻿/*
update : 20101128
email : gao.fei@hotmail.com
*/

jQuery.noConflict();
var GFscript={};

(function($){

GFscript = {
	version: '1.0',
	author:'goofy',
	require: function(libraryName) {
		document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
	},
	load: function() {
		if((typeof jQuery=='undefined')|| parseFloat(jQuery.fn.jquery.split(".")[0] + "." + jQuery.fn.jquery.split(".")[1]) < 1.4)
		throw("GFscript requires the Jquery JavaScript framework >= 1.4.0");
    
		$("script").map( function() {
			return($(this).attr("src") && $(this).attr("src").match(/gfscript\.js(\?.*)?$/))?this:null;
		}).each( function() {
			var path = $(this).attr("src").replace(/gfscript\.js(\?.*)?$/,'');
			var includes = $(this).attr("src").match(/\?.*load=([\w,]*)/);
			$((includes ? includes[1] : 'public,gallery,triggerBox,tag').split(',')).each(function(str) { 
				GFscript.require(path+this+'.js')																														 			});
		});
	}
}
//GFscript.load();

GFscript.Base={
	getElement:function(el){
		return typeof el=="string"?$("#"+el):$(el);
	},
	extend:function(destination, source) {
		for (var property in source) {
			destination[property] = source[property];  
		}
		return destination;
	}
}

GFscript.Common={};

GFscript.Common.Tag=function (container,config){
	this.container=GFscript.Base.getElement(container);
	this.tags=this.container.find(".tag");
	this.conts=this.container.find(".cont");
	this.txts=this.container.find(".txt");
	this.openBtn=this.container.find(".open");
	this.closeBtn=this.container.find(".close");
	this.hasTxt=false;
	
	this.showEffectIndex=0;
	this.hideEffectIndex=0;
	this.txtShowEffectIndex=0;
	this.txtHideEffectIndex=0;
	this.showBeforeHandler=null;
	this.showAfterHandler=null;
	this.duration=500;
	this.txtDuration=200;

	this.flag=0;
	
	if(config){
		GFscript.Base.extend(this,config)
	}
	
	for(var i=0;i<this.tags.length;i++){
		this.tags[i].index=i;
		this.conts[i].index=i;
	}
	this.goTo(this.flag);
	var self=this;
	this.tags.click(function(){
		if(self.index!=self.flag)
			self.goTo(this.index);
	})
	if(this.hasTxt){
		this.openBtn.click(function(){
			(self.getShowEffectFunc(self.txtShowEffectIndex))($(self.txts[self.flag]),self.txtDuration);
		})
		this.closeBtn.click(function(){
			(self.getHideEffectFunc(self.txtHideEffectIndex))($(self.txts[self.flag]),self.txtDuration);
		})
	}
}
GFscript.Common.Tag.prototype={
	goTo:function(num){
		if(this.showBeforeHandler)
			this.showBeforeHandler(this.tags[num],this.conts[num]);
			
		$(this.tags[this.flag]).removeClass("current");
		$(this.tags[num]).addClass("current");

		(this.getHideEffectFunc(this.hideEffectIndex))($(this.conts[this.flag]),this.duration);
		(this.getShowEffectFunc(this.showEffectIndex))($(this.conts[num]),this.duration);
		
		if(this.hasTxt&&$(this.txts[this.flag]).css("display")!=="none"){
			(this.getHideEffectFunc(this.txtHideEffectIndex))($(this.txts[this.flag]),this.txtDuration);
			(this.getShowEffectFunc(this.txtShowEffectIndex))($(this.txts[num]),this.txtDuration);
		}
		
		if(this.showAfterHandler)
			this.showAfterHandler(this.tags[num],this.conts[num]);
		this.flag=num;
	},
	getShowEffectFunc:function(index){
		if(!this.showEffectHandler)
			return $.map(GFscript.Common.Tag.Effect,(function(o){
				return o.index==index?o.showFunc:null
			}))[0]
		else
			return this.showEffectHandler
	},
	getHideEffectFunc:function(index){
		if(!this.hideEffectHandler)
			return $.map(GFscript.Common.Tag.Effect,(function(o){
				return o.index==index?o.hideFunc:null
			}))[0]
		else
			return this.hideEffectHandler
			
	}
}

GFscript.Common.Tag.Effect=[{
	index:0,
	showFunc:function(el,t){
		el.show();
	},
	hideFunc:function(el,t){
		el.hide();
	}
},{
	index:1,
	showFunc:function(el,t){
		el.slideDown(t);
	},
	hideFunc:function(el,t){
		el.slideUp(t);
	}
},{
	index:2,
	showFunc:function(el,t){
		el.fadeIn(t);
	},
	hideFunc:function(el,t){
		el.fadeOut(t);
	}
},{
	index:3,
	showFunc:function(el,t){
		el.animate({
			width:"show",
			height:"show",
			paddingTop:"show",
			paddingRight:"show",
			paddingBottom:"show",
			paddingLeft:"show",
			marginTop:"show",
			marginRight:"show",
			marginBottom:"show",
			marginLeft:"show",
			borderTopWidth:"show",
			borderRightWidth:"show",
			borderBottomWidth:"show",
			borderLeftWidth:"show"
		},t)
	},
	hideFunc:function(el,t){  
		el.animate({
			width:"hide",
			height:"hide",
			paddingTop:"hide",
			paddingRight:"hide",
			paddingBottom:"hide",
			paddingLeft:"hide",
			marginTop:"hide",
			marginRight:"hide",
			marginBottom:"hide",
			marginLeft:"hide",
			borderTopWidth:"hide",
			borderRightWidth:"hide",
			borderBottomWidth:"hide",
			borderLeftWidth:"hide"
		},t)
	}
}]


GFscript.Common.TriggerBox=function(trigger,menuLayer,config){
	this.trigger=GFscript.Base.getElement(trigger);
	this.menuLayer=GFscript.Base.getElement(menuLayer);
	this.position=0;
	this.showEffectIndex=0;
	this.hideEffectIndex=0;

	this.showBeforeHandler=null;
	this.showAfterHandler=null;
	this.hideBeforeHandler=null;
	this.hideAfterHandler=null;
	this.showEffectHandler=null;
	this.hideEffectHandler=null;
	this.setPositionHandler=null;
	
	this.showAnimation=null;
	this.hideAnimation=null;
	this.clearAnimation=null;
	
	this.showTime=150;
	this.hideTime=100;
	this.showDelayTime=0;
	this.hideDelayTime=0;
	
	if(config){
		GFscript.Base.extend(this,config)
	}
	
	if(this.position!=0){
		this.menuLayer.css({ position:"absolute"});
	}
	
	this.menuLayer.css({ overflow:"hidden"});
	var self=this;
	
	this.trigger.bind("mousemove",function(e){
		self.clearClearAnimation();
		self.clearHideAnimation();
		//if(self.position=="jsDynamic2"){
			self.clearShowAnimation();
		//}
		
		if(self.showAnimation==null){
			self.showAnimation=setTimeout(function(){
				var position=(self.getSetPostionFunc(self.position))(self.trigger,self.menuLayer,e);
				if(self.setPositionHandler){
					var _position=self.setPositionHandler(position);
					position=(_position==undefined?position:_position);
				}	
				self.menuLayer.css({left:position.left+"px",top:position.top+"px"});
				(self.getShowEffectFunc(self.showEffectIndex))(self.menuLayer,self.showTime);	
			},self.showDelayTime);
		}
	})
	
	this.trigger.mouseout(function(){
		self.clearAnimation=setTimeout(function(){
			self.clearShowAnimation();
		},1)
		self.hideAnimation=setTimeout(function(){
			(self.getHideEffectFunc(self.hideEffectIndex))(self.menuLayer,self.hideTime);
		},self.hideDelayTime);
	})
	
	this.menuLayer.mouseover(function(){
		self.clearClearAnimation();
		self.clearHideAnimation();
		//if(self.position=="jsDynamic2"){
			self.clearShowAnimation();
		//}
	})
	
	this.menuLayer.mouseout(function(){
		self.hideAnimation=setTimeout(function(){
			(self.getHideEffectFunc(self.hideEffectIndex))(self.menuLayer,self.hideTime);
		},self.hideDelayTime);
		self.clearAnimation=setTimeout(function(){
			self.clearShowAnimation();
		},1)
	})
	
	GFscript.Common.TriggerBox.instances.push(this);
}
GFscript.Common.TriggerBox.instances=[];
GFscript.Common.TriggerBox.prototype={
	clearHideAnimation:function(){
		if(this.hideAnimation!=null) {
			clearTimeout(this.hideAnimation);
			this.hideAnimation=null;
		}	
	},
	clearShowAnimation:function(){
		if(this.showAnimation!=null) {
			clearTimeout(this.showAnimation);
			this.showAnimation=null;
		}	
	},
	clearClearAnimation:function(){
		if(this.clearAnimation)
			clearTimeout(this.clearAnimation);
	},
	getSetPostionFunc:function(i){
		return $.map(GFscript.Common.TriggerBox.SetPosition,(function(o){
			return o.index==i?o.func:null
		}))[0]
	},

	getShowEffectFunc:function(index){
		if(!this.showEffectHandler)
			return $.map(GFscript.Common.TriggerBox.Effect,(function(o){
				return o.index==index?o.showFunc:null
			}))[0]
		else
			return this.showEffectHandler
	},
	getHideEffectFunc:function(index){
		if(!this.hideEffectHandler)
			return $.map(GFscript.Common.TriggerBox.Effect,(function(o){
				return o.index==index?o.hideFunc:null
			}))[0]
		else
			return this.hideEffectHandler
			
	}
}
GFscript.Common.TriggerBox.SetPosition=[{
	index:0,
	func:function(trigger,menuLayer){
		menuLayer.show();
		var left=parseInt(menuLayer.css("left").slice(0,-2));
		var top=parseInt(menuLayer.css("top").slice(0,-2));
		menuLayer.hide();
		left=isNaN(left)?0:left;
		top=isNaN(top)?0:top;
		return {left:left,top:top}
	}
},{
	index:1,
	func:function(trigger,menuLayer){
		var t_offset=trigger.offset();
		var top=t_offset.top+trigger[0].offsetHeight;
		return {left:t_offset.left,top:top}
		
	}
},{
	index:2,
	func:function(trigger,menuLayer,e){
		return {left:e.pageX,top:e.pageY}
	}
}]

GFscript.Common.TriggerBox.Effect=[{
	index:0,
	showFunc:function(el,t){
		el.show();
	},
	hideFunc:function(el,t){
		el.hide();
	}
},{
	index:1,
	showFunc:function(el,t){
		el.slideDown(t);
	},
	hideFunc:function(el,t){
		el.slideUp(t);
	}
},{
	index:2,
	showFunc:function(el,t){
		el.fadeIn(t);
	},
	hideFunc:function(el,t){
		el.fadeOut(t);
	}
},{
	index:3,
	showFunc:function(el,t){
		el.animate({
			width:"show",
			height:"show",
			paddingTop:"show",
			paddingRight:"show",
			paddingBottom:"show",
			paddingLeft:"show",
			marginTop:"show",
			marginRight:"show",
			marginBottom:"show",
			marginLeft:"show",
			borderTopWidth:"show",
			borderRightWidth:"show",
			borderBottomWidth:"show",
			borderLeftWidth:"show"
		},t)
	},
	hideFunc:function(el,t){  
		el.animate({
			width:"hide",
			height:"hide",
			paddingTop:"hide",
			paddingRight:"hide",
			paddingBottom:"hide",
			paddingLeft:"hide",
			marginTop:"hide",
			marginRight:"hide",
			marginBottom:"hide",
			marginLeft:"hide",
			borderTopWidth:"hide",
			borderRightWidth:"hide",
			borderBottomWidth:"hide",
			borderLeftWidth:"hide"
		},t)
	}
}]


GFscript.Common.Gallery=function(wrapper,innerWrapper,config){
	this.wrapper=GFscript.Base.getElement(wrapper);
	this.innerWrapper=GFscript.Base.getElement(innerWrapper);
	this.prevTriggers=null;
	this.nextTriggers=null;
	this.direction='HORIZONTAL';/*['VERTICAL','HORIZONTAL']*/
	this.unitDistance=100;
	this.singleTime=10;
	this.singleVolume=8;
	this.animation=null;
	this.startHandler=null;
	this.endHandler=null;
	this.beforeHandler=null;
	this.afterHandler=null;
	this.innerLen=0;
	this.wrapperLen=0;


	if(config){
		GFscript.Base.extend(this,config)
	}
	var myThis=this;
	
	if(this.direction=='VERTICAL'){
		this.GalleryStr='scrollTop';
		this.innerLen=this.innerWrapper.height();
		this.wrapperLen=this.wrapper.height();
		
	}else{
		this.GalleryStr='scrollLeft';
		this.wrapperLen=this.wrapper.width();
		this.innerWrapper.children().each(function(){
			myThis.innerLen+=this.offsetWidth;
		})
	}
		
	if(this.prevTriggers){
		this.prevTriggers.click(function(){
			myThis.prev();
		})
	}
	if(this.nextTriggers){
		this.nextTriggers.click(function(){
			myThis.next();
		})
	}
}
GFscript.Common.Gallery.prototype={
	next:function(){
		if(this.beforeHandler)
			this.beforeHandler(this);
		if(this.animation)
			clearInterval(this.animation)
		var myThis=this;
		this.animation=setInterval(function(){
			myThis._nextAction();
		},this.singleTime)
		
	},
	prev:function(){
		if(this.beforeHandler)
			this.beforeHandler(this);
		if(this.animation)
			clearInterval(this.animation)
		var myThis=this;
		this.animation=setInterval(function(){
			myThis._preAction();
		},this.singleTime)
	},
	_nextAction:function(i){
		var GalleryChangeBefore=this.wrapper.attr(this.GalleryStr);

		var remainder=GalleryChangeBefore % this.unitDistance;
		var GalleryChangeAfter=0;

		if(remainder + this.singleVolume >= this.unitDistance){
			var curWC=Math.abs(this.unitDistance - remainder);
			GalleryChangeAfter=GalleryChangeBefore + curWC

			clearInterval(this.animation)
		}else{
			GalleryChangeAfter=GalleryChangeBefore + this.singleVolume;
		}
		if(GalleryChangeBefore==GalleryChangeAfter){

			clearInterval(this.animation)
		}
		this.wrapper.attr(this.GalleryStr,GalleryChangeAfter);
		if(this.endHandler&&this.wrapper.attr(this.GalleryStr)==(this.innerLen-this.wrapperLen)){
			this.endHandler(this);
			clearInterval(this.animation)
		}

		
	},
	_preAction:function(){
		var GalleryChangeBefore=this.wrapper.attr(this.GalleryStr);
		var GalleryChangeAfter=0;
		
		this.wrapper.attr(this.GalleryStr,GalleryChangeBefore - this.singleVolume);
		var GalleryChangeAfter=this.wrapper.attr(this.GalleryStr);
		var remainder=GalleryChangeAfter % this.unitDistance;

		if(GalleryChangeAfter % this.unitDistance <= this.singleVolume){
			var curWC=Math.abs(remainder);
			GalleryChangeAfter=GalleryChangeAfter-curWC;
			this.wrapper.attr(this.GalleryStr,GalleryChangeAfter);
			clearInterval(this.animation)
		}
		if(GalleryChangeBefore==GalleryChangeAfter){
			clearInterval(this.animation)
		}
		if(this.endHandler&&this.wrapper.attr(this.GalleryStr)==0){
			this.startHandler(this);
			clearInterval(this.animation)
		}
	}
}


$(document).ready(function(){
	$("#aside-nav dt").click(function(){
		if($(this).next("dd").css("display")=="none"){
			$("#aside-nav dd").slideUp(200);
			$("#aside-nav dt>a").removeClass("current");
			$(this).next("dd").slideDown(200);
			$(this).find("a").addClass("current");
		}else{
			$(this).find("a").removeClass("current");
			$(this).next("dd").slideUp(200);
		}
	});
	$("#header .search .txt").attr("value","请输入关键字");
	$("#header .search .txt").focus(function(){
		if(this.value=="请输入关键字")
			this.value="";
			
	})
	$("#header .search .txt").blur(function(){
		if($.trim(this.value)=="")
			this.value="请输入关键字";
	})
		
}); 
	
	
})(jQuery)





