;(function($) {
	var Slide = window.Slide = function(args) {
		return Slide.fn.init(args);
	};
	Slide.fn = Slide.prototype = {
		auto: true,
		target: false,
		panel: 'li',
		activeIndex: 0,
		triggerType: 'mouseover',
		trigger: 'li',
		activeTriggersCls: 'cur',
		delay: 3,
		_lastIndex: -1,
		_length: 0,
		_timer: 0,
		_slide: function() {
			if (this._lastIndex < 0) {
				this.panels.hide().css('opacity', 0.2);
			} else {
				this.panels.eq(this._lastIndex).fadeTo('fast', 0.2).hide();
			}
			this.panels.eq(this.activeIndex).fadeTo('slow', 1.0).show();
		},
		switchView: function() {
			this._slide();
			this.triggers.removeClass(this.activeTriggersCls).eq(this.activeIndex).addClass(this.activeTriggersCls);
		},
		prev: function() {
			this._lastIndex = this.activeIndex;
			this.activeIndex = this.activeIndex > 0 ? this.activeIndex - 1 : this._length - 1;
			this.switchView();
		},
		next: function() {
			this._lastIndex = this.activeIndex;
			this.activeIndex = this.activeIndex < this._length - 1 ? this.activeIndex + 1 : 0;
			this.switchView();
		},
		stop: function() {
			clearInterval(Slide.fn._timer);
		},
		play: function() {
			Slide.fn.stop();
			if (Slide.fn.auto) {
				Slide.fn._timer = setInterval(function() {
					Slide.fn.next();
				}, Slide.fn.delay * 1000);
			}
		},
		events: function() {
			var target = this.target ? this.target : document, _this = this;
			$(target).delegate(this.trigger, this.triggerType, function() {
				var $this = $(this), index = _this.triggers.index(this);
				if (_this.activeIndex == index) return;
				/*
				$this.siblings().removeClass(_this.activeTriggersCls);
				$this.addClass(_this.activeTriggersCls);
				*/
				_this._lastIndex = _this.activeIndex;
				_this.activeIndex = index;
				_this.switchView();
			}).delegate(this.trigger, 'mouseenter', this.stop).delegate(this.trigger, 'mouseleave', this.play);
		},
		query: function() {
			this.panels = this.target ? $(this.target).find(this.panel) : $(this.panel);
			this.triggers = this.target ? $(this.target).find(this.trigger) : $(this.trigger);
			this._length = this.panels.length == this.triggers.length ? this.panels.length : Math.min(this.panels.length, this.triggers.length);
			
			this.switchView();
			//this.triggers.removeClass(this.activeTriggersCls).eq(this.activeIndex).addClass(this.activeTriggersCls);

			this.events();
			this.play();
		},
		apply: function(o, c, defaults){
			if(defaults){
				this.apply(o, defaults);
			}
			if(o && c && typeof c == 'object'){
				for(var p in c){
					o[p] = c[p];
				}
			}
			return o;
		},
		init: function(args) {
			this.apply(this, args);
			$(document).ready(function() {
				Slide.fn.query();
			});
		}
	}
	Slide.fn.init.prototype = Slide.fn;
})(jQuery);
$(function() {
	var isIE = !!window.ActiveXObject, isIE6 = isIE && !window.XMLHttpRequest;
	$('nav li:has(.x-sub)').hover(function() {
		var $this = $(this), width = $(document).width(), o = $this.find('> a').next(),
			left  = $this.offset().left, _x = parseInt(o.css('left')) || parseInt(o.css('right')) || 0, 
			_l = o.outerWidth();
		if (o.is('.sub-nav')) {
			if (width - _x - left > _l) {
				if (o.css('left') == 'auto') {
					o.css({
						'left': _x + 'px',
						'right': 'auto'
					})
				}
			} else {
				o.css({
					'left': 'auto',
					'right': _x + 'px'
				})
			}
		}
		$this.addClass('active').find('> a').css('opacity', '0.8').end().next().addClass('actived');
		if (o.hasClass('x-demo')) {
			$this.addClass('active').find('> a').css('opacity', '0.9')
		}
	}, function() {
		$(this).removeClass('active').find('> a').css('opacity', '1.0').end().next().removeClass('actived');
	});
	isIE6 && $('.sub-nav li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	$('.slide-title').click(function() {
		var $this = $(this);
		$this.next().slideToggle(function() {
			$this.hasClass('cur') ? $this.removeClass('cur') : $this.addClass('cur');
		});
	});
	$("#roll").length && $("#roll").roll({
		auto: true
	});

	$('.x-share a').click(function() {
		var $this = $(this);
		var title = '上海大众汽车有限公司';
		var url = 'http://www.csvw.com/';
		var str = '';
		if ($this.hasClass('x-sina')) {
			str = 'http://v.t.sina.com.cn/share/share.php?title='+encodeURIComponent(title)+'&url='+encodeURIComponent(url)+'&source=bookmark','_blank','width=450,height=400';
		} else if ($this.hasClass('x-qzone')) {
			str = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title);
		} else if ($this.hasClass('x-kaixin')) {
			str = 'http://www.kaixin001.com/repaste/share.php?rtitle='+encodeURIComponent(title)+'&rurl='+encodeURIComponent(url)+'&rcontent=','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes';
		} else if ($this.hasClass('x-renren')) {
			str = 'http://share.renren.com/share/buttonshare.do?title='+encodeURIComponent(title)+'&link='+encodeURIComponent(url)+'&content=','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes';
		}
		window.open(str);
		return false;
	});

	var _list = $('.banner li');
	_list.length && _list.hide().eq(Math.floor(Math.random()*(_list.length))).show();
});
;(function($) {
	var Gallery = window.Gallery = function(args) {
		return Gallery.fn.init(args);
	};
	Gallery.fn = Gallery.prototype = {
		_index: 0,
		_length: 0,
		_bindEvents: function() {
			$('.slide-box .arrow-prev').click(this._clickX);

			$('.slide-box .arrow-next').click(this._clickY);

			$('.slide-box li').click(function() {
				var list = $('.slide-box li'), index = list.index(this);
				list.removeClass('cur').eq(index).addClass('cur');
				Gallery.fn._show(index);
			});
		},
		_active: function(n) {
			var box = $(".slide-box ul"), list = $('.slide-box li'), _w = list.eq(0).outerWidth(true);
			list.removeClass('cur').eq(n).addClass('cur');

			if (Gallery.fn._length <= 5) {
				return false;
			}
			if (n >= 2 && n < (Gallery.fn._length) - 3) {
				box.animate({
					left: '-' + _w * (n - 2) + "px"
				}, "slow", function () {
					
				});
			} else {
				if (n >= (Gallery.fn._length) - 3) {
					 box.animate({
						left: -(Gallery.fn._length - 5) * _w + "px"
					}, "slow");
				} else {
					if (n < 3) {
						box.animate({
							left: "0"
						}, "slow");
					}
				}
			}
		},
		_show: function(n) {
			$('.slide-demo li').hide().eq(n).fadeTo("fast", 0, function() {
				Gallery.fn._index = n;
				Gallery.fn._active(n);
				$(this).fadeTo('fast', 1);
			});
		},
		_clickX: function() {
			if (Gallery.fn._index > 0) {
				Gallery.fn._index--;
				Gallery.fn._show(Gallery.fn._index);
			} else {
				Gallery.fn._index = 0;
			}
		},
		_clickY: function() {
			if (Gallery.fn._index < Gallery.fn._length - 1) {
				Gallery.fn._index++;
				Gallery.fn._show(Gallery.fn._index);
			}
		},
		_getReady: function() {
			if ($('.slide-demo li').length !== $('.slide-box li').length) return;
			this._length =  $('.slide-box li').length;
			this._bindEvents();
			this._show(this._index);
		},
		init: function(args) {
			$(document).ready(function() {
				Gallery.fn._getReady();
			});
		}
	}
	Gallery.fn.init.prototype = Gallery.fn;
})(jQuery);

