﻿jQuery(function(){

	initToolTips();

	initHoverIE();

	initDropDown();

	initSlide();

});

function initDropDown(){

	var nav = document.getElementById("nav");

	if(nav) {

		var lis = nav.getElementsByTagName("div");

		for (var i=0; i<lis.length; i++) {

			if(lis[i].getElementsByTagName("ul").length > 0) {

				lis[i].className += " has-drop-down"

				lis[i].getElementsByTagName("a")[0].className += " has-drop-down-a"

			}

			lis[i].onmouseover = function()	{

				this.className += " hover";

			}

			lis[i].onmouseout = function() {

				this.className = this.className.replace(" hover", "");

			}

		}

	}

}

function initSlide(){
	jQuery('div.gallery').scrollGallery({
		autoRotation: true
	});
}

jQuery.fn.scrollGallery = function(_options){

	var _options = jQuery.extend({

		sliderHolder: '>div',

		slider:'>ul',

		slides: '>li',

		pagerLinks:'div.pager a',

		btnPrev:'a.link-prev',

		btnNext:'a.link-next',

		activeClass:'active',

		disabledClass:'disabled',

		generatePagination:'.switch',

		curNum:'em.scur-num',

		allNum:'em.sall-num',

		circleSlide:true,

		pauseClass:'gallery-paused',

		pauseButton:'none',

		pauseOnHover:true,

		autoRotation:true,

		stopAfterClick:false,

		switchTime:5000,

		duration:650,

		easing:'swing',

		event:'click',

		afterInit:false,

		vertical:false,

		step:false,

		startElement : 0

	},_options);



	return this.each(function(){

		var _this = jQuery(this);

		var _sliderHolder = jQuery(_options.sliderHolder, _this);

		var _slider = jQuery(_options.slider, _sliderHolder);

		var _slides = jQuery(_options.slides, _slider);

		var _btnPrev = jQuery(_options.btnPrev, _this);

		var _btnNext = jQuery(_options.btnNext, _this);

		var _pagerLinks = jQuery(_options.pagerLinks, _this);

		var _generatePagination = jQuery(_options.generatePagination, _this);

		var _curNum = jQuery(_options.curNum, _this);

		var _allNum = jQuery(_options.allNum, _this);

		var _pauseButton = jQuery(_options.pauseButton, _this);

		var _pauseOnHover = _options.pauseOnHover;

		var _pauseClass = _options.pauseClass;

		var _autoRotation = _options.autoRotation;

		var _activeClass = _options.activeClass;

		var _disabledClass = _options.disabledClass;

		var _easing = _options.easing;

		var _duration = _options.duration;

		var _switchTime = _options.switchTime;

		var _controlEvent = _options.event;

		var _step = _options.step;

		var _vertical = _options.vertical;

		var _circleSlide = _options.circleSlide;

		var _stopAfterClick = _options.stopAfterClick;

		var _afterInit = _options.afterInit;

		

		if(!_slides.length) return;

		var _currentStep = _options.startElement;

		var _sumWidth = 0;

		var _sumHeight = 0;

		var _hover = false;

		var _stepWidth;

		var _stepHeight;

		var _stepCount;

		var _offset;

		var _timer;



		_slides.each(function(){

			_sumWidth+=jQuery(this).outerWidth(true);

			_sumHeight+=jQuery(this).outerHeight(true);

		});

		function recalcOffsets() {

			if(_vertical) {

				if(_step) {

					_stepHeight = _slides.eq(_currentStep).outerHeight(true);

					_stepCount = Math.ceil((_sumHeight-_sliderHolder.height())/_stepHeight)+1;

					_offset = -_stepHeight*_currentStep;

				} else {

					_stepHeight = _sliderHolder.height();

					_stepCount = Math.ceil(_sumHeight/_stepHeight);

					_offset = -_stepHeight*_currentStep;

					if(_offset < _stepHeight-_sumHeight) _offset = _stepHeight-_sumHeight;

				}

			} else {

				if(_step) {

					_stepWidth = _slides.eq(_currentStep).outerWidth(true)*_step;

					_stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth)+1;

					_offset = -_stepWidth*_currentStep;

					if(_offset < _sliderHolder.width()-_sumWidth) _offset = _sliderHolder.width()-_sumWidth;

				} else {

					_stepWidth = _sliderHolder.width();

					_stepCount = Math.ceil(_sumWidth/_stepWidth);

					_offset = -_stepWidth*_currentStep;

					if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth;

				}

			}

		}

		if(_btnPrev.length) {

			_btnPrev.bind(_controlEvent,function(){

				if(_stopAfterClick) stopAutoSlide();

				prevSlide();

				return false;

			});

		}

		if(_btnNext.length) {

			_btnNext.bind(_controlEvent,function(){

				if(_stopAfterClick) stopAutoSlide();

				nextSlide();

				return false;

			});

		}

		if(_generatePagination.length) {

			_generatePagination.empty();

			recalcOffsets();

			for(var i=0; i<_stepCount; i++) jQuery('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_generatePagination);

			_pagerLinks = _generatePagination.children();

		}

		if(_pagerLinks.length) {

			_pagerLinks.each(function(_ind){

				jQuery(this).bind(_controlEvent,function(){

					if(_currentStep != _ind) {

						if(_stopAfterClick) stopAutoSlide();

						_currentStep = _ind;

						switchSlide();

					}

					return false;

				});

			});

		}

		function prevSlide() {

			recalcOffsets();

			if(_currentStep > 0) _currentStep--;

			else if(_circleSlide) _currentStep = _stepCount-1;

			switchSlide();

		}

		function nextSlide() {

			recalcOffsets();

			if(_currentStep < _stepCount-1) _currentStep++;

			else if(_circleSlide) _currentStep = 0;

			switchSlide();

		}

		function refreshStatus() {

			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);

			if(!_circleSlide) {

				_btnPrev.removeClass(_disabledClass);

				_btnNext.removeClass(_disabledClass);

				if(_currentStep == 0) _btnPrev.addClass(_disabledClass);

				if(_currentStep == _stepCount-1) _btnNext.addClass(_disabledClass);

			}

			if(_curNum.length) _curNum.text(_currentStep+1);

			if(_allNum.length) _allNum.text(_stepCount);

		}

		function switchSlide() {

			recalcOffsets();

			if(_vertical) _slider.animate({marginTop:_offset},{duration:_duration,queue:false,easing:_easing});

			else _slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});

			refreshStatus();

			autoSlide();

		}

		function stopAutoSlide() {

			if(_timer) clearTimeout(_timer);

			_autoRotation = false;

		}

		function autoSlide() {

			if(!_autoRotation || _hover) return;

			if(_timer) clearTimeout(_timer);

			_timer = setTimeout(nextSlide,_switchTime+_duration);

		}

		if(_pauseOnHover) {

			_this.hover(function(){

				_hover = true;

				if(_timer) clearTimeout(_timer);

			},function(){

				_hover = false;

				autoSlide();

			});

		}

		

		recalcOffsets();

		refreshStatus();

		autoSlide();

		_slider.css({

			marginLeft:_offset

		});

		if(_pauseButton.length) {

			_pauseButton.click(function(){

				if(_this.hasClass(_pauseClass)) {

					_this.removeClass(_pauseClass);

					_autoRotation = true;

					autoSlide();

				} else {

					_this.addClass(_pauseClass);

					stopAutoSlide();

				}

				return false;

			});

		}

		if(_afterInit && typeof _afterInit === 'function') _afterInit(_this, _slides);

	});

}

