//banner特效
if (typeof this.laneige == 'undefined') {
		var laneige = {};
		laneige.Slider = new Class({
			Implements: [Events, Options],
			index: 0,
			items: null,
			controlBox: null,
			fxs: [],
			lastSlide: 0,
			playTT: null,
			options: {
				controlItem: 'li.ctl-item',
				controlItemCurrent: 'current',
				showStyles: null,
				hideStyles: null,
				fxDuration: 500,
				activeEvent: 'click',
				fxTransition: Fx.Transitions.Sine.easeOut,
				startIndex: 0,
				autoPlay: 0,
				forceShowCtl: false
			},
			initialize: function(items, controlBox, options) {
				if (typeOf(items) == 'string')
					items = $$(items);
				if (!instanceOf(items, Elements) || items.length <= 0)
					return false;
//					throw new Error('Slider items could not be empty!');
				// 绑定本地属性
				this.setOptions(options);
				this.items = items;
				controlBox = $(controlBox);
				if (controlBox)
					this.controlBox = controlBox;
				this.index = this.options.startIndex;
				// 激活init事件
				this.fireEvent('init');
				// 初始化controlBox
				if ((items.length > 1 || this.options.forceShowCtl)) {
					var ctls = this.initControlBox(items, controlBox);
					this.controls = function() {
						return ctls;
					}
					var slider = this;
					if (this.controlBox) {
						this.controlBox.addEvents({
							mouseenter: function(event) {
								slider.pause();
							},
							mouseleave: function(event) {
								slider.restart();
							}
						});
					}
				}
				if (this.options.autoPlay > 0) {
					this.playTT = setTimeout(function() {
						this.slide(this.index + 1);
					}.bind(this), this.options.autoPlay * 1000);
				}
			},
			pause: function() {
				if (this.options.autoPlay > 0 && this.playTT != null) {
					clearTimeout(this.playTT);
				}
			},
			restart: function() {
				if (this.options.autoPlay > 0 && this.playTT != null) {
					clearTimeout(this.playTT);
				}
				if (this.options.autoPlay > 0) {
					this.playTT = setTimeout(function() {
						this.slide(this.index + 1);
					}.bind(this), this.options.autoPlay * 1000);
				}
			},
			slide: function(changeIndex) {
				if (this.options.autoPlay > 0 && this.playTT != null) {
					clearTimeout(this.playTT);
				}
				var currentIndex = this.index;
				if (changeIndex == currentIndex)
					return;
				if (changeIndex < 0)
					changeIndex = this.items.length - 1;
				else if (changeIndex > this.items.length - 1)
					changeIndex = 0;
				var slideTime = Number(new Date());
				var slideDelay = this.lastSlide <= 0 ? this.lastSlide : slideTime - this.lastSlide;
				this.lastSlide = slideTime;
				var changeFx = this.fxs[changeIndex], currentFx = this.fxs[currentIndex];
				changeFx.clearChain();
				changeFx.pause();
				currentFx.clearChain();
				currentFx.pause();
				this.fireEvent('slide', [changeFx, currentFx, changeIndex - currentIndex, slideDelay, changeIndex]);
				if (!this.isCancel) {
					this.index = changeIndex;
					if (this.controlBox) {
						var controls = this.controls();
						controls.removeClass(this.options.controlItemCurrent);
						if (controls[changeIndex])
							controls[changeIndex].addClass(this.options.controlItemCurrent);
					}
					if (this.options.autoPlay > 0) {
						this.playTT = setTimeout(function() {
							this.slide(this.index + 1);
						}.bind(this), this.options.autoPlay * 1000);
					}
				}
			},
			initControlBox: function(items, controlBox) {
				var ctls = new Elements(), slider = this;
				for (var i = 0, l = items.length; i < l; i++) {
					var item = items[i];
					if (this.controlBox && (items.length > 1 || this.options.forceShowCtl)) {
						var ctlItem = new Element(this.options.controlItem);
						// 切换的事件
						ctlItem.addEvent(this.options.activeEvent, function(event) {
							event.stop();
							slider.slide(ctls.indexOf(this));
						});
						if (i == this.index) {
							ctlItem.addClass(this.options.controlItemCurrent);
							if (this.options.showStyles)
								item.setStyles(this.options.showStyles);
						}
						else {
							if (this.options.hideStyles)
								item.setStyles(this.options.hideStyles);
						}
						ctlItem.inject(this.controlBox);
						ctls.push(ctlItem);
					}

					if (!this.fxs[i]) {
						this.fxs[i] = new Fx.Morph(item, {duration: this.options.fxDuration, transition: this.options.fxTransition});
					}
				}
				return ctls;
			}
		});
	}

//添加樣式current
function changeItems(item,checked,box){
	if(item && item.length > 0){
		if(checked == 1 && box){
			item[0].addClass('current');
			box[0].setStyles({display:'block'});
		}
		item.addEvents({
			click:function(){
				if(checked == 0 || checked == 2){
					item.removeClass('current');
					this.addClass('current');
					if(box && box.length > 0){
						box.setStyles({display:'none'});
						box[item.indexOf(this)].setStyles({display:'block'});
					}
				}
			},
			mouseenter:function(){
				if(checked == 1 || checked == 2){
					item.removeClass('current');
					this.addClass('current');
					if(box && box.length > 0){
						box.setStyles({display:'none'});
						box[item.indexOf(this)].setStyles({display:'block'});
					}
				}

			}
		});
	}
}


