jQuery.noConflict();
jQuery(function() {
	initSlider();
});

/* initSlider */
function initSlider(){
	jQuery('.slider-box').each(function(){
		var set = jQuery(this);
		if(this.load) return; else this.load = true;
		new SliderNV(set,{
			slider:'ul',
			slides:'>li',
			effect: false,
			autoRotation: true,
			switchTime:5000,
			autoHeight: true
		});
	});
};
/* slider module */
(function($){
	SliderNV = function(){
		this.init.apply(this,arguments)
	}
	SliderNV.prototype = {
		init: function(context, options){
			/* default options */
			this.options = jQuery.extend({
				sliderHolder: '>div',
				slider:'>ul',
				slides: '>li',
				pagerLinks:'div.pager a',
				generatePagination: false,
				generatePaginationMarkup: '<li><a href="#">&amp;nbsp;</a></li>',
				btnPrev:'a.btn-prev111',
				btnNext:'a.btn-next111',
				activeClass:'active',
				disabledClass:'disabled',
				circleSlide: true,
				effect: true, // true == slide effect & false == fade effect
				pauseClass:'gallery-paused',
				pauseOnHover:true,
				autoRotation:false,
				switchTime:5000,
				animSpeed:650,
				easing:'swing',
				pagerEvent: 'click',
				butttonEvent:'click',
				beforeInit: function(){},
				afterInit:function(){},
				beforeAnimation: function(){},
				afterAnimation: function(){},
				vertical:false,
				reverse: false,
				step: false,
				startElement: 0,
				autoHeight: false
			},options);
			if(!context) return;
			this.set = context;
			if(typeof this.options.beforeInit === 'function') this.options.beforeInit(this);
			this.elementInit();
			if(!this.slides.length) return;
			this.eventInit();
			this.startPosition(this.currentElement);
			if(typeof this.options.afterInit === 'function') this.options.afterInit(this);
			this.set.data('sliderNV', this);
		},
		elementInit: function(){
			this.sliderHolder = jQuery(this.options.sliderHolder, this.set);
			this.slider = jQuery(this.options.slider, this.set);
			this.slides = jQuery(this.options.slides, this.slider);
			this.reCalc();
			this.currentElement = this.prevElement = this.options.startElement || 0;
			this.pagination();
			this.stepCount = this.slides.length;
			this.autoSlide();
		},
		reCalc: function(){
			this.sumHeight = 0;
			this.sumWidth = 0;
			this.slides.each(jQuery.proxy(function(i, elem){
				this.sumHeight += jQuery(elem).outerHeight(true);
				this.sumWidth += jQuery(elem).outerWidth(true);
			},this));
		},
		eventInit: function(){
			this.set.find(this.options.btnPrev).unbind(this.options.butttonEvent).bind(this.options.butttonEvent, jQuery.proxy(function(){
				this.prev();
				return false;
			}, this));
			this.set.find(this.options.btnNext).unbind(this.options.butttonEvent).bind(this.options.butttonEvent, jQuery.proxy(function(){
				this.next();
				return false;
			}, this));
			if(this.options.pauseOnHover){
				this.set.bind('mouseenter', jQuery.proxy(function(){
					this.set.addClass(this.options.pauseClass);
					clearTimeout(this.autoTimer);
				}, this)).bind('mouseleave',jQuery.proxy(function(){
					this.set.removeClass(this.options.pauseClass);
					this.autoSlide();
				}, this));
			};
		},
		pagination: function(){
			if(this.options.generatePagination) {
				this.set.find(this.options.generatePagination).empty()
				this.slides.each(jQuery.proxy(function(){
					var temp = jQuery(this.options.generatePaginationMarkup);
					this.set.find(this.options.generatePagination).append(temp);
				},this));
			};
			this.pagerLinks = jQuery(this.options.pagerLinks, this.set);
			if(this.pagerLinks){
				this.pagerLinks.each(jQuery.proxy(function(idx, elem){
					jQuery(elem).unbind(this.options.pagerEvent).bind(this.options.pagerEvent, jQuery.proxy(function(){
						if(!jQuery(elem).hasClass(this.options.activeClass)){
							this.prevElement = this.currentElement;
							this.currentElement = idx;
							this.swichFunction(this.currentElement);
							this.set.data('sliderNV', this);
						};
						return false;
					},this));
				},this));
			};
		},
		prev: function(){
			this.prevElement = this.currentElement;
			if(this.currentElement > 0){
				this.currentElement--;
			}
			else if(this.options.circleSlide) {
				this.currentElement = this.stepCount-1;
			};
			this.swichFunction(this.currentElement);
		},
		next: function(){
			this.prevElement = this.currentElement;
			if(this.currentElement < this.stepCount-1){
				this.currentElement++;
			}
			else if(this.options.circleSlide) {
				this.currentElement = 0;
			};
			this.swichFunction(this.currentElement);
		},
		disableButton: function(){
			if(!this.options.circleSlide) {
				this.set.find(this.options.btnPrev).removeClass(this.options.disabledClass);
				this.set.find(this.options.btnNext).removeClass(this.options.disabledClass);
				if(this.currentElement == 0){
					this.set.find(this.options.btnPrev).addClass(this.options.disabledClass);
				}
				else if(this.currentElement == this.stepCount-1){
					this.set.find(this.options.btnNext).addClass(this.options.disabledClass);
				};
			};
		},
		recalcOffsets: function(curr){
			if(this.options.vertical) {
				/* slide vertical */
				if(this.options.step) {
					this.stepHeight = this.slides.eq(curr).outerHeight(true)*parseInt(this.options.step);
					this.stepCount = Math.ceil((this.sumHeight-this.sliderHolder.height())/this.stepHeight)+1;
					this.offset = -this.stepHeight*curr;
				} else {
					this.stepHeight = this.sliderHolder.height();
					this.stepCount = Math.ceil(this.sumHeight/this.stepHeight);
					this.offset = -this.stepHeight*curr;
					if(this.offset < this.stepHeight-this.sumHeight) this.offset = this.stepHeight-this.sumHeight;
				}
			}
			else {
				/* slide horizontal */
				if(this.options.step) {
					this.stepWidth = this.slides.eq(curr).outerWidth(true)*parseInt(this.options.step);
					this.stepCount = Math.ceil((this.sumWidth-this.sliderHolder.width())/this.stepWidth)+1;
					this.offset = -this.stepWidth*curr;
					if(this.offset < this.sliderHolder.width()-this.sumWidth) this.offset = this.sliderHolder.width()-this.sumWidth;
				} else {
					this.stepWidth = this.sliderHolder.width();
					this.stepCount = Math.ceil(this.sumWidth/this.stepWidth);
					this.offset = -this.stepWidth*curr;
					if(this.offset < this.stepWidth-this.sumWidth) this.offset = this.stepWidth-this.sumWidth;
				}
			};
		},
		swichFunction: function(curr){
			if(this.prevElement == curr || !this.slides.eq(curr).length) return;
			if(!this.options.effect){
				/* fade effect */
				if(typeof this.options.beforeAnimation ==='function') this.options.beforeAnimation(this);
				this.slides.filter(':visible').stop(false, true).fadeOut(this.options.animSpeed);
				this.slides.eq(curr).stop(false, true).fadeIn(this.options.animSpeed, jQuery.proxy(function(){
					if(typeof this.options.afterAnimation ==='function') this.options.afterAnimation(this);
					this.autoSlide();
					if(this.options.autoHeight) this.slider.css({ position: 'relative', height: this.slides.eq(this.currentElement).outerHeight(true) });
					this.set.data('sliderNV', this);
				},this));
			}
			else {
				/* slide effect */
				var marginType = {};
				this.recalcOffsets(curr);
				marginType[this.options.vertical ? 'marginTop' : 'marginLeft'] = this.offset;
				if(typeof this.options.beforeAnimation ==='function') this.options.beforeAnimation(this);
				this.slider.animate(
					marginType,
					{
						queue: false,
						duration: this.options.animSpeed,
						complete: jQuery.proxy(function(){
							if(typeof this.options.afterAnimation ==='function') this.options.afterAnimation(this);
							this.autoSlide();
							this.set.data('sliderNV', this);
						},this)
					}
				);
			};
			this.pagerLinks.removeClass(this.options.activeClass).eq(this.currentElement).addClass(this.options.activeClass);
			if(!this.options.circleSlide) this.disableButton();
			this.set.data('sliderNV', this);
		},
		startPosition: function(curr){
			if(this.options.effect) {
				var marginType = {};
				this.recalcOffsets(curr);
				marginType[this.options.vertical ? 'marginTop' : 'marginLeft'] = this.offset;
				this.slider.css(marginType);
			}
			else {
				this.slides.css({ position: 'absolute', top: 0, left: 0 });
				this.slider.css({ position: 'relative' });
				if(this.options.autoHeight) this.slider.css({ position: 'relative', height: this.slides.eq(this.currentElement).outerHeight(true) });
				this.slides.hide().eq(this.currentElement).show();
			};
			this.disableButton();
			this.pagerLinks.removeClass(this.options.activeClass).eq(this.currentElement).addClass(this.options.activeClass);
		},
		autoSlide: function(){
			if(this.options.autoRotation && !this.set.hasClass(this.options.pauseClass)){
				if(this.autoTimer) clearTimeout(this.autoTimer);
				this.autoTimer = setTimeout(jQuery.proxy(function(){
					if(this.options.reverse) {
						this.prev();
					}
					else {
						this.next();
					}
				},this), this.options.switchTime + this.options.animSpeed);
			}
		}
	};
})( jQuery );