function initHoverIE(){

	jQuery('body').NVhoverIE({

		hoverItem:'input.submit, ul#nav li'

	});

}

jQuery.fn.NVhoverIE = function(_options){

	var _options = jQuery.extend({

		hoverItem:'div',

		hoverClass: 'hover'

		

	},_options);



	return this.each(function(){

		var _this = jQuery(this);

		var _hoverItem = jQuery(_options.hoverItem, _this);

		var _hoverClass = _options.hoverClass;

		if (jQuery.browser.msie && jQuery.browser.version < 7) {

			_hoverItem.each(function(){

				jQuery(this).hover(

					function(){

						jQuery(this).addClass(_hoverClass)

					},

					function(){

						jQuery(this).removeClass(_hoverClass)

					}

				)

			})

		}

	});

}

function initToolTips(){

	jQuery(".item-holder li a").easyTooltip({

		xOffset:-46,

		yOffset:20

	});

}

(function(jQuery) {

	jQuery.fn.easyTooltip = function(options){

		// default configuration properties

		var defaults = {

			xOffset:10,

			yOffset:10,

			tooltipId: "easyTooltip",

			tooltipBefore: "",

			tooltipAfter: "",

			clickRemove: false,

			content: "",

			useElement: ""

		}; 



		var options = jQuery.extend(defaults, options);

		var content;



		this.each(function() {

			var title = jQuery(this).attr("title");

			jQuery(this).hover(function(e){

				content = (options.content != "") ? options.content : title;

				content = (options.useElement != "") ? jQuery("#" + options.useElement).html() : content;

				jQuery(this).attr("title","");

				if (content != "" && content != undefined){

					jQuery("body").append("<div id='"+ options.tooltipId +"'>"+ options.tooltipBefore + content + options.tooltipAfter +"</div>");

					jQuery("#" + options.tooltipId)

						.css("position","absolute")

						.css("top",(e.pageY - options.yOffset) + "px")

						.css("left",(e.pageX + options.xOffset) + "px")

						.css("display","none")

						.fadeIn("fast")

				}

			},

			function(){	

				jQuery("#" + options.tooltipId).remove();

				jQuery(this).attr("title",title);

			});	

			jQuery(this).mousemove(function(e){

				jQuery("#" + options.tooltipId)

					.css("top",(e.pageY - options.yOffset) + "px")

					.css("left",(e.pageX + options.xOffset) + "px")

			});	

			if(options.clickRemove){

				jQuery(this).mousedown(function(e){

					jQuery("#" + options.tooltipId).remove();

					jQuery(this).attr("title",title);

				});

			}

		});

	};

})(jQuery);