//左右滾動
function laneigeScrool(show_left,show_list,show_right,maxWidth,boxWidth){
	if(show_list){
		show_list.setStyles({width:show_list.getElements('li').length*boxWidth});
		var scroolWidth = show_list.getWidth() - maxWidth;
		var tabMorph = new Fx.Morph(show_list,{duration:400});
		var mouseenter_tt = null;
		var is_enter = false;
		if(scroolWidth <= 0){
			show_left.setStyles({visibility:'hidden'});
			show_right.setStyles({visibility:'hidden'});
		}
		else{
			show_left.setStyles({visibility:'hidden'});
		}
		show_left.addEvents({
			click: function() {
				is_enter = true;
				if (mouseenter_tt != null) mouseenter_tt = clearTimeout(mouseenter_tt);
				var pos = show_list.getStyle('left');
				if (pos.toInt() < 0 && pos.toInt()%boxWidth == 0) {
					tabMorph.clearChain().pause().start({'left':(pos.toInt() + boxWidth)}).chain(function(){
						show_right.setStyles({visibility:'visible'});
						if(show_list.getStyle('left').toInt() == 0){
							show_left.setStyles({visibility:'hidden'});
						}
					});
				}
			},
			mouseleave: function() {
				is_enter = false;
				if (mouseenter_tt != null) mouseenter_tt = clearTimeout(mouseenter_tt);
			}
		});
		show_right.addEvents({
			click: function() {
				is_enter = true;
				if (mouseenter_tt != null) mouseenter_tt = clearTimeout(mouseenter_tt);
				var pos = show_list.getStyle('left');
				if (pos.toInt() <= 0 && pos.toInt() > -scroolWidth && pos.toInt()%boxWidth == 0){
					tabMorph.clearChain().pause().start({'left':(pos.toInt() - boxWidth)}).chain(function(){
						show_left.setStyles({visibility:'visible'});
						if(show_list.getStyle('left').toInt() == -scroolWidth){
							show_right.setStyles({visibility:'hidden'});
						}
					});
				}
			},
			mouseleave: function() {
				is_enter = false;
				if (mouseenter_tt != null) mouseenter_tt = clearTimeout(mouseenter_tt);
			}
		});
	}
}

//圖片垂直居中
function boxMargin(item,boxWidth,boxHeight){
	if(item && item.length > 0){
		item.getParent().setStyles({overflow:'hidden'});
		item.each(function(e,index){
			Asset.images(e.get('src'),{
				onComplete:function(){
					var w = (boxWidth - e.getWidth().toInt())/2
					var h = (boxHeight - e.getHeight().toInt())/2;
					e.setStyles({margin:h+'px '+w+'px '+h+'px '+w+'px'});
				}
			});
		});
	}
}

//飛出Box
function flyBox(item,box){
	var boxMorph = new Fx.Morph(box,{duration:100,transition:Fx.Transitions.easyInOut});
	item.addEvents({
		click:function(event){
			event.stop();
			if(box.getStyle('display') == 'none'){
				boxMorph.clearChain().pause().start({display:'block',top:41});
			}
			else{
				boxMorph.clearChain().pause().start({top:150}).chain(function(){
					boxMorph.clearChain().pause().start({display:'none'}).chain(function(){
						boxMorph.clearChain().pause().start({top:200})
					})
				});
			}
		}
	});
}

//清空輸入框的內容
function changeInput(item){
	var value = item.get('value');
	item.addEvents({
		focus:function(event){
			event.stop();
			this.set('value','');
		}/*,
		blur:function(event){
			event.stop();
			if(this.get('value') == ''){
				this.set('value',value);
			}
		}*/
	});
}

//首頁sharing效果
function showBox(item,box,num,wrap){
	var boxMorph = new Fx.Morph(box,{duration:200,transition:Fx.Transitions.Sine.easyInOut});
	item.addEvents({
		click:function(event){
			event.stop();
			boxMorph.clearChain().pause().start({height:num});
		}
	});
	wrap.addEvents({
		mouseleave:function(event){
			event.stop();
			boxMorph.clearChain().pause().start({height:0});
		}
	});	
}

//getValue
function getValue(item,input){
	item.addEvents({
		click:function(event){
			event.stop();
			item.removeClass('current');
			this.addClass('current');
			input.set('value',item.indexOf(this) + 1);
		}
	});
}

//about spoke
function spokeScroll(scrollBox){
	var topBtn = scrollBox.getElements('div.top-arrow');
	var downBtn = scrollBox.getElements('div.down-arrow'); 
	var fx = new Fx.Morph(scrollBox,{duration:400,transition:Fx.Transitions.Sine.easyInOut});
	topBtn.addEvents({
		click:function(event){
			event.stop();
			fx.clearChain().pause().start({top:scrollBox.getStyle('top').toInt() + 515});
		}
	});
	downBtn.addEvents({
		click:function(event){
			event.stop();
			fx.clearChain().pause().start({top:scrollBox.getStyle('top').toInt() - 515});
		}
	});
}

Event.implement({
	hovered: function(el) {
		el = $(el);
		if (!el) return false;
		var p = this.page;
		var pos = el.getCoordinates();
		return (p.x >= pos.left && p.x <= pos.right && p.y >= pos.top && p.y <= pos.bottom)
	}
});











	
