(function($){
	var W = window;
	var D = document;
	var Y = {};

	Y.isIE6 = !window.XMLHttpRequest;

	/*
		slide function
	*/
	Y.slide = function(ctn,ctrl,cfg){
		var defaults  = {
				effect : 'fade',
				speed : 500,
				pauseTime :5000,
				auto : true,
				curCls : 'current',
				event : 'mouseover'
		};
		var slideCtrl = $(ctrl).children();
		var slideCtn = $(ctn).children();
		var slideCon = $(ctn);
		var cfg = $.extend({},defaults,cfg);
		var slideW = slideCtn.eq(0).width();
		var slideH = slideCtn.eq(0).height();
		var len = slideCtn.length;
		var autoPlay;
		var autoTimer;
		var curIdx = 0;

		if(len < 2){ 
			return false;
		};

		if(cfg.effect =='scrollX'){
			slideCon.width(slideW*len);
		}
		else if(cfg.effect =='scrollY'){
			slideCon.height(slideH*len);
		}

		var slideEffect = {
			fade : function(idx){
				slideCtrl.eq(idx).addClass(cfg.curCls).siblings().removeClass(cfg.curCls);
				slideCtn.eq(idx).addClass(cfg.curCls).stop().animate({opacity:1},cfg.speed).siblings().stop().animate({opacity:0},cfg.speed).removeClass(cfg.curCls);
			},
			
			scrollX : function(idx){
				slideCtrl.eq(idx).addClass(cfg.curCls).siblings().removeClass(cfg.curCls);
				slideCon.stop().animate({left:(-slideW*idx)},cfg.speed);
			},
			
			scrollY : function(idx){
				slideCtrl.eq(idx).addClass(cfg.curCls).siblings().removeClass(cfg.curCls);
				slideCon.stop().animate({top:(-slideH*idx)},cfg.speed);
			}
		}
		

		$.each(slideCtrl,function(idx){
			$(this).bind(cfg.event,function(){
				curIdx = idx;
				
				slideEffect[cfg.effect](idx);
			})
		});

		autoPlay = function(){
			
			
			slideEffect[cfg.effect](curIdx);
			curIdx++;
			curIdx = (curIdx < slideCtn.length) ? curIdx : 0;
			
		}
		if(cfg.auto){
			autoTimer = setInterval(autoPlay,cfg.pauseTime)
		}
		slideCtn.hover(function(){
			if(autoTimer){
				clearInterval(autoTimer);
			}
		},function(){
			if(cfg.auto){
				autoTimer = setInterval(autoPlay,cfg.pauseTime)
			}
		});
	};

	/*
		slidePages function
	*/
	Y.slidePages = function(ctn,ctrl,pages,cfg){
		var defaults  = {
				effect : 'fade',
				speed : 500,
				pauseTime :5000,
				auto : true,
				curCls : 'current',
				circle: true, //点击下一页时是否可以从最后一页跳转到第一页
				prevDis : 'prev_dis',
				nextDis : 'next_dis',
				event : 'mouseover'
		};
		var slideCtrl = $(ctrl).children();
		var slideCtn = $(ctn).children();
		var slideCon = $(ctn);
		var pages = $(pages);
		var prev = pages.find('.prev');
		var next = pages.find('.next');
		var cfg = $.extend({},defaults,cfg);
		var slideW = slideCtn.eq(0).width();
		var slideH = slideCtn.eq(0).height();
		var len = slideCtn.length;
		var autoPlay;
		var autoTimer;
		var curIdx = 0;

		if(len < 2){ 
			return false;
		};

		if(cfg.effect =='scrollX'){
			slideCon.width(slideW*len);
		}
		else if(cfg.effect =='scrollY'){
			slideCon.height(slideH*len);
		}

		setPages();

		var slideEffect = {
			fade : function(idx){
				slideCtrl.eq(idx).addClass(cfg.curCls).siblings().removeClass(cfg.curCls);
				slideCtn.eq(idx).addClass(cfg.curCls).stop().animate({opacity:1},cfg.speed).siblings().stop().animate({opacity:0},cfg.speed).removeClass(cfg.curCls);
				setPages();
			},
			
			scrollX : function(idx){
				slideCtrl.eq(idx).addClass(cfg.curCls).siblings().removeClass(cfg.curCls);
				slideCon.stop().animate({left:(-slideW*idx)},cfg.speed);
				setPages();
			},
			
			scrollY : function(idx){
				slideCtrl.eq(idx).addClass(cfg.curCls).siblings().removeClass(cfg.curCls);
				slideCon.stop().animate({top:(-slideH*idx)},cfg.speed);
				setPages();
			}
		}
		

		$.each(slideCtrl,function(idx){
			$(this).bind(cfg.event,function(){
				curIdx = idx;
				slideEffect[cfg.effect](idx);
			})
		});

		autoPlay = function(){
			curIdx++;
			curIdx = (curIdx < slideCtn.length) ? curIdx : 0;
			slideEffect[cfg.effect](curIdx);
		}
		if(cfg.auto){
			autoTimer = setInterval(autoPlay,cfg.pauseTime)
		}
		slideCtn.hover(function(){
			if(autoTimer){
				clearInterval(autoTimer);
			}
		},function(){
			if(cfg.auto){
				autoTimer = setInterval(autoPlay,cfg.pauseTime)
			}
		});

		prev.click(function(e){
			e.preventDefault();
			if($(this).hasClass(cfg.prevDis)) return;

			curIdx--;
			curIdx = (curIdx < 0) ? (len -1) : curIdx;
			slideEffect[cfg.effect](curIdx);
		}).hover(function(){
			if(autoTimer){
				clearInterval(autoTimer);
			}
		},function(){
			if(cfg.auto){
				autoTimer = setInterval(autoPlay,cfg.pauseTime)
			}
		});
		next.click(function(e){
			e.preventDefault();
			if($(this).hasClass(cfg.prevDis)) return;

			curIdx++;
			curIdx = (curIdx < len) ? curIdx : 0;
			slideEffect[cfg.effect](curIdx);
		}).hover(function(){
			if(autoTimer){
				clearInterval(autoTimer);
			}
		},function(){
			if(cfg.auto){
				autoTimer = setInterval(autoPlay,cfg.pauseTime)
			}
		});

		function setPages(){
			if(cfg.circle) return;

			if(curIdx == 0){
				prev.addClass(cfg.prevDis);
			}else{
				prev.removeClass(cfg.prevDis);
			}
			if(curIdx == len){
				next.addClass(cfg.nextDis);
			}else{
				next.removeClass(cfg.nextDis);
			}
		}
	};

	/*
		tab function
	*/
	Y.tab = function(ctn,ctrl,cfg){
		var defaults = {
			cls : 'current',
			event : 'mouseover'
		}
		var tabCtn = $(ctn).children();
		var tabCtrl = $(ctrl).children();
		var cfg = $.extend({},defaults,cfg);

		$.each(tabCtrl,function(idx){
			$(this).bind(cfg.event,function(){
				$.each(tabCtrl,function(_idx){_idx==idx ? $(this).addClass(cfg.cls) : $(this).removeClass(cfg.cls);});
				$.each(tabCtn,function(_idx){_idx==idx ? $(this).show() : $(this).hide();});
			})
		});
	};

	/*
		textSlide
	*/
	Y.textSlide = function(tag,cfg){
		var defaults  = {
			speed : 500,
			pauseTime :4000
		};
		var cfg = $.extend({},defaults,cfg);
		var slideWrap = $(tag);
		var slideCtn = slideWrap.children();
		var slideHeight = slideCtn.eq(0).outerHeight();
		var len = slideCtn.length;

		if(len < 2){
			return false;
		}

		var slidePlay = function(){
			slideWrap.animate({top:-slideHeight},cfg.speed,function(){
				slideWrap.find(':first').appendTo(slideWrap);
				slideWrap.css({top:0});
			})
		};

		var autoPlay = setInterval(slidePlay,cfg.pauseTime);

		slideCtn.hover(function(){
			clearInterval(autoPlay);
		},function(){
			autoPlay = setInterval(slidePlay,cfg.pauseTime);
		});

	};

	/*
		popup function
	*/
	Y.Popup = function(options){
			var defaults = {
				title : 'title',
				content : {},
				width : '400',
				height : '',
				movable : true,
				zIndex : 1000,
				maskOpacity : 0.2,
				escKeyClose : true,
				onOpen : false,
				onClose : false,
				buttons : []
			}
			this.options = $.extend({},defaults,options);
			this.status;
			this.onOpen = typeof(this.options.onOpen) === 'function' ? this.options.onOpen : false;
			this.onClose = typeof(this.options.onClose) === 'function' ? this.options.onClose : false;
			this.init();
			
			!Y.Popup._collection ? Y.Popup._collection = [this] : Y.Popup._collection.push(this);
			
	}

	Y.Popup._createMask = function(){
		var that = this;
		if(!this.mask){
			this.mask = $('<div />');
			this.mask.attr('class','mod_popup_mask').css({
				position : Y.isIE6 ? 'absolute' : 'fixed',
				top : 0,
				left : 0,
				width : '100%',
				height : Y.isIE6 ? Math.max($(window).height(),$('body').height()) : '100%',
				backgroundColor : '#000',
				opacity : 0.2,
				zIndex : 990
			});

			this.mask.appendTo('body');
			if(Y.isIE6){
				$('<iframe />').css({width:'100%',height:'100%',opacity:0}).appendTo(this.mask);
				$(window).bind('resize',function(){
						that._IE6MaskAdjust();
				});
			}
		}
		else{
			this.mask.show();
		}
	};

	Y.Popup._IE6MaskAdjust = function(){
		this.mask.css({height:Math.max($(window).height(),$('body').height()),width:Math.max($(window).width(),$('body').width())});
	};

	Y.Popup.closeAll = function(){
		try {
			for(var i=Y.Popup._collection.length-1; i>=0; i--){
				Y.Popup._collection[i].close();
			}
		}catch(e){}
	}
	

	//Popup.prototype
	Y.Popup.prototype = {
		init : function(){
			var that = this;
			this._showPopup();
			this.status = 1;
			this.popCloseBtn.bind('click',function(e){
				that.close();
				e.preventDefault();
			});

			$(window).bind('resize scroll',function(){
				that._recenter();
			});

			$(document).bind('keydown',function(e){
				var key = e.keyCode;
				if (that.options.escKeyClose && key === 27) {
					that.close();
					e.preventDefault();
				}
			});

			if(this.options.movable){
				this._dragEventBind();
			}
			
			if(this.options.buttons.length != 0){
				this._buttonsEventBind();
			}

			if(this.onOpen){this.onOpen();}
		},

		_showPopup : function(){
			Y.Popup._createMask();
			this.popContainer = $('<div />').attr('class','mod_popup_container').css({
				position:'absolute',
				top: $(window).height() > this.options.height ? $(window).scrollTop()+($(window).height()-this.options.height)/2 : $(window).scrollTop(),
				left:'50%',
				width:this.options.width,
				//height:this.options.height,
				backgroundColor:'#fff',
				zIndex:this.options.zIndex,
				marginLeft:-(this.options.width/2)
			});


			this.popHd = $('<div />').attr({'class':'mod_popup_hd'}).append(
				this.popTitle = $('<h3 />').html(this.options.title),
				this.popCloseBtn = $('<a />').attr({'href': 'javascript:void(0);', 'class': 'close_btn'})
			);

			this.popBd = $('<div />').attr({'class':'mod_popup_bd'}).css({height:this.options.height});

			if(this.options.content){
				if(this.options.content.html){
					this.popTargetCtn = this.options.content.html;
				}
				else if(this.options.content.inline){
					this.popTargetCtn = $(this.options.content.inline).children();
					this.popTargetCtnOrigin = $(this.options.content.inline);
				}
				else if(this.options.content.iframe){
					this.popTargetCtn = $('<iframe />').attr({'src':this.options.content.iframe, 'frameBorder':0,'width':'100%','height':'100%'});
				}
				this.popBd.append(this.popTargetCtn);
			}

			if(this.options.buttons.length != 0){
				this.popFt = $('<div />').attr({'class':'mod_popup_ft'});
				for(var i = 0,len = this.options.buttons.length ; i < len ; i++){
						this.popFt.append($('<a href="javascript:;" class="pop_btn '+(this.options.buttons[i].extraClass ? this.options.buttons[i].extraClass : '')+'"><span>'+this.options.buttons[i].name+'</span></a>'));
				}
			}

			this.popContainer.append(this.popHd,this.popBd,this.popFt).appendTo('body');

		},


		_buttonsEventBind : function(){
			var that = this;
			var buttons = this.popFt.children();
			var handle;
			for(i=0, len = buttons.length ; i < len ; i++){
				handle = (typeof(this.options.buttons[i].handle) === 'function') ? this.options.buttons[i].handle : function(){that.close()};
				$(buttons[i]).bind('click',handle);
			}
		},

		_recenter : function(){
			if($(window).height() < this.popContainer.outerHeight()) {return false;};
			this.popContainer.css({
					top: $(window).scrollTop()+($(window).height()-this.popContainer.outerHeight())/2
			});
		},
		
		_dragEventBind: function(){
			var posX,posY;
			var that = this;
			this.popHd.css({'cursor':'move','-moz-user-select':'none','-webkit-user-select':'none'})
			.bind('selectstart',function(){
				return false;
			});
			this.popHd.find('h3').bind('mousedown',function(e){
				posX = e.pageX;
				posY = e.pageY;
				that.dragActive = true;
			});

			$(document).bind('mousemove',function(e){
				
				if(that.dragActive){
					var popX = that.popContainer.offset().left;
					var popY = that.popContainer.offset().top;
					that.popContainer.css({
						left : (that.popContainer.position().left + e.pageX - posX),
						top : (that.popContainer.position().top + e.pageY - posY)
					});
					posX = e.pageX ;
					posY = e.pageY ;
					
				}
			})
			.bind('mouseup',function(){
				that.dragActive = false;
			});
		},

		close : function(){
			var that = this;
			if(this.popTargetCtnOrigin){
				this.popTargetCtn.appendTo(this.popTargetCtnOrigin);
			}
			$(window).unbind('resize scroll',function(){
				that._recenter();
			});
			this.popContainer.fadeOut(50).remove();
			this.status = 0;
			if(this.onClose){
				this.onClose();
			}

			for(var i=Y.Popup._collection.length-1; i>=0; i--){
				if(!Y.Popup._collection[i].status){
					Y.Popup._collection.splice(i,1);
				}
			}

			if(Y.Popup._collection.length != 0){return false;}
			Y.Popup.mask.fadeOut(50);

		}

	};



	/*
		productPop function
	*/
	Y.productPop = function(el,type){
		var el,w,h,title,href;
		el = $(el);
		title = el.attr('title');
		href  = el.attr('href');
		if(type == 'try' && typeof(YS_LOGIN_INFO) !== 'undefined'){
			W.open(href);
			return false;
		}
		switch(type){
			case 'intention' :
				w = 650;
				h = 650;
				break;
			case 'try' :
				w = 500;
				h = 560;
				break;
			case 'buy' :
				w = 500;
				h = 500;
				break;
			default :
				w = 500;
				h = 500;
				break;
		}
		el.blur();
		new Y.Popup({title:title,content:{'iframe' : href},width:w,height:h});
	};

	/*
		openVideo function
	*/
	Y.openVideo = function(el,w,h){
		var el,w,h,title,href;
		el = $(el);
		title = el.attr('title');
		href  = el.attr('href');
		new Y.Popup({title:title,content:{'iframe' : href},width:w,height:h,movable:false});
		el.blur();
	};

	Y.openIM = function(){
			var url = "http://im.youshang.com/live800/chatClient/chatbox.jsp?companyID=10001&configID=11&enterurl="+encodeURIComponent("http://www.youshang.com/saas/");
			window.open(url,'chatbox73303','toolbar=0,scrollbars=0,location=0,height=450,width=577');
	};

	/*
		cookie
	*/
	Y.cookie = {
			set : function(key,val,h){
				if (h) {
					var date = new Date();
					date.setTime(date.getTime() + (h * 60 * 60 * 1000));
					var expires = "; expires=" + date.toGMTString();
				} else {
					var expires = '';
				}
				D.cookie = key + '=' + val + expires + "; path=/";
			},
			
			get : function(key){
					var n = key + '=';
					var ca = D.cookie.split(';');
					for (var i = 0; i < ca.length; i++) {
						var c = ca[i];
						while (c.charAt(0) == ' '){
							c = c.substring(1, c.length);
						}
						if (c.indexOf(n) == 0) {
							return c.substring(n.length, c.length);
						}
					}
					return null;
			},

			remove: function(key){
				this.set(key, '', -1);
			}
	}

	/*
		presale consult
		QQ售前咨询代码需与营销QQ代码配合使用
		营销QQ代码如下：
		<!-- 营销QQ -->
		<script charset="utf-8" src="http://wpa.b.qq.com/cgi/wpa.php"></script>
		<script type="text/javascript">
		BizQQWPA.addCustom({
			nameAccount: '4008300755',
			aty: '0',
			node: $('.presale_consult .way .qq')[0]
		});
		</script>
		<!-- 营销QQ end -->
	*/
	Y.createPresaleConsult = function(){
		var cls = 'presale_consult', hide_cls = 'presale_hide';
		var style = [
			'.', cls, ' .btn,.', cls, ' .way a,.', cls, ' .way span{background:url("http://images.youshang.com/resources/portal/v10/images/product/consult_spr.png") no-repeat;}',
			'.', cls, '{position:fixed;right:0;top:200px;box-shadow:-1px 0 2px rgba(0,0,0,0.19);}',
			'.', cls, ' .btn{display:block;width:100px;height:19px;background-position:-294px -78px;line-height:200px;overflow:hidden;}',
			'.', cls, ' .way{border-left:1px solid #c8d2d6;border-right:1px solid #c8d2d6;}',
			'.', cls, ' .way a,.', cls, ' .way span{display:block;width:98px;height:90px;border-bottom:1px solid #c8d2d6;line-height:200px;overflow:hidden;}',
			'.', cls, ' .way .qq{background-position:0 -90px;}',
			'.', cls, ' .way .phone{background-position:-98px -90px;}',
			'.', cls, ' .way .online{background-position:-196px -90px;}',
			'.', cls, ' .way :hover.qq{background-position:0 0;}',
			'.', cls, ' .way :hover.online{background-position:-196px 0;}',
			'.', hide_cls, '{box-shadow:none;}',
			'.', hide_cls, ' .btn{width:22px;height:78px;background-position:-294px 0;}',
			'.', hide_cls, ' .way{display:none;}'
		].join('');
		var html = [
			'<div class="', cls, '">',
				'<a href="#" class="btn" title="隐藏售前咨询">售前咨询</a>',
				'<div class="way">',
					'<a href="" id="wpa_right" class="qq" onclick="if(\'undefined\'!=typeof pageTracker){pageTracker._trackPageview(\'/product_presale_qq\');}">QQ售前咨询</a>',
					'<span class="phone">电话售前咨询4008-800-898</span>',
					'<a href="#" class="online" onclick="if(\'undefined\'!=typeof pageTracker){pageTracker._trackPageview(\'/website_presale_online\');}">在线售前咨询</a>',
				'</div>',
			'</div>'
		].join('');
		$('<style type="text/css">' + style + '</style>').appendTo($('body'));
		$('body').append(html);

		//绑定事件
		var presale = $('.' + cls), btn = presale.find('.btn'), online = presale.find('.online');
		//展开/隐藏
		btn.toggle(function(){
			presale.addClass(hide_cls);
			return false;
		},function(){
			presale.removeClass(hide_cls);
			return false;
		});
		//在线售前咨询
		online.click(function(){
			Y.openIM();
			return false;
		});
		
		if(Y.isIE6){
			presale.css({'position' : 'absolute'});
			var top = parseInt(presale.css('top')), sty = presale[0].style, html = $('html')[0];
			html.style.backgroundImage = 'url(about:blank)';
			html.style.backgroundAttachment = 'fixed';
			sty.setExpression('top', 'eval((document.documentElement || document.body).scrollTop + ' + top + ') + "px"');
		}
	}

	W.YSL = Y;
})(jQuery);