// mobile browsers detect
browserPlatform = {
	platforms: [
		{ uaString:['symbian','midp'], cssFile:'symbian.css' }, // Symbian phones
		{ uaString:['opera','mobi'], cssFile:'opera.css' }, // Opera Mobile
		{ uaString:['msie','ppc'], cssFile:'ieppc.css' }, // IE Mobile <6
		{ uaString:'iemobile', cssFile:'iemobile.css' }, // IE Mobile 6+
		{ uaString:'webos', cssFile:'webos.css' }, // Palm WebOS
		{ uaString:'Android', cssFile:'android.css' }, // Android
		{ uaString:['BlackBerry','/6.0','mobi'], cssFile:'blackberry6.css' },	// Blackberry 6
		{ uaString:['BlackBerry','/7.0','mobi'], cssFile:'blackberry7.css' },	// Blackberry 7+
		{ uaString:'ipad', cssFile:'ipad.css', miscHead:'<meta name="viewport" content="width=device-width" />' }, // iPad
		{ uaString:['safari','mobi'], cssFile:'safari.css', miscHead:'<meta name="viewport" content="width=device-width" />' } // iPhone and other webkit browsers
	],
	options: {
		cssPath:'css/',
		mobileCSS:'allmobile.css'
	},
	init:function(){
		this.checkMobile();
		this.parsePlatforms();
		return this;
	},
	checkMobile: function() {
		if(this.uaMatch('mobi') || this.uaMatch('midp') || this.uaMatch('ppc') || this.uaMatch('webos')) {
			this.attachStyles({cssFile:this.options.mobileCSS});
		}
	},
	parsePlatforms: function() {
		for(var i = 0; i < this.platforms.length; i++) {
			if(typeof this.platforms[i].uaString === 'string') {
				if(this.uaMatch(this.platforms[i].uaString)) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			} else {
				for(var j = 0, allMatch = true; j < this.platforms[i].uaString.length; j++) {
					if(!this.uaMatch(this.platforms[i].uaString[j])) {
						allMatch = false;
					}
				}
				if(allMatch) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			}
		}
	},
	attachStyles: function(platform) {
		var head = document.getElementsByTagName('head')[0], fragment;
		var cssText = '<link rel="stylesheet" href="' + this.options.cssPath + platform.cssFile + '" type="text/css"/>';
		var miscText = platform.miscHead;
		if(platform.cssFile) {
			if(document.body) {
				fragment = document.createElement('div');
				fragment.innerHTML = cssText;
				head.appendChild(fragment.childNodes[0]);
			} else {
				document.write(cssText);
			}
		}
		if(platform.miscHead) {
			if(document.body) {
				fragment = document.createElement('div');
				fragment.innerHTML = miscText;
				head.appendChild(fragment.childNodes[0]);
			} else {
				document.write(miscText);
			}
		}
	},
	uaMatch:function(str) {
		if(!this.ua) {
			this.ua = navigator.userAgent.toLowerCase();
		}
		return this.ua.indexOf(str.toLowerCase()) != -1;
	}
}.init();